import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import Pts from '~/components/business/commonModel/Pts';
import Utils from "~/utils";
import Default from "../model/default"
import BigNumber from "bignumber.js";

export default {
  mixins: [commonFunctions,Default],
  methods: {
    // 点击获取按钮拉取当前key字段下的表单数据
    queryFormData(key) {
      console.log(key);
    },


    apprulChange(v) {
      if (v !== 'OTHR') {
        this.model.ledgrp.rec.apprultxt = ''
      }
    },


    openAddAmount(val) {
      if (val === '' && !(this.model.ledgrp.cbs.max2.amt == '0.000' && this.model.ledgrp.blk.addamtcov === '')) {
        this.$confirm('是否确定要删除此合同的附加金额?', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning',
          showClose: false
        }).then(() => {//yes的执行在这里写
          this.model.letp.addamtflg = ''

          this.model.ledgrp.cbs.max2.amt ='0.000';
          this.model.ledgrp.blk.addamtcov = '';
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {//No的功能在这里写
          this.model.letp.addamtflg = 'X'
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      }
    },

    // 如果adv,con,iss,rmb,trb的值有变动,同时更新avb的值
    async bankChanged(){
      this.avbwthFlg();
    },


    // 如果a2b的值被清空,则清空cnfins的值
    async a2bBankChange(){
      if(this.model.ledgrp.a2b.pts.extkey==''){
        this.model.ledgrp.rec.cnfins = ''
      }
    },

    //指定银行
    async avbwthFlg() {
      switch (this.model.ledgrp.rec.avbwth) {
          // Advising bank,adv
        case "A":
          Utils.copyValueFromVoData(this.model.ledgrp.avb.pts, this.model.ledgrp.adv.pts);
          break;

          // Confirming Bank,con
        case "C":
          Utils.copyValueFromVoData(this.model.ledgrp.avb.pts, this.model.ledgrp.con.pts);
          break;

          // Issuing Bank,iss
        case "I":
          Utils.copyValueFromVoData(this.model.ledgrp.avb.pts, this.model.ledgrp.iss.pts);
          break;

          //Reimbursing Bank,rmb
        case "R":
          Utils.copyValueFromVoData(this.model.ledgrp.avb.pts, this.model.ledgrp.rmb.pts);
          break;

          //Specified Bank,
        case "S":
          this.model.ledgrp.avb.pts = new Pts().data;
          break;

          //Transferring Bank, trb
        case "T":
          Utils.copyValueFromVoData(this.model.ledgrp.avb.pts, this.model.ledgrp.trb.pts);
          break;

          //Any Bank, no role
        case "O":
          // if any bank is empty or is not modified, then default value is 'Any Bank'
          if(this.model.ledgrp.blk.avbwthtxt ===''||(this.model.ledgrp.blk.modifySet && !this.model.ledgrp.blk.modifySet.includes('avbwthtxt'))){
            this.model.ledgrp.blk.avbwthtxt = 'Any Bank';
          }

          break;

      }

      // 如果avb被赋值的话,设置avb角色为AVB
      if(this.model.ledgrp.avb.pts.ptainr!==''){
        this.model.ledgrp.avb.pts.rol ="AVB";
      }
    },

    async avbbyChange() {

      // mixdet为disable时,清除其内容
      if(this.model.ledgrp.rec.avbby != 'M'){
        this.model.ledgrp.blk.mixdet = '';
      }

      // draft at为disable时,清除draftat内容,同时清除drawee
      if(  this.model.ledgrp.rec.avbby == 'D' ||
          this.model.ledgrp.rec.avbby == 'M' ||
          this.model.ledgrp.rec.avbby == ''){
        this.model.ledgrp.blk.dftat = '';

        // 清除drawee的内容
        this.model.ledgrp.drw.pts = new Pts().data;
      }

      // defdet 为disable时,清除其内容
      if(this.model.ledgrp.rec.avbby != 'D' &&
          this.model.ledgrp.rec.avbby != 'N'){
        this.model.ledgrp.blk.defdet = '';
      }

      // by payment时,tenor day为disable,清空tenor day
      if(this.model.ledgrp.rec.avbby == 'P'){
        this.model.ledgrp.rec.tenmaxday=0;
      }
    },



    // 当iss改变时,如果stacty没有哦被修改过,则默认将iss所在地区的赋值给stacty字段
    changeStacty() {
      // ledgrp.rec.stacty not modified, when change iss, change the stacty.
      if (!(this.model.ledgrp.rec.modifySet != null && this.model.ledgrp.rec.modifySet.includes("stacty"))) {
        Api.post('/Lc/adr/getloccty', this.model.ledgrp.iss.pts.ptainr).then(res => {
          if (res.respCode == SUCCESS) {
            this.model.ledgrp.rec.stacty = res.data;
          }
        })
      }

      // 同时改变avb的值
      this.bankChanged();
    },

    // 正则匹配特殊字符替换,例如将'[',替换成'\['这样的话就不会作为正则表达式的转义字符处理了
    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;
    },


    // New Amount( 新的信用证金额,swiadd.newamt)更新的时候,同时更新Amended Amount(修改金额,swiadd.ameAmt)
    changeSwiaddNewamt() {
      if (this.model.swiadd.newamt === '') {
        this.model.swiadd.newamt = 0;
      }

      // swiadd.newamt - oldledgrp.cbs.nom1.amt, 保留两位小数
      this.model.swiadd.ameamt = new BigNumber(this.model.swiadd.newamt).minus(new BigNumber(this.model.oldledgrp.cbs.nom1.amt)).toFixed(2);

      this.maxamt();
      this.opn1Amt();
      this.engamt();
      this.defaultNom1Amt2000();
      this.defaultMaxamt950();

    },

    newnomtopChange(){

      let exp=/^\d+$/;
      if(!exp.test(this.model.swiadd.newnomtop)){
        this.model.swiadd.newnomtop = 0;
      }else{
        this.model.swiadd.newnomtop = Number(this.model.swiadd.newnomtop);
      }

      this.defaultLedgrpRecNomtop1500();

      this.maxamt();
      this.opn1Amt();
      this.engamt();
      this.defaultNom1Amt2000();
      this.defaultMaxamt950();
    },

    newnomtonChange(){
      let exp=/^\d+$/;
      if(!exp.test(this.model.swiadd.newnomton)){
        this.model.swiadd.newnomton = 0;
      }else{
        this.model.swiadd.newnomton = Number(this.model.swiadd.newnomton);
      }

      this.defaultLedgrpRecNomton1500();
    },

    amenbrChange(){
      let exp=/^\d+$/;
      if(!exp.test(this.model.swiadd.amenbr)){
        this.model.swiadd.amenbr = 0;
      }else{
        this.model.swiadd.amenbr = Number(this.model.swiadd.amenbr);
      }
    },

    swiaddAddmatChange(){
      this.changeSwiaddAddamtcov();
      this.model.ledgrp.cbs.max2.amt = this.model.swiadd.addamt;
      this.model.ledgrp.cbs.opn2.amt = this.model.ledgrp.cbs.max2.amt;
    },

    // 当max2 amt改变时,同时拼接addamtcov的值
    changeSwiaddAddamtcov(){
      // if addmatcov not modified, then automatic connect it.
      if(!this.model.swiadd.modifySet
          || !(this.model.swiadd.modifySet!=null&&this.model.swiadd.modifySet.includes("addamtcov"))){

        if(new BigNumber(this.model.swiadd.addamt).comparedTo(new BigNumber("0"))>0){
          this.model.ledgrp.cbs.max2.cur = this.model.ledgrp.cbs.nom1.cur;

          let _currency = this.model.ledgrp.cbs.max2.cur;
          let _opn2amt =  this.model.swiadd.addamt;
          if (_currency === "KRW" || _currency === "JPY") {
            _opn2amt = new BigNumber(_opn2amt).toFixed(0);
          }else {
            _opn2amt = new BigNumber(_opn2amt).toFixed(2);
          }
          this.model.swiadd.addamtcov = _currency + " " +_opn2amt;

          this.model.ledgrp.blk.addamtcov = this.model.swiadd.addamtcov;
        }
      }
    },

    // ledgrp.rec.expdat
    defaultLedgrpRecExpdat2000(){
      if(this.model.swiadd.newexpdat!=null&&this.model.swiadd.newexpdat!=''){
        this.model.ledgrp.rec.expdat = this.model.swiadd.newexpdat;
      }else{
        this.model.ledgrp.rec.expdat = this.model.oldledgrp.rec.expdat;
      }
    },

    defaultLedgrpRecNomtop1500(){
      // always has to be equal to amended field
      this.model.ledgrp.rec.nomtop = this.model.swiadd.newnomtop;
    },

    defaultLedgrpRecNomton1500(){
      // always has to be equal to amended field
      this.model.ledgrp.rec.nomton = this.model.swiadd.newnomton;
    },

    newexpdatChange(){
      this.defaultLedgrpRecExpdat2000();
    },

    newshpdatChange(){
      if(this.model.swiadd.newshpdat!=null&&this.model.swiadd.newshpdat!=''){
        this.model.ledgrp.rec.shpdat = this.model.swiadd.newshpdat;
      }
    },

  }
};