<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="configId" :request-api="listConfigApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['system:config:add']" icon="CirclePlus" @click="openDialog(1, '参数配置新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['system:config:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button type="danger" v-auth="['system:config:remove']" icon="Refresh" plain @click="handleRefreshCache"> 刷新缓存 </el-button>
        <el-button
          type="danger"
          v-auth="['system:config: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:config:query']" @click="openDialog(3, '参数配置查看', scope.row)">
          查看
        </el-button>
        <el-button type="primary" link icon="EditPen" v-auth="['system:config:edit']" @click="openDialog(2, '参数配置编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['system:config:remove']" @click="deleteConfig(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" :items-options="itemsOptions" :model="model" />
  </div>
</template>

<script setup lang="tsx" name="Config">
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessage, ElMessageBox } from 'element-plus'
import FormDialog from '@/components/FormDialog/index.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import {
  listConfigApi,
  delConfigApi,
  addConfigApi,
  updateConfigApi,
  exportConfigApi,
  getConfigApi,
  refreshCacheApi
} from '@/api/modules/system/config'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { sys_y_n } = toRefs<any>(proxy?.useDict('sys_y_n'))

// ProTable 实例
const proTable = ref<ProTableInstance>()
// 表单model
const model = ref({})
// 删除参数配置信息
const deleteConfig = async (params: any) => {
  await useHandleData(delConfigApi, params.configId, '删除【' + params.configId + '】参数配置')
  proTable.value?.getTableList()
}

// 批量删除参数配置信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delConfigApi, ids, '删除所选参数配置信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

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

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

/** 刷新缓存按钮操作 */
const handleRefreshCache = async () => {
  await refreshCacheApi()
  ElMessage.success('刷新成功')
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  {
    prop: 'configName',
    label: '参数名称',
    search: {
      el: 'input'
    }
  },
  {
    prop: 'configKey',
    label: '参数键名',
    search: {
      el: 'input'
    }
  },
  {
    prop: 'configValue',
    label: '参数键值'
  },
  {
    prop: 'configType',
    label: '系统内置',
    tag: true,
    search: {
      el: 'select'
    },
    enum: sys_y_n
  },
  {
    prop: 'remark',
    label: '备注',
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230 }
])
// 表单配置项
let itemsOptions = reactive<ProForm.ItemsOptions[]>([])
const setItemsOptions = () => {
  itemsOptions = [
    {
      label: '参数主键',
      prop: 'configId',
      rules: [{ required: true, message: '参数主键不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入参数主键'
      }
    },
    {
      label: '参数名称',
      prop: 'configName',
      rules: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入参数名称'
      }
    },
    {
      label: '参数键名',
      prop: 'configKey',
      rules: [{ required: true, message: '参数键名不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入参数键名'
      }
    },
    {
      label: '参数键值',
      prop: 'configValue',
      rules: [{ required: true, message: '参数键值不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入参数键值'
      }
    },
    {
      label: '系统内置',
      prop: 'configType',
      rules: [{ required: true, message: '系统内置不能为空', trigger: 'change' }],
      compOptions: {
        elTagName: 'radio-button',
        enum: sys_y_n.value
      }
    },
    {
      label: '备注',
      prop: 'remark',
      compOptions: {
        placeholder: '请输入备注'
      }
    }
  ]
}
</script>
