import Api from "~/service/Api";
import commonFunctions from "~/mixin/commonFunctions.js";
import buildFn from "./buildCommons.js";
import Pts from "~/components/business/commonModel/Pts.js";
import { cloneDeep } from "lodash";

export default {
  mixins: [commonFunctions],
  methods: {
    // 点击获取按钮拉取当前key字段下的表单数据
    queryFormData(key) {
      console.log(key);
    },
    // 业务信息=》基本信息=》保函编号-----获取保函编号
    async queryOwnref() {
      let params = {
        ptainr: this.model.gidgrp.apl.pts.ptainr,
        businessType: "LC",
        tbl: "LC",
      };
      const loading = this.loading();
      let res = await Api.post("/service/litame/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.lidgrp[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.lidgrp, row.role.toLowerCase(), res.data);
      }
    },
    // 业务信息=》基本信息=》支出目的
    purposChange(key, value) {
      this.handleChangeForm(key, value);
    },
    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;
      }
    },

    //获取信用证编号
    async onLitpButgetref() {
      let params = {
        businessType: "LC",
        littyp: "LC",
        ptainr: this.model.lidgrp.apl.pts.ptainr,
        ownref: this.model.lidgrp.rec.ownref,
      };
      const loading = this.loading();
      let res = await Api.post("/Lc/ref/lid", params);
      if (res.respCode == SUCCESS) {
        loading.close();
        this.model.lidgrp.rec.ownref = res.data;
      } else {
        loading.close();
      }
    },

    // 获取码表
    async getdbCode(codeType, uil, codeNam) {
      let params = {
        codeType: codeType,
        uil: uil ? uil : "EN",
      };
      let rtnmsg = await Api.post("/manager/dic/listDicInfo", params);
      if (rtnmsg.respCode === SUCCESS) {
        let curList = rtnmsg.data.map((item) => ({
          value: item.codeValue,
          label: item.codeName,
        }));
        this.dbCodes[codeNam] = curList;
      }
    },

    onSeainf() {},
    onAvbwthChange() {
      // 1.控制是否可输入
      let val = this.model.lidgrp.rec.avbwth;
      if (val == "O" && (this.model.lidgrp.blk.avbwthtxt == null || this.model.lidgrp.blk.avbwthtxt == "")) {
        let value = this.getCodeTableValue("O", "avbwth");
        this.model.lidgrp.blk.avbwthtxt = value;
      }
      // 2.填充字段
      // 先将原有数据清空
      this.model.lidgrp.avb.pts = new Pts().data;
      let ptsList = [];
      ptsList.push(this.model.lidgrp.adv);
      ptsList.push(this.model.lidgrp.con);
      ptsList.push(this.model.lidgrp.rmb);
      ptsList.push(this.model.litp.own);
      let rol = "";
      if ("A" == this.model.lidgrp.rec.avbwth) {
        rol = "ADV";
      } else if ("C" == this.model.lidgrp.rec.avbwth) {
        rol = "CON";
      } else if ("R" == this.model.lidgrp.rec.avbwth) {
        rol = "RMB";
      } else if ("I" == this.model.lidgrp.rec.avbwth) {
        rol = "OWN";
      }
      let res = ptsList.find((item) => item.pts.rol == rol);
      if (res != null && res != "") {
        let newRes = cloneDeep(res);
        this.model.lidgrp.avb = newRes;
        this.model.lidgrp.avb.pts.rol = "AVB";
      } else {
        if (rol === "ADV") {
          this.$alert("Please enter an advising bank or change Available with.", "警告", {
            confirmButtonText: "确定",
          });
        } else if (rol === "CON") {
          this.$alert("Please enter an Confirming Bank on Parties penal or change Available with.", "警告", {
            confirmButtonText: "确定",
          });
        }
      }
    },

    // 正则匹配特殊字符替换,例如将'[',替换成'\['这样的话就不会作为正则表达式的转义字符处理了
    specialCharactersProcess(inputStr) {
      let specialCharacterArr = ["[", "]", "\\", "$", ".", "|", "?", "*", "+", "(", ")", "{", "}"];
      let processedStr = "";
      let index;
      for (index = 0; index < inputStr.length; index++) {
        let indexChar = inputStr.charAt(index);
        // 包含特殊字符
        if (specialCharacterArr.indexOf(indexChar) != -1) {
          processedStr += "\\" + indexChar;
        } else {
          processedStr += indexChar;
        }
      }
      return processedStr;
    },

    // 需客户确认按钮是否显示
    async judgementNeedCustomerButtonShow() {
      let trninr;
      if (this.$route.query.businessInr) {
        trninr = this.$route.query.businessInr;
      } else {
        trninr = "";
      }
      let type;
      if (this.$route.query.businessType) {
        type = this.$route.query.businessType;
      } else {
        type = "";
      }
      let data = {
        extkey: this.$store.state.UserContext.currentOrg.departmentNumber,
        trninr: trninr,
        type: type,
      };
      const loading = this.loading();
      let res = await Api.post("/Lc/litopn/judgementNeedCustomerButtonShow", data);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.needcusVisible = res.data;
      }
    },

    async onAplChange() {
      // 查询是否亮显"需客户确认"按钮
      if (this.model.lidgrp.apl.pts.extkey != "") {
        let map = {
          aplExtkey: this.model.lidgrp.apl.pts.extkey,
        };
        let res = await Api.post("/Lc/litopn/havePermissionToEdit", map);
        if (res.respCode == SUCCESS) {
          this.model.needcusEnable = res.data;
          if (this.model.needcusEnable !== 'Y') {
            this.model.lidgrp.rec.needcuscomopn = "";
          }
        }
      } else {
        this.model.needcusEnable = "";
        this.model.lidgrp.rec.needcuscomopn = "";
      }
    },
  },
};