<template>
  <div style="width: 360px;margin: 20px auto;">
    <a-upload
      :multiple="true"
      :action="url"
      :headers="headers"
      :accept="accept"
      :disabled="feedback"
      @change="changeFileList"
      ref="upload">
      <a-button size="small" :disabled="feedback" type="primary" style="margin: 0">上传</a-button>
    </a-upload>
  </div>
</template>

<script>
export default {
  props: {
    url: {
      type: String,
      default: '',
    },
    accept: {
      type: String,
      default: '',
    },
    feedback: {
      type: Boolean,
      default: false,
    },
  }, // 上传基础路径
  data() {
    return {
      fileList: [],
      resList: [],
      length: 0, // 上传列表长度
      sum: 0, // 上传成功次数
      allurl: '', // 上传完整路径
      header: {
        persId: localStorage.getItem('persId'),
        accessToken: localStorage.getItem('accessToken'),
      },
    };
  },
  computed: {
    userid() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '';
    },
  },
  created() {},
  methods: {
    // 初始化，
    init() {
      // this.$refs.upload.clearFiles();
      this.length = 0;
      this.sum = 0;
      this.allurl = '';
      this.resList = [];
    },
    // 开始上传，文件列表为空直接返回turn表示结束，不为空触发上传并返回false，由uploadOver判断结束时间
    submitUpload(id) {
      this.allurl = this.url + id;
      if (this.length) {
        this.$nextTick(() => {
          this.$refs.upload.submit();
        });
        return false;
      } else {
        return true;
      }
    },
    // 上传出错，向上触发结束
    upError(err, file, fileList) {
      this.$message.error('上传出错，请修改文件！');
      this.$emit('uploadOver');
      this.init();
    },
    // 全部上传成功，向上触发结束
    uploadSuccess(response, file, fileList) {
      if (!response.success) {
        this.$message.error('上传出错，请修改文件！');
        this.$emit('uploadOver');
        this.init();
        return;
      }
      this.resList.push(response.data);
      console.log(this.resList);
      this.sum = this.sum + 1;
      if (this.sum == this.length) {
        this.$emit('uploadOver', this.resList);
        this.init();
      }
    },
    // 及时更新列表长度
    changeFileList(file, fileList) {
      this.length = fileList.length;
    },
  },
};
</script>

<style lang='less' scoped>

</style>
