<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title + $t('common.xuxibiji')"
    width="80%"
  >
    <el-form :model="form" :rules="rules" ref="formRef" label-position="top" class="edit_note">
      <el-form-item label="" prop="noteTitle">
        <el-input v-model="form.noteTitle" placeholder="请输入标题" size="large" show-word-limit maxlength="20"/>
      </el-form-item>
      <el-form-item label="" prop="noteContent">
        <BlockNoteEditor v-model="editorContent" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="flex-center">
        <div>
          <el-button @click="dialogVisible = false" type="primary" plain>{{ $t('common.cancel') }}</el-button>
          <el-button type="primary" @click="submitForm" class="gradient">{{ $t('common.confirm') }}</el-button>
        </div>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import BlockNoteEditor from '@/components/BlockNoteEditor.vue'
import { ref, reactive, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n' 
const { t } = useI18n()
const emit = defineEmits(['submit']);

const dialogVisible = ref(false)
const title = ref('');
// 编辑器内容
// 表单引用
const formRef = ref(null);

const editorContent = ref(null);
const form = reactive({
  noteId: '',
  noteTitle: '',
  noteContent: ''
})
watchEffect(() => {
  // 将编辑器内容赋值给表单的workflowContent
  form.noteContent = JSON.stringify(editorContent.value);
})
const rules = {
  noteTitle: [{ required: true, message: t('common.pleaseInputTitle'), trigger: 'blur' }],
  noteContent: [{ required: true, message: t('common.pleaseInputContent'), trigger: 'change' }]
}
const submitForm = async () => {
   await formRef.value.validate((valid, fields,name) => {
    console.log(valid, fields,name)
    if (!valid) {
      //报错第一个key
      let firstKey = Object.keys(fields)[0]
      DGTMessage.warning(fields[firstKey][0].message)
      return
    }
    emit('submit', form);
    dialogVisible.value = false
  })
};
const openDialog = (item) => {
  dialogVisible.value = true;
  editorContent.value = [
    {
      "id": "77efedfb-1c9e-4377-bea4-4d4dfdc20c59",
      "type": "paragraph",
      "props": {
            "backgroundColor": "default",
            "textColor": "default",
            "textAlignment": "left"
        },
        "content": [],
        "children": []
    }
  ];
  if(item){
    form.noteId = item.noteId;
    form.noteTitle = item.noteTitle;
    form.noteContent = "";
    // 解析JSON字符串为对象
    if(item.noteContent){
      editorContent.value = JSON.parse(item.noteContent);
    }
  }
  if(item.noteId){
    title.value = t('common.edit')
  }else{
    title.value = t('common.add')
  }
};

defineExpose({
  openDialog
});
</script>
<style lang="scss">
.edit_note{
  height: calc(100vh - 300px);
  min-height: 400px;
  overflow-y: auto;
  .el-input--large .el-input__wrapper {
    border: none !important;
    box-shadow: none !important;
    border-top:1px solid #e5e7eb !important;
    border-bottom:1px solid #e5e7eb !important;
  }
  .el-input__inner{
    font-size: 18px !important;
  }
}
</style>
