<script>
import { importData, fileImport} from '@/api/requisition/basic';
import { changeImport, downloadFailData} from '@/api/changeApply/basic';
export default {
  name:'BatchImport',
  props:{},
  data(){
    return {
      title:'批量导入',
      visible:false,
      fileData:[],
      size:'mini',
    }
  },
  methods:{
    beforeColse(done){
      this.cancal();
      done();

    },
    handleClick(){
      this.visible = true;
    },
    confirmUpdate(){
      if (this.fileData.length) {

        let formData = new FormData();

        formData.append('file', this.fileData[0].raw);
        importData(formData).then(res => {
          if (res.code == 200) {
            this.visible = false;
            this.fileData = [];
            if (res.data.flag) {
              this.failLoad = true;
              console.log(res.data.datas)
              let param = {failDatas: res.data.datas}
              if (null != param) {

                fileImport(param).then(res => {
                  console.log('res',res)
                  const isBlob = blobValidate(res);
                  if (isBlob) {
                    const blob = new Blob([res]);
                    saveAs(blob, '导入失败的物料申请单数据.xlsx');
                  }
                  this.failLoad = false;
                })
              }
            }
            this.$notify({
              message: res.data.msg,
              type: res.data.flag ? 'warning' : 'success'
            });
          } else {
            this.$notify({
              message: res.msg,
              type: res.code == 200 ? 'success' : 'warning'
            });
          }
        })
      } else {
      this.$notify({
        title:'警告',
        message: '请上传文件之后在确认！',
        type: 'warning'
      });
      }
    },
  
    handleDownTemplate(){},
    handleFileRemove(file, fileList) {
        console.log('删除文件', file, 'file', fileList, 'fileList');
        this.fileData = fileList;
      },
    handleChangeFile(file, fileList){
      this.fileData = fileList;
    },
    cancal(){
      this.fileData = [];
      this.visible = false;
    },
  },
  created(){},
}
</script>

<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    type="primary"
    @click="handleClick"
    :size="size"
  >
    {{ title }}
    <el-dialog 
      :title="title" 
      :visible.sync="visible"
      width="35%" 
      center
      append-to-body
      :before-close="beforeColse"
    >
      <div style="display: flex;justify-content: center;">
        <el-upload 
          accept=".xls, .xlsx" 
          ref="upload" 
          action="#" 
          :on-remove="handleFileRemove"
          :file-list="fileData" 
          :auto-upload="false" 
          :on-change="handleChangeFile" 
          :limit="1"
        >
          <el-button slot="trigger" :size="size" type="primary">选取文件</el-button>
          <el-button 
            style="margin-left: 10px;" 
            :size="size"
            type="success"
            @click="handleDownTemplate"
          >下载模板
          </el-button>
          <div slot="tip" class="el-upload__tip">只能上传Excel文件</div>
        </el-upload>

      </div>
      <span slot="footer">
        <el-button :size="size" @click="cancal">取 消</el-button>
        <el-button type="primary" :size="size" @click="confirmUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </el-button>
</template>