<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="postId" :request-api="listPostApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['system:post:add']" :icon="CirclePlus" @click="openDialog(1, '岗位信息新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['system:post:import']" :icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['system:post: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:post:query']" @click="openDialog(3, '岗位信息查看', scope.row)">
          查看
        </el-button>
        <el-button type="primary" link :icon="EditPen" v-auth="['system:post:edit']" @click="openDialog(2, '岗位信息编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link :icon="Delete" v-auth="['system:post:remove']" @click="deletePost(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="PostManage">
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 {
  listPostApi,
  delPostApi,
  addPostApi,
  updatePostApi,
  importTemplateApi,
  importDataApi,
  exportApi,
  getPostApi
} from '@/api/modules/system/post'

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

// 删除岗位信息信息
const deletePost = async (params: any) => {
  await useHandleData(delPostApi, params.postId, `删除【${params.postId}】岗位信息`)
  proTable.value?.getTableList()
}

// 批量删除岗位信息信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delPostApi, ids, '删除所选岗位信息信息')
  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?.postId) {
    res = await getPostApi(row?.postId || null)
  }
  // 重置表单
  setFieldList()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    fieldList: fieldList,
    model: type == 1 ? {} : res.data,
    api: type == 1 ? addPostApi : updatePostApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'postId', label: '岗位ID' },
  {
    prop: 'postCode',
    label: '岗位编码',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'postName',
    label: '岗位名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'postSort',
    label: '显示顺序',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'status',
    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: 'postCode',
      rules: [{ required: true, message: '岗位编码不能为空', trigger: 'blur' }],
      placeholder: '请输入岗位编码'
    },
    {
      label: '岗位名称',
      field: 'postName',
      rules: [{ required: true, message: '岗位名称不能为空', trigger: 'blur' }],
      placeholder: '请输入岗位名称'
    },
    {
      label: '显示顺序',
      field: 'postSort',
      rules: [{ required: true, message: '显示顺序不能为空', trigger: 'blur' }],
      placeholder: '请输入显示顺序'
    },
    {
      label: '状态',
      field: 'status',
      rules: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
      placeholder: '请输入状态'
    },
    {
      label: '备注',
      field: 'remark',
      placeholder: '请输入备注'
    }
  ]
}
</script>
