<template>
  <el-upload
    class="avatar-uploader"
    action
    :accept="accept"
    :show-file-list="showFileList"
    :http-request="httpRequest"
    v-bind="$attrs"
  >
    <slot>
      <img v-if="value" :src="value" class="avatar" />
      <i v-else class="el-icon-plus uploader-icon"></i>
    </slot>
  </el-upload>
</template>

<script>
import cAction from '@action'

export default {
  name: 'VUpload',
  props: {
    value: { type: String },
    accept: { type: String, default: 'image/*' },
    showFileList: { type: Boolean, default: false }
  },
  data() {
    return {}
  },
  computed: {
    fileName() {
      return this.value ? this.value.split('/').pop() : ''
    }
  },
  methods: {
    httpRequest(xhr) {
      cAction.Player.uploadFile({ file: xhr.file })
        .then(response => {
          if (response.success) {
            this.$emit('input', response.url)
          }
        })
        .catch(error => {
          console.log(error)
        })
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #b80037;
}
.uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 220px;
  height: 220px;
  line-height: 220px;
  text-align: center;
}
.avatar {
  width: 220px;
  height: 220px;
  display: block;
  object-fit: contain;
}
</style>
