<template>
  <div class="qs-upload-picture" :style="style" v-loading="loading">
    <el-upload v-bind="uploadOptions">
      <div v-show="url" :style="{ width, height }" class="preview">
        <qs-picture-view :url="url" />
      </div>
      <div v-show="!url" class="upload">
        <i class="el-icon-plus" :style="{ fontSize: iconSize }"></i>
      </div>
    </el-upload>
    <span v-show="url" class="remove" @click="reset">
      <nm-icon name="close" />
    </span>
  </div>
</template>
<script>
import mixins from '../upload-mixins'

// 接口
const api = $api.base.uploader

export default {
  mixins: [mixins],
  data() {
    return {
      previewUrl: ''
    }
  },
  props: {
    /** 宽度 */
    width: {
      type: String,
      default: '60px'
    },
    /** 高度 */
    height: {
      type: String,
      default: '60px'
    },
    /** 接受上传的文件类型 */
    accept: {
      type: String,
      default: 'image/jpeg,image/png'
    },
    /** 可接受文件类型 */
    fileType: {
      type: Array,
      default() {
        return ['jpg', 'jpeg', 'png']
      }
    },
    /** 图标大小 */
    iconSize: {
      type: String,
      default: '2em'
    }
  },
  computed: {
    uploadOptions() {
      return {
        action: api.getUploadUrl(),
        headers: {
          Authorization: 'Bearer ' + this.accessToken
        },
        data: {
          module: this.module,
          group: this.group
        },
        accept: this.accept,
        drag: this.drag,
        autoUpload: true,
        showFileList: false,
        beforeUpload: this.onBeforeUpload,
        onSuccess: this.onSuccess,
        onError: this.onError,
        disabled: this.disabled
      }
    },
    style() {
      return { width: this.width, height: this.height }
    }
  },
  methods: {
    onBeforeUpload(file) {
      // 文件格式验证
      if (!this.verifyFileType(file)) {
        return false
      }
      // 验证是否超出最大上传数
      if (this.maxSizeBytes && file.size > this.maxSizeBytes) {
        this._error(`文件大小${this.getSizeUnit(file.size)}超过了最大允许值${this.maxSize}`)
        return false
      }
      if (this.beforeUpload && typeof this.beforeUpload === 'function' && !this.beforeUpload(file)) {
        return false
      }

      this.loading = true
      return true
    },
    onSuccess(res, file) {
      const data = res
      if (data.code === 1) {
        this.url = data.data.fullPath
        this.$emit('input', this.url)
        this.$emit('success', res, file)
      } else {
        this.$emit('error')
      }
      this.loading = false
    },
    reset() {
      this.url = ''
      this.$emit('input', this.url)
    }
  }
}
</script>

<style lang="scss">
.qs-upload-picture {
  position: relative;
  display: inline-block;
  box-sizing: border-box;
  border: 1px dashed #ccc;
  background-color: #fff;
  text-align: center;
  color: #8c939d;
  cursor: pointer;
  border-radius: 5px;
  overflow: hidden;

  > div {
    width: 100%;
    height: 100%;
  }

  .preview {
    width: 100%;
    height: 100%;
    line-height: 100%;
    text-align: center;
    img {
      max-width: 100%;
      max-height: 100%;
    }
    .no-picture {
      font-size: 2em;
    }
  }

  .el-upload {
    width: 100%;
    height: 100%;
  }

  .el-upload-dragger {
    position: relative;
    top: 50%;
    border: none;
    margin-top: -50%;
    width: 100%;
    height: auto;
    .text-box {
      font-size: 14px;

      .el-icon-upload {
        margin: auto;
        font-size: 5em;
        color: #c0c4cc !important;
      }

      em {
        color: #409eff;
        font-style: normal;
      }

      .text {
        font-size: 13px;
        line-height: 130%;
      }
    }
  }

  .el-loading-spinner {
    line-height: 100%;
  }

  .remove {
    position: absolute;
    display: none;
    top: 0;
    right: 0;
    width: 30px;
    height: 30px;
    line-height: 27px;
    border-radius: 15px;
    background-color: #f56c6c;
    color: #ccc;
    font-size: 16px;

    &:hover {
      color: #fff;
    }
  }

  &:hover {
    border-color: #409eff;

    .remove {
      display: inline-block;
    }
  }
}
</style>
