<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>();

watch(
  () => [props.type, props.itemData],
  (newVal) => {
    readOnly.value = props.type === 'edit' ? true : false;
    if (props.type === 'edit' && props.itemData) {
      form.value = JSON.parse(JSON.stringify(props.itemData))
    }
  },
  { deep: true, immediate: true }
);

// 取消
const cancel = () => { 
  if (props.type === 'edit') { 
    form.value = props.itemData
    readOnly.value = true
  } 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 = {}
    }
  })
}


</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;
}
</style>