import Api from "~/service/Api";
import commonFunctions from "~/mixin/commonFunctions.js";
import Pts from "~/components/business/commonModel/Pts.js";
import { cloneDeep } from "lodash";
export default {
  mixins: [commonFunctions],
  methods: {
    // 获取弹框表格数据
    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);
      }
    },
    //获取信用证编号
    async onLitpButgetref() {
      let ptainr = this.model.lidgrp.apl.pts.ptainr;
      if (this.model.lidgrp.rec.dkflg === "X") {
        ptainr = this.model.lidgrp.apo.pts.ptainr;
      }
      let params = {
        businessType: "LC",
        littyp: "LC",
        ptainr: ptainr,
        ownref: this.model.lidgrp.rec.ownref,
      };
      const loading = this.loading();
      let res = await Api.post("/Lc/ref/lid", params);
      loading.close();
      if (res.respCode == SUCCESS) {
        this.model.lidgrp.rec.ownref = res.data;
      } else {
        this.$message.error(res.respMsg);
      }
    },
    //最大金额的修改
    async nomtopChange() {
      let params = {
        nomtop: this.model.lidgrp.rec.nomtop,
        nom1amt: this.model.lidgrp.cbs.nom1.amt,
        maxcur: this.model.lidgrp.cbs.max.cur,
      };
      let res = await Api.post("/service/litopn/maxamt", params);
      if (res.respCode == SUCCESS) {
        this.model.lidgrp.cbs.max.amt = res.data.maxamt;
      }
    },

    // 获取码表
    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;
      }
    },
    async getIdType(ptyinr) {
      if (ptyinr == "") {
        return;
      }
      let data = {
        ptyinr: ptyinr,
      };
      const loading = this.loading();
      let res = await Api.post("/Lc/litopn/getIdType", data);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.idtyp = res.data;
        if (res.data === "2") {
          this.model.lidgrp.rec.dkflg = "X";
        } else {
          this.model.lidgrp.rec.dkflg = "N";
        }
      }
    },
    onAdvChange() {
      this.onAvbwthChange();
      // 对手行代码赋值
      if (
        typeof this.model.trnInfo !== "undefined" &&
        this.model.trnInfo.zmqflg === "X"
      ) {
        if (this.model.lidgrp.adv.pts.extkey !== "") {
          this.model.lidgrp.rec.oppbnk = this.model.lidgrp.adv.pts.extkey.slice(
            0,
            11
          );
        } else {
          this.model.lidgrp.rec.oppbnk = "";
        }
      }
    },
    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: "确定",
            }
          );
        }
      }
    },

    // 需客户确认按钮是否显示
    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() {
      let ptyinr = this.model.lidgrp.apl.pts.ptyinr;
      if (ptyinr !== "") {
        this.getIdType(ptyinr);
      }
      // 查询是否亮显"需客户确认"按钮
      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 = "";
      }
    },
  },
};