<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>
      <template #options="{ item }">
        <template v-if="item.prop === 'type'">
          <el-option v-for="(i, index) in typeList" :key="`type-${index}`" :label="i.label" :value="i.value"></el-option>
        </template>
      </template>
    </data-table>
  </div>
</template>

<script>
const { productType } = require('@common/dict/index');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: product } = createNamespacedHelpers('product');
export default {
  name: 'productIndex',
  props: {},
  components: {},
  data: function () {
    return {
      list: [],
      total: 0,
      opera: [
        {
          label: '修改',
          method: 'edit',
        },
        {
          label: '删除',
          method: 'delete',
          type: 'danger',
        },
      ],
      fields: [
        { label: '名称', prop: 'name', filter: true },
        { label: '企业名称', prop: 'company', filter: true },
        {
          label: '类型',
          prop: 'type',
          format: (i) => {
            const r = productType.find((f) => f.value === i);
            if (r) return r.label;
            return '';
          },
          filter: 'select',
        },
        { label: '所属领域', prop: 'field' },
        { label: '联系人', prop: 'contacts' },
        { label: '联系电话', prop: 'phone' },
      ],
      typeList: productType,
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...product(['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/product/detail');
    },
    toEdit({ data }) {
      this.$router.push({ path: '/adminCenter/product/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>
