<template>
  <el-dialog :title="title" v-model="dialogVisible" width="800px">
    <el-form :model="form" ref="addForm" :rules="rules" label-width="100px">
      <el-form-item label="知识库名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="资源类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="1"> 文件 </el-radio>
          <el-radio :label="2"> 纯图片 </el-radio>
          <el-radio :label="3"> 自主编辑 </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <Editor v-model:modelValue="form.remark" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
const dialogVisible = ref(false) // 弹窗的是否展示

const form = ref()
const rules = ref({
  name: { required: true, message: '名称不可为空', trigger: 'blur' }
})

const title = ref('')

const addForm = ref()

const emit = defineEmits(['update'])

const open = (val) => {
  dialogVisible.value = true
  if (val) {
    title.value = '修改知识库'
    form.value = { ...val }
  } else {
    title.value = '新增知识库'
    form.value = {
      name: '',
      type: 1,
      remark: undefined
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

function handleSave() {
  addForm.value.validate((valid) => {
    if (valid) {
      emit('update', form.value)
      dialogVisible.value = false
    }
  })
}
</script>

<style scoped></style>
