<template>
  <div class="main flex-1">
    <div style="width: 100%" v-if="lists.list && lists.list.length > 0">
      <a-table
        :columns="columns"
        :data-source="lists.list"
        :loading="loading"
        :pagination="pagination"
        :row-key="record => record.id"
        @change="handleTableChange"
        :scroll="{ y: height }"
        bordered
        class="white"
      >
        <template slot="id" slot-scope="text, record, index">
          <span>{{ index + 1 }}</span>
        </template>

        <template slot="model" slot-scope="text, record">
          <span v-if="record.model === 0">成果</span>
          <span v-if="record.model === 1">仪器</span>
          <span v-if="record.model === 2">实验室</span>
        </template>

        <template slot="edit" slot-scope="text, record">
          <a-icon @click="showEditModal(record)" class="pointer" theme="twoTone" type="edit" />
          <a-popconfirm @confirm="() => onDelete(record.id)" title="确定要删除这一条?" v-if="lists.list.length">
            <a-icon class="ml-4 pointer" theme="twoTone" two-tone-color="#ff0000" type="delete" />
          </a-popconfirm>
        </template>
      </a-table>
    </div>
    <a-empty v-else />

    <!-- 编辑 -->
    <manage-edit :editItem="editItem" :editVisible="editVisible" @closeModal="closeModal" @selModelSearch="selModelSearch" />
  </div>
</template>

<script>
import ManageEdit from 'components/Manage/ManageEdit.vue';
import { selModelDelete } from 'config/api';

const columns = [
  {
    title: '序号',
    align: 'center',
    dataIndex: 'id',
    key: 'id',
    width: '7%',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '类型',
    align: 'center',
    dataIndex: 'model',
    key: 'model',
    scopedSlots: { customRender: 'model' },
  },
  {
    title: '分类名称',
    align: 'center',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '编辑',
    align: 'center',
    dataIndex: 'edit',
    key: 'edit',
    scopedSlots: { customRender: 'edit' },
  },
];

export default {
  name: 'ManageDate',
  components: {
    ManageEdit,
  },

  props: { lists: { type: Object, default: () => {} }, pagination: { type: Object, default: () => {} } },

  data() {
    return {
      columns,
      loading: false,
      editingKey: '',
      height: '',
      editItem: null, // 修改的那条
      editVisible: false,
    };
  },

  mounted() {
    let th = 150;
    let wh = window.innerHeight;
    this.height = wh - th;
    window.onresize = () => {
      return (() => {
        wh = window.innerHeight;
        this.height = wh - th;
      })();
    };
  },

  methods: {
    showEditModal(record) {
      this.editItem = record;
      this.editVisible = true;
    },

    closeModal() {
      this.editVisible = false;
    },

    async selModelSearch() {
      await this.$emit('selModelSearch');
    },

    // 删除
    async onDelete(id) {
      try {
        const params = { param: { id } };
        const res = await selModelDelete(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.$message.success('删除成功');
          this.$emit('selModelSearch');
        } else {
          throw msg;
        }
      } catch (error) {
        this.$message.error(error || '删除失败');
      }
    },
    handleTableChange(pagination) {
      const { current, pageSize } = pagination;
      const condition = { current, pageSize };
      this.$emit('selModelSearch', condition);
    },
  },
};
</script>

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