<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"
      :on-remove="handleRemove">
      <i class="el-icon-plus"></i>
      <div slot="tip" class="el-upload__tip">
        至少上传一张照片<br>
        只能上传jpg/png文件，且不超过2M
      </div>
    </el-upload>
    <el-dialog v-model="dialogVisible" size="tiny">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        url: 'http://api.medmeeting.com',
        dialogVisible: false,
        dialogImageUrl: ''
      }
    },
    props: {
      value: Array
    },
    methods: {
      update (file, fileList) {
        console.log(file)
        console.log(fileList)
      },
      onSuccess (response, file, fileList) {
        let a = fileList.map((item) => {
          return item.response.extra.absQiniuImgHash
        })
  
        this.$emit('input', a)
      },
      onError () {
        this.$message.error('上传失败')
      },
      beforeAvatarUpload (file) {
        const isJPG = file.type === 'image/jpeg'
        const isPNG = file.type === 'image/png'
        const isLt2M = file.size / 1024 / 1024 < 2
  
        if (!isJPG && !isPNG) {
          this.$message.error('图片只能是 JPG或者 PNG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('图片大小不能超过 2MB!')
        }
        return (isJPG || isPNG) && isLt2M
      },
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      handleRemove (item, item2) {
        console.log(item)
        console.log(item2)
      }
    }
  }
</script>
<style lang = 'less' scoped>
  .mt-pic-upload-list {
    .el-upload-list--picture-card .el-upload-list__item{
      line-height: 148px;
    }
  }
</style>
