<template>
  <div style="text-align: center">
    <div class="upload-container">
      <el-upload
        :multiple="false"
        :show-file-list="false"
        :on-success="handleImageSuccess"
        class="image-uploader"
        drag
        :action="uploadUrl"
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text"><em>点击上传</em></div>
      </el-upload>
      <div v-show="url" class="image-preview">
        <div v-show="url" class="image-preview-wrapper">
          <el-image class="img" :src="url" alt="">
            <div slot="error">
              <el-image :src="fileImage" />
            </div>
          </el-image>
          <div class="image-preview-action">
<!--            <i v-if="isImage" class="el-icon-zoom-in" style="margin-right: 15px" />-->
            <i class="el-icon-download" @click="handleDownload()" />
            <i class="el-icon-delete" style="margin-left: 15px;" @click="handleRemove()" />
          </div>
        </div>
      </div>
    </div>
    <el-tag v-if="title" style="margin-top: 5px;">{{ title }}</el-tag>
  </div>
</template>

<script>

export default {
  name: 'UploadFile',
  props: {
    value: String,
    title: String
  },
  data() {
    return {
      uploadUrl: process.env.VUE_APP_FILE_UPLOAD_URL,
      fileImage: require('@/assets/images/file-icon.png'),
      url: null,
      name: null // 文件名
    }
  },
  computed: {
    isImage() {
      if (this.url == null) {
        return false
      }
      const suffix = this.getFileUploadExtension()
      const regexp = new RegExp('(gif|jpg|jpeg|png|GIF|JPG|PNG)')
      return regexp.test(suffix)
    }
  },
  watch: {
    value(val) {
      this.url = val
    }
  },
  created() {
    this.url = this.value
  },
  methods: {
    rmImage() {
      this.emitInput('')
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    handleImageSuccess(response) {
      const { url } = response
      this.emitInput(url)
    },
    handleDownload() {
      window.open(this.url)
    },
    handleRemove(file, fileList) {
      this.url = ''
      this.$emit('input', '')
    },
    /**
     * 获取文件扩展名
     */
    getFileUploadExtension() {
      if (this.url !== null) {
        const value = this.url.trim()
        if (value.length > 0) {
          const index = value.lastIndexOf('.') + 1
          return value.substring(index).toLowerCase()
        }
      }
      return ''
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-container {
  width: 148px;
  height: 148px;
  position: relative;
  .image-uploader {
    height: 100%;
  }
  .image-preview {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    border: 1px dashed #d9d9d9;
    .image-preview-wrapper {
      padding: 10px;
      position: relative;
      width: 100%;
      height: 100%;
      .img {
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, .5);
      transition: opacity .3s;
      cursor: pointer;
      line-height: 148px;
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
}

</style>
