<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="句子内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入句子内容"
          clearable
          size="small"
          maxlength="100"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入来源"
          clearable
          size="small"
          maxlength="50"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="释义" prop="note">
        <el-input
          v-model="queryParams.note"
          placeholder="请输入释义"
          clearable
          size="small"
          maxlength="100"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['openapi:aword:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['openapi:aword:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  v-loading="loading" border height="585" :data="awordList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="">
            <el-form-item label="">
              <el-image
                style="width: 108px; height: 141px"
                :src="props.row.imgurl"
                :preview-src-list="[props.row.imgurl]">
              </el-image>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="数据ID" align="center" prop="dataId" width="100px"/>
      <el-table-column label="句子内容" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="来源" align="center" prop="source" width="150px" :show-overflow-tooltip="true"/>
      <el-table-column label="释义" align="center" prop="note" :show-overflow-tooltip="true"/>
      <el-table-column label="音频地址" align="center" prop="tts" width="350px">
        <template slot-scope="scope">
          <audio :src="scope.row.tts" controls="controls">
            您的浏览器不支持 audio 标签。
          </audio>
        </template>
      </el-table-column>

      <el-table-column label="句子产生时间" align="center" prop="date" width="150px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px">
        <template slot-scope="scope">
          <el-button
            circle
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['openapi:aword:remove']"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listAword, delAword} from "@/api/business/openapi/aword";

export default {
  name: "Aword",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 每日一句表格数据
      awordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        source: null,
        note: null,
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询每日一句列表 */
    getList() {
      this.loading = true;
      listAword(this.queryParams).then(response => {
        this.awordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dataId: null,
        content: null,
        source: null,
        note: null,
        tts: null,
        imgurl: null,
        date: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除每日一句编号为"' + ids + '"的数据项？').then(function () {
        return delAword(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('openapi/aword/export', {
        ...this.queryParams
      }, `aword_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
