<template>
  <div class="main-content">
    <BaseTable
      :tableHeader="tableHeader"
      :tableData="tableData"
      :tableOperate="tableOperate"
      :searchParams="searchParams"
      @searchFn="searchFn"
      @addFn="addFn"
    >
      <template #search>
        <el-form-item label="字典名称">
          <el-input v-model.trim="searchParams.name" placeholder="请输入字典名称" />
        </el-form-item>
      </template>
    </BaseTable>

    <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="name">
          <el-input v-model.trim="formDialog.formData.name" />
        </el-form-item>
        <el-form-item label="字典编码" prop="code">
          <el-input v-model.trim="formDialog.formData.code" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model.trim="formDialog.formData.description" type="textarea" :rows="3" />
        </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 { queryDictPage, createDict, updateDict, removeDict } 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,
  name: ''
})

const tableHeader = [
  { label: '字典名称', prop: 'name', align: 'center' as const, minWidth: 150 },
  { label: '字典编码', prop: 'code', align: 'center' as const, minWidth: 150 },
  { label: '描述', prop: 'description', align: 'center' as const, minWidth: 200 }
]

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 = {
  name: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入字典编码', trigger: 'blur' }]
}

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

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

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 ? updateDict : createDict
    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(() => {
    removeDict({ id: row.id }).then(
      () => { ElMessage.success('删除成功'); searchFn(true) },
      (error: any) => ElNotification.error({ title: error?.data?.msg || '', message: error?.data?.data || '' })
    )
  })
}

searchFn()
</script>
