<script>
export default {
  name: 'AttachmentsMixin',
  methods: {
    // 添加附件
    handleAddAttachment (file) {
      this.form.attachments.push({
        uid: file.uid,
        name: file.name,
        lastModified: file.lastModified,
        size: file.size,
        // 文件是否正在上传
        __uploading: true
      })
    },
    // 附件上传成功
    handleAttachmentUploadSuccess ({ data, file }) {
      const targetFile = this.form.attachments.find(item => item.uid === file.uid)
      if (targetFile != null) {
        targetFile.fileKey = data.fileKey
        delete targetFile.__uploading
      }
    },
    // 附件上传失败
    handleAttachmentUploadError ({ error, file }) {
      const targetFileIndex = this.form.attachments.findIndex(item => item.uid === file.uid)
      if (targetFileIndex !== -1) {
        this.form.attachments.splice(targetFileIndex, 1)
        this.$tip.apiFailed(`附件 ${file.name} 上传失败，原因：${error}`)
      }
    }
  }
}
</script>
