<template>
  <div>
    <el-upload
      class ="mt-pic-upload-list"
      :action="url +'/v1/files'"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :before-upload="beforeAvatarUpload"
      :on-change = "update"
      :on-success = "onSuccess"
      :on-error = "onError"
      :file-list="value"
      :on-remove="handleRemove">
      <i class="el-icon-plus"></i>
      <!-- <div slot="tip" class="el-upload__tip">
        至少上传一张照, 只能上传jpg/png文件，且不超过{{this.size ? this.size : '2MB'}}。
      </div> -->
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" size="tiny" append-to-body>
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        url: 'https://api.medmeeting.com',
        dialogVisible: false,
        dialogImageUrl: '',
        pic_list: []
      }
    },
    props: {
      value: Array,
      size: String, //图片大小的限制 ‘300Kb 3MB ’
      multi: Number //最多上传的图片数量
    },
    watch: {
      value: {
        handler (v) {
          this.pic_list = v
        },
        immediate: true
      }
    },
    methods: {
      update (file, fileList) {
      },
      onSuccess (response, file, fileList) {
        this.pic_list = fileList.map((item) => {
          if (item.response) {
            return {url: item.response.extra.absQiniuImgHash, status: item.status, name: item.name, size: item.size, uid: item.uid}
          }
          return {url: item.url, status: item.status, name: item.name, size: item.size, uid: item.uid}
        })
        this.$emit('input', this.pic_list)
      },
      onError () {
        this.$message.error('上传失败')
      },
      beforeAvatarUpload (file) {
        if (this.multi && this.pic_list.length >= this.multi) {
          this.$message.error(`最多只能上传${this.multi}张图片！`)
          this.$emit('input', this.pic_list)
          this.onOff = true
          return false
        }
        const isJPG = file.type === 'image/jpeg'
        const isPNG = file.type === 'image/png'
  
        var size = this.size ? this.size : '2MB'
        var sizeLowerCase = size.toLowerCase()
        var isrightsize = false
  
        if (sizeLowerCase.indexOf('kb') !== -1) {
          isrightsize = file.size / 1024 <= Number(sizeLowerCase.substr(0, sizeLowerCase.indexOf('kb')))
        }
        if (sizeLowerCase.indexOf('mb') !== -1) {
          isrightsize = file.size / 1024 / 1024 <= Number(sizeLowerCase.substr(0, sizeLowerCase.indexOf('mb')))
        }
  
        if (!isJPG && !isPNG) {
          this.$message.error('图片只能是 JPG或者 PNG 格式!')
          this.onOff = true
        }
        if (!isrightsize) {
          this.$message.error(`图片大小不能超过 ${size}!`)
          this.onOff = true
        }
        return (isJPG || isPNG) && isrightsize
      },
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      handleRemove (item, item2) {
        if (!this.onOff) {
          this.pic_list.splice(this.pic_list.findIndex((i) => {
            if (item.response) {
              return i.url === item.response.extra.absQiniuImgHash
            }
            return i.url === item.url
          }), 1)
          this.$emit('input', this.pic_list)
        }
        this.onOff = false
      }
    }
  }
</script>
<style lang = 'less' scoped>
  .mt-pic-upload-list {
    .el-upload-list--picture-card .el-upload-list__item{
      line-height: 148px;
    }
  }
</style>
<style lang="less">
.el-upload-list__item{
  // position: relative;
}
.el-upload-list--picture-card .el-upload-list__item-thumbnail{

}
.el-upload-list--picture-card .el-upload-list__item-status-label{
  background:#3094fa;
}
</style>
