<template>
  <div class="inline-auto-upload">
    <a-upload
      ref="upload"
      :action="url"
      :accept="accept"
      :multiple="false"
      :headers="header"
      @change="handleChange">
      <a-button type="primary" icon="upload" size="small">{{ desc }}</a-button><span style="font-size: 10px; color: red;">{{ autoComment }}</span>
    </a-upload>
  </div>
</template>

<script>
export default {
  props: [
    'url',
    'accept',
    'gwComFiles',
    'fileDesc',
    'comment',
    'uuid',
    'limit',
  ], // 上传基础路径
  data() {
    return {
      fileList: [],
      resList: [],
      length: 0, // 上传列表长度
      sum: 0, // 上传成功次数
      allurl: '', // 上传完整路径
      header: {
        persId: localStorage.getItem('persId'),
        accessToken: localStorage.getItem('accessToken'),
        orgId: localStorage.getItem('orgId'),
      },
      desc: '请选择要上传的文件',
      // autoComment: '注：.xls,.xlsx格式，不超过50MB',
      autoComment: '',
      autoUrl: '',
      filePath: '',
      fileName: '',
      dialogVisible: false,
    };
  },
  mounted() {
    if (this.gwComFiles) {
      this.fileList = this.gwComFiles;
    }

    if (this.fileDesc) {
      this.desc = this.fileDesc;
    }

    if (this.comment) {
      this.autoComment = this.comment;
    }
  },
  methods: {
    init(data) {
      this.length = 0;
      this.sum = 0;
      this.resList = [];
      this.$refs.upload.handleRemove(data);
    },
    handleChange(info) {
      this.length = info.fileList.length;
      if (info.file.status == 'error') {
        this.$message.error('上传出错，请修改文件！');
        this.$emit('uploadOver');
        this.$nextTick(() => {
          this.init(info.file);
        });
        return;
      }
      if (info.file.status == 'done') {
        this.sum = this.sum + 1;
        this.resList.push(info.fileList);
        if (this.sum == this.length) {
          this.$emit('uploadOver', info.file.response);
          this.$nextTick(() => {
            this.init(info.file);
          });
        }
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
