<template>
  <div class="table-box">
    <ProTable ref="proTableRef" title="参数配置" row-key="configId" :columns="columns" :search-columns="searchColumns" :request-api="getTableList">
      <!-- 表格 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 v-if="row.isLock !== '1'" type="primary" link icon="Delete" @click="deleteRow(row)"> 删除 </el-button>
      </template>
    </ProTable>
    <ConfigDrawer ref="drawerRef" size="40%" />
  </div>
</template>
<script lang="tsx" setup name="ConfigManage">
import ConfigApi from '@/api/module/system/config'
import ConfigDrawer from './components/ConfigDrawer.vue'
import { ColumnProps, ProTableInstance, SearchProps } from '@/components/ProTable/interface'
import { useDictOptions, useHandleData } from '@/hooks'
import { ConfigBO, ConfigQuery, ConfigVO } from '@/api/interface/system/config'

const proTableRef = ref<ProTableInstance>()

// 表格配置项
const columns: ColumnProps<ConfigVO>[] = [
  { type: 'selection', width: 60, selectable: row => row.isLock !== '1' },
  { type: 'index', width: 60 },
  { prop: 'name', label: '参数名称' },
  { prop: 'configKey', label: '参数Key', tag: true },
  { prop: 'configValue', label: '参数Value' },
  {
    prop: 'status',
    label: '状态',
    tag: true,
    enum: useDictOptions('COMMON_STATUS'),
    width: 80,
    fieldNames: { label: 'dictLabel', value: 'dictValue', tagType: 'callbackShowStyle' }
  },
  { prop: 'orderNum', label: '排序' },
  { prop: 'remark', label: '备注' },
  { prop: 'createByName', label: '创建人' },
  { prop: 'createTime', label: '创建时间' },
  { prop: 'operation', label: '操作', width: 250, fixed: 'right' }
]

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

// 获取table列表
const getTableList = (params: ConfigQuery) => ConfigApi.page(params)

// 单行删除
const deleteRow = async (row: ConfigVO) => {
  await useHandleData(ConfigApi.delete, [row.configId], `删除【${row.configKey}】数据`)
  proTableRef.value?.getTableList()
}

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

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