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

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

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

// 表格配置项
const columns = reactive<ColumnProps<Role.ResRoleList>[]>([
  { prop: "roleId", label: "角色编号", width: 100 },
  { prop: "roleName", label: "角色名称", search: { el: "input" } },
  { prop: "roleKey", label: "标识字符", search: { el: "input" } },
  { prop: "roleSort", label: "显示顺序" },
  {
    prop: "status",
    label: "状态",
    sortable: true,
    enum: () => getDictOptions("sys_normal_disable"),
    search: { el: "select" },
    fieldNames: { label: "dictLabel", value: "dictValue" }
  },
  { prop: "createTime", label: "创建时间", search: { el: "date-range-picker", custom: true } },
  { prop: "operation", label: "操作", width: 330, fixed: "right" }
]);

// 分页列表获取数据
const getTableList = (params: Role.ReqRoleParams) => {
  if (params.createTime) {
    const [beginTime, endTime] = params.createTime;
    params.params = {
      beginTime,
      endTime
    };
    delete params.createTime;
  }
  return getRoleList(params);
};

// 删除用户信息
const deleteRoleHanlde = async (params: Role.ResRoleList) => {
  await useHandleData(deleteRole, [params.roleId], `删除角色【${params.roleId}】`);
  proTable.value?.getTableList();
};

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