<template>
  <el-dialog
    v-model="dialogVisible"
    :title="isAdd ? '添加' : '编辑'"
    :width="500"
    center
    align-center
    :close-on-click-modal="false"
    @closed="closed"
  >
    <el-form :model="formData" ref="formRef" label-width="90px" :rules="formRules">
      <el-form-item label="字典名称" prop="dictName">
        <el-input v-model="formData.dictName" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="formData.dictType" placeholder="请输入字典类型" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="formData.status" active-value="0" inactive-value="1" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" class="ml30" v-throttle="handleSubmit">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup name="editTeamMember">
import { reactive, ref, defineExpose, nextTick } from "vue";
import { FormInstance, FormRules, ElMessage } from "element-plus";
import { editDict, addDict } from "@/api/modules/system/dict";
import { Dict } from "@/api/interface";

//* 接收注入
const dialogVisible = ref(false);
//* data
//* ref
const formRef = ref<FormInstance>();
const openPromise = ref<any>({});
interface FormData extends Partial<Dict.ResDictList>, Record<string, any> {}
const isAdd = ref(false);
const formData = ref<FormData>(getFormDefaultValue());
//* 方法

// 校验规则
let formRules = reactive<FormRules>({
  dictName: [{ required: true, message: "字典名称不能为空", trigger: "blur" }],
  dictType: [{ required: true, message: "字典类型不能为空", trigger: "blur" }]
});

function getFormDefaultValue(): FormData {
  return {
    dictId: undefined,
    dictName: undefined,
    dictType: undefined,
    remark: undefined,
    status: "0"
  };
}

function closed() {
  formData.value = getFormDefaultValue();
  nextTick(formRef.value?.clearValidate);
}
// 取消
function handleCancel() {
  dialogVisible.value = false;
}
//提交
function handleSubmit() {
  formRef.value?.validate(async valid => {
    if (!valid) return;
    const params = {
      ...formData.value
    };
    if (isAdd.value) {
      await addDict(params);
      ElMessage.success("新增成功");
    } else {
      await editDict(params);
      ElMessage.success("编辑成功");
    }
    openPromise.value.resolve();
    dialogVisible.value = false;
  });
}

function open(params?: FormData) {
  isAdd.value = !params;
  if (params) {
    const defaultValue: FormData = {};
    for (const key of Object.keys(formData.value)) {
      defaultValue[key] = params[key];
    }
    formData.value = defaultValue;
  }
  dialogVisible.value = true;
  return new Promise(resolve => {
    openPromise.value.resolve = resolve;
  });
}
defineExpose({
  open
});
</script>

<style lang="scss"></style>
