import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import buildFn from './buildCommons.js';
import Utils from "~/utils"

export default {
  mixins: [commonFunctions],
  methods: {
   
    getAddamtflg(){
			if (this.model.gidgrp.cbs.max2.amt > 0) {
        this.model.gitp.aamp.addamtflg = "X";
        this.model.gidgrp.cbs.total.cur = this.model.gidgrp.cbs.max.cur
        this.model.gidgrp.cbs.total2.cur = this.model.gidgrp.cbs.max2.cur
        this.model.gidgrp.cbs.total.amt = parseFloat(this.model.gidgrp.cbs.max.amt) + parseFloat(this.model.gidgrp.cbs.max2.amt)
        this.model.gidgrp.cbs.total2.amt = parseFloat(this.model.gidgrp.cbs.opn1.amt) + parseFloat(this.model.gidgrp.cbs.opn2.amt)
			} else {
			  this.model.gitp.aamp.addamtflg = "";
			}
		 },
  async  getgtxInfo(){
    let rtnmsg = await Api.post('/business/gitsel/getAtxinr');
    if(rtnmsg.respCode == SUCCESS){
      this.model.atxinrList = rtnmsg.data
      this.model.atxinrcList = rtnmsg.data
      }
    },
    
    defaultFuc() {
      if (this.model.ptsptaList == undefined || this.model.ptsptaList == null) {
        this.model.ptsptaList = [];
      }
      this.calcName();
      this.model.gidgrp.rec.orddat = this.model.gidgrp.rec.opndat
      if (this.model.gidgrp.rec.purposin != "" && this.model.gidgrp.rec.purpos =="") {
        if (this.model.gidgrp.rec.purposin == "ISCO") {
          this.model.gidgrp.rec.purpos = "ISSU"
          this.model.gidgrp.rec.hndtyp = "OT"
        }
        if (this.model.gidgrp.rec.purposin == "ICCO") {
          this.model.gidgrp.rec.purpos = "ISCO"
          this.model.gidgrp.rec.hndtyp = "OT"
        }
      }
      if(this.model.gidgrp.cbs.max.amt!='' && this.model.gidgrp.cbs.max2.amt!=''){
        this.model.gidgrp.cbs.total.amt = parseFloat(this.model.gidgrp.cbs.max.amt) + parseFloat(this.model.gidgrp.cbs.max2.amt);
        this.model.gidgrp.cbs.total.cur = this.model.gidgrp.cbs.max.cur;
      }
      if(this.model.gidgrp.cbs.opn1.amt!='' && this.model.gidgrp.cbs.opn2.amt!=''){
        this.model.gidgrp.cbs.total2.amt = parseFloat(this.model.gidgrp.cbs.opn1.amt) + parseFloat(this.model.gidgrp.cbs.opn2.amt);
        this.model.gidgrp.cbs.total2.cur = this.model.gidgrp.cbs.max2.cur;
      }
      
      if (this.model.gidgrp.ben && this.model.gidgrp.ben.pts && this.model.gidgrp.ben.pts.ennam && this.model.gidgrp.ben.pts.enadr) {
        this.model.gidgrp.benadrlin = this.model.gidgrp.ben.pts.ennam.replace(/[\n]/g, ",") + "," + this.model.gidgrp.ben.pts.enadr.replace(/[\n]/g, ",");
    }

      if (this.model.gidgrp.rec.purpos.indexOf("CO") > -1) {
        this.model.gidgrp.cbs.mac.cur = this.model.gidgrp.cbs.max.cur;
        this.model.gidgrp.cbs.mac.amt = this.model.gidgrp.cbs.max.amt;
      }

      if(this.model.gidgrp.rec.atxinr!=""){
        this.handleChangeGartyp();
      }
      if(this.model.gidgrp.rec.gtxinrc!=""){
        this.handleChangeGartypc();
      }
      if(!this.isInDisplay){
        if(this.model.gidgrp.rec.hndtyp != "OT"){
          this.model.gitp.prepermodflg ='X';
          this.model.gitp.covgodsrvmodflg = 'X';
        }
        if(this.model.addbcb.bccls1 == ''){
          this.model.addbcb.bccls1 = '2';
        }
      }
      
      if(this.model.gidgrp.ben.pts.adrblk!="" && this.model.gidgrp.ben.pts.ennam==""){
        let ennam = this.model.gidgrp.ben.pts.adrblk.split("\n")
        this.$set(this.model.gidgrp.ben.pts, 'ennam',  ennam[0]+"\n"+ennam[1])
      }
    },
    handleChangeGartyp(e) {
			if (e === undefined) {
				return
			}
      let _this = this
      let data = {
        gtxpurpos: _this.model.gidgrp.rec.purpos,
        gartyp: _this.model.gidgrp.rec.gartyp,
        hndtyp: _this.model.gidgrp.rec.hndtyp,
        gtxinr: _this.model.gidgrp.rec.gtxinr,
        giduil: _this.model.gidgrp.rec.giduil,
        segtyp: _this.model.gidgrp.ghd.segtyp,
        objinr: _this.model.trnmod.swiadd.objinr || "",
        msgtyp: ""
      }
      Api.post(`/business/gitopn/defaultGidgrpRecGtxinrN1000`, data).then(res => {
        _this.model.atxinrList = res.data
        if(res.data!=null && res.data.length>0){
          _this.model.gidgrp.rec.atxinr = res.data[0].value
        }
        _this.changeAtxinr(_this.model.gidgrp.rec.atxinr)
      })

      // _this.defaultGidgrpCbsMaxCurN1000();
    },
    handleChangeGartypc(e) {
			if (e === undefined) {
				return
			}
      let _this = this
      let data = {
        gtxpurpos: _this.model.gidgrp.rec.purpos,
        legfrmc: _this.model.gidgrp.rec.legfrmc,
        liatypc: _this.model.gidgrp.rec.liatypc,
        purpos: _this.model.gidgrp.rec.purpos,
        gartypc: _this.model.gidgrp.rec.gartypc,
        hndtyp: _this.model.gidgrp.rec.hndtyp,
        stdwrduilc: _this.model.gidgrp.rec.stdwrduilc,
        apcPtsPtyInr: _this.model.gidgrp.apc.pts.ptyinr,
        becPtsPtyInr: "",
        issPtsPtyInr: "",
      }
      Api.post(`/business/gitopn/defaultGidgrpRecGtxinrcN200`, data).then(res => {
        _this.model.atxinrcList = res.data
        if(res.data!=null && res.data.length>0){
          _this.model.gidgrp.rec.gtxinrc = res.data[0].value
        }
        _this.changeAtxinrc(_this.model.gidgrp.rec.gtxinrc)
      })

      // _this.defaultGidgrpCbsMaxCurN1000();
    },
    async tabClick (tab) {
      if (this.isInDisplay) {
        return;
      }
      let name = tab.name;
      switch (name) {
        case 'liacombo':
         this.queryCbbByCcvData();
        //  this.handleSearch1();
         this.querylialist();
          break;
        case 'cbsp':
          this.getCbsTyp();
          this.queryCbbData();
          break;
        case 'cbep':
          this.queryCbeData();
          break;
        case 'fep':
          this.queryFepData();
          this.queryFcpData();
          break;
        case 'trnp':
          this.queryTrnData();
          break;
        case 'smhp':
          this.querySmhData();
        default:
          return;
      }
    },
    // 点击获取按钮拉取当前key字段下的表单数据
    queryFormData (key) {
    },
    // 公共组件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
      }
    },
    // 公共组件ccvpan处理方式改变触发联动
    changeCcvpan (emitParams) {
      let reqParams = {}
      switch (emitParams.code) {
        case 'liaccvAdd':
          reqParams = buildFn.buildCcvpan(this.model, this.trnName);
          this.liaccvGetFcdList(reqParams)
          break
        case 'liaccvPick':
          reqParams = {
            ...buildFn.buildCcvpan(this.model, this.trnName),
            ...emitParams.params
          };
          this.liaccvPick(reqParams);
          break
        case 'liaccvDel':
          reqParams = {
            ...buildFn.buildCcvpan(this.model, this.trnName),
            ...emitParams.params
          };
          this.liaccvDel(reqParams);
          break
       
        default:
          return
      }
    },
    // model.gidgrp.rec.ownref
		// 业务信息=》基本信息=》保函编号-----获取保函编号
		queryOwnref () {
      let params = this.model.gidgrp.rec
      if (!this.model.gidgrp.apl.pts.ptainr) {
        return
      }
      let ptainr = this.model.gidgrp.apl.pts.ptainr;
			const loading = this.loading();
			let trnName = this.root.trnName.toUpperCase()
      Api.post(`/business/ref/gid?ptaInr=${ptainr}&trnName=${trnName}`, params).then(res => {
        loading.close();
        if (res.respCode == SUCCESS) {
          this.model.gidgrp.rec.ownref = res.data;
        }
      }).catch(() => {
        loading.close();
      });
    },

    async getCbsTyp() {
      let rtnmsg = await Api.post('/business/gitsel/getCbbTyp', {objtyp: this.obj.objtyp,objinr: this.obj.objinr});    
      if (rtnmsg.respCode === SUCCESS) {
        this.cbbtyp = rtnmsg.data;
      }
    },

    async queryCbbData(){
      let rtnmsg = await Api.post('/business/gitsel/queryCbbByCbc', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,cbc:this.model.infcon.cbctxt});
      if(rtnmsg.respCode == SUCCESS){
        this.cbstableData = rtnmsg.data
      }
    },

    changeCbsData(cbsData){
      this.cbstableData = cbsData
    },

    async queryCbbDataInCbb(){
      let rtnmsg = await Api.post('/business/gitsel/queryCbbByCbc', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,cbc:this.model.infcon.cbctxt});
      if(rtnmsg.respCode == SUCCESS){
        this.$emit("changeCbsData",rtnmsg.data)
      }
     
    },

    async queryCbeData(){
     
      let rtnmsg = await Api.post('/business/gitsel/queryCbe', {objtyp:this.obj.objtyp,objinr:this.obj.objinr});
      if(rtnmsg.respCode  == SUCCESS){
        this.cbetableData = rtnmsg.data
      }
     
    },

    async queryFepData(){
     
      let rtnmsg = await Api.post('/business/gitsel/queryFep', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,feecod:this.model.infcon.fepfeecod,fepdsp:this.model.infcon.fepdsp});
      if(rtnmsg.respCode  == SUCCESS){
        this.feptableData = rtnmsg.data
      }
    
    },
    async queryFepDataInFep(){
     
      let rtnmsg = await Api.post('/business/gitsel/queryFep', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,feecod:this.model.infcon.fepfeecod,fepdsp:this.model.infcon.fepdsp});
      if(rtnmsg.respCode  == SUCCESS){
        this.$emit("changeFepData",rtnmsg.data)
      }
    
    },
    changeFepData(fepData){
      this.feptableData = fepData
    },

    async queryFcpData(){
      let rtnmsg = await Api.post('/business/gitsel/queryFcp', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,fcpdsp:this.model.infcon.fcpdsp});
      if(rtnmsg.respCode  == SUCCESS){
        this.fcptableData = rtnmsg.data
      }
    },
    async queryFcpDataInFcp(){
      let rtnmsg = await Api.post('/business/gitsel/queryFcp', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,fcpdsp:this.model.infcon.fcpdsp});
      if(rtnmsg.respCode  == SUCCESS){
        this.$emit("changeFcpData",rtnmsg.data)
      }
      
    },
    changeFcpData(fcpData){
      this.fcptableData = fcpData
    },

    async queryTrnData(){
      let rtnmsg = await Api.post('/business/gitsel/queryTrn', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,subobjtyp:this.obj.subobjtyp,chksubcon:this.model.infcon.chksubcon,chktrnsta:this.model.infcon.chktrnsta});
      if(rtnmsg.respCode  == SUCCESS){
        this.trntableData = rtnmsg.data
      }
    },

    async queryTrnDataInTrn(){
      let rtnmsg = await Api.post('/business/gitsel/queryTrn', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,subobjtyp:this.obj.subobjtyp,chksubcon:this.model.infcon.chksubcon,chktrnsta:this.model.infcon.chktrnsta});
      if(rtnmsg.respCode  == SUCCESS){
        this.$emit("changeTrnData",rtnmsg.data) 
      }

    },

    changeTrnData(trnData){
      this.trntableData = trnData
    },
    calcName() {
			this.model.gidgrp.cbs.mac.amt = this.model.gidgrp.cbs.max.amt;
			this.model.gidgrp.cbs.opn1.cur = this.model.gidgrp.cbs.max.cur;
			this.model.gidgrp.cbs.mac.cur = this.model.gidgrp.cbs.max.cur;
			this.model.gidgrp.cbs.max2.cur = this.model.gidgrp.cbs.max.cur;
			this.model.gidgrp.cbs.opn2.cur = this.model.gidgrp.cbs.max.cur;
			this.model.gidgrp.cbs.total2.cur = this.model.gidgrp.cbs.max.cur;
			this.model.gidgrp.cbs.total.cur = this.model.gidgrp.cbs.max.cur;
			// if (this.model.gitp.aamp.addamtflg === "X") {
			//     this.model.gidgrp.cbs.opn1.amt = parseFloat(this.model.gidgrp.cbs.max.amt) + parseFloat(this.model.gidgrp.cbs.max2.amt);

			// } else {
			this.model.gidgrp.cbs.opn1.amt = this.model.gidgrp.cbs.max.amt
			// }
			if (this.model.gidgrp.cbs.max2.amt == "") {
				this.model.gidgrp.cbs.total.amt = parseFloat(this.model.gidgrp.cbs.max.amt);
			} else {
				this.model.gidgrp.cbs.total.amt = parseFloat(this.model.gidgrp.cbs.max.amt) + parseFloat(this.model.gidgrp.cbs.max2.amt);
			}
			let model = this.model;
			this.model.gidgrp.rec.nam = model.gidgrp.cbs.max.cur + " " +
				model.gidgrp.cbs.max.amt + " " +
				(model.gidgrp.rec.stacty || "") + "." +
				model.gidgrp.apl.pts.nam
			// this.onCfaCurDate()
		},
    async getGidDataByInr(opentyp){
      if(opentyp === 'GJBH'){
      let res = await Api.post('/business/gitsel/selectGidByPrimaryKey', {...this.model.gidgrp.rec});
      if (res.respCode === SUCCESS) {
       Utils.copyValueFromVoData(this.model.gidgrp,res.data)
       this.model.limpan.gjownref = this.model.gidgrp.rec.ownref;
       this.model.limpan.branch = this.model.gidgrp.rec.bchkeyinr;
       this.model.cshpct = this.model.gidgrp.rec.cshpct
       this.getAddamtflg();
       }  
      }else{
        let res = await Api.post('/business/gitsel/selectNidByPrimaryKey', {...this.model.nidgrp.rec});
        if (res.respCode === SUCCESS) {
         Utils.copyValueFromVoData(this.model.nidgrp,res.data)
         this.model.limpan.gjownref = this.model.nidgrp.rec.ownref;
         this.model.limpan.branch = this.model.nidgrp.rec.bchkeyinr;
         this.model.cshpct = this.model.nidgrp.rec.cshpct
         }  
      }
     },  
     changeVal(tabVal){
       this.tabVal = tabVal
     },

    //  async queryTrnDetail(row){
      
    //   let rtnmsg = await Api.post('/business/gitsel/queryTrnDetail', {trninr:row.inr});
    //   if(rtnmsg.respCode  == SUCCESS){
    //     this.model.infcon.trn = rtnmsg.data.trn
    //     this.model.infcon.trs = rtnmsg.data.trs
    //     this.smhtableDataT = rtnmsg.data.smhLst
    //     this.trstableData = rtnmsg.data.trsLst
    //   }     
    //  },

     async  trnTableDblRow(row){
      let rtnmsg = await Api.post('/business/gitsel/queryTrnDetail', {trninr:row.inr});
      if(rtnmsg.respCode  == SUCCESS){
        this.model.infcon.trn = rtnmsg.data.trn
        this.model.infcon.trs = rtnmsg.data.trs
        this.$emit("changeData",rtnmsg.data.smhLst,rtnmsg.data.trsLst)
      } 
      this.model.trnShow = true;
      this.$emit("changeVal",'trnp0')
    },

    changeData(smhtableDataT,trstableData){
      this.smhtableDataT = smhtableDataT;
      this.trstableData = trstableData;
    },
    
    async querySmhData(){
      let rtnmsg = await Api.post('/business/gitsel/querySmh', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,cortyp:this.model.infcon.smhcortyp,datfrom:this.model.infcon.smhdatfrom,datto:this.model.infcon.smhdatto,dir:this.model.infcon.smhdir,chktrn:this.model.infcon.chktrn});
      if(rtnmsg.respCode  == SUCCESS){
        this.smhtableData = rtnmsg.data
      }
    },

    async querySmhDataInSmh(){
      let rtnmsg = await Api.post('/business/gitsel/querySmh', {objtyp:this.obj.objtyp,objinr:this.obj.objinr,cortyp:this.model.infcon.smhcortyp,datfrom:this.model.infcon.smhdatfrom,datto:this.model.infcon.smhdatto,dir:this.model.infcon.smhdir,chktrn:this.model.infcon.chktrn});
      if(rtnmsg.respCode  == SUCCESS){
        this.$emit("changeSmhData",rtnmsg.data) 
      }

    },

    changeSmhData(smhData){
      this.smhtableData = smhData
    },

    async queryCbbByCcvData(){
      let rtnmsg = await Api.post('/business/gitsel/queryCbbByCcv', { objtyp: this.obj.objtyp, objinr: this.obj.objinr});
      if(rtnmsg.respCode  == SUCCESS){
        if(rtnmsg.data.ccvamt){
        this.model.infcon.ccvamt = rtnmsg.data.ccvamt;
       }
       if(rtnmsg.data.ccvresamt){
        this.model.infcon.ccvresamt = rtnmsg.data.ccvresamt;
      }
      if(rtnmsg.data.ccvtotalamt){
        this.model.infcon.ccvtotamt = rtnmsg.data.ccvtotalamt.amt;
      }
      this.ccvtableData = rtnmsg.data.ccvlst;
      }
    },

    async showMsg(row){
      this.$refs.msgView.fileViewDispaly(-1,{smhinr:row.inr})
    },

    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.model.dbCodes[codeNam] = curList
      }
    },
    getCodelabel(value,codenam) {
      const codeobj = this.model.dbCodes[codenam].find(obj => obj.value === value)
			return codeobj ? codeobj.label : value;
    },
    async handleSearch1() {
   
      const loading = this.loading();
      Api.post("/business/inflsb/queryInflsb1", {
        ...this.model
      }).then(rtnmsg => {
        loading.close();
      if (rtnmsg.respCode == SUCCESS) {
        this.feptableData1 = rtnmsg.data;
      }
    })
  
    },

    async querylialist(){
      let rtnmsg = await Api.post('/business/gitsel/queryLiasum', {objtyp:this.obj.objtyp,objinr:this.obj.objinr});
      if(rtnmsg.respCode  == SUCCESS){
        this.liatableData = rtnmsg.data
      }
    },
  },
};