<template>
    <div class="app-container">
    <el-row :gutter="10">
            <el-col :span="22" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>上传开票</span>
          </div>
          <div class="form">
                <el-form  label-width="100px" style="width:45%">
                <el-button type="primary" :underline="false" style="vertical-align: baseline;" @click="importKPTemplate">点击下载开票表模板</el-button>
                <el-upload
                  class="upload-demo"
                  :multiple="false"
                  action="#"
                  :limit="1"
                  :on-exceed="handleKPUploadExceed"
                  accept=".xlsx"
                  :auto-upload="false"
                  :on-change="handleKPUploadChange"
                  :on-remove="handleKPUploadRemove"
                  drag>
                  <i class="el-icon-upload"></i>
                  <div class="el-upload__text">将收入文件拖到此处，或<em>点击上传</em></div>
                  <div class="el-upload__tip" slot="tip">只能上传xlsx文件</div>
                </el-upload>
                <el-form-item  style="margin-top:30px" size="large" label-width="0px">
                  <el-popconfirm
                    confirm-button-text='确定'
                    cancel-button-text='等一下再说'
                    icon="el-icon-info"
                    icon-color="black"
                    @confirm="submitKPForm()"
                    title="确定要上传开票吗？"
                  >
                    <el-button slot="reference" :loading="KPbtnloading" type="primary">提交开票表</el-button>
                  </el-popconfirm>
                </el-form-item>
                <span style="font-size:16px">最后一次上传开票表：&nbsp;&nbsp;&nbsp;<strong>{{KPhistoryTime}}</strong></span>
              </el-form>
              <div id="message" style="font-size:18px;color:red;margin-bottom:4%;border:1px dashed red"></div>
          </div>
          <div class="form">
                <el-form :rules="rules" ref="form" :model="form" label-width="100px" style="width:45%">
                <el-button type="primary" :underline="false" style="vertical-align: baseline;" @click="importKPCFTemplate">点击下载开票表拆分表模板</el-button>
                <el-upload
                  class="upload-demo"
                  :multiple="false"
                  action="#"
                  :limit="1"
                  :on-exceed="handleKPCFUploadExceed"
                  accept=".xlsx"
                  :auto-upload="false"
                  :on-change="handleKPCFUploadChange"
                  :on-remove="handleKPCFUploadRemove"
                  drag>
                  <i class="el-icon-upload"></i>
                  <div class="el-upload__text">将收入文件拖到此处，或<em>点击上传</em></div>
                  <div class="el-upload__tip" slot="tip">只能上传xlsx文件</div>
                </el-upload>
                <el-form-item  style="margin-top:30px" size="large" label-width="0px">
                  <el-popconfirm
                    confirm-button-text='确定'
                    cancel-button-text='等一下再说'
                    icon="el-icon-info"
                    icon-color="black"
                    @confirm="submitKPCFForm()"
                    title="确定要上传开票表拆分表吗？"
                  >
                    <el-button slot="reference" :loading="KPCFbtnloading" type="primary">提交开票表拆分表</el-button>
                  </el-popconfirm>
                </el-form-item>
                <span style="font-size:16px">最后一次上传开票拆分表：&nbsp;&nbsp;&nbsp;<strong>{{KPCFhistoryTime}}</strong></span>
              </el-form>
              <div id="message2" style="font-size:18px;color:red;margin-bottom:4%;border:1px dashed red"></div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    </div>
</template>



<script>
import { HaveSingleSheetName,HaveData3Para,import_excel_to_json,makeInvoiceJSONstring,checkInvoiceFile,checkInvoiceSplitFile,makeInvoiceSplitJSONstring } from "@/api/check.xlsx.full";
import { uploadBillInfo,uploadBillSplitInfo,getHistoryTime } from "@/api/finance/bill/bill"
import Cookies from "js-cookie";

import { Loading } from 'element-ui'
let billUploadLoadingInstance;
export default {
    data(){
        return{
          KPhistoryTime:"您还没有上传过开票表",
          KPCFhistoryTime:"您还没有上传过开票拆分表",
          KPfile:{},
          KPCFfile:{},
          KPbtnloading:false,
          KPCFbtnloading:false,
          jsona:"",
          jsona2:""
    }
},
created(){
  this.getHistoryTime();
},
methods:{
  getHistoryTime(){
      getHistoryTime(Cookies.get("username")).then(res => {
        if(res.KPLastTime != null){
          this.KPhistoryTime = res.KPLastTime;
        }
        if(res.KPCFLastTime != null){
          this.KPCFhistoryTime = res.KPCFLastTime;
        }
      })
  },
  //开票表上传文件
  handleKPUploadExceed(){
      this.$message({
                showClose: true,
                message: '超过最大上传文件数量，操作失败！',
                type: 'error'
      });
  },
  handleKPUploadChange(file,filelist){
      this.KPfile = file;
  },
  handleKPUploadRemove(file,filelist){
    this.KPfile = {};
  },
  submitKPForm(){
      document.getElementById("message").innerHTML = "";
            if(this.KPfile == {} || this.KPfile == null || this.KPfile.name == "" || this.KPfile.name == null){
              this.$message({
                  showClose: true,
                  message: '请先上传开票表文件',
                  type: 'error'
                });
            }else{
            if (!/\.xlsx$/g.test(this.KPfile.name)) {
              this.$message({
                  showClose: true,
                  message: '仅支持读取xlsx格式文件！',
                  type: 'error'
                });
            }else{
            var userName = Cookies.get("username");
            let data = this.KPfile.raw;
            import_excel_to_json(data).then(res => {
              let workbook = res;
              if (HaveSingleSheetName(workbook,"KP") != true){
                   document.getElementById('message').innerHTML += HaveSingleSheetName(workbook,"KP");
                   return;
                };
            
              if (HaveData3Para(workbook,"KP","W1") != true){
                document.getElementById('message').innerHTML += HaveData3Para(workbook,"KP","W1")
                return;
              };
            // this.uploadBtnLoading = true;
            billUploadLoadingInstance = Loading.service({ text: "正在上传开票表，请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
            checkInvoiceFile(workbook,"message",userName);
            // alert(makeIncomeJSONstring(workbook));
             // if (document.getElementById('TextBox3').innerHTML == ""&&document.getElementById('FileUpload1').value != "") {
                
             //     document.getElementById('Button1').disabled =false;
                  //alert("pass!")
            //  }

            if(document.getElementById("message").innerHTML == null || document.getElementById("message").innerHTML == ""){
              this.$message({
                  showClose: true,
                  message: '开票文件数据校验通过！',
                  type: 'success'
                });
              this.jsona = makeInvoiceJSONstring(workbook);
              uploadBillInfo(this.jsona).then(res => {
                if(res.msg == "操作成功"){
                  billUploadLoadingInstance.close();
                  this.$message({
                  showClose: true,
                  message: '上传开票表成功！',
                  type: 'success'
                  });
                  this.getHistoryTime();
                }else{
                  billUploadLoadingInstance.close();
                  this.$message({
                  showClose: true,
                  message: '上传失败，请联系管理员！',
                  type: 'error'
                  });
                }
              })
            }else{
              billUploadLoadingInstance.close();
              this.$message({
                  showClose: true,
                  message: '开票表文件数据校验不通过！请参考底部错误日志栏！',
                  type: 'error'
                });
            }

            });
            }}
          
  },
  /** 下载模板操作 */
  importKPTemplate() {
    this.download('/finance/bill/KPdownload', {
    },'开票表模板'+'_'+Cookies.get("username")+'.xlsx')
  },
  //开票拆分表上传文件
  handleKPCFUploadExceed(){
      this.$message({
                showClose: true,
                message: '超过最大上传文件数量，操作失败！',
                type: 'error'
      });
  },
  handleKPCFUploadChange(file,filelist){
      this.KPCFfile = file;
  },
  handleKPCFUploadRemove(file,filelist){
    this.KPCFfile = {};
  },
  submitKPCFForm(){
      document.getElementById("message2").innerHTML = "";
            if(this.KPCFfile == {} || this.KPCFfile == null || this.KPCFfile.name == "" || this.KPCFfile.name == null){
              this.$message({
                  showClose: true,
                  message: '请先上传开票拆分表文件',
                  type: 'error'
                });
            }else{
            if (!/\.xlsx$/g.test(this.KPCFfile.name)) {
              this.$message({
                  showClose: true,
                  message: '仅支持读取xlsx格式文件！',
                  type: 'error'
                });
            }else{
            this.KPbtnloading = true;
            this.KPCFbtnloading = true;
            var userName = Cookies.get("username");
            let data = this.KPCFfile.raw;
            import_excel_to_json(data).then(res => {
              let workbook = res;
              if (HaveSingleSheetName(workbook,"KPCF") != true){
                document.getElementById('message2').innerHTML += HaveSingleSheetName(workbook,"KPCF");
                return;
              };
            
              if (HaveData3Para(workbook,"KPCF","H1") != true){
                document.getElementById('message2').innerHTML += HaveData3Para(workbook,"KPCF","H1")
                return;
              };
            // this.uploadBtnLoading = true;
            billUploadLoadingInstance = Loading.service({ text: "正在上传开票拆分表，请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
            checkInvoiceSplitFile(workbook,"message2",userName);
            // alert(makeIncomeJSONstring(workbook));
             // if (document.getElementById('TextBox3').innerHTML == ""&&document.getElementById('FileUpload1').value != "") {
                
             //     document.getElementById('Button1').disabled =false;
                  //alert("pass!")
            //  }

            if(document.getElementById("message2").innerHTML == null || document.getElementById("message2").innerHTML == ""){
              this.KPbtnloading = false;
              this.KPCFbtnloading = false;
              this.$message({
                  showClose: true,
                  message: '开票拆分表文件数据校验通过！',
                  type: 'success'
                });
              this.jsona2 = makeInvoiceSplitJSONstring(workbook);
              uploadBillSplitInfo(this.jsona2).then(res => {
                if(res.msg == "操作成功"){
                  billUploadLoadingInstance.close();
                  this.getHistoryTime();
                  this.$message({
                  showClose: true,
                  message: '上传开票拆分表成功！',
                  type: 'success'
                  });
                }else{
                  billUploadLoadingInstance.close();
                  this.$message({
                  showClose: true,
                  message: '上传失败，请联系管理员！',
                  type: 'error'
                  });
                }
              })
            }else{
              billUploadLoadingInstance.close();
              this.KPbtnloading = false;
              this.KPCFbtnloading = false;
              this.uploadBtnLoading = false;
              this.$message({
                  showClose: true,
                  message: '开票拆分表文件数据校验不通过！请参考底部错误日志栏！',
                  type: 'error'
                });
            }

            });
            }}
          
  },
/** 下载模板操作 */
  importKPCFTemplate() {
    this.download('/finance/bill/KPCFdownload', {
    },'开票拆分表模板'+'_'+Cookies.get("username")+'.xlsx')
  },
}
}
</script>