<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="top">
          <el-popover placement="bottom" width="370" trigger="click" style="margin-right:10px">
            <template #reference>
              <el-button type="primary" size="mini">导入</el-button>
            </template>
            <el-upload
              class="upload-demo"
              drag
              action="/files/platform/patent_import/upload"
              multiple
              :on-success="uSuccess"
              :on-error="uError"
              :before-upload="uBefore"
              :show-file-list="false"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </el-popover>
          <el-button type="primary" size="mini" @click="getTemplate">下载导入模板</el-button>
          <el-button type="primary" size="mini" @click="toExport">导出</el-button>
          <el-button type="primary" size="mini" @click="toResult">查看导出结果</el-button>
          <el-button type="primary" size="mini" @click="add">添加</el-button>
        </el-col>
        <el-col :span="24" class="down">
          <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @edit="toEdit" @delete="toDelete"></data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="dialog" title="错误记录">
      <el-table border stripe :data="errorList">
        <el-table-column align="center" label="错误记录">
          <template v-slot="{ row }">{{ row }}</template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import dataTable from '@common/src/components/frame/filter-page-table.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patent } = createNamespacedHelpers('patent');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'index',
  props: {},
  components: { dataTable },
  data: function() {
    return {
      opera: [
        {
          label: '编辑',
          method: 'edit',
        },
        {
          label: '删除',
          method: 'delete',
        },
      ],
      fields: [
        { label: '申请号', prop: 'create_number', filter: 'input' },
        { label: '公开(公告)号', prop: 'success_number', filter: 'input' },
        { label: '标题', prop: 'name', filter: 'input' },
        { label: '发明人', prop: 'inventor', filter: 'input' },
        { label: '专利有效性', prop: 'term' },
        { label: '专利类型', prop: 'type' },
      ],
      list: [],
      total: 0,
      dialog: false,
      errorList: [],
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...patent(['query', 'fetch', 'create', 'update', 'delete', 'import', 'export']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      let res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 修改
    toEdit({ data }) {
      this.$router.push({ path: '/patent/detail', query: { id: data.id } });
    },
    // 删除
    async toDelete({ data }) {
      let res = await this.delete(data.id);
      if (this.$checkRes(res)) {
        this.$message({
          message: '信息删除成功',
          type: 'success',
        });
        this.search();
      }
    },
    // 添加数据
    add() {
      this.$router.push({ path: '/patent/detail' });
    },
    // 导出
    async toExport() {
      const res = await this.export();
      this.$checkRes(res, '正在导出,详情请点击"查看导出结果"进行查看', res.errmsg || '导出失败');
    },
    // 查看导出结果
    async toResult() {
      this.$router.push({ path: '/patent/result' });
    },
    // 导入
    async toImport(uri) {
      const res = await this.import({ uri });
      const { data } = res;
      if (data) {
        this.$message.error('导入失败');
        this.$set(this, 'errorList', data);
        this.dialog = true;
      } else {
        this.$message.success('导入成功');
      }
    },
    // 上传成功
    uSuccess(response, file, fileList) {
      const { uri } = response;
      this.$checkRes(response, '上传成功!正在导入,请稍后...');
      this.toImport(uri);
    },
    // 上传失败
    uError(err, file, fileList) {
      this.$message.error('上传失败');
    },
    // 正在上传
    uBefore() {
      this.$message('正在上传');
    },
    // 下载模板
    getTemplate() {
      window.open('./数据模板.xlsx');
    },
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

<style lang="less" scoped>
.main {
  .top {
    text-align: right;
    margin: 0 0 10px 0;
  }
}
</style>
