<template>
  <div class="main-content">
    <BaseTable
      :tableHeader="tableHeader"
      :tableData="tableData"
      :tableOperate="tableOperate"
      :searchParams="searchParams"
      @searchFn="searchFn"
      @addFn="addFn"
    />

    <el-dialog :title="formDialog.title" v-model="formDialog.visible" :close-on-click-modal="false">
      <el-form ref="formRef" size="small" label-width="120px" :model="formDialog.formData" :rules="rules">
        <el-form-item label="字典项名称" prop="label">
          <el-input v-model.trim="formDialog.formData.label" />
        </el-form-item>
        <el-form-item label="字典项值" prop="value">
          <el-input v-model.trim="formDialog.formData.value" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="formDialog.formData.sort" :min="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" :loading="formDialog.loading" @click="submitFn">确定</el-button>
        <el-button @click="cancelFn">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
import { queryDictOptions, createDictOption, updateDictOption, removeDictOption } from '@/api/dictionary'
import type { FormInstance, FormRules } from 'element-plus'

const formRef = ref<FormInstance>()

const defaultSearch = { current: 1, size: 10, total: 0 }

const searchParams = reactive({ ...defaultSearch })

const tableHeader = [
  { label: '字典项名称', prop: 'label', align: 'center' as const, minWidth: 150 },
  { label: '字典项值', prop: 'value', align: 'center' as const, minWidth: 150 },
  { label: '排序', prop: 'sort', align: 'center' as const, width: 100 }
]

const tableData = ref<any[]>([])

const tableOperate = [
  { label: '编辑', type: 'primary', icon: 'el-icon-edit', func: (row: any) => editFn(row) },
  { label: '删除', type: 'danger', icon: 'el-icon-delete', func: (row: any) => delFn(row) }
]

const formDialog = reactive({
  title: '',
  visible: false,
  loading: false,
  formData: {} as any
})

const rules: FormRules = {
  label: [{ required: true, message: '请输入字典项名称', trigger: 'blur' }],
  value: [{ required: true, message: '请输入字典项值', trigger: 'blur' }]
}

function searchFn(isReset = false) {
  if (isReset) Object.assign(searchParams, defaultSearch)
  queryDictOptions(searchParams).then(
    (res: any) => {
      tableData.value = res.data?.records || res.data || []
      searchParams.total = res.data?.total || (Array.isArray(res.data) ? res.data.length : 0)
    },
    (error: any) => ElNotification.error({ title: error?.data?.msg || '', message: error?.data?.data || '' })
  )
}

function addFn() {
  formDialog.title = '新增'
  formDialog.visible = true
  formDialog.formData = { sort: 0 }
}

function editFn(row: any) {
  formDialog.title = '编辑'
  formDialog.formData = { ...row }
  formDialog.visible = true
}

function cancelFn() {
  formDialog.visible = false
  formDialog.formData = {}
  formRef.value?.resetFields()
}

function submitFn() {
  if (!formRef.value) return
  formDialog.loading = true
  formRef.value.validate((valid: boolean) => {
    if (!valid) { formDialog.loading = false; return }
    const api = formDialog.formData.id ? updateDictOption : createDictOption
    api(formDialog.formData).then(
      () => {
        ElMessage.success(formDialog.formData.id ? '修改成功' : '新增成功')
        formDialog.visible = false
        formDialog.loading = false
        searchFn(true)
      },
      (error: any) => {
        ElNotification.error({ title: error?.data?.msg || '', message: error?.data?.data || '' })
        formDialog.loading = false
      }
    )
  })
}

function delFn(row: any) {
  ElMessageBox.confirm('此操作将删除该记录, 是否继续?', '提示', { type: 'warning' }).then(() => {
    removeDictOption({ id: row.id }).then(
      () => { ElMessage.success('删除成功'); searchFn(true) },
      (error: any) => ElNotification.error({ title: error?.data?.msg || '', message: error?.data?.data || '' })
    )
  })
}

searchFn()
</script>
