<template>
  <div class="main-box">
    <TreeFilter
      title="组织架构"
      ref="treeFilterRef"
      :request-api="OrgApi.tree"
      label="name"
      :default-value="initParam.positionId"
      @change="changeTree" />
    <div class="table-box">
      <ProTable
        ref="proTableRef"
        title="组织列表"
        row-key="positionId"
        :columns="columns"
        :init-param="initParam"
        :search-columns="searchColumns"
        :request-api="getTableList"
        @reset="reset">
        <!-- 表格 header 按钮 -->
        <template #tableHeader="scope">
          <el-button type="primary" icon="CirclePlus" @click="openDrawer('新增')"> 新增职位 </el-button>
          <el-button type="danger" icon="Delete" plain :disabled="!scope.isSelected" @click="batchDelete(scope.selectedListIds)">
            批量删除
          </el-button>
        </template>

        <template #operation="{ row }">
          <el-button type="primary" link icon="EditPen" @click="openDrawer('编辑', row)"> 编辑 </el-button>
          <el-button type="primary" link icon="Delete" @click="deleteRow(row)"> 删除 </el-button>
        </template>
      </ProTable>
    </div>
    <PositionDrawer ref="drawerRef" />
  </div>
</template>
<script lang="tsx" setup name="OrgManage">
import OrgApi from '@/api/module/system/org'
import PositionApi from '@/api/module/system/position'
import PositionDrawer from './components/PositionDrawer.vue'
import TreeFilter from '@/components/TreeFilter/index.vue'
import { ColumnProps, ProTableInstance, SearchProps } from '@/components/ProTable/interface'
import { PositionBO, PositionQuery, PositionVO } from '@/api/interface/system/position'
import { useDictOptions, useHandleData } from '@/hooks'

const initParam = reactive({ positionId: '', orgId: '' })

const selectTreeId = ref<string>('')
const proTableRef = ref<ProTableInstance>()
const treeFilterRef = ref<InstanceType<typeof TreeFilter>>()

// 树形筛选切换
const changeTree = (val: string) => {
  if (val) {
    selectTreeId.value = val
    initParam.orgId = val
    proTableRef.value?.clearSelection()
  }
}

// 表格配置项
const columns: ColumnProps<PositionVO>[] = [
  { type: 'selection', width: 60 },
  { prop: 'name', label: '职位名称' },
  { prop: 'orderNum', label: '排序' },
  {
    prop: 'status',
    label: '状态',
    tag: true,
    enum: useDictOptions('COMMON_STATUS'),
    width: 80,
    fieldNames: { label: 'dictLabel', value: 'dictValue', tagType: 'callbackShowStyle' }
  },
  { prop: 'remark', label: '备注' },
  { prop: 'createByName', label: '创建人' },
  { prop: 'createTime', label: '创建时间' },
  { prop: 'operation', label: '操作', width: 250, fixed: 'right' }
]

// 表格配置项
const searchColumns: SearchProps[] = [{ prop: 'name', label: '职位名称', el: 'input' }]

// 获取table列表
const getTableList = (params: PositionQuery) => PositionApi.page(params)
// 重置
const reset = () => {
  initParam.orgId = ''
  selectTreeId.value = ''
  proTableRef.value?.clearSelection()
  treeFilterRef.value?.treeRef?.setCurrentKey()
}
// 单行删除
const deleteRow = async (row: PositionVO) => {
  await useHandleData(PositionApi.delete, [row.positionId], `删除【${row.name}】职位`)
  proTableRef.value?.getTableList()
}

// 批量删除信息
const batchDelete = async (ids: (string | number)[]) => {
  await useHandleData(PositionApi.delete, ids, '删除所选职位信息')
  proTableRef.value?.clearSelection()
  proTableRef.value?.getTableList()
}

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof PositionDrawer> | null>(null)
const openDrawer = (title: string, row: Partial<PositionBO> = {}) => {
  const params = {
    title,
    row: title === '新增' ? { orgId: selectTreeId.value } : { ...row },
    isView: title === '查看',
    api: title === '新增' ? PositionApi.add : title === '编辑' ? PositionApi.edit : undefined,
    getTableList: proTableRef.value?.getTableList
  }
  drawerRef.value?.acceptParams(params)
}
</script>
