import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import Utils from "~/utils/index";
import BigNumber from 'bignumber.js';
export default {
  mixins: [commonFunctions],
  methods: {
    // 获取业务编号
    async getOwnref() {
      if (this.model.jsdgrp.apl.pts.ptainr == "") {
        this.$notify.warning({
          title: '提示',
          message: '请先选择申请人!'
        });
        return;
      }
      let params = {
        ownref: this.model.jsdgrp.rec.ownref,
        ptainr: this.model.jsdgrp.apl.pts.ptainr,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/jstopm/getRef`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.jsdgrp.rec.ownref = res.data;
      } else {
        this.$message.error(res.respMsg);
      }
    },
    // 事件 -- 买卖方向
    pngcfxChange() {
      if (this.model.jsdgrp.rec.pngcfx == '0') {
        this.model.jsdgrp.cbs.max2.amt = new BigNumber(0);
      }
      if (this.model.jsdgrp.rec.pngcfx == '1') {
        this.model.jsdgrp.cbs.max1.amt = new BigNumber(0);
      }
      this.setMaxCurAndAmt();
    },
    // 事件 -- 银行买入金额币种
    max1CurChange() {
      this.setMaxCurAndAmt();
      this.clearRzActInfo();
      this.rzhtypChange();
      this.czhtypChange();
    },
    // 事件 -- 银行买入金额
    max1AmtBlur() {
      this.setMaxCurAndAmt();
    },
    // 事件 -- 银行卖出金额币种
    max2CurChange() {
      this.setMaxCurAndAmt();
      this.clearCzActInfo();
      this.rzhtypChange();
      this.czhtypChange();
    },
    // 事件 -- 银行卖出金额
    max2AmtBlur() {
      this.setMaxCurAndAmt();
      // this.calAmt();
    },
    // 事件 -- 客户选择
    khwtxzChange() {
      // KHWTXZ 客户选择 ==> 1:接受时价、2:输入最小获利期望
      if (this.model.jsdgrp.rec.khwtxz == '1') {
        this.model.jsdgrp.rec.weithl = '0';
        this.model.jsdgrp.rec.jqmrhl = '0';
      }
    },
    // 事件 -- 委托汇率
    weithlBlur() {
      this.model.jsdgrp.blk.weithl = this.model.jsdgrp.rec.weithl;
    },
    // 事件 -- 申请人
    async aplChange() {
      let params = {
        ptainr: this.model.jsdgrp.apl.pts.ptainr,
        ptyinr: this.model.jsdgrp.apl.pts.ptyinr,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/jstopm/getWrkBranch`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.model.jsdgrp.rec.ruzhjg = res.data.ruzhjg;
        this.model.jsdgrp.rec.dianhh = res.data.dianhh;
        this.model.jsdgrp.rec.dfkhlb = res.data.dfkhlb;
        if (this.model.jsdgrp.rec.dfkhlb == '01') {
          this.$notify.warning("请确认,该客户是否为我行");
        }
      }
      this.clearRzActInfo();
      this.clearCzActInfo();
      this.rzhtypChange();
      this.czhtypChange();
    },
    // 事件 -- 获取入账账号列表
    async rzhtypChange() {
      if(this.model.jsdgrp.apl.pts.ptyinr == ''){
        return;
      }
      if(this.model.jsdgrp.rec.rzhtyp == ''){
        return;
      }
      if(this.model.jsdgrp.cbs.max1.cur == ''){
        return;
      }
      if(this.model.jsdgrp.apl.pts.ptainr == ''){
        return;
      }
      let params = {
        ptyinr: this.model.jsdgrp.apl.pts.ptyinr,
        trmtyp: this.model.jsdgrp.rec.rzhtyp,
        cur: this.model.jsdgrp.cbs.max1.cur,
        ptainr: this.model.jsdgrp.apl.pts.ptainr,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/jstopm/getAccList`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.zha1zhList = res.data;
      }
    },
    // 事件 -- 入账账号校验
    async zha1zhChange() {
      var result = await this.checkAccount(this.model.jsdgrp.rec.zha1zh);
      // var txt = "【" + this.model.jsdgrp.rec.zha1zh + "】账户性质为待核查账户,请确认是否符合相关规定,是否使用此账户?";
      if ("Y" == result.dhcflg) {
        this.model.jsdgrp.rec.zha1zh = '';
        this.$notify.warning({
          title: '提示',
          message: '外汇买卖品种下不能使用待核查账户!请重新选择账户'
        });
        // this.$confirm(txt, '提示', {
        //   confirmButtonText: '是',
        //   cancelButtonText: '否',
        //   type: 'warning',
        //   showClose: false
        // }).then(() => { //yes的执行在这里写

        // }).catch(() => { //No的功能在这里写
        //   this.model.jsdgrp.rec.zha1zh = '';
        //   this.$message({
        //     type: 'info',
        //     message: '请重新选择账户'
        //   });
        // });
      }
    },
    // 事件 -- 获取出账账号列表
    async czhtypChange() {
      if(this.model.jsdgrp.apl.pts.ptyinr == ''){
        return;
      }
      if(this.model.jsdgrp.rec.czhtyp == ''){
        return;
      }
      if(this.model.jsdgrp.cbs.max2.cur == ''){
        return;
      }
      if(this.model.jsdgrp.apl.pts.ptainr == ''){
        return;
      }
      let params = {
        ptyinr: this.model.jsdgrp.apl.pts.ptyinr,
        trmtyp: this.model.jsdgrp.rec.czhtyp,
        cur: this.model.jsdgrp.cbs.max2.cur,
        ptainr: this.model.jsdgrp.apl.pts.ptainr,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/jstopm/getAccList`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        this.zhanghList = res.data;
      }
    },
    // 事件 -- 出账账号校验
    async zhanghChange() {
      var result = await this.checkAccount(this.model.jsdgrp.rec.zhangh);
      // var txt = "【" + this.model.jsdgrp.rec.zhangh + "】账户性质为待核查账户,请确认是否符合相关规定,是否使用此账户?";
      if ("Y" == result.dhcflg) {
        this.model.jsdgrp.rec.zhangh = '';
        this.$notify.warning({
          title: '提示',
          message: '外汇买卖品种下不能使用待核查账户!请重新选择账户'
        });
        // this.$confirm(txt, '提示', {
        //   confirmButtonText: '是',
        //   cancelButtonText: '否',
        //   type: 'warning',
        //   showClose: false
        // }).then(() => { //yes的执行在这里写

        // }).catch(() => { //No的功能在这里写
        //   this.model.jsdgrp.rec.zhangh = '';
        //   this.$message({
        //     type: 'info',
        //     message: '请重新选择账户'
        //   });
        // });
      }
    },
    // 校验账号
    async checkAccount(accountTemp) {
      if (this.model.jsdgrp.apl.pts.ptainr == "") {
        this.$notify.warning({
          title: '提示',
          message: '请先选择申请人!'
        });
        return;
      }
      let params = {
        account: accountTemp,
        ptainr: this.model.jsdgrp.apl.pts.ptainr,
      }
      const loading = this.loading();
      let res = await Api.post(`/${this.moduleRouter()}/jstopn/checkAccount`, params);
      loading.close();
      if (res.respCode === SUCCESS) {
        return res.data;
      } else {
        this.$message.error(res.respMsg);
      }
    },
    // 事件 -- 获取账号具体信息
    async getCoreAccountInfo(actVal, curVal) {
      if (actVal === '') {
        this.$alert('请先获取账号!', '提示', {
          confirmButtonText: '确定'
        });
        return;
      }
      let request = {
        account: actVal
      }
      let loading = this.loading('正在获取详情信息中...');
      let rtnmsg = await Api.post(`/${this.moduleRouter()}/jstopn/getCoreAccountCode`, request);
      if (rtnmsg.respCode === SUCCESS) {
        if (rtnmsg.data.errorMsg !== '' && rtnmsg.data.errorMsg !== null) {
          this.$alert(rtnmsg.data.errorMsg, '提示', {
            confirmButtonText: '确定'
          });
        } else {
          var acccode = rtnmsg.data.acctCode;
          var zhuzwm = rtnmsg.data.zhuzwm;
          var wjzhxz = rtnmsg.data.wjzhxz;
          var accountBalance = rtnmsg.data.acctBalance;
          this.$alert('账号:' + actVal + '<br/>户名为:' + zhuzwm + '<br/>账号性质为:' + wjzhxz + '<br/>账户科目号为:' + acccode + '<br/>账户余额为:' + curVal + this.moneyFormat(accountBalance, curVal) + '<br/>账户状态为:' + rtnmsg.data.jiluzt, '提示', {
            confirmButtonText: '确定',
            dangerouslyUseHTMLString: true
          });
        }
      }
      loading.close();
    },
    // max.cur max.amt
    setMaxCurAndAmt() {
      if (this.model.jsdgrp.rec.pngcfx == '0') {
        this.model.jsdgrp.cbs.max.cur = this.model.jsdgrp.cbs.max1.cur;
        this.model.jsdgrp.cbs.max.amt = this.model.jsdgrp.cbs.max1.amt;
      }
      if (this.model.jsdgrp.rec.pngcfx == '1') {
        this.model.jsdgrp.cbs.max.cur = this.model.jsdgrp.cbs.max2.cur;
        this.model.jsdgrp.cbs.max.amt = this.model.jsdgrp.cbs.max2.amt;
      }
    },
    // 清空(入账)账号、账号类型
    clearRzActInfo() {
      this.model.jsdgrp.rec.rzhtyp = 'LO';
      this.model.jsdgrp.rec.zha1zh = '';
    },
    // 清空(出账)账号、账号类型
    clearCzActInfo() {
      this.model.jsdgrp.rec.czhtyp = 'LO';
      this.model.jsdgrp.rec.zhangh = '';
    },
    // 禁用对方客户类别字段
    dfkhlbDisabled() {
      var tempList = ['01', '02', '03', '04', '05', '06'];
      var index = tempList.indexOf(this.model.jsdgrp.rec.dfkhlb);
      return index != -1 && this.model.jsdgrp.apl.pts.ptyinr != '';
    }

  }
}