<script setup lang="ts" name="AddDomain">
import { onMounted, reactive, ref, toRefs, watch } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from 'element-plus'
import { checkNameExitDataArea, addDataArea } from '@/api/ruleConfig/discover'

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

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

const data = reactive({
  form: <any>{
    
  },
  rules: {
    dataarea: [
      {
        required: true,
        message: "请输入数据域名称",
        trigger: ["blur"],
      },
    ],
  },
});

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

// 取消
const cancel = () => { 
  if (props.type === 'edit') { 
    readOnly.value = true
    const data = JSON.parse(JSON.stringify(props.itemData));
    form.value = { ...form.value, ...data };
  } else if (props.type === 'add') { 
    emit('cancel')
    form.value = {}
  }
}

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

//  检验数据域名称
const getCheckNameExitDataArea = () => {
  let data = {
    areaname: form.value.dataarea
  }
  if (props.type === 'edit') { 
    data = {...data,...{id: form.value.id}}
  }
  checkNameExitDataArea(data).then(res => {
    console.log(res);
    const { flag, data } = res
    if (flag) {
      addDataAreaFun()
    }
  })
}

// 添加数据域
const addDataAreaFun = () => { 
  let tCoreDataArea = {
    dataarea: form.value.dataarea,
    remark: form.value.remark
  }
  if (props.type === 'edit') { 
    tCoreDataArea = {...tCoreDataArea,...{id: form.value.id}}
  }
  addDataArea({tCoreDataArea: tCoreDataArea}).then(res => {
    const { flag } = res
    if (flag) {
      ElMessage.success(props.type === "add" ? "新增成功" : "修改成功");
      if (props.type === 'edit') { 
        readOnly.value = true
      }
      emit('confirm')
      form.value = {}
    }
  })
}

watch(
  () => props.type,
  (newVal) => {
    readOnly.value = props.type === 'edit' ? true : false;
  },
  { deep: true, immediate: true }
);

watch(
  () => props.itemData,
  (newVal) => {
    if (props.type === "add") {
      form.value = {}
    }
    if (props.type === 'edit' && props.itemData) {
      const data = JSON.parse(JSON.stringify(props.itemData));
      form.value = { ...form.value, ...data };
    }
  },
  { deep: true, immediate: true }
);

</script>

<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" :disabled="readOnly">
      <el-form-item label="数据域名称" prop="dataarea" required>
        <el-input v-model="form.dataarea" :placeholder="!readOnly ? '请输入数据域名称' : ''"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" rows="4" v-model="form.remark" :placeholder="!readOnly ? '请输入备注' : ''"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" style="width: 150px;" @click="readOnly = false" v-if="readOnly">编辑</el-button>
      <el-button type="info" style="width: 150px;" @click="cancel" v-if="!readOnly">取消</el-button>
      <el-button type="primary" style="width: 150px;" @click="confirm" v-if="!readOnly">确认</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>