<template>
  <el-dialog title="上传附件" :visible.sync="open" width="500px" append-to-body>
    <el-upload
      ref="upload"
      multiple
      accept=".xlsx, .xls"
      :headers="upload.headers"
      :action="upload.url"
      :disabled="upload.isUploading"
      :on-success="successHandleList"
      :auto-upload="false"
      :data="params"
      :file-list="fileList"
      drag>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        将文件拖到此处，或
        <em>点击上传</em>
      </div>
      <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
    </el-upload>
  </el-dialog>
</template>

<script>
  import FileUpload from '@/components/FileUpload'
  import { getToken } from "@/utils/auth";
  export default {
    components: {
      FileUpload
    },
    props: {
      attachType: {
        type: String
      }
    },
    data() {
      return {
        // 项目导入参数
        upload: {
          title: "导入资料附件",
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/attach/upload"
        },
        open: false,
        disabled: false,
        form: {
          fileList: ''
        },
        rules: {
        },
        params: {
          attachType: this.attachType
        }
      }
    },
    methods: {
      init() {
        this.open = true;
        this.form.fileList = '';
        this.resetForm("form");
      },
      handleSuccess(files) {
        this.form.fileList = files;
        this.$refs.form.validateField('fileList');
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
        this.$emit('refreshData')
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      cancel() {
        this.open = false;
      },
      successHandleList (response, file, fileList) {
      	this.fileList = fileList;
        fileList.forEach((item, index) => {
          if (item.url == undefined && item.response != undefined) {
            fileList[index].fileName = item.response.data.fileName;
            fileList[index].filePath = item.response.data.url;
            fileList[index].url = item.response.url;
          }
        });
        console.log(this.fileList)
      	// if(this.index == -1){
      	// 	this.$emit('input', this.fileList)
      	// }else{
      	// 	this.$emit('input', this.fileList, this.index)
      	// }
      }
    }
  }
</script>

<style>
</style>
