<script setup lang="ts">
import type { FormInstance } from 'element-plus'

import { ElMessage } from 'element-plus'

import dictManagementApi from '#/api/modules/setting/dictManagement'
import { delConfirm } from '#/utils/tool'

const props = defineProps({
  showDia: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '查看',
  },
  diaData: {
    type: Object,
    default: () => ({}),
  },
})

const emit = defineEmits(['update:showDia', 'onRefresh'])

const show = computed({
  get() {
    return props.showDia
  },
  set(val) {
    emit('update:showDia', val)
  },
})
const showTool = computed(() => props.title !== '查看')

const rules = {
  code: {
    required: true,
    message: '请填写编码',
    trigger: 'blur',
  },
  name: {
    required: true,
    message: '请填写名称',
    trigger: 'blur',
  },
}

const form = ref({
  code: '',
  name: '',
  remark: '',
  sort: 0,
  status: 0,
})
const ruleFormRef = ref<FormInstance>()

watch(
  () => props.showDia,
  async (newVal) => {
    form.value = {
      code: '',
      name: '',
      remark: '',
      sort: 0,
      status: 0,
    }
    show.value = newVal
    if (props.diaData.code && newVal) {
      const { code, name, remark, status, sort } = props.diaData
      form.value = { code, name, remark, status, sort }
    }
  },
)

async function handleSave(formEl: FormInstance | undefined) {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      await (props.title === '新增'
        ? dictManagementApi.addDictType({ ...form.value })
        : dictManagementApi.editDictType({
            id: props.diaData.id,
            ...form.value,
          }))
      emit('onRefresh')
      show.value = false
    } else {
      ElMessage.error('请输入编码或名称')
    }
  })
}
async function onClose() {
  let flag = false
  for (const key in form.value) {
    if ((form.value as anyObj)[key] !== props.diaData[key]) {
      flag = true
      break
    }
  }
  if (flag) {
    await delConfirm(true)
  }
  show.value = false
}
</script>

<template>
  <el-dialog
    v-model="show"
    :title="title"
    width="80vw"
    top="10vh"
    class="box"
    destroy-on-close
    :before-close="onClose"
  >
    <el-form
      ref="ruleFormRef"
      :model="form"
      label-width="80px"
      :rules="rules"
      :disabled="!showTool"
    >
      <el-form-item label="编码" prop="code">
        <el-input v-model="form.code" :disabled="title === '编辑'" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="form.sort" :min="-1" :max="1000" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <Switch v-model="form.status" :inactive-value="1" :active-value="0" />
      </el-form-item>
    </el-form>
    <template v-if="showTool" #footer>
      <span class="dialog-footer">
        <el-button @click="onClose">关闭</el-button>
        <el-button type="primary" @click="handleSave(ruleFormRef)">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
