import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import buildFn from './buildCommons.js';
import setmod from '~/components/business/setmod/event';
import glentry from '~/components/business/glentry/event';
import engp from '~/components/business/engp/event';
import docpan from '~/components/business/docpan/event';
import ccvpan from '~/components/business/ccvpan/event';
import limitbody from '~/components/business/limitbody/event';
import doctre from '~/components/business/doctre/event';
export default {
  mixins: [commonFunctions],
  methods: {
    ...setmod,
    ...glentry,
    ...engp,
    ...docpan,
    ...ccvpan,
    ...limitbody,
    ...doctre,
    async tabClick (tab) {
      if (this.isInDisplay) {
        return;
      }
      let name = tab.name;
      switch (name) {
        case 'ccvpan':
          this.queryCbbByCcvData();
          break;
        case 'engp':
          let engpRequest = buildFn.buildEngp(this.model, this.trnName);
          this.processLiaall(engpRequest);
          break;
        case 'setpan':
          let setfegRequest = buildFn.buildSetfeg(this.model, this.trnName);
          // 此处利用回调是为了等setfeg的接口调用完成后才去获取setglg参数,由于setglg参数依赖于setfeg函数的返回值
          this.processSetpan(setfegRequest, () => {
            return buildFn.buildSetglg(this.model, this.trnName);
          });
          break;
        case 'docpan':
          let docpanRequest = buildFn.buildDocpan(this.model, this.trnName);
          this.processTrndoc(docpanRequest);
          break;
        case 'glepan':
          let glentryRequest = buildFn.buildGlentry(this.model, this.trnName);
          this.processGlentry(glentryRequest);
          break;
        case 'limitbody':
          let limitbodyRequest = buildFn.buildLimitbody(this.model, this.trnName);
          this.processLimitbody(limitbodyRequest);
          break;
        case 'doctre':
          let doctreRequest = buildFn.buildDoctre(this.model, this.trnName);
          this.processDoctre(doctreRequest);
          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
      }
    },
    // 业务信息=》基本信息=》保函编号-----获取保函编号
		queryOwnref () {
      let params = this.model.nidgrp.rec
      if (!this.model.nidgrp.apl.pts.ptainr || !this.model.nidgrp.rec.hndtyp || !this.model.nidgrp.rec.gartyp) {
        return
      }
      let ptainr = this.model.nidgrp.apl.pts.ptainr;
			const loading = this.loading();
			let trnName = this.root.trnName.toUpperCase()
      Api.post(`/business/ref/nid?ptaInr=${ptainr}&trnName=${trnName}`, params).then(res => {
				loading.close();
        if (res.respCode == SUCCESS) {
          this.model.nidgrp.rec.ownref = res.data;
        }
      }).catch(() => {
        loading.close();
      });
    },

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

    async queryCbbData(){
      let rtnmsg = await Api.post('/business/gitsel/queryCbbByCbc', {objtyp:this.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp,objinr:this.model.nidgrp.rec.inr});
      if(rtnmsg.respCode  == SUCCESS){
        this.cbetableData = rtnmsg.data
      }
     
    },

    async queryFepData(){
     
      let rtnmsg = await Api.post('/business/gitsel/queryFep', {objtyp:this.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp,objinr:this.model.nidgrp.rec.inr,subobjtyp:this.subobjtyp,chksubcon:this.model.infcon.chksubcon,chktrnsta:this.model.infcon.chktrnsta});
      if(rtnmsg.respCode  == SUCCESS){
        this.trntableData = rtnmsg.data
      }
      // console.log("trntableData:" , this.trntableData)
    },
    async queryTrnDataInTrn(){
      console.log("objtyp:" + this.objtyp)
      console.log("subobjtyp:" + this.subobjtyp)
      let rtnmsg = await Api.post('/business/gitsel/queryTrn', {objtyp:this.objtyp,objinr:this.model.nidgrp.rec.inr,subobjtyp:this.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
    },

    async getNidDataByInr(){
      let res = await Api.post('/business/gitsel/selectNidByPrimaryKey', this.model.nidgrp.rec);
      if (res.respCode === SUCCESS) {
        this.model.nidgrp = res.data
       }  
     },  
     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.smhtableData = rtnmsg.data.smhLst
        this.trstableData = rtnmsg.data.trsLst
      }     
      //  console.log("smhtableData:" ,this.smhtableData) 
      //  console.log("trstableData:" ,this.trstableData) 
     },

     async   trnTableDblRow(row){
      // this.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.$emit("changeData",rtnmsg.data.smhLst,rtnmsg.data.trsLst)
      } 
      this.model.trnShow = true;
      this.$emit("changeVal",'trnp0')
    },

    changeData(smhtableData,trstableData){
      this.smhtableData = smhtableData;
      this.trstableData = trstableData;
    },
      
    async querySmhData(){
      let rtnmsg = await Api.post('/business/gitsel/querySmh', {objtyp:this.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp,objinr:this.model.nidgrp.rec.inr,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.objtyp, objinr: this.model.nidgrp.rec.inr});
      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;
      }
    },
  },
};