import Api from '~/service/Api';
import buildFn from '../event/buildCommons.js';
import Utils from '~/utils/index.js';
import commonFunctions from '~/mixin/commonFunctions.js';
import Pts from '~/components/business/commonModel/Pts';
import Brdgrp from '~/components/business/commonModel/brdgrp/index.js';
import Bcdgrp from '~/components/business/commonModel/bcdgrp/index.js';
import moment from 'moment';
import {
  cloneDeep
} from "lodash";


export default {
  mixins: [commonFunctions, buildFn],
  methods: {
    changeAmt() {
      this.model.trdgrp.cbs.opn1.amt = this.model.trdgrp.cbs.max.amt;
      this.model.finmod.intamt = 0;
      this.namChange();
      this.onPctfin();
      this.onWbdcrepVisiable();
    },

    extkeyChange() {
      this.model.finmod.act1 = "";
      this.model.finmod.act3 = "";
      this.model.finmod.intact = "";
      // this.model.trdgrp.rec.actrat = 0;
    },

    flttypChange() {
      if (this.model.finmod.flttyp === '0') {
        this.model.finmod.fltval = "";
      }
    },

    changeCur() {
      this.model.finmod.act1 = '';
      this.model.finmod.act3 = '';
      this.model.finmod.intact = "";
      this.model.trdgrp.rec.intrat = 0;
      this.model.trtp.wbdp.actpro = '';
      this.model.trtp.wbdp.act = '';
      this.model.trdgrp.cbs.opn1.cur = this.model.trdgrp.cbs.max.cur;
      this.getActSet('changeCur');
      this.namChange();
      this.onWbdcrepVisiable();
    },

    onRatchgtyp() {
      if (!['7', '8', '9', 'G', 'H', 'I', 'J'].includes(this.model.finmod.ratchgtyp)) {
        this.model.finmod.ratchgprd = "";
      }
      if (['3', '4', '5', '6'].includes(this.model.finmod.ratchgtyp)) {
        if (this.model.finmod.ratchgtyp != '6' && !this.model.finmod.modifySet.includes("ratchgdat")) {
          this.model.finmod.ratchgdat = "00";
        }
      } else {
        this.model.finmod.ratchgdat = "";
      }
      this.model.trdgrp.rec.intrat = 0;
    },


    ratchgprdChange() {
      this.model.trdgrp.rec.intrat = 0;
    },

    actratChange() {
      this.model.finmod.intamt = 0;
    },

    onTenday() {
      let date1 = this.model.trdgrp.rec.matdat;
      let date2 = this.model.trdgrp.rec.stttendat;
      if (date1 == "" || date1 == null || date2 == "" || date2 == null) {
        this.model.trdgrp.rec.tenday = 0;
        return;
      }
      let day1 = moment(new Date(date1)).format('YYYY-MM-DD');
      let day2 = moment(date2).format('YYYY-MM-DD');
      this.model.trdgrp.rec.tenday = moment(day1).diff(day2, 'days');
    },

    //修改fip角色,tenday会变
    async onTendayN1000() {
      if (!this.model.trdgrp.fip.pts.ptyinr) {
        return;
      }
      if (this.model.trdgrp.rec.modifySet && this.model.trdgrp.rec.modifySet.includes("tenday")) {
        return;
      }
      let data = {
        ptyinr: this.model.trdgrp.fip.pts.ptyinr
      }
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/defaultTrdgrpRecTendayN1000`, data);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.trdgrp.rec.tenday = res.data;
      }
    },

    async changeTenday() {
      await this.onTendayN1000();
      this.onTenday();
    },

    fintypChange() {
      this.model.trdgrp.rec.avbby = "";
      //切换融资类型,清除父交易数据
      this.model.trdgrp.rec.pntref = "";
      this.model.trdgrp.rec.pnttyp = "";
      this.model.trdgrp.rec.pntinr = "";
      this.model.trdgrp.rec.pntnam = "";
      this.$set(this.model, "brdgrp", new Brdgrp().data);
      this.$set(this.model, "bcdgrp", new Bcdgrp().data);
      if (['BRF', 'ECF'].includes(this.model.trdgrp.rec.fintyp)) {
        this.model.trdgrp.rec.matpretyp = "D";
      } else {
        this.model.trdgrp.rec.matpretyp = "";
      }
      this.onWbdcrepVisiable();
      this.onFintyp();
      this.namChange();
      this.disableExtkey();
    },

    onFintyp() {
      this.model.trdgrp.rec.tenorday = 0;
      const restObj = {
        pts: new Pts().data,
        namelc: "", //  名称		.trdgrp.fip.namelc
        adrelc: "", //  地址		.trdgrp.fip.adrelc
        dbfadrblkcn: "", //  Chinese address		.trdgrp.fip.dbfadrblkcn
      };
      Utils.copyValueFromVoData(this.model.trdgrp.fip, restObj);
      this.model.trdgrp.rec.prbkbic = "";
      this.model.trdgrp.rec.prbkname = "";
      this.model.trdgrp.rec.sellername = "";
      const pnttyp = this.model.trdgrp.rec.pnttyp;
      switch (this.model.trdgrp.rec.fintyp) {
        case "BRF":
          this.model.dftcre.dfdgrp.rec.trntyp = "BR";
          this.model.trdgrp.rec.pnttyp = "BRD";
          break;
        case "BCF":
          this.model.dftcre.dfdgrp.rec.trntyp = "BC";
          this.model.trdgrp.rec.pnttyp = "BCD";
          break;
        case "ECF":
          this.model.dftcre.dfdgrp.rec.trntyp = "CP";
          this.model.trdgrp.rec.pnttyp = "CPD";
          break;
        default:
          this.model.dftcre.dfdgrp.rec.trntyp = "";
          this.model.trdgrp.rec.pnttyp = "";
      }
      this.model.trdgrp.rec.pnttyp = pnttyp;
    },

    stttendatChange() {
      this.onTenday();
      if (this.model.finmod.intprd == "7") {
        this.model.trdgrp.rec.intrat = 0;
        this.model.finmod.intamt = 0;
      }
    },

    changeIntprd() {
      if (this.model.finmod.intprd !== '7') {
        this.model.finmod.intamt = 0;
        this.model.finmod.inttyp = '';
        this.model.finmod.intact = '';
      }
      if (['1', '2', '3', '4', '5'].includes(this.model.finmod.intprd)) {
        if (this.model.finmod.intdat === '') {
          this.model.finmod.intdat = '20';
        }
      } else {
        this.model.finmod.intdat = '';
      }
    },

    async matdatChange() {
      await this.matdatChange1();
      this.onTenday();
      this.model.trdgrp.rec.intrat = 0;
      this.model.finmod.intamt = 0;
    },

    async matdatChange1() {
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/matdatChange`, this.model);
      loading.close();
      if (res.respCode === SUCCESS) {
        if (res.data > 6) {
          this.$confirm("融资期限超半年,请确认是否完成超权限审批!", "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
          }).catch(() => {
            this.model.trdgrp.rec.matdat = "";
            this.model.trdgrp.rec.tenday = 0;
            this.model.trdgrp.rec.intrat = 0;
            this.model.finmod.intamt = 0;
          });
        }
      }
    },

    tendayChange() {
      if (this.model.trdgrp.rec.tenday == "" || this.model.trdgrp.rec.tenday == null) {
        this.model.trdgrp.rec.matdat = "";
      }
      if (this.model.trdgrp.rec.stttendat && this.model.trdgrp.rec.tenday) {
        this.model.trdgrp.rec.matdat = moment(this.model.trdgrp.rec.stttendat).add(this.model.trdgrp.rec.tenday, 'days').format('YYYY-MM-DD');
      }
      this.model.trdgrp.rec.intrat = 0;
      this.model.finmod.intamt = 0;
      this.matdatChange1();
    },

    async namChange() {
      let data = {
        fintyp: this.model.trdgrp.rec.fintyp,
        cur: this.model.trdgrp.cbs.max.cur,
        amt: this.model.trdgrp.cbs.max.amt,
        cnnam: this.model.trdgrp.fip.pts.cnnam
      }
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/defaultTrdgrpRecNamN1000`, data);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.trdgrp.rec.nam = res.data;
      }
    },

    async queryOwnref() {
      let params = {
        ownref: this.model.trdgrp.rec.ownref,
        ptainr: this.model.trdgrp.fip.pts.ptainr,
        fintyp: this.model.trdgrp.rec.fintyp,
      }
      const loading = this.loading();
      Api.post(`/Financing/trtopn/getRef`, params).then(res => {
        loading.close();
        if (res.respCode == SUCCESS) {
          this.model.trdgrp.rec.ownref = res.data;
          this.getFincod();
        } else {
          this.$message.error(res.respMsg);
        }
      }).catch(() => {
        loading.close();
      });
    },

    async getIdType() {
      if (this.model.trdgrp.fip.pts.ptyinr == "") {
        return;
      }
      let data = {
        ptyinr: this.model.trdgrp.fip.pts.ptyinr,
      }
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/getIdType`, data);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.idtype = res.data;
      }
    },

    async getFincod() {
      if (this.model.trdgrp.rec.ownref == null || this.model.trdgrp.rec.ownref == "") {
        return;
      }
      let data = {
        ownref: this.model.trdgrp.rec.ownref
      }
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/getFincod`, data);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.finmod.fincod = res.data;
      }
    },

    async disableExtkey() {
      // if (this.model.trdgrp.rec.fintyp != "ECF" || this.model.trdgrp.fip.pts.ref == "") {
      // 	return;
      // }
      let data = {
        pnttyp: this.model.trdgrp.rec.pnttyp,
        fintyp: this.model.trdgrp.rec.fintyp,
        ref: this.model.trdgrp.fip.pts.ref,
      }
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/defaultTrdgrpFipPtsExtkeyN1100`, data);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.fipDisabledFlg = res.data;
      }
    },

    changeInttyp() {
      if (this.model.finmod.inttyp === '1') {
        if (this.model.finmod.acttyp === '0' || this.model.finmod.acttyp === '2') {
          this.model.finmod.intact = this.model.finmod.act3;
        }
      } else {
        this.model.finmod.intact = '';
      }
    },

    changeActpro() {
      this.model.trtp.wbdp.act = '';
      this.getActSet('changeActpro');
    },

    async getActSet(type) {
      if (this.model.trdgrp.fip.pts.ptyinr == "" || this.model.trdgrp.cbs.max.cur == "") {
        return;
      }
      let data = {
        ptyinr: this.model.trdgrp.fip.pts.ptyinr,
        ptainr: this.model.trdgrp.fip.pts.ptainr,
        cur: this.model.trdgrp.cbs.max.cur,
        amt: this.model.trdgrp.cbs.max.amt,
        actpro: this.model.trtp.wbdp.actpro,
      }
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/getActSet`, data);
      loading.close();
      if (res.respCode === SUCCESS) {
        if (type === 'changeCur') {
          this.model.finmod.act1Set = res.data.act1Set;
          this.model.finmod.intactSet = res.data.intactSet;
        }
        this.model.trtp.wbdp.actSet = res.data.actSet;
      }
    },

    async onPctfin() {
      const loading = this.loading();
      let res = await Api.post(`/Financing/trtopn/defaultDefaultPctfinN1000`, {
        ...this.model.trdgrp,
      });
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.trdgrp.rec.pctfin = res.data.rec.pctfin;
        this.model.trdgrp.cbs.max.amt = res.data.cbs.max.amt;
      }
    },

    //WBDCREP面板显示条件
    async onWbdcrepVisiable() {
      let maxcur = this.model.trdgrp.cbs.max.cur;
      let maxamt = this.model.trdgrp.cbs.max.amt;
      let fintyp = this.model.trdgrp.rec.fintyp;
      let ffactl = this.model.trtp.wbdp.ffactl;
      if (ffactl === '' && Number(maxamt) > 0 && maxcur !== 'CNY' && maxcur !== '' && ['BRF', 'BCF', 'ECF'].includes(fintyp)) {
        this.model.trtp.wbdp.visiableFlag = 'X';
        this.model.trdgrp.rec.ffaflg = 'Y';
      } else {
        this.model.trtp.wbdp.visiableFlag = '';
        this.model.trtp.wbdp.act = '';
        this.model.trtp.wbdp.actpro = '';
        this.model.trdgrp.rec.ffaflg = '';
      }
    },

    async getIntrat() {
      this.model.trdgrp.rec.intrat = 0;
      if (this.model.trdgrp.cbs.max.cur == "") {
        this.$notify.error({
          title: '错误',
          message: '请先选择币种!'
        });
        return;
      }
      if (this.model.finmod.ratchgtyp == "") {
        this.$notify.error({
          title: '错误',
          message: '请先选择利率调整方式!'
        });
        return;
      }
      if (this.model.trdgrp.rec.matdat == null || this.model.trdgrp.rec.matdat == "") {
        this.$notify.error({
          title: '错误',
          message: '请先选择到期日!'
        });
        return;
      }
      if (['7', '8', '9'].includes(this.model.finmod.ratchgtyp) && this.model.finmod.ratchgprd == "") {
        this.$notify.error({
          title: '错误',
          message: '请先选择利率调整周期!'
        });
        return;
      }
      if (this.model.trdgrp.cbs.max.cur == "CNY" && this.model.finmod.dinmod.jzllgz == "1" && this.model.trdgrp.rec.lprtyp == "") {
        this.$notify.error({
          title: '错误',
          message: "请选择'LPR期限品种'"
        });
        return;
      }
      //调用2571获取接口
      let request = {
        fintyp: this.model.trdgrp.rec.fintyp,
        cur: this.model.trdgrp.cbs.max.cur,
        opndat: this.model.trdgrp.rec.stttendat,
        matdat: this.model.trdgrp.rec.matdat,
        ratchgtyp: this.model.finmod.ratchgtyp,
        ratchgprd: this.model.finmod.ratchgprd,
        lpctyp: this.model.trdgrp.rec.lprtyp,
        jzllgz: this.model.finmod.dinmod.jzllgz
      }
      let loading = this.loading('正在请求2571接口');
      let rtnmsg = await Api.post(`/Financing/trtopn/call2571`, request);
      if (rtnmsg.respCode === SUCCESS) {
        if (rtnmsg.data.errorMsg !== '' && rtnmsg.data.errorMsg !== null) {
          this.$alert(rtnmsg.data.errorMsg, '提示', {
            confirmButtonText: '确定'
          });
        } else {
          this.model.trdgrp.rec.intrat = rtnmsg.data.intrat;
        }
      }
      loading.close();
    },

    async getIntamt() {
      this.model.finmod.intamt = 0;
      if (this.model.trdgrp.rec.matdat == null || this.model.trdgrp.rec.matdat == "") {
        this.$notify.error({
          title: '错误',
          message: '请先选择到期日!'
        });
        return;
      }
      if (this.model.trdgrp.cbs.max.amt <= 0) {
        this.$notify.error({
          title: '错误',
          message: '融资金额必须大于0!'
        });
        return;
      }
      if (this.model.trdgrp.rec.actrat == "") {
        this.$notify.error({
          title: '错误',
          message: '执行利率不能为空!'
        });
        return;
      }
      //调用6153接口获取预扣息金额
      // let request = this.model.finmod;
      this.model.finmod.itftyp = "1";
      let loading = this.loading('正在请求6153接口');
      let rtnmsg = await Api.post(`/Financing/trtopn/call6153`, this.model);
      if (rtnmsg.respCode === SUCCESS) {
        if (rtnmsg.data.errorMsg !== '' && rtnmsg.data.errorMsg !== null) {
          this.$alert(rtnmsg.data.errorMsg, '提示', {
            confirmButtonText: '确定'
          });
        } else {
          this.model.finmod.intamt = rtnmsg.data.intamt;
        }
      }
      loading.close();
    },

    //后端还没有获取dinjdm接口
    async getDinjdm() {
      let opnflg = this.model.finmod.opnflg;
      if (opnflg === 'Y') {
        //调用接口CPC3获取定价数据
        const loading = this.loading();
        let params = {
          dinjdm: this.model.finmod.dinjdm,
        }
        let rtnmsg = await Api.post(`/Financing/trtopn/callCPC3`, params);
        if (rtnmsg.respCode === SUCCESS) {
          if (rtnmsg.data.errorMsg !== '' && rtnmsg.data.errorMsg !== null) {
            this.$alert(rtnmsg.data.errorMsg, '提示', {
              confirmButtonText: '确定'
            });
          }
          this.model.finmod.dinmod = cloneDeep(rtnmsg.data.dinmod);
          if (rtnmsg.data.dinSta === '1') {
            this.model.trdgrp.rec.jzllgz = this.model.finmod.dinmod.jzllgz;
            this.model.trdgrp.rec.lprtyp = this.model.finmod.dinmod.lprtyp;
            if (Number(this.model.finmod.dinmod.shpjgh) !== 0) {
              this.model.trdgrp.rec.actrat = this.model.finmod.dinmod.shpjgh;
            }
            if (this.model.finmod.dinmod.bizhog !== '') {
              if (this.model.trdgrp.cbs.max.cur !== this.model.finmod.dinmod.bizhog) {
                this.customAddModify(this.model.trdgrp.cbs.max, 'cur');
                this.model.trdgrp.cbs.max.cur = this.model.finmod.dinmod.bizhog;
                this.changeCur();
              }
            }
          }
        }
        loading.close();
      } else {
        this.$alert("定价审批CPC3接口已关闭", '提示', {
          confirmButtonText: '确定'
        });
      }
    },

    async getBrdgrp() {
      const loading = this.loading();
      let rtnmsg = await Api.post(`/Financing/trtopn/ruleBrdgrpRecOwnrefN1250`, this.model);
      loading.close();
      if (rtnmsg.respCode === SUCCESS) {
        this.model.trdgrp = rtnmsg.data.trdgrp;
      }
    },

    changeActtyp() {
      if (this.model.finmod.acttyp === '') {
        this.model.finmod.intact = '';
        this.model.finmod.act3 = '';
      }
      if (this.model.finmod.inttyp === '1') {
        if (this.model.finmod.acttyp === '0' || this.model.finmod.acttyp === '2') {
          this.model.finmod.intact = this.model.finmod.act3;
        }
      }
    },

    changeAct3() {
      if (this.model.finmod.inttyp === '1') {
        if (this.model.finmod.acttyp === '0' || this.model.finmod.acttyp === '2') {
          this.model.finmod.intact = this.model.finmod.act3;
        }
      }
    },

    changeRzkxp(type) {
      if (type === '1') {
        if (this.model.financ.rzkx1 === 'X') {
          this.model.financ.rzkx2 = '';
          this.model.financ.ref2 = '';
          this.model.financ.rzkx3 = '';
          this.model.financ.ref3 = '';
          this.model.financ.rzkx4 = '';
          this.model.financ.ref4 = '';
          this.model.financ.rzkx5 = '';
          this.model.financ.ref5 = '';
        }
      }
      if (type === '2') {
        if (this.model.financ.rzkx2 === 'X') {
          this.model.financ.rzkx1 = '';
          this.model.financ.acc = '';
          this.model.financ.rzkx3 = '';
          this.model.financ.ref3 = '';
          this.model.financ.rzkx4 = '';
          this.model.financ.ref4 = '';
        }
      }
      if (type === '3') {
        if (this.model.financ.rzkx3 === 'X') {
          this.model.financ.rzkx1 = '';
          this.model.financ.acc = '';
          this.model.financ.rzkx2 = '';
          this.model.financ.ref2 = '';
          this.model.financ.rzkx4 = '';
          this.model.financ.ref4 = '';
        }
      }
      if (type === '4') {
        if (this.model.financ.rzkx4 === 'X') {
          this.model.financ.rzkx1 = '';
          this.model.financ.acc = '';
          this.model.financ.rzkx2 = '';
          this.model.financ.ref2 = '';
          this.model.financ.rzkx3 = '';
          this.model.financ.ref3 = '';
        }
      }
      if (type === '5') {
        if (this.model.financ.rzkx5 === 'X') {
          this.model.financ.rzkx1 = '';
          this.model.financ.acc = '';
        }
      }
    },

    async getCpdinfo() {
      if (this.model.trdgrp.rec.fintyp == 'ECF' && !this.model.trdgrp.rec.pnttyp && this.model.trdgrp.fip.pts.ref) {
        console.log(this.model.trdgrp.fip.pts.ref);
        const loading = this.loading();
        let res = await Api.post(`/Financing/trtopn/getCpdinfo`, this.model);
        loading.close();
        if (res.respCode === SUCCESS) {
          this.model.trdgrp = res.data.trdgrp;
        }
      }
    },

    async getBopcodList() {
      this.bopcodDialogTableVisible = true;
      this.tableList = [];
      let request = {
        transName: "bptopn",
        typ: "I",
        trntyp: this.model.trdgrp.rec.trntyp
      };
      let loading = this.loading("正在请求交易性质列表");
      let res = await Api.post(
        `/${this.moduleRouter()}/codetable/bopcod/list`,
        request
      );
      if (res.respCode === SUCCESS) {
        this.tableList = res.data;
      }
      loading.close();
    },

    changeAct1() {
      if (this.model.zmqflg === 'X') {
        let act1 = this.model.finmod.act1;
        let oppaccModified = false;
        if (this.model.trdgrp.rec
            && this.model.trdgrp.rec.modifySet
            && this.model.trdgrp.rec.modifySet.includes('oppacc')) {
              oppaccModified = true;
        }
        if (!oppaccModified) {
          this.model.trdgrp.rec.oppacc = act1;
        }
      }
    },

  },
};