<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      row-key="roleId"
      :request-api="listRoleApi"
      :init-param="initParam"
      :data-callback="dataCallback"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['system:role:add']" :icon="CirclePlus" @click="openDialog(1, '角色信息新增')">
          新增
        </el-button>
        <el-button type="primary" v-auth="['system:role:import']" :icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['system:role:export']" :icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button
          type="danger"
          v-auth="['system:user:remove']"
          :icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button
          type="primary"
          link
          :icon="View"
          v-auth="['system:role:query']"
          @click="openDialog(3, '角色信息查看', scope.row)"
        >
          查看
        </el-button>
        <el-button
          type="primary"
          link
          :icon="EditPen"
          v-auth="['system:role:edit']"
          @click="openDialog(2, '角色信息编辑', scope.row)"
        >
          编辑
        </el-button>
        <el-button type="primary" link :icon="Delete" v-auth="['system:role:remove']" @click="deleteRole(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="Role">
import { ref, reactive } from 'vue'
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import ProTable from '@/components/ProTable/index.vue'
import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/DialogOld/form.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { Delete, EditPen, Download, Upload, View, CirclePlus } from '@element-plus/icons-vue'
import {
  listRoleApi,
  delRoleApi,
  addRoleApi,
  updateRoleApi,
  importTemplateApi,
  importDataApi,
  exportApi,
  getRoleApi
} from '@/api/modules/system/role'

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

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

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，可以在这里进行处理成这些字段
const dataCallback = (data: any) => {
  const page = proTable.value!.pageable
  return {
    list: data.data,
    total: data.total,
    pageNum: page.pageNum,
    pageSize: page.pageSize
  }
}

// 删除角色信息信息
const deleteRole = async (params: any) => {
  await useHandleData(delRoleApi, { id: [params.roleId] }, `删除【${params.id}】角色信息`)
  proTable.value?.getTableList()
}

// 批量删除角色信息信息
const batchDelete = async (id: string[]) => {
  await useHandleData(delRoleApi, { id }, '删除所选角色信息信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 导出角色信息列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出角色信息数据?', '温馨提示', { type: 'warning' }).then(() =>
    useDownload(exportApi, '角色信息列表', proTable.value?.searchParam)
  )
}

// 批量添加角色信息
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '角色信息',
    tempApi: importTemplateApi,
    importApi: importDataApi,
    getTableList: proTable.value?.getTableList
  }
  dialogRef.value?.acceptParams(params)
}

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  let res = { data: {} }
  if (row?.roleId) {
    res = await getRoleApi(row?.roleId || null)
  }
  // 重置表单
  setFieldList()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    fieldList: fieldList,
    model: type == 1 ? {} : res.data,
    api: type == 1 ? addRoleApi : updateRoleApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'roleId', label: '角色ID' },
  {
    prop: 'roleName',
    label: '角色名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'roleKey',
    label: '角色权限字符串',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'roleSort',
    label: '显示顺序',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'dataScope',
    label: '数据范围',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'menuCheckStrictly',
    label: '菜单树选择项是否关联显示',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'deptCheckStrictly',
    label: '部门树选择项是否关联显示',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'status',
    label: '角色状态',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'delFlag',
    label: '删除标志',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'createBy',
    label: '创建者',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'createTime',
    label: '创建时间',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'updateBy',
    label: '更新者',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'updateTime',
    label: '更新时间',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'remark',
    label: '备注',
    search: {
      el: 'input'
    },
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
])
// 表单配置项
let fieldList: Form.FieldItem[] = []
const setFieldList = () => {
  fieldList = [
    {
      label: '角色名称',
      field: 'roleName',
      rules: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
      placeholder: '请输入角色名称'
    },
    {
      label: '角色权限字符串',
      field: 'roleKey',
      rules: [{ required: true, message: '角色权限字符串不能为空', trigger: 'blur' }],
      placeholder: '请输入角色权限字符串'
    },
    {
      label: '显示顺序',
      field: 'roleSort',
      rules: [{ required: true, message: '显示顺序不能为空', trigger: 'blur' }],
      placeholder: '请输入显示顺序'
    },
    {
      label: '数据范围',
      field: 'dataScope',
      placeholder: '请输入数据范围'
    },
    {
      label: '菜单树选择项是否关联显示',
      field: 'menuCheckStrictly',
      placeholder: '请输入菜单树选择项是否关联显示'
    },
    {
      label: '部门树选择项是否关联显示',
      field: 'deptCheckStrictly',
      placeholder: '请输入部门树选择项是否关联显示'
    },
    {
      label: '角色状态',
      field: 'status',
      rules: [{ required: true, message: '角色状态不能为空', trigger: 'blur' }],
      placeholder: '请输入角色状态'
    },
    {
      label: '删除标志',
      field: 'delFlag',
      placeholder: '请输入删除标志'
    },
    {
      label: '备注',
      field: 'remark',
      placeholder: '请输入备注'
    }
  ]
}
</script>
