<template>
  <el-form
    :label-position="'right'"
    label-width="120px"
    :model="directoryForm"
    :rules="directoryFormRules"
    ref="directoryRef"
    style="max-width: 80%"
  >
    <el-form-item label="上级目录" prop="parent">
      <el-input v-model="directoryForm.parent" style="width: 80%;" />
      <el-form-item label="" style="padding-left: 16px">
        <el-checkbox v-model="directoryForm.level" label="顶级"/>
      </el-form-item>
    </el-form-item>
    
    <el-form-item label="名称" prop="name">
      <el-input v-model="directoryForm.name" />
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'

const directoryForm = reactive({
  parent: '',
  name: '',
  level: ''
})

const directoryFormRules = reactive({
  parent: [
    { required: true, message: '请输入上级目录', trigger: 'blur' },
  ],
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' },
  ],
})
const directoryRef = ref(null)
const emit = defineEmits(['action'])

const submitForm = async () => {
  if (!directoryRef) return
  await directoryRef.value.validate((valid, fields) => {
    if (valid) {
      emit('action', directoryForm)
    } else {
      emit('action',null)
    }
  })
}
const clearForm = () => {
  if (!directoryRef) return
  directoryRef.value.resetFields()
} 

const setForm = (data) => {
  directoryForm.value = Object.assign(directoryForm, data);
}

onMounted(() => {
   
})

defineExpose({ submitForm, clearForm, setForm })
</script>