<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="dictType">
        <el-input v-model="formData.dictType" disabled />
      </el-form-item>
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input v-model="formData.dictLabel" placeholder="请输入数据标签" />
      </el-form-item>
      <el-form-item label="数据键值" prop="dictValue">
        <el-input v-model="formData.dictValue" placeholder="请输入数据键值" />
      </el-form-item>
      <el-form-item label="样式属性" prop="cssClass">
        <el-input v-model="formData.cssClass" placeholder="请输入样式属性" />
      </el-form-item>
      <el-form-item label="显示排序" prop="dictSort">
        <el-input-number v-model="formData.dictSort" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="回显样式" prop="listClass">
        <el-select v-model="formData.listClass">
          <el-option
            v-for="item in listClassOptions"
            :key="item.value"
            :label="item.label + '(' + item.value + ')'"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容"></el-input>
      </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 { editDictData, addDictData } 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.ResDictDataList>, Record<string, any> {}
const isAdd = ref(false);
const formData = ref<FormData>(getFormDefaultValue());

const listClassOptions = ref([
  { value: "default", label: "默认" },
  { value: "primary", label: "主要" },
  { value: "success", label: "成功" },
  { value: "info", label: "信息" },
  { value: "warning", label: "警告" },
  { value: "danger", label: "危险" }
]);
//* 方法

// 校验规则
let formRules = reactive<FormRules>({
  dictLabel: [{ required: true, message: "数据标签不能为空", trigger: "blur" }],
  dictValue: [{ required: true, message: "数据键值不能为空", trigger: "blur" }],
  dictSort: [{ required: true, message: "数据顺序不能为空", trigger: "blur" }]
});

function getFormDefaultValue(): FormData {
  return {
    dictCode: undefined,
    dictType: undefined,
    dictLabel: undefined,
    dictValue: undefined,
    cssClass: undefined,
    dictSort: undefined,
    listClass: 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 addDictData(params);
      ElMessage.success("新增成功");
    } else {
      await editDictData(params);
      ElMessage.success("编辑成功");
    }
    openPromise.value.resolve();
    dialogVisible.value = false;
  });
}

function open(params: FormData) {
  isAdd.value = !params.dictCode;
  if (isAdd.value) {
    formData.value.dictType = params.dictType;
  } else {
    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>
