import Api from "~/service/Api";
import commonFunctions from "~/mixin/commonFunctions.js";
import buildFn from "./buildCommons.js";
import BigNumber from "bignumber.js";

export default {
  mixins: [commonFunctions],
  methods: {
    // 点击获取按钮拉取当前key字段下的表单数据
    queryFormData(key) {
      console.log(key);
    },
    // 业务信息=》基本信息=》保函编号-----获取保函编号
    async queryOwnref() {
      let params = {
        ptainr: this.model.gidgrp.apl.pts.ptainr,
        businessType: "LG",
        tbl: "LG",
      };
      const loading = this.loading();
      let res = await Api.post("/service/brtdck/getOwnRef", params);
      if (res.respCode == SUCCESS) {
        loading.close();
        this.model.gidgrp.rec.ownref = res.data;
      }
    },
    // 获取gitopn弹框表格数据
    async queryGridEtyPromptDialogData(type, ptytyp) {
      let params = {
        userId: window.sessionStorage.userId || "ZL",
        ptytyp: ptytyp,
        extkey: this.model.brdgrp[type.toLowerCase()].pts.extkey,
      };
      let res = await Api.post("/service/ptspta/list", params);
      if (res.respCode == SUCCESS) {
        this.root.$refs["etyDialog"].show = true;
        this.root.promptData.data = res.data.ptaInfos;
        this.root.promptData.type = type;
      }
    },
    // 选中gitopn弹框表格的行数据
    async selectGridEtyPromptData(row) {
      let params = {
        ...row,
      };
      let res = await Api.post("/service/ptspta/fetch", params);
      if (res.respCode == SUCCESS) {
        this.$set(this.model.brdgrp, row.role.toLowerCase(), res.data);
      }
    },
    // 业务信息=》基本信息=》支出目的
    purposChange(key, value) {
      this.handleChangeForm(key, value);
    },
    // 初始化保函开立类型码表下拉列表
    async queryHndtypCodeTableList(trnName) {
      let params = {
        gitp: {
          swiftflg: this.model.gitp.swiftflg,
        },
        gidgrp: {
          rec: {
            purpos: this.model.gidgrp.rec.purpos,
          },
        },
        transName: trnName.toUpperCase(),
      };
      let res = await Api.post("/service/brtdck/initHndtyp", params);
      if (res.respCode == SUCCESS) {
        this.$set(this.codes, "voHndtyp", res.data);
      }
    },
    // 是否SWIFT格式修改--联动保函开立类型
    handleChangeSwiftflg() {
      this.queryHndtypCodeTableList(this.root.trnName);
    },
    // 支出目的修改--联动保函开立类型
    handleChangePurpos() {
      this.queryHndtypCodeTableList(this.root.trnName);
    },
    buildLiaccv() {
      let params = {
        ...buildFn.buildCommonData(this.model, this.trnName),
        liaccvg: this.model.liaccv.liaccvg,
        oldamt: this.model.liaccv.oldamt,
        chgamt: this.model.liaccv.chgamt,
        concur: this.model.liaccv.concur,
      };

      return params;
    },
    // 公共组件setmod处理方式改变触发联动
    changeSetmodModel(emitParams) {
      let reqParams = {};
      switch (emitParams.code) {
        case "processMethods":
          reqParams = buildFn.buildSetglg(this.model, this.trnName);
          this.calcPayDetail(reqParams);
          break;
        case "changeDsp":
          let index = emitParams.index;
          let setglgRequest = buildFn.buildSetglg(this.model, this.trnName);
          setglgRequest.setglg = { setgll: emitParams.list };
          this.setgllAccts(setglgRequest, index);
          break;
        default:
          return;
      }
    },
    fillmatInfOnChange() {
      if (
        (this.model.brdgrp.rec.matdat != null && this.model.brdgrp.rec.matdat != "") ||
        this.model.brdgrp.rec.docflg == "P"
      ) {
        this.model.brdgrp.blk.matper = "";
      }
      if (
        (this.model.brdgrp.rec.matper != null && this.model.brdgrp.rec.matper != "") ||
        this.model.brdgrp.rec.docflg == "P"
      ) {
        this.model.brdgrp.blk.matdat = "";
      }
    },
    fillDocstaByChange() {
      if (
        (this.model.brdgrp.rec.advdat == null || this.model.brdgrp.rec.advdat == "") &&
        (this.model.brdgrp.rec.disdat == null || this.model.brdgrp.rec.disdat == "")
      ) {
        let rcvFlg = this.model.brdgrp.rec.rcvdat == null || this.model.brdgrp.rec.rcvdat == "";
        let dscinsflgFlg = this.model.brdgrp.rec.dscinsflg == null || this.model.brdgrp.rec.dscinsflg == "";
        let docdiFlg = this.model.brdgrp.blk.docdis == null || this.model.brdgrp.blk.docdis == "";
        if (!rcvFlg && dscinsflgFlg) {
          this.model.brdgrp.rec.docsta = "G";
        }
        if (!rcvFlg && docdiFlg && !dscinsflgFlg) {
          this.model.brdgrp.rec.docsta = "H";
        }
        if (!rcvFlg && !docdiFlg && !dscinsflgFlg) {
          this.model.brdgrp.rec.docsta = "I";
        }
      }
    },
    getBigNumber() {
      // 配置BigNumber计算规则为保留两位小数,并向上舍入
      var BN = BigNumber.clone();
      BN.config({ DECIMAL_PLACES: 2, ROUNDING_MODE: 2 });
      return BN;
    },
    fillDocdis() {
      var BN = this.getBigNumber();
      let descr = "";
      if (this.model.lidgrp.rec.shpdat !== null && this.model.brdgrp.rec.shpdat > this.model.lidgrp.rec.shpdat) {
        descr = descr + "Late shipment.\n";
      }
      if (this.model.brdgrp.rec.predat > this.model.lidgrp.rec.expdat) {
        descr = descr + "Documents presented after expiry of the LC.\n";
      }
      let tmp = BN(this.model.lidgrp.cbs.opn1.amt).plus(this.model.oldbrdgrp.cbs.max.amt);
      if (BN(this.model.brdgrp.cbs.max.amt).comparedTo(tmp) > 0) {
        let amt = BN(this.model.brdgrp.cbs.max.amt)
          .minus(this.model.lidgrp.cbs.opn1.amt)
          .minus(this.model.oldbrdgrp.cbs.max.amt)
          .toFixed(2);
        let exces = this.model.brdgrp.cbs.max.cur + " " + amt;
        descr = descr + "The LC is overdrawn by " + exces + "\n";
      }
      let tmp2 = BN(this.model.lidgrp.cbs.opn2.amt).plus(this.model.oldbrdgrp.cbs.opn2.amt);
      if (BN(this.model.brdgrp.cbs.max2.amt).comparedTo(tmp2) > 0) {
        let addamt = BN(this.model.brdgrp.cbs.max2.amt)
          .minus(this.model.lidgrp.cbs.opn2.amt)
          .minus(this.model.oldbrdgrp.cbs.opn2.amt)
          .toFixed(2);
        let excesadd = this.model.brdgrp.cbs.max2.cur + " " + addamt;
        descr = descr + "The Additional Amount is overdrawn by " + excesadd + "\n";
      }
      if (this.model.lidgrp.rec.shppar === "N" && this.model.lidgrp.utlnbr >= 1) {
        descr = descr + "Partial shipment effected.\n";
      }
      if (this.model.brdgrp.rec.docflg === "P") {
        if (this.model.lidgrp.rec.avbby === "A" || this.model.lidgrp.rec.avbby === "D") {
          descr = descr + "Sight documents presented instead of usance.\n";
        }
        if (this.model.lidgrp.rec.avbby === "M") {
          descr = descr + "Only Sight documents presented instead of mixed payment.\n";
        }
      }
      if (this.model.brdgrp.rec.docflg === "A" || this.model.brdgrp.rec.docflg === "D") {
        if (this.model.lidgrp.rec.avbby === "P") {
          descr = descr + "Usance documents presented instead of sight.\n";
        }
        if (this.model.lidgrp.rec.avbby === "M") {
          descr = descr + "Only Usance documents presented instead of mixed payment.\n";
        }
      }
      if (this.model.brdgrp.rec.docflg === "M") {
        if (this.model.lidgrp.rec.avbby === "P") {
          descr = descr + "Mixed payment documents presented instead of sight.\n";
        }
        if (this.model.lidgrp.rec.avbby === "A" || this.model.lidgrp.rec.avbby === "D") {
          descr = descr + "Mixed payment documents presented instead of usance.\n";
        }
      }
      this.model.brdgrp.blk.docdis = descr;
    },
  },
};