<!--
数据分级表码列表
@Author: linqian
@Date: 2021-03-31 15:30:11
-->
<template>
  <div>
    <!-- 搜索栏 -->
    <search-bar :conditionForm="conditionForm" @submitSearch="receiveSearch"></search-bar>
    <!-- 操作栏 -->
    <operate-bar :pageOptList="pageOptList" @submitPageOpt="receviceOpt"></operate-bar>
    <!-- 表格 -->
    <new-table
      ref="table"
      :tableHeader="dataLevelTableHeader"
      :tableUrl="dataLevelTableUrl"
      :condition="condition"
      :tableOptList="tableOptList"
      @submitTableOpt="receviceOpt"
    >
    </new-table>
  </div>
</template>

<script>
import { dataLevelTableUrl, delDataLevel, getDataLevelDetail, saveDataLevel } from '@/api/data-auth';
import { dataLevelSyncUrl } from '@/api/sync';
import { dataLevelImportUrl } from '@/api/import';
import searchBar from '@/components/search-bar';
import operateBar from '@/components/operate-bar';
import newTable from '@/pages/common/new-table';
import { searchOpt, importOpt, syncOpt, addEditOpt } from '@/mixins/page-opt';
import { dataLevelTableHeader, tableOptList,  pageOptList, dataLevelForm } from './DataConfig';
const area = ['500px', '400px'];
const labelWidth = '100px';

export default {
  components: { searchBar, operateBar, newTable },
  mixins: [searchOpt, importOpt, syncOpt, addEditOpt],
  data() {
    return {
      dataLevelTableUrl,
      dataLevelTableHeader,
      tableOptList: tableOptList.slice(1, 3),
      pageOptList,
      conditionForm: [
        {
          label: '数据分级',
          name: 'levelName',
          op: 'like',
          value: '',
          component: 'ElInput'
        }
      ]
    };
  },
  computed: {},
  methods: {
    async receviceOpt(opt, row) {
      const otherParams = {
        config: dataLevelForm,
        labelWidth,
        area
      };
      if (opt == '新增') {
        this.handleOpenEditForm('新增', 'add', otherParams);
      } else if (opt == '修改') {
        const detailInfo = await this.getDetail(row.id);
        this.handleOpenEditForm('编辑', 'update', { ...otherParams, detailInfo });
      } else if (opt == '删除') {
        this.handleDelete(row);
      } else if (opt == '同步') {
        this.handleSync(dataLevelSyncUrl);
      } else if (opt == '导入') {
        this.handleImport(dataLevelImportUrl, 'dataLevel', '数据分级表码导入');
      }
    },
    /**
     * 删除
     */
    handleDelete(row) {
      this.$dgConfirm(`是否确定删除这条数据！`, '提示', {}).then(() => {
        delDataLevel(row.id).then((res) => {
          this.$message.success('删除成功!');
          this.handleSearch();
        });
      });
    },
    /**
     * 获取详情
     */
    getDetail(id) {
      return new Promise((resolve) => {
        getDataLevelDetail(id).then((res) => {
          resolve(res.data.content);
        });
      });
    },
    /**
     * @description: 保存表单
     * @param { 表单数据 } form
     * @param { 新增还是编辑 } type
     * @param { 弹框 } layer
     */
    saveForm(form, type, layer) {
      saveDataLevel(form)
        .then((res) => {
          this.$message.success('保存成功!');
          layer.close(layer.dialogIndex);
          this.handleSearch();
        })
        .catch((error) => {
          this.$message.error(error);
        });
    }
  },
  created() {},
  mounted() {}
};
</script>

<style lang='scss' scoped>
@import url('./index.scss');
</style>