<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"
      :pageSize="this.$store.state.tablePageSize"
      @filter-change="selectType"
    >
      <!-- 表头工具栏 -->
      <!-- <template v-slot:toolbar>
        <dict-data-search @search="reload">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
        </dict-data-search>
      </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: 'selection',
            type: 'selection',
            width: 45,
            align: 'center'
          },
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'code',
            label: '字典编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'name',
            label: '字典名称',
            align: 'center',
            showOverflowTooltip: true
          },
          // {
          //   prop: 'appType',
          //   label: '字典类型',

          //   showOverflowTooltip: true,
          //   minWidth: 110
          // },
          {//修改此prop名称时，请同步修改columnKey属性和下方selectType方法
            prop: 'appType',
            align: 'center',
            label: '应用类型',
            showOverflowTooltip: true,
            slot: 'appType',
            filters:[
              { value: 1, text:'业务字段' },
              { value: 2, text:'数据字典' },
            ],
            filterMultiple: false,
            columnKey: 'appType'
          },
          {
            prop: 'remark',
            label: '描述',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'updateTime',
            label: '上次更新时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 130,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        types: {
          1: '业务字典',
          2: '数据字典'
        },
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        id: ''
      };
    },

    methods: {
      selectType(value) {
        let where = {}
        if (value.appType.length > 0) {
          where['appType'] = value.appType[0]
        }
        this.reload(where)
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return listDictionaries({ pageNum: page, size: limit, ...where });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 显示编辑 */
      openEdit(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>
