<template>
  <div id="index">
    <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @edit="toEdit" @delete="toDelete">
      <template #selfbtn>
        <el-button type="primary" size="mini" @click="toAdd">添加专利</el-button>
      </template>
    </data-table>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patent } = createNamespacedHelpers('patent');
export default {
  name: 'patentIndex',
  props: {},
  components: {},
  data: function () {
    return {
      list: [],
      total: 0,
      opera: [
        {
          label: '修改',
          method: 'edit',
        },
        {
          label: '删除',
          method: 'delete',
          type: 'danger',
        },
      ],
      fields: [
        { label: '申请号', prop: 'create_number', filter: true },
        { label: '申请日', prop: 'create_date' },
        { label: '公开（公告）号', prop: 'success_number' },
        { label: '公开（公告）日', prop: 'success_date' },
        { label: '标题', prop: 'name' },
        { label: '发明人', prop: 'inventor' },
        { label: '申请人', prop: 'apply_personal' },
        { label: '专利有效性', prop: 'term' },
        { label: '专利类型', prop: 'type' },
      ],
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...patent(['query', 'delete']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    toAdd() {
      this.$router.push('/adminCenter/patent/detail');
    },
    toEdit({ data }) {
      this.$router.push({ path: '/adminCenter/patent/detail', query: { id: data._id } });
    },
    async toDelete({ data }) {
      const res = await this.delete(data._id);
      if (this.$checkRes(res, '删除成功', '删除失败')) {
        this.search();
      }
    },
  },
  computed: {
    ...mapState(['user', 'menuParams']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

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