<template>
  <div class="main-box">
    <div class="table-box">
      <ProTable
        ref="proTable"
        row-key="deptId"
        :columns="columns"
        :request-api="getTableList"
        :init-param="initParam"
        :search-col="{ xs: 1, sm: 1, md: 2, lg: 3, xl: 3 }"
        :pagination="false"
        default-expand-all
      >
        <!-- 表格 header 按钮 -->
        <template #tableHeader>
          <el-button v-auth="['system:dept:add']" type="primary" :icon="CirclePlus" @click="openDialog()">新增</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation="scope">
          <el-button v-auth="['system:dept:edit']" type="primary" link :icon="EditPen" @click="openDialog(scope.row)">
            编辑
          </el-button>
          <el-button
            v-auth="['system:dept:add']"
            :disabled="scope.row.deptType === 'F'"
            type="primary"
            link
            :icon="Plus"
            @click="openDialog({ parentId: scope.row.deptId })"
          >
            新增
          </el-button>
          <el-button v-auth="['system:dept:remove']" type="primary" link :icon="Delete" @click="deleteRole(scope.row)">
            删除
          </el-button>
        </template>
      </ProTable>
      <DeptDialog ref="dialogRef" />
    </div>
  </div>
</template>
<script setup lang="ts" name="roleManage">
import { ref, reactive, provide } from "vue";
import { Dept } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import DeptDialog from "./components/DeptDialog.vue";
import { getDictOptions } from "@/api/modules/common";
import { getDeptList, deleteDept } from "@/api/modules/system/dept";
import { generateTree } from "@/utils";
import { Plus, CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";

// ProTable 实例
const proTable = ref<ProTableInstance>();
const deptOptions = ref<Dept.TreeDept[]>([]);

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

// 表格配置项
const columns = reactive<ColumnProps<Dept.ResDeptList>[]>([
  { prop: "deptName", label: "部门名称", search: { el: "input" } },
  { prop: "orderNum", label: "显示顺序" },
  {
    prop: "status",
    label: "状态",
    enum: () => getDictOptions("sys_normal_disable"),
    search: { el: "select" },
    fieldNames: { label: "dictLabel", value: "dictValue" }
  },
  { prop: "createTime", label: "创建时间" },
  { prop: "operation", label: "操作", width: 330, fixed: "right" }
]);

// 分页列表获取数据
const getTableList = (params: Dept.ReqDeptParams) => {
  return getDeptList(params).then(res => {
    deptOptions.value = generateTree(res.data as any, { idField: "deptId" }) as Dept.TreeDept[];
    return {
      ...res,
      data: deptOptions.value
    };
  });
};
// 删除
const deleteRole = async (params: Dept.ResDeptList) => {
  await useHandleData(deleteDept, [params.deptId], `删除部门【${params.deptName}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof DeptDialog> | null>(null);
const openDialog = (row?: Partial<Dept.ResDeptList>) => {
  dialogRef.value?.open(row).then(proTable.value?.getTableList);
};
</script>
