<template>
  <div class="upload-wrapper upload-wrapper-drag">
    <el-upload
      ref="upload"
      accept=".jpg,.png"
      drag
      :before-upload="beforeUpload"
      :action="url"
      :on-exceed="handleExceed"
      :limit="limit"
      :on-success="handleSuccess"
      :file-list="fileList"
      :disabled="disabled"
    >
      <div class="plus-icon"><i class="el-icon-plus"></i></div>
      <div class="cn-tips">点击或拖拽图片到此上传</div>
      <div class="en-tips">Only png, jpg can be uploaded, and the size does not exceed 100MB</div>
    </el-upload>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth';
export default {
  name: 'UploadAudio',
  props: {
    limit: {
      type: Number,
      default: 1,
    },
    itemIndex: {
      type: Number,
      default: null,
    },
    fileList: {
      type: Array,
      default: () => [],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {};
  },
  computed: {
    url() {
      let userInfor = JSON.parse(getToken());
      let SessionID = '';
      let UserCode = '';
      let UserType = '';
      if (userInfor) {
        UserCode = userInfor.user_code;
        UserType = userInfor.user_type;
        SessionID = userInfor.session_id;
      }
      return `${process.env.VUE_APP_BASE_API}/GCLSFileServer/WebFileUpload?UserCode=${UserCode}&UserType=${UserType}&SessionID=${SessionID}&SecurityLevel=Mid"`;
    },
  },
  watch: {},
  methods: {
    beforeUpload(file) {
      // 可以用来限制文件大小
      if (file.size > 100 * 1024 * 1024) {
        this.$message.warning('上传图片大小不能超过100M');
        return false; // 必须返回false
      }
    },
    upload(file) {},
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 ${this.limit ? this.limit : 1} 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`,
      );
    },
    handleSuccess(response, file, fileList) {
      if (response.status === 1) {
        this.$message.success('上传成功');
        this.$emit('changeFillId', response.file_info_list[0], fileList);
      }
    },
    onProgress(event, file, fileList) {
      let num = ((event.loaded / event.total) * 100) | 0; //百分比
      this.curPercentage = num;
      if (this.curPercentage == 100) {
        this.isShowJinDuTiao = false;
        this.curPercentage = 0;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.upload-wrapper {
  min-height: 224px;
  background-color: #f5f5f5;
  .plus-icon {
    margin: 50px auto 20px auto;
    color: #4e5969;
  }
  .cn-tips {
    font-size: 14px;
    font-weight: 400;
    line-height: 22px;
    color: #1d2129;
  }
  .en-tips {
    font-size: 12px;
    font-weight: 400;
    line-height: 20px;
    color: #86909c;
  }
}
</style>
<style lang="scss">
.upload-wrapper-drag {
  .el-upload {
    width: 100%;
  }
  .el-upload-dragger {
    border: none;
    background-color: transparent;
    width: 100%;
  }
}
</style>
