<template>
  <el-dialog title="导入数据" :visible.sync="inShow"  width="400px">
    <!-- 解析导入xlsx,目前只支持一个文件,多个文件只会解析最后导入的 -->
    <el-upload
      class="upload-demo"
      action
      :on-change="handleChange"
      :on-exceed="handleExceed"
      :on-remove="handleRemove"
      :file-list="fileListUpload"
      :limit="limitUpload"
      drag
      accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
      :auto-upload="false"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        将文件拖到此处，或
        <em>点击上传</em>
      </div>
      <!-- <el-button size="small" type="primary">点击导入</el-button> -->
      <div slot="tip" class="el-upload__tip" style="color:red">提示： 只能导入拓展名为 .xlsx / .xls 的文件</div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFileForm">确 定</el-button>
      <el-button @click="$emit('update:uploadShow', false)">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "XlsxInput",
  data() {
    return {
    inShow:this.uploadShow,
      limitUpload: 1, //最大上传的文件数,目前只支持一个
      fileListUpload: [], //上传文件信息File数组
      fileTemp: null, //单条数据File信息
      xlsxData: null //处理过后的数据
    };
  },
  watch:{
      inShow(nv){
          this.$emit('update:uploadShow',nv)
      },
      uploadShow(nv){
          this.inShow = nv
      }
  },
  props: {
    uploadShow: { type: Boolean, default: false }
  },
  methods: {
    // 添加事件回调
    handleChange(file, fileList) {
      this.fileTemp = file.raw;
          this.$emit('handleChange',file,fileList)
    },
    //超出文件数触发事件
    handleExceed(file, fileList) {
          this.$emit('handleExceed',file,fileList)
    },
    // 删除触发事件
    handleRemove(file, fileList) {
      this.fileTemp = null;
          this.$emit('handleRemove',file,fileList)
    },
     submitFileForm() {
      if (this.fileTemp) {
        if (
          this.fileTemp.type ==
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
          this.fileTemp.type == "application/vnd.ms-excel"
        ) {
           this.importfxx(this.fileTemp);
        } else {
          this.$message({
            type: "warning",
            message: "附件格式错误，请删除后重新上传！"
          });
        }
      } else {
        this.$message({
          type: "warning",
          message: "请上传附件！"
        });
      }
    },
    //处理xlsx数据返回表数据
    importfxx(obj) {
      let _this = this;
      // 通过DOM取文件数据
      this.file = obj;
      var rABS = false; //是否将文件读取为二进制字符串
      var f = this.file;
      var reader = new FileReader();
      //if (!FileReader.prototype.readAsBinaryString) {
      FileReader.prototype.readAsBinaryString = function(f) {
        var binary = "";
        var rABS = false; //是否将文件读取为二进制字符串
        var pt = this;
        var wb; //读取完成的数据
        var outdata;
        var reader = new FileReader();
        reader.onload = async function(e) {
          var bytes = new Uint8Array(reader.result);
          var length = bytes.byteLength;
          for (var i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          var XLSX = require("xlsx");
          if (rABS) {
            wb = XLSX.read(btoa(fixdata(binary)), {
              //手动转化
              type: "base64"
            });
          } else {
            wb = XLSX.read(binary, {
              type: "binary"
            });
          }
          outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); //outdata就是获取到的数据,不过
          //关闭弹窗同时返回初步处理的数据,触发自定义方法
          _this.$emit("update:uploadShow", false);
          _this.xlsxData =  outdata;
          //确定事件回调
          _this.$emit("subMitFileForm", _this.xlsxData);
          return outdata;
        };
        reader.readAsArrayBuffer(f);
      };

      if (rABS) {
        reader.readAsArrayBuffer(f);
      } else {
        reader.readAsBinaryString(f);
      }
    }
  }
};
</script>

<style>
</style>