<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="dictCode" :request-api="getTableList" :init-param="initParam">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['system/dict:data:add']" @click="openDialog(1, '字典数据新增')">新增</el-button>
        <el-button type="primary" v-auth="['system/dict:data: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/dict:data:query']" @click="openDialog(3, '字典数据查看', scope.row)">
          查看
        </el-button>
        <el-button type="primary" link icon="EditPen" v-auth="['system/dict:data:edit']" @click="openDialog(2, '字典数据编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['system/dict:data:remove']" @click="deleteDictData(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" :items-options="itemsOptions" :model="model" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="Data">
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'

import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/FormDialog/index.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { listDataApi, delDataApi, addDataApi, updateDataApi, getDataApi, exportApi } from '@/api/modules/system/dictData'
import { getDictApi } from '@/api/modules/system/dict'

const route = useRoute()
// ProTable 实例
const proTable = ref<ProTableInstance>()
// 表单model
const model = ref({})
// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({})
// 数据标签回显样式
const listClassOptions = ref([
  { value: 'default', label: '默认' },
  { value: 'primary', label: '主要' },
  { value: 'success', label: '成功' },
  { value: 'info', label: '信息' },
  { value: 'warning', label: '警告' },
  { value: 'danger', label: '危险' }
])

const defaultDictType = ref('')
const getTableList = async (params: any) => {
  const res = await getDictApi(route.params && (route.params.dictId as string))
  params.dictType = res.data.dictType
  defaultDictType.value = res.data.dictType
  return listDataApi(params)
}
// 删除用户信息
const deleteDictData = async (params: any) => {
  await useHandleData(delDataApi, params.dictCode, `删除【${params.dictLabel}】字典数据`)
  proTable.value?.getTableList()
}

// 批量删除用户信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delDataApi, ids, '删除所选字典数据信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

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

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  model.value = {}
  let res = { data: {} }
  if (row?.dictCode) {
    res = await getDataApi(row?.dictCode || null)
  }
  model.value = type == 1 ? { dictType: defaultDictType.value, version: 0 } : { ...res.data, version: 0 }
  setItemsOptions()
  const params = {
    title,
    width: 500,
    isEdit: type !== 3,
    api: type == 1 ? addDataApi : updateDataApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'dictCode', label: '字典编码' },
  {
    prop: 'dictLabel',
    label: '数据标签',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'dictSort',
    label: '显示排序排序',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'dictValue',
    label: '数据键值',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'cssClass',
    label: '样式属性',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'createTime',
    label: '创建时间',
    width: 120
  },
  {
    prop: 'remark',
    label: '备注',
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
])

// 表单配置项
let itemsOptions = reactive<ProForm.ItemsOptions[]>([])
const setItemsOptions = () => {
  itemsOptions = [
    {
      label: '标签类型',
      prop: 'dictType',
      compOptions: {
        disabled: true
      }
    },
    {
      label: '数据标签',
      prop: 'dictLabel',
      rules: [{ required: true, message: '数据标签不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入数据标签'
      }
    },
    {
      label: '数据键值',
      prop: 'dictValue',
      rules: [{ required: true, message: '数据键值不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入数据键值'
      }
    },
    {
      label: '显示排序',
      prop: 'dictSort',
      rules: [{ required: true, message: '显示排序不能为空', trigger: 'blur' }],
      compOptions: {
        elTagName: 'input-number',
        min: 0,
        placeholder: '请输入显示排序'
      }
    },
    {
      label: '样式属性',
      prop: 'cssClass',
      compOptions: {
        placeholder: '请输入样式属性'
      }
    },
    {
      label: '回显样式',
      prop: 'listClass',
      compOptions: {
        elTagName: 'select',
        enum: listClassOptions.value,
        valueKey: 'value',
        labelKey: 'label',
        placeholder: '请输入回显样式'
      }
    },
    {
      label: '备注',
      prop: 'remark',
      compOptions: {
        type: 'textarea',
        placeholder: '请输入备注'
      }
    }
  ]
}
</script>
