<!--
 * @Author: zqm
 * @Date: 2021-12-23 15:21:02
 * @LastEditTime: 2022-01-06 17:19:38
 * @LastEditors: Please set LastEditors
 * @Description: 导入弹窗
 * @FilePath: \taxformtoolframework1\src\components\importFile.vue
-->
<template>
  <div>
    <el-dialog class="dialogShadow" title="请选择导入文件" :visible.sync="importDialog.visible" top="5%" width="25%" center :close-on-click-modal="false" :close-on-press-escape="false" :modal="false">
      <el-upload class="upload-demo" ref="upload" action="/" :file-list="importDialog.fileList" :auto-upload="false" accept=".txt" :on-remove="handleRemove" :before-upload="beforeUpload" :on-change="handleChange">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">导 入</el-button>
        <div slot="tip" class="el-upload__tip">只能上传txt文件!!</div>
      </el-upload>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "importFile",
  data() {
    return {
      importDialog: {
        visible: false,
        fileList: [],
        fileJson: ""
      },
    }
  },
  methods: {
    //移除导入文件
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    //导入之前，判断文件类型
    beforeUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1);
      const extension = testmsg === 'txt';
      if (!extension) {
        this.$message({
          message: '上传文件只能是txt格式!',
          type: 'warning'
        });
      }
      return extension;
    },
    // 文件长度控制
    handleChange(file, fileList) {
      if (fileList.length > 0) {
        this.importDialog.fileList = [fileList[fileList.length - 1]];
        this.importDialog.fileJson = file;
      }
    },
    // 导入 读取文件
    submitUpload() {
      var file = this.importDialog.fileJson.raw;
      if (file == undefined || file == 'undefined' || file == null || file == 'null') {
        this.$alert("请选择导入文件！", '提示', { type: 'warning' });
      } else {
        var reader = new FileReader();
        reader.readAsText(file);
        var _self = this;
        reader.onload = function () {
          // this.result为读取到的json字符串，需转成json对象
          if (typeof this.result != 'string') {
            _self.$alert("导入的数据格式不符！", '提示', { type: 'warning' });
            return;
          }
          var txtData = JSON.parse(this.result);
          var openData = _self.$store.state.data.openData;
          if (typeof txtData != 'object') {
            _self.$alert("导入的数据格式不符！", '提示', { type: 'warning' });
            return;
          } else if (txtData.bblxHeader && txtData.bblxHeader.sksssjq && txtData.bblxHeader.sksssjz) {
            if (txtData.bblxHeader.sksssjq != openData.sksssjq || txtData.bblxHeader.sksssjz != openData.sksssjz) {
              _self.$alert("导入的数据税款所属期不符！", '提示', { type: 'warning' });
              return;
            } else if (txtData.bblxHeader && txtData.bblxHeader.detailMultiPatch && txtData.bblxHeader.detailPcdm) {
              if (txtData.bblxHeader.detailMultiPatch != openData.detailMultiPatch && txtData.bblxHeader.detailPcdm != openData.detailPcdm) {
                _self.$alert("导入的数据批次不符！", '提示', { type: 'warning' });
                return;
              }
            }
          }
          // 检测是否导入成功
          _self.importDataFun(txtData);
        }
      }
    },
    importDataFun(txtData) {
      var bblxHeader = this.$store.state.data.bblxHeader;
      if (JSON.stringify(txtData) != "{}" && txtData.bblxHeader) {
        if (txtData.bblxHeader.bblxMc != bblxHeader.bblxMc) {
          this.$alert("当前凭证种类为《" + bblxHeader.bblxMc + "》，导入的数据与当前凭证种类不符！", '提示', { type: 'warning' });
        } else if (txtData.bblxHeader.djxh != bblxHeader.djxh && txtData.bblxHeader.nsrmc != bblxHeader.nsrmc) {
          this.$alert("当前纳税人为：" + bblxHeader.nsrmc + "，导入的数据与当前纳税人不符！", '提示', { type: 'warning' });
        } else {
          var _self = this;
          var menu = Enumerable.From(_self.$store.state.data.menuList).Where(function (val) {
            return val.bbDm + '_' + val.detailPcdm == _self.$store.state.data.defaultMenu
          }).ToArray()[0];
          menu = txtData.bblxHeader;
          menu.versionNumber = txtData.data.bbdm.split("_")[2];
          menu.tableData = txtData.data.tableData;
          menu.initData = txtData.data.tableData.otherData.injectData.initData;
          this.$parent.createEngine(menu);
          this.importDialog.visible = false;
        }
      }
    },
  }
}
</script>
<style scoped></style>