<script>
import { publish, save } from '@/api/cms/article'
export default {
  name: 'BaseMixin',
  methods: {
    // 打开封面选择
    openCoverSelect () {
      this.$refs.articleCoverSelect.open(this.$refs.richEditor.getImages())
    },
    // 保存草稿/更新文章
    save () {
      if (this.isWorking.save) {
        return Promise.resolve()
      }
      if (this.form.title.trim() === '') {
        this.$tip.warning('请填写文章标题！')
        return
      }
      for (const attach of this.form.attachments) {
        if (attach.__uploading) {
          this.$tip.warning('存在正在上传中的附件，请等待上传完成！')
          return
        }
      }
      return new Promise((resolve, reject) => {
        this.isWorking.save = true
        save ({
          ...this.form,
          // 在前后方添加“,“，方便模糊查询
          categoryIds: ',' + this.form.categoryIds.join(',') + ',',
          // 在前后方添加“,"，方便模糊查询
          tagIds: ',' + this.form.tagIds.join(',') + ',',
          // 图片集
          images: JSON.stringify(this.$refs.richEditor.getImages()),
          // 附件集
          attachments: JSON.stringify(this.form.attachments)
        })
          .then(data => {
            this.form.id = data.id
            // 拷贝一份作为已保存的数据，用于保存检测
            this.savedForm = JSON.parse(JSON.stringify(this.form))
            resolve(data)
          })
          .catch(e => {
            this.$tip.apiFailed(e)
            reject(e)
          })
          .finally(() => {
            this.isWorking.save = false
          })
      })
    },
    // 保存并关闭
    saveAndClose () {
      this.save()
        .then(() => {
          this.forceClose()
        })
    },
    // 强制关闭
    forceClose () {
      this.visible = false
      this.exitDialog.visible = false
    },
    // 退出
    exit () {
      // 已保存或未发生任何改动，直接关闭
      // - 内容会受到编辑器初始化值影响，编辑器初始化空值为<p><br></p>，需要做进一步空处理
      const copyForm = JSON.parse(JSON.stringify(this.form))
      copyForm.content = copyForm.content === '<p><br></p>' ? '' : copyForm.content
      if (JSON.stringify(copyForm) === JSON.stringify(this.savedForm)) {
        this.visible = false
        return
      }
      // 未保存，做弹窗提示
      let operaName = '保存'
      if (this.form.status === this.$const.ARTICLE_STATUS.ONLINE) {
        operaName = '更新'
      }
      this.exitDialog.message = `当前存在数据还未${operaName}，是否继续退出？`
      this.exitDialog.confirmButtonText = `${operaName}并退出`
      this.exitDialog.visible = true
    },
    // 发布
    publish () {
      if (this.isWorking.publish) {
        return
      }
      if (this.form.title.trim() === '') {
        this.$tip.warning('请填写文章标题！')
        return
      }
      if (this.$refs.richEditor.getText().trim() === '' &&
        this.$refs.richEditor.getImages().length === 0 &&
        this.form.attachments.length === 0) {
        this.$tip.warning('请输入文章内容！')
        return
      }
      for (const attach of this.form.attachments) {
        if (attach.__uploading) {
          this.$tip.warning('存在正在上传中的附件，请等待上传完成！')
          return
        }
      }
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        this.isWorking.publish = true
        // 执行保存
        this.save()
          .then(() => {
            // 执行发布
            publish(this.form.id)
              .then(() => {
                // 发布成功后关闭窗口
                this.visible = false
              })
              .catch(e => {
                this.$tip.apiFailed(e)
              })
              .finally(() => {
                this.isWorking.publish = false
              })
          })
          .catch(() => {
            this.isWorking.publish = false
          })
      })
    },
    // 提取摘要
    extractDigest () {
      this.form.contentDigest = this.$refs.richEditor.getDigest()
    },
    // 编辑器创建完成
    handleEditorCreated () {
      // 启用图片上传功能
      this.$refs.richEditor.enableImageUpload({
        url: '/cms/article/upload/image',
        maxFileSize: 5
      })
    }
  }
}
</script>
