<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 v-auth="['system:post:add']" type="primary" :icon="CirclePlus" @click="openDialog()">新增</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation="scope">
          <template v-if="!scope.row.admin">
            <el-button v-auth="['system:post:edit']" type="primary" link :icon="EditPen" @click="openDialog(scope.row)">
              编辑
            </el-button>
            <el-button v-auth="['system:post:remove']" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)">
              删除
            </el-button>
          </template>
        </template>
      </ProTable>
      <PostDialog ref="dialogRef" />
    </div>
  </div>
</template>
<script setup lang="ts" name="accountManage">
import { ref, reactive } from "vue";
import { Post } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import PostDialog from "./components/PostDialog.vue";
import { getDictOptions } from "@/api/modules/common";
import { getPostList, deletePost } from "@/api/modules/system/post";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";

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

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

// 表格配置项
const columns = reactive<ColumnProps<Post.ResPostList>[]>([
  { prop: "postId", label: "岗位编号", width: 100 },
  { prop: "postCode", label: "岗位编码", search: { el: "input" } },
  { prop: "postName", label: "岗位名称", search: { el: "input" } },
  { prop: "postSort", 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: Post.ReqPostParams) => {
  return getPostList(params);
};

// 删除
const deleteAccount = async (params: Post.ResPostList) => {
  await useHandleData(deletePost, [params.postId], `删除岗位【${params.postName}】`);
  proTable.value?.getTableList();
};

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