<template>
  <div class="UploadFile-container">
    <el-upload :action="define.comUploadUrl+'/'+type" :headers="uploadHeaders" ref="elUpload"
      :on-success="handleSuccess" :multiple="limit!==1" :show-file-list="false" :accept="accept"
      :before-upload="beforeUpload" :on-exceed="handleExceed" :disabled="disabled" :limit="limit" v-if="!disabled">
      <el-button size="small" icon="el-icon-upload" v-if="!disabled" :disabled="disabled" :on-preview="handlePreview">{{buttonText}}</el-button>
      <div slot="tip" class="el-upload__tip" v-show="showTip">
        只能上传不超过{{fileSize}}{{sizeUnit}}的{{accept}}文件
      </div>
    </el-upload>
    <template v-if="fileList.length">
      <transition-group class="el-upload-list el-upload-list el-upload-list--text" tag="ul"
        name="el-list">
        <li class="el-upload-list__item is-success" v-for="(file,index) in fileList"
          :key="file.fileId">
          <el-tooltip class="item" effect="dark" placement="left">
            <div slot="content">
              <img class="avatar" :src="define.comUrl + file.url" style="max-width: 400px; max-height: 400px;">
            </div>
            <a class="el-upload-list__item-name" @click="handleClick(file)" style="flex: 1;">
              <i class="el-icon-download"></i>{{file.name}}
            </a>
          </el-tooltip>
          <label class="el-upload-list__item-status-label">
            <i class="el-icon-upload-success el-icon-circle-check"></i>
          </label>
          <i class="el-icon-close" v-if="!disabled" @click="handleRemove(file,index)"></i>
        </li>
      </transition-group>
    </template>
  </div>
</template>

<script>
const units = {
  KB: 1024,
  MB: 1024 * 1024,
  GB: 1024 * 1024 * 1024
}
import { getDownloadUrl } from '@/api/common'
export default {
  name: 'UploadFile',
  props: {
    value: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'annex'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    showTip: {
      type: Boolean,
      default: false
    },
    limit: {
      type: Number,
      default: 0
    },
    accept: {
      type: String,
      default: '*'
    },
    buttonText: {
      type: String,
      default: '选择文件'
    },
    sizeUnit: {
      type: String,
      default: 'MB'
    },
    fileSize: {
      default: 50
    },
  },
  data() {
    return {
      fileList: this.value,
      uploadHeaders: { Authorization: this.$store.getters.token }
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.fileList = val
      }
    }
  },
  methods: {
    beforeUpload(file) {
      const unitNum = units[this.sizeUnit];
      if (!this.fileSize) return true
      let isRightSize = file.size / unitNum < this.fileSize
      if (!isRightSize) {
        this.$message.error(`文件大小超过${this.fileSize}${this.sizeUnit}`)
      }
      // let isAccept = new RegExp(this.accept).test(file.type)
      // if (!isAccept) {
      //   this.$message.error(`应该选择${this.accept}类型的文件`)
      // }
      return isRightSize;
    },
    handleSuccess(res, file, fileList) {
      if (res.code == 200) {
        this.fileList.push({
          name: file.name,
          fileId: res.data.name,
          url: res.data.url
        })
        this.$emit('input', this.fileList)
      } else {
        fileList.filter(o => o.uid != file.uid)
        this.$emit('input', this.fileList)
        this.$message({ message: res.msg, type: 'error', duration: 1500 })
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制最多可以上传${this.limit}个文件`);
    },
    handlePreview(file) {
        // console.log(file);
    },
    handleRemove(file, index) {
      this.fileList.splice(index, 1)
      this.$refs.elUpload.uploadFiles.splice(index, 1)
      this.$emit("input", this.fileList)
      // this.$confirm(`确定移除${file.name}？`, '提示').then(() => {
      // }).catch(() => { })
    },
    handleChaeck(file, index) {
      window.open(this.define.comUrl + file.url, '_blank')
    },
    handleClick(file) {
      // 点击下载文件
      if (!file.fileId) return
      getDownloadUrl(this.type, file.fileId, file.name).then(res => {
        if (res.data.url) window.location.href = this.define.comUrl + res.data.url
      })
    }
  }
}
</script>