export default {
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  data () {
    return {
      formData: {
        content: '',
        size: 0,
        type: ''
      },
      isImage: false,
      isVideo: false,
      loading: false,
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        }
      }
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCancel () {
      this.$emit('input', false)
      this.form.resetFields()
      this.formData = {
        content: '',
        size: 0,
        type: ''
      }
    },
    handleImageChange (info) {
      if (info.file.status === 'uploading') {
        this.loading = true
        return
      }
      if (info.file.status === 'done') {
        this.formData.content = info.file.response.link
        this.isImage = info.file.type.indexOf('image') > -1
        this.isVideo = info.file.type.indexOf('video') > -1
        if (this.isVideo) {
          this.formData.duration = 0
        }
        this.formData.type = info.file.type
        this.formData.size = info.file.size
        this.loading = false
      }
    },
    beforeImageUpload (file) {
      const isImage = file.type.indexOf('image') > -1
      const isVideo = file.type.indexOf('video') > -1

      if (!isImage && !isVideo) {
        this.$message.error('请上传图片或视频')
      }

      let isLt = false
      const fileSize = file.size / 1024 / 1024

      if (isImage) {
        if (fileSize > 5) {
          this.$message.error('图片必须小于5MB!')
        } else {
          isLt = true
        }
      }

      if (isVideo) {
        if (fileSize > 100) {
          this.$message.error('视频必须小于100MB!')
        } else {
          isLt = true
        }
      }

      return (isImage || isVideo) && isLt
    },
    normFile (e) {
      if (e.file.status === 'done') {
        return e.file.response.link
      } else {
        return false
      }
    }
  }
}
