<template>
  <div class="upload">
    <el-upload action :disabled="disabled" :show-file-list="false" :http-request="httpRequest">
      <slot></slot>
      <el-button type="text" icon="el-icon-upload">{{ $t('viewerWork.clickUpload') }}</el-button>
      <template v-slot:tip>
        <div class="el-upload__tips">
          <slot name="tip"></slot>
        </div>
      </template>
    </el-upload>
    <div class="file-list" v-if="fileList.length">
      <div class="file-list-item" v-for="(item, index) in fileList" :key="index">
        <a :href="item.url" :download="item.name" target="_blank">
          <i class="el-icon-document"></i>
          {{ item.name }}
        </a>
        <div>
          <a href="javascript:;" @click="handleRemove(index)" style="margin-right: 10px" v-if="!disabled">
            <el-tooltip effect="dark" :content="$t('viewerWork.delete')">
              <i class="el-icon-delete"></i>
            </el-tooltip>
          </a>
          <a :href="item.url" :download="item.name" target="_blank">
            <el-tooltip effect="dark" :content="$t('viewerWork.download')">
              <i class="el-icon-download"></i>
            </el-tooltip>
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { uploadFile } from '@/api/common'

export default {
  name: 'VUpload',
  props: {
    value: { type: [String, Array] },
    disabled: { type: Boolean, default: false }
  },
  data() {
    return {
      fileList: []
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(value) {
        if (!value) {
          return
        }
        let fileList = []
        if (Array.isArray(value)) {
          fileList = value.map(item => {
            return { name: item.name || item, url: item.url || item }
          })
        } else {
          fileList.push({ name: this.$t('viewerWork.downloadName'), url: value })
        }
        this.fileList = fileList
      }
    }
  },
  methods: {
    httpRequest(xhr) {
      this.isUploading = true
      uploadFile({ file: xhr.file })
        .then(response => {
          if (response.success) {
            if (Array.isArray(this.value)) {
              this.fileList.push({ name: xhr.file.name, url: response.url })
              this.$emit('input', this.fileList)
            } else {
              this.fileList = [response.url]
              this.$emit('input', response.url)
            }
          }
        })
        .catch(error => {
          this.$message.error(error.message)
        })
    },
    handleRemove(index) {
      this.fileList.splice(index, 1)
      this.$emit('input', Array.isArray(this.value) ? this.fileList : '')
    }
  }
}
</script>

<style lang="scss" scoped>
.file-list-item {
  display: flex;
  margin-bottom: 10px;
  padding: 0 10px;
  justify-content: space-between;
  line-height: 30px;
  background-color: #fff;
  border-radius: 4px;

  a {
    text-decoration: none;
    color: #333;
    &:hover {
      color: #b49441;
    }
  }
}
</style>
