<script setup lang="ts" name="AddClass">
import { onMounted, reactive, ref, toRefs, watch } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";

import { checkNameExitType, addDictionaryType, initEditType } from '@/api/ruleConfig/dictionary'

const props = defineProps<{
  itemData: boolean;
  type: string;
}>();

const emit = defineEmits(["cancel", "confirm"]);

const data = reactive({
  form: <any>{
    id: "",
    remark: "",
    typename: "",
  },
  rules: {
    typename: [
      {
        required: true,
        message: "请输入字典分类名称",
        trigger: ["blur"],
      },
    ],
  },
});

const { form, rules } = toRefs(data);
const formRef = ref<FormInstance>();

// 重置
const reset = () => {
  form.value = {
    id: "",
    remark: "",
    typename: "",
  };
};

// 取消
const cancel = () => {
  emit("cancel");
};

const confirm = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      getcheckNameExitType()
    } else {
    }
  });
};

//  检验数据域名称
const getcheckNameExitType = () => {
  const data = {
    typename: form.value.typename
  }
  checkNameExitType(data).then(res => {
    const { flag, data } = res
    if (flag) {
      addDictionaryTypeFun()
    }
  })
}

// 添加数据域
const addDictionaryTypeFun = () => {
  addDictionaryType({dictionaryType: form.value}).then(res => {
    const { flag } = res
    if (flag) {
      ElMessage.success(props.type === "add" ? "新增成功" : "修改成功");
      emit("confirm");
      reset()
    }
  })
}

const getInitEditType = (id) => { 
  initEditType({ typeId: id }).then(res => {
    const { flag, data } = res
    const { tConsoleDictionarytype } = data
    const { id, remark, typename } = tConsoleDictionarytype
    if (flag) {
      form.value.id = id
      form.value.remark = remark
      form.value.typename = typename
    }
  })
}

watch(
  () => props.itemData,
  (newVal) => {
    if (props.type === "add") {
      reset();
    }
    if (props.type === "edit" && props.itemData) {
      const { id } = JSON.parse(JSON.stringify(props.itemData));
      getInitEditType(id)
    }
  },
  { deep: true, immediate: true }
);
</script>

<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="字典分类" prop="typename" required>
        <el-input v-model="form.typename" placeholder="请输入字典分类"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" rows="2" maxlength="200"
          show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="info" style="width: 150px;" @click="cancel">取消</el-button>
      <el-button type="primary" style="width: 150px;" @click="confirm">确认</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.btn {
  padding: 20px;
  display: flex;
  justify-content: center;
}
:deep(.el-form-item__error) {
  left: auto;
  right: 30px;
  top: 24%;
}
</style>