<template>
  <!-- doc文档的新增或更新笔记-->
  <el-dialog
    title="添加笔记"
    :visible.sync="dialogVisible"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :append-to-body="true"
    v-loading="loading"
  >
    <el-form :model="form" label-width="50px" :rules="rules" ref="form">
      <el-form-item label="原文">
        <el-input
          type="textarea"
          rows="5"
          v-model="form.bookmarkContent"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="笔记" prop="noteContent">
        <el-input
          placeholder="请输入笔记"
          v-model="form.noteContent"
          type="textarea"
          rows="5"
        ></el-input>
      </el-form-item>
      <el-form-item label="标签" disabled style="width: 100%">
        <el-select
          style="width: 100%"
          v-model="tagsArr"
          multiple
          filterable
          remote
          default-first-option
          allow-create
          placeholder="请输入文章标签（可多个）"
          :remote-method="remoteMethod"
          :loading="loading"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="handleCancelSubmit">取消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmitAddNote"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addOrUpdateBookMarkDoc } from "@/api/doc/bookMark";
import { tagSuggest } from "@/api/doc/tag";

export default {
  props: {
    formData: {
      type: Object,
      default: () => ({
        docId: "", //文档Id
        bookmarkContent: "", //笔记内容
      }),
    },
  },
  data() {
    return {
      dialogVisible: false, //新建笔记窗口可见性
      form: {
        docId: "",
        docSType: "doc", //文档类型
        bookmarkContent: "", //原文
        noteContent: "", //笔记
        type: "note", //	笔记类型，新增时必传，[underline-下划线,note-笔记,highlight-高亮]
        offset: "0", //偏移量，新增时必传
        sectionId: "0", //父节点ID，新增时必传
      },
      tagsArr: [], //标签，提交给后台的时候使用多个用逗号分割
      options: [], //标签可选项
      rules: {
        noteContent: [
          {
            required: true,
            message: "请输入笔记",
            trigger: "change",
          },
        ],
      },
      loading: false,
    };
  },
  watch: {
    formData: {
      handler(value) {
        console.log("value", value);
        const { docId, bookmarkContent } = value;
        if (docId && bookmarkContent) {
          this.form.docId = docId;
          this.form.bookmarkContent = bookmarkContent;
        }
      },
      deep: true,
      immediate: true,
    },
  },

  methods: {
    //提交新增笔记
    handleSubmitAddNote() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const formData = new FormData();
          for (const key in this.form) {
            formData.append(key, this.form[key]);
          }
          formData.append("tags", this.tagsArr.join(","));
          let res = await addOrUpdateBookMarkDoc(formData);
          if (res.code == 200) {
            this.$message.success('新建笔记成功！')
            this.dialogVisible = false
            this.$emit('update')
          }
        }
      });
    },

    //取消新增笔记
    handleCancelSubmit() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    // 远程搜索标签
    remoteMethod(keyword) {
      if (keyword !== "") {
        this.loading = true;
        const params = {
          keyword,
        };
        tagSuggest(params).then((res) => {
          this.loading = false;
          if (res.code == 200) {
            this.options = res.data.map((item) => {
              return {
                label: item.tagName,
                value: item.id,
              };
            });
          }
        });
      } else {
        this.options = [];
      }
    },
  },
};
</script>

<style></style>
