<template>
  <div style="width: 360px;margin: 20px auto;">
    <a-upload-dragger
      v-if="!uploadText"
      :headers="header"
      :multiple="true"
      :action="allurl"
      :accept="accept"
      :remove="handleRemove"
      :before-upload="beforeUpload"
      ref="upload">
      <p class="ant-upload-drag-icon">
        <a-icon type="inbox" />
      </p>
      <p class="ant-upload-text">
        将文件拖到此处，或<em>点击添加</em>
      </p>
    </a-upload-dragger>
  </div>
</template>

<script>
import request from '@/utils/gw_ajax_request';

export default {
  props: ['url', 'accept', 'uploadText', 'dcType', 'picType'], // 上传基础路径
  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: {
    handleRemove(file) {
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList;
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file];
      return false;
    },
    init(data) {
      this.length = 0;
      this.sum = 0;
      this.resList = [];
      this.fileList = [];
      console.log(this.$refs.upload.remove);
      // this.$refs.upload.handleRemove(data)
    },
    submit() {
      //debugger
      const { fileList } = this;
      const resData = [];
      let flag = 0;
      fileList.forEach((file, index) => {
        const formData = new FormData();
        formData.append('file', file);
        request
          .post(this.allurl.replace('/pdcApi', ''), formData)
          .then((res) => {
            flag++;
            if (res.success) {
              resData[index] = res.data;
              if (flag == fileList.length) {
                this.$emit('uploadOver', resData);
              }
            } else {
              this.$message.error(res.message);
              this.$emit('uploadOver');
            }
          })
          .catch(() => {
            this.$message.error('上传文件失败!');
            this.$emit('uploadOver');
          });
      });
    },
    submitUpload(id) {
      console.log('上传', id);
      if (this.dcType == 'siluan') {
        // 处理江西省级平台 四乱迁移 核实情况为是的时候需要上传图片
        this.allurl = this.url + '?bizId=' + id + '&bizType=proff';
      } else {
        this.allurl = this.url + id;
      }
      // 处理江西省级平台 四乱迁移 上传历史图片和佐证材料
      if (this.picType == 'history') {
        this.allurl = this.url + id + '&bizType=history';
      } else if (this.picType == 'confirm') {
        this.allurl = this.url + id + '&bizType=proff';
      }
      if (this.fileList && this.fileList.length > 0) {
        this.submit();
        return false;
      } else {
        return true;
      }
    },
    handleChange(info, ...args) {
      console.log(info, 'handleChange', ...args);
      if (info.file.status == 'error') {
        this.$message.error('上传出错，请修改文件！');
        // this.$emit('uploadOver')
        this.init(info.file);
        return;
      }
      this.resList.push(info.fileList);
      this.sum = this.sum + 1;
      if (this.sum == this.length) {
        this.$emit('uploadOver', this.resList);
        this.init(info.file);
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
