import commonFunctions from '~/mixin/commonFunctions.js';
import Api from '~/service/Api';
import Utils from "~/utils";
import moment from "moment";
export default {
  mixins: [commonFunctions],
  methods: {
    // 列表查询
    async searchTable() {
      if (this.model.begdat == "") {
        this.$notify.warning({
          title: '提示',
          message: '请先选择开始日期!'
        });
        return;
      }
      if (this.model.enddat == "") {
        this.$notify.warning({
          title: '提示',
          message: '请先选择结束日期!'
        });
        return;
      }
      if (this.model.decnum == "") {
        this.$notify.warning({
          title: '提示',
          message: '请先输入金融机构标识码!'
        });
        return;
      }
      let params = {
        decnum: this.model.decnum,
        begdat: this.model.begdat,
        enddat: this.model.enddat,
        rcrdno: this.model.rcrdno,
        taxpyidnfno: this.model.taxpyidnfno,
        bsnsts: this.model.bsnsts,
        bsnsrlno: this.model.bsnsrlno,
        itnlincmpaydclrtnno: this.model.itnlincmpaydclrtnno,
        pymtbnknm: this.model.pymtbnknm,
        pageSize: this.model.pageSize,
        pageNum: this.model.pageNum,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/listPagingInfo`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.tableData = res.data.list;
        this.model.total = res.data.total;
        this.model.pageNum = res.data.pageNumber;
        this.model.pageSize = res.data.pageSize;
        this.searchLoading = false;
      } else {
        this.searchLoading = false;
        this.$notify.error(res.respMsg);
      }
    },

    // 重新上链
    async reupInfo(row) {
      if(row.bizstate != 'FAIL'){
        this.$notify.warning("请选择状态为上链失败的业务");
        return;
      }
      let params = {
        bsnsrlno: row.bsnsrlno,
        bizstate: row.bizstate,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/reup`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.$notify.success(res.data);
      } else {
        this.$notify.error(res.respMsg);
      }
    },
    // 提交核验
    async subtxf() {
      if (this.dbwtxfDialog.txfmod.rcrdno == "") {
        this.$notify.warning({
          title: '提示',
          message: '请先输入备案表编号!'
        });
        return;
      }
      if (this.dbwtxfDialog.txfmod.dataverfcd == "") {
        this.$notify.warning({
          title: '提示',
          message: '请先输入验证码!'
        });
        return;
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/subtxf`, this.dbwtxfDialog);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.dbwtxfDialog = res.data;
      } else {}
    },

    // 查看
    async show(row) {
      let params = {
        bsnsrlno: row.bsnsrlno,
        paymentstate: row.paymentstate,
        bizstate: row.bizstate,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/show`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.dbwtxfDialogInfo = res.data;
      } else {
        this.$notify.error(res.respMsg);
      }
    },

    // 修改
    async upd(row) {
      let params = {
        bsnsrlno: row.bsnsrlno,
        paymentstate: row.paymentstate,
        bizstate: row.bizstate,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/upd`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.dbwtxfDialogUpd = res.data;
      } else {
        this.$notify.error(res.respMsg);
      }
    },

    // 处理
    async manage(row) {
      let params = {
        bsnsrlno: row.bsnsrlno,
        paymentstate: row.paymentstate,
        bizstate: row.bizstate,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/manage`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.dbwtxfDialogMge = res.data;
      } else {
        this.$notify.error(res.respMsg);
      }
    },

    // 付汇修改
    async updpay() {
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/updpay`, this.dbwtxfDialogUpd);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.$notify.success(res.data);
      } else {
        this.$notify.error(res.respMsg);
      }
    },

    // 业务受理
    async busacp() {
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/busacp`, this.dbwtxfDialog);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.payBtnIsShow = "Y";
        this.$notify.success(res.data);
      } else {
        this.payBtnIsShow = "N";
        this.$notify.error(res.respMsg);
      }
    },

    // 付汇取消
    async calpay(reqData) {
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/calpay`, reqData);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.$notify.success(res.data);
      } else {
        this.$notify.error(res.respMsg);
      }
    },

    // 付汇确认
    async paycom(reqData) {
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/paycom`, reqData);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.$notify.success(res.data);
      } else {
        this.$notify.error(res.respMsg);
      }
    },

    // 下载
    async downloadInfo(reqData) {
      let result = await this.showPdf(reqData);
      if(result == "SUCCESS"){
        this.$refs.downloadA.click();
        this.$notify.success("生成PDF文件成功!");
      } else {
        this.$notify.error("生成PDF文件失败!");
      }
    },

    // 预览
    async showPdf(reqData) {
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/dbwtxf/showPdf`, reqData);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.viewPdf(res.data.data);
        return "SUCCESS";
      } else {
        this.$notify.error(res.respMsg);
        return "ERROR";
      }
    },

    viewPdf(content) {
      if (!content) {
        this.$message.error("无备案表详情内容");
        return;
      }
      const blob = this.base64ToBlob(content);
      const fileURL = URL.createObjectURL(blob);
      this.viewPdfUrl = fileURL + "#view=FitH,top";
    },

    base64ToBlob(code) {
      code = code.replace(/[\n\r]/g, "");
      const raw = window.atob(code);
      const rawLength = raw.length;
      const uInt8Array = new Uint8Array(rawLength);
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
      return new Blob([uInt8Array], {
        type: "application/pdf"
      });
    },

    // 获取交易编码列表
    async selectBopcod() {
      let rec = this.buildReqData();
      let request = this.model.txfmod;
      request.rec = rec;
      request.errorMsg = '';
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/txfmod/selectBopcod`,request);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.txncdList = res.data;
      } else {
        this.txncdList = [];
        this.$notify.error("查询收支申报交易编码报错");
      }
    },
    // 格式化数据
    formatLabel(item) {
      return item.cod + ' - ' + item.txt
    },
    // 导出excel
    async exportExcel() {
      this.showDig = false
      let obj = []
      let arr =['序号','国结业务编号','付汇业务编号','备案表编号','受理银行机构代码','机构名称或个人姓名','纳税人识别号','受理时间','付汇业务状态','上链状态','国结业务状态']
      obj.push(arr);
      this.tableData.forEach(item => {
        let array =  [
          item.no,
          item.ownref,
          item.bsnsrlno,
          item.rcrdno,
          item.acptinstcd,
          item.pymtbnknm,
          item.taxpyidnfno,
          item.updatetime,
          item.paymentstatelabel,
          item.bizstatelabel,
          item.gjsta
        ]
        obj.push(array)
      });
      let usrName = window.sessionStorage.userName
      let dateStr = moment(new Date()).format("YYYYMMDD.HHmmss");
      let fileName = usrName + ".dbwtxf." + dateStr;
      if (this.exportType === '1') {
        let cFileName = fileName + ".xlsx"
        Utils.exportToExcel (obj, cFileName, "税务备案表") ;
      } else if (this.exportType === '2') {
        let newObj = obj.join("\r\n")
        let export_blob = new Blob([newObj])
        let save_link = document.createElement("a")
        save_link.href = window.URL.createObjectURL(export_blob)
        save_link.download = fileName + ".txt"
        let event = new MouseEvent("click",{
          bubbles: true,
          cancelable: true,
          view: window,
        })
        save_link.dispatchEvent(event)
      }
    },
  }
}