<template>
  <div class="main-box">
    <div class="table-box">
      <ProTable
        ref="proTable"
        :columns="columns"
        :request-api="getTableList"
        :init-param="initParam"
        :search-col="{ xs: 1, sm: 1, md: 2, lg: 3, xl: 3 }"
      >
        <!-- 表格 header 按钮 -->
        <template #tableHeader>
          <el-button type="primary" :icon="CirclePlus" @click="openDialog()">新增</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation="scope">
          <el-button type="primary" link :icon="EditPen" @click="openDialog(scope.row)"> 编辑 </el-button>
          <el-button type="primary" link :icon="Delete" @click="deleteAccount(scope.row)"> 删除 </el-button>
        </template>
      </ProTable>
      <DictDataDialog ref="dialogRef" />
    </div>
  </div>
</template>
<script setup lang="ts" name="dictData">
import { ref, reactive } from "vue";
import { useRoute } from "vue-router";
import { Dict } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import DictDataDialog from "./components/DictDataDialog.vue";
import { getDictOptions } from "@/api/modules/common";
import { getDictDataList, deleteDictData } from "@/api/modules/system/dict";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";

const route = useRoute();
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({ dictType: route.query.dictType });

// 表格配置项
const columns = reactive<ColumnProps<Dict.ResDictList>[]>([
  { prop: "dictCode", label: "字典编码", width: 100 },
  { prop: "dictLabel", label: "字典标签", search: { el: "input" } },
  { prop: "dictValue", label: "字典键值" },
  { prop: "dictSort", label: "字典排序" },
  {
    prop: "status",
    label: "状态",
    enum: () => getDictOptions("sys_normal_disable"),
    search: { el: "select" },
    fieldNames: { label: "dictLabel", value: "dictValue" }
  },
  { prop: "remark", label: "备注" },

  { prop: "createTime", label: "创建时间" },
  { prop: "operation", label: "操作", width: 330, fixed: "right" }
]);
// 分页列表获取数据
const getTableList = (params: Dict.ReqDictDataParams) => {
  return getDictDataList(params);
};

// 删除
const deleteAccount = async (params: Dict.ResDictDataList) => {
  await useHandleData(deleteDictData, [params.dictCode], `删除编码【${params.dictCode}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof DictDataDialog> | null>(null);
const openDialog = (row?: Dict.ResDictDataList) => {
  dialogRef.value?.open(row || ({ dictType: route.query.dictType } as Dict.ResDictDataList)).then(proTable.value?.getTableList);
};
</script>
