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';
import Utils from "~/utils"

export default {
  mixins: [commonFunctions],
  methods: {
    ...setmod,
    ...glentry,
    ...engp,
    ...docpan,
    ...ccvpan,
    ...limitbody,
    ...doctre,
    async init () {
      
      const params = {
        spt: JSON.parse(localStorage.getItem('row_' + this.trnName)),
        trnmod:{
          trn:JSON.parse(localStorage.getItem('review_'+this.trnName))
        }
      }
      if ( typeof(this.$route.query.inr) == 'string' ){
        params.spt = null
        params.trnmod.trn =null
      }
      const res = await Api.post('/service/litdla/init', {
        ...params,
        transName: this.trnName,
        userId: window.sessionStorage.userId || 'ZL',
        lidgrp:{
          rec:{
            inr:this.$route.query.inr
          }
        }
      });
      if (!res.data) {
        return
      }
      this.copyValueFromVoData(this.model, res.data)
      // this.copyValueFromVoData(this.model.setmod.setfeg, res.data.setfeg)
      // this.copyValueFromVoData(this.model.setmod.setfog, res.data.setfog)
      // this.copyValueFromVoData(this.model.setmod.setglg, res.data.setglg)

    },
    // 兼容处理在前端model中定义了字段,后端返回的数据中不存在字段的问题
    copyValueFromVoData (model, data) {
      let keysList = Object.keys(model)
      keysList.map((key) => {
        if (data[key]) {
          if (Utils.typeOf(model[key]) === 'Object') {
            this.copyValueFromVoData(model[key], data[key])
          } else {
            this.$set(model, key, data[key])
          }
        }
      })
    },
    async tabClick(tab) {
      if (this.isInDisplay) {
        return;
      }
      let name = tab.name;
      switch (name) {
        case 'ccvpan':
          let ccvpanRequest = buildFn.buildCcvpan(this.model, this.trnName);
          this.processLiaccv(ccvpanRequest);
          break;
        case 'engp':
          let engpRequest = buildFn.buildEngp(this.model, this.trnName);
          this.processLiaall(engpRequest);
          break;
        case 'setmod':
          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;
        default:
          return;
      }
    },
    // 点击获取按钮拉取当前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/litdla/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.gidgrp[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.gidgrp, 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
      }
    },
    onSeainf() {},
    max2Amt(){
      this.executeDefault("max2.amt").then(
                  (res) => {
                      if (res.respCode == SUCCESS){
                          Utils.copyValueFromVO(this.model, res.data);
                      }
                  }
              );
    },
    choose(row){
     this.model.amenbr = row.srcStr.split('\t')[0]
     this.model.amecur = row.srcStr.split('\t')[2]
     this.model.reduceamt = row.srcStr.split('\t')[3]
     this.model.amedat = row.srcStr.split('\t')[4]
     this.model.ameflg = '接受'
    },

    open(val) {
      if (val === "" && !(this.model.lidgrp.blk.adlcnd == "")) {
        this.$confirm(
          "是否确定要重置手动修改?",
          "提示",
          {
            cancelButtonText: "撤销",
            confirmButtonText: "确定",
            type: "warning",
            showClose: false,
          }
        )
          .then(() => {
            //yes的执行在这里写
            this.model.lidgrp.blk.adlcnd = "";
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          })
          .catch(() => {
            //No的功能在这里写
            this.model.litamep.adlcndamep.modflg = "X";
            this.$message({
              type: "info",
              message: "已取消",
            });
          });
      } else if (val == "X" && this.model.litamep.adlcndamep.modmanflg == "X") {
        this.$confirm("请先重置内部修改", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning",
          showClose: false,
        }).then(() => {
          //yes的执行在这里写
          this.model.litamep.adlcndamep.modflg = "";
          this.flag1 = true;
          this.flag2 = false;
        });
      }
    },
    open1(val) {
      if (val === "" && !(this.model.lidgrp.blk.adlcnd == "")) {
        this.$confirm(
          "是否确定要重置手动修改?",
          "提示",
          {
            cancelButtonText: "撤销",
            confirmButtonText: "确定",
            type: "warning",
            showClose: false,
          }
        )
          .then(() => {
            //yes的执行在这里写
            this.model.lidgrp.blk.adlcnd = "";
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          })
          .catch(() => {
            //No的功能在这里写
            this.model.litamep.adlcndamep.modmanflg = "X";
            this.$message({
              type: "info",
              message: "已取消",
            });
          });
      } else if (
        this.model.litamep.adlcndamep.modflg == "X" &&
        this.model.litamep.adlcndamep.modmanflg == "X"
      ) {
        this.$confirm("请先重置修改以替换", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "",
          showCancelButton: false,
          type: "warning",
          showClose: false,
        }).then(() => {
          //yes的执行在这里写
          this.model.litamep.adlcndamep.modmanflg = "";
          this.flag1 = true;
          this.flag2 = false;
        });
      }
    },
  },
};