<script setup lang="ts" name="AddDomain">
import { onMounted, reactive, ref, toRefs, watch } from "vue";

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

const emit = defineEmits(["update:modelValue", 'cancel','confirm']);

const data = reactive({
  rules: {},
});

const { rules } = toRefs(data);

const readOnly = ref(true);

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

// 取消
const cancel = () => { 
  if (props.type === 'edit') { 
    readOnly.value = true
  }else if (props.type === 'add') { 
    emit('cancel')
  }
}

const confirm = () => {
  if (props.type === 'edit') { 
    readOnly.value = true
  }
  emit('confirm')
}


</script>

<template>
  <div>
    <el-form ref="formRef" :model="modelValue" label-width="100px" :disabled="readOnly">
      <el-form-item label="数据域名称" required>
        <el-input v-model="modelValue.name" :placeholder="!readOnly ? '请输入数据域名称' : ''"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" rows="4" v-model="modelValue.name" :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>