<template>
  <div>
    <!-- 数据表格 -->
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      :need-page="true"
      :toolkit="[]"
      :selection.sync="selection"
      height="calc(100vh - 350px)"
      full-height="calc(100vh - 116px)"
      tool-class="ele-toolbar-form"
      cache-key="systemDictDataTable"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            添加
          </el-button>
      </template>
      <!-- 操作列 -->
      <template v-slot:appType="{ row }">
        {{ types[row.appType] }}
      </template>
      <template v-slot:action="{ row }">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="openEdit(row)"
        >
          修改
        </el-link>
        <el-popconfirm
          class="ele-action"
          title="确定要删除此字典项吗？"
          @confirm="remove(row)"
        >
          <template v-slot:reference>
            <el-link type="danger" :underline="false" icon="el-icon-delete">
              删除
            </el-link>
          </template>
        </el-popconfirm>
      </template>
    </ele-pro-table>
    <!-- 编辑弹窗 -->
    <dict-edit :visible.sync="showEdit" :id="id" @done="reload" />
  </div>
</template>

<script>
  import DictDataSearch from './dict-data-search.vue';
  import DictDataEdit from './dict-data-edit.vue';
  import {
    pageDictionaryData,
    removeDictionaryData,
    removeDictionaryDataBatch
  } from '@/api/system/dictionary-data';
  import { listDictionaries, removeDictionary } from '@/api/system/dictionary';
  import DictEdit from './dict-edit.vue';

  export default {
    components: { DictDataSearch, DictDataEdit, DictEdit },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            showOverflowTooltip: true,
            label:'序号'
          },
          {
            prop: 'code',
            label: '编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'name',
            label: '名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'group',
            label: '物料组',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'brand',
            align: 'center',
            label: '牌号',
            showOverflowTooltip: true,
            slot: 'appType'
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'specifications',
            label: '规格',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'measurement',
            label: '计量单位',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'unit',
            align: 'center',
            label: '包装单位',
            showOverflowTooltip: true,
            slot: 'appType'
          },
          {
            prop: 'category',
            label: '分类',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 130,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        types: {
          1: '业务字典',
          2: '数据字典'
        },
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        id: ''
      };
    },

    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return listDictionaries({ pageNum: page, size: limit, ...where });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 显示编辑 */
      openEdit(row) {
        if(row){
          this.id = row.id;
          this.current = row;
        }
        this.showEdit = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removeDictionary(row.id)
          .then((msg) => {
            loading.close();
            this.$message.success('删除' + msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            // this.$message.error(e.message);
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的字典项吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            removeDictionaryDataBatch(this.selection.map((d) => d.dictDataId))
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
                this.reload();
              })
              .catch((e) => {
                loading.close();
                // this.$message.error(e.message);
              });
          })
          .catch(() => {});
      }
    },
    watch: {
      // 监听字典id变化
      // dictId() {
      //   this.reload();
      // }
    }
  };
</script>
