<template>
  <div>
    <el-upload
      :action="path"
      :headers="{ Authorization: token }"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-change="handleAvatarChange"
      :limit="limit"
      :class="{hide: hideUploadEdit}"
      :before-upload="beforeAvatarUpload"
      :file-list="fileList"
      ref="upload"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog v-if="!isLogist" :visible.sync="dialogVisible">
      <img class="dialog-img" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>

<script>
import Cookie from 'js-cookie'
import config from '../plugins/config'
export default {
  data () {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      resourcesUrl: '',
      initEd: false,
      hideUploadEdit: false,
      path: config.path + '/p/file/upload'
    }
  },
  props: {
    value: {
      default: '',
      type: String
    },
    // 最大上传数量
    limit: {
      default: 3,
      type: Number
    },
    fileList: {
      default:()=>[],
      type: Array
    },
    isLogist: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    token () {
      return  Cookie.get('token')
    }
  },
  mounted() {
    this.hideUploadEdit = this.fileList.length >= this.limit
  },
  methods: {
    /**
     * 图片预上传
     */
    handleAvatarChange() {
      if (this.$refs.upload.uploadFiles.length >= this.limit) { this.hideUploadEdit = true }
      this.$forceUpdate()
    },
    // 图片上传
    handleUploadSuccess (response, file, fileList) {
      console.log(fileList)
      this.resourcesUrl = file.response.resourcesUrl
      let pics = fileList.map(file => {
        return file.response ? file.response.filePath : file.url.replace(config.picDomain, '')
      }).join(',')
      this.$emit('input', pics)
    },
    // 限制图片上传大小
    beforeAvatarUpload (file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error(this.$t('imageSizeCannotExceed2MB'))
        return false
      }

      // 判断图片类型来筛选图片格式上传
      let _type = file.type;
      if (_type.indexOf('png') == -1 && _type.indexOf('jpg') == -1 && _type.indexOf('jpeg') == -1) {
        this.$message.error(this.$t('imageNoformatUpload'))
        return false
      }
      return
    },
    handleRemove (file, fileList) {
      console.log(fileList)
      let pics = fileList.map(file => {
        return file.response ? file.response.filePath : file.url.replace(config.picDomain, '')
      }).join(',')
      this.$emit('input', pics)
      setTimeout(() => { this.hideUploadEdit = false }, 600)
    },
    handlePictureCardPreview (file) {
      console.log(file);
      if (this.isLogist) {
        this.$emit('imgPreview', file.url)
      } else {
        this.dialogVisible = true
        this.dialogImageUrl = file.url
      }
    }
  }
}
</script>

<style>
.hide .el-upload--picture-card{
  display: none;
}
.dialog-img {
  display: block;
  object-fit: contain;
  margin: 0 auto;
  max-width: 100%;
}
</style>
