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

const props = defineProps<{
  itemData: Object;
  type: string;
  encryptionTypeDict: Array<any>;
  typeNameDict: any;
}>();

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

const data = reactive({
  form: <any>{
    
  },
  rules: {
    encryption_type: [
      {
        required: true,
        message: "请选择加密方式",
        trigger: ["change"],
      },
    ],
  },
});

const { form, rules } = toRefs(data);

const readOnly = ref(true);
const typeNameList = ref([])
const encryptionTypeOptions = computed(() => props.encryptionTypeDict)
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) {
      addDataFun()
    } else {
    }
  });
}

// 添加加密分类
const addDataFun = () => {
  if (props.type === 'edit') { 
    form.value = {...form.value,...{id: form.value.id}}
  }
  save(form.value).then(res => {
    const { flag } = res
    if (flag) {
      ElMessage.success((props.type === "add" || !form.value.id) ? "新增成功" : "修改成功");
      if (props.type === 'edit') {
        readOnly.value = true
      } else if (props.type === 'add') { 
        form.value = {}
      }
      emit('confirm', props.type)
    }
  })
}

// 分类称验证
const validateRulename = (rule, value, callback) => {
  const state = typeNameList.value.includes(value);
  if (state) {
    callback(new Error('加密名称已存在'));
  } else {
    callback();
  }
};

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

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

watch(
  () => props.typeNameDict,
  (newVal) => {
    typeNameList.value = props.typeNameDict
    const data = JSON.parse(JSON.stringify(props.itemData));
    if (props.type === 'edit' && data) {
      typeNameList.value = typeNameList.value.filter(item => item !== data.encryption_name)
    }
  },
  { 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="encryption_name" required :rules="[{ required: true, message: '请输入加密名称', trigger:'blur' },{ required: true, validator: validateRulename, trigger:'blur' }]">
        <el-input v-model="form.encryption_name"></el-input>
      </el-form-item>
      <el-form-item label="加密方式" prop="encryption_type" required>
        <div style="width: 100%;background: #F3F5FA;">
          <el-select v-model="form.encryption_type">
            <el-option v-for="item in encryptionTypeOptions" :key="item.value" :label="item.text" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" rows="4" v-model="form.note"></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>