<template>
  <div id="index">
    <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @view="toView">
      <template #btn>
        <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_cms_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>
      </template>
    </data-table>
    <el-dialog title="错误记录" center :visible.sync="dialog" :destroy-on-close="true">
      <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 { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: disclosure } = createNamespacedHelpers('disclosure');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      list: [],
      total: 0,
      fields: [
        { label: '专利名称', model: 'name', filter: true },
        { label: '申请人', model: 'apply_name' },
        { label: '申请类型', model: 'type' },
        { label: '技术联系人', model: 'contact' },
        { label: '联系人电话', model: 'phone' },
        { label: '联系人邮箱', model: 'email' },
      ],
      opera: [{ label: '查看', method: 'view' }],
      dialog: false,
      errorList: [],
    };
  },
  created() {},
  methods: {
    ...disclosure(['import']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      console.log('in function:');
    },
    toView({ data }) {
      // TODO查看
    },
    // 导入
    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('正在上传');
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped></style>
