
import { objAddPrefix } from '@/utils';

export default {

  computed: {
  },

  methods: {
    /**  ----------------------------- 所有簿记 公共用的一样的事件提取出来 ---------------------------- */
    // 删除
    handleDelete() {},
    async IsHoliday(date) {
      const data = {
        code: this.formData.underlying,
        valueDate: date,
        type: 'PAIR',
      };
      const res = await this.$apis.getdcsisHolidayByUnderlying(data);
      return res.data.result;
    },
    // get交易对手名称
    getDCSqueryCptyName(val) {
      this.$apis.getDCSqueryCptyName({ code: val }).then((res) => {
        const result = res.data.result;
        this.formData.cptyName = result;
      });
    },
    // get账户名称
    getDCSqueryFolderById(val) {
      this.$apis.getDCSqueryFolderById({ code: val }).then((res) => {
        const result = res.data.result;
        this.formData.folderName = result.localName;
      });
    },
    // 打开 要复制交易 弹窗
    handleQuickSearch() {
      this.quickSearchVisible = false;
      setTimeout(() => {
        this.quickSearchVisible = true;
      }, 20);
    },
    // 关闭 要复制交易 弹窗
    handleCloseQuickSearch() {
      this.quickSearchVisible = false;
    },
    // 打开 附件管理 弹窗
    handleattachmentVisible() {
      this.attachmentVisible = false;
      setTimeout(() => {
        this.attachmentVisible = true;
      }, 20);
    },
    // 关闭 附件管理 弹窗
    handleCloseAttachment() {
      this.attachmentVisible = false;
    },
    /** ----------------------------单个页面内的methods最终都要移到这下面来，短期先不改方便开发 -----------------------  */
    /** -------------------------------------- 外汇即期 -----------------------------------------  */
    // 初始化数据
    getOptions_ForexSpot() {
      const data = {
        codifierGrpCodes:
          'settleMode,static_folders,static_cptys,DealStatusRcs,static_accBank,CashflowDirection,DealFlagRcs,AppId,Tpurpose,Tacount,static_currencies,static_pairs,static_pmPairs,OptionSettlementMode,BuySellRcs,static_pmCurrencies',
      };
      this.$apis.getDcsOptions(data).then((res) => {
        const result = res.data.result;
        this.zdArr[0].formArr[0].options = result.pairs;
        this.zdArr[2].formArr[0].options = result.DealFlagRcs;
        this.zdArr[2].formArr[2].options = result.AppId;
        this.zdArr[2].formArr[5].options = result.Tacount;
        this.zdArr[2].formArr[6].options = result.Tpurpose;
        this.zdArr[3].formArr[6].options = result.settleMode;
      });
    },
    // get日期
    getSystemDate_ForexSpot() {
      this.$apis.getDCSgetSystemDate({}).then((res) => {
        const result = res.data.result;
        this.formData.tradeDate = this.formData.transactionDate =
          result.date.toString();
        this.formData.tradeTime = this.formData.transactionTime = result.time
          .toString()
          .slice(0, 4);
      });
    },
    // get金额1
    getamount1_ForexSpot() {
      const data = {
        expression: `${this.formData.amount2} / ${
          this.formData.spotRate || 1
        } * -1 / ${Math.pow(10, this.formData.quotationUnit || 0)}`,
        precision: this.formData.noDecimal1 || 2,
      };
      this.$apis.getDCSdoEvaluation(data).then((res) => {
        this.formData.amount1 = res.data.result;
      });
    },
    // get金额2
    getamount2_ForexSpot() {
      const data = {
        expression: `${this.formData.amount1} * ${
          this.formData.spotRate || 1
        } * -1 / ${Math.pow(10, this.formData.quotationUnit || 0)}`,
        precision: this.formData.noDecimal2 || 2,
      };
      this.$apis.getDCSdoEvaluation(data).then((res) => {
        this.formData.amount2 = res.data.result;
      });
    },
    // 要复制交易 table row 双击事件
    handleQuickSearchdblclick_ForexSpot(row) {
      const data = {
        'queryParam.dealId': row.dealId,
        'queryParam.instrument': row.instrument,
        'queryParam.product': row.product,
      };
      this.$bus.$emit('bujiselectRow', row); // 现金流（注意和现金流预览区分）
      this.$apis.getdealQueryQueryList(data).then((res) => {
        const result = res.data.result[0];
        // this.formData = {
        //   ...this.formData,
        //   ...result,
        //   maturityDate: result.maturityDate.toString(),
        //   tradeDate: result.tradeDate.toString(),
        //   tradeTime: result.tradeTime.toString(),
        //   transactionDate: result.transactionDate.toString(),
        //   transactionTime: result.transactionTime.toString(),
        //   // transactionTime: '',
        // };

        this.formData = {
          folder: '',
          folderName: '',
          cptyName: '',
          spotDealId: '',
          userId: '',
          contractId: '',
          dealId: '',
          globalId: '',
          instrument: result.instrument,
          product: result.product,
          bank: result.bank,
          underlyingType: result.underlyingType,
          inputMode: result.inputMode,
          takerId: result.takerId,
          ext19: result.ext19,
          internalTradeFlag: result.internalTradeFlag,
          cpty: result.cpty,
          dbCheck: result.dbCheck,
          conflictFlag: result.conflictFlag,
          capturedAmount: result.capturedAmount,
          amount1bak: result.amount1bak,
          quotationUnit: result.quotationUnit,
          amount2bak: result.amount2bak,
          splitInfo: `{"splitRate2":${result.splitSpotRateSecond},"splitSpotRate":${result.splitSpotRateFirst},"firstDealPair":${result.underlying}}`,
          underlying: result.underlying,
          spotRate: result.spotRate,
          amount1: result.amount1,
          amount2: result.amount2,
          buySell: result.buySell,
          tradeDate: result.tradeDate?.toString(),
          tradeTime: result.tradeTime?.toString(),
          valueDate: result.valueDate?.toString(),
          dealFlag: result.dealFlag,
          sourceName: result.sourceName,
          templateCode: result.templateCode,
          transactionDate: result.transactionDate?.toString(),
          transactionTime: result.transactionTime?.toString(),
          tacount: result.tacount,
          tpurpose: result.tpurpose,
          ext20: result.ext20,
          comments: result.comments,
          ssiCodeOurBuyCcy1: result.ssiCodeOurBuyCcy1 || '',
          ssiCodeCptyBuyCcy1: result.ssiCodeCptyBuyCcy1 || '',
          ssiCodeOurSellCcy1: result.ssiCodeOurSellCcy1 || '',
          ssiCodeCptySellCcy1: result.ssiCodeCptySellCcy1 || '',
          ssiCodeOurBuyCcy2: result.ssiCodeOurBuyCcy2 || '',
          ssiCodeCptyBuyCcy2: result.ssiCodeCptyBuyCcy2 || '',
          ssiCodeOurSellCcy2: result.ssiCodeOurSellCcy2 || '',
          ssiCodeCptySellCcy2: result.ssiCodeCptySellCcy2 || '',
          ext1: result.ext1,
          ext2: result.ext2,
          ext3: result.ext3,
          ext4: result.ext4,
          settlementMode: result.settlementMode,
          isPass: result.isPass,
          isBondDuration: result.isBondDuration,
          extContent:
            '@undefined|\u8d27\u5e011\u8d26\u6237\u884c@|\u8d27\u5e012\u8d26\u6237\u884c@|\u6e05\u7b97\u65b9\u5f0f@|\u4e1a\u52a1\u5458@undefined',
          maturityDate: result.maturityDate?.toString(),
        };

        this.setccy1ccy2_ForexSpot(result.underlying);
        this.getdcsPairInfo_ForexSpot(result.underlying);
        // this.getDCSqueryFolderById(result.folder);
        this.getDCSqueryCptyName(result.cpty);
      });

      this.quickSearchVisible = false;
    },
    // 附件管理 事件
    handleAttachmentdblclick_ForexSpot(row) {
      this.attachmentVisible = false;
    },
    handleclearForm_ForexSpot() {
      this.zdArr[0].formArr[1].attrs.label = `金额()`;
      this.zdArr[0].formArr[3].attrs.label = `金额()`;
      this.zdArr[3].formArr[0].attrs.label = `我方收结算路径()`;
      this.zdArr[3].formArr[1].attrs.label = `对手方收结算路径()`;
      this.zdArr[3].formArr[2].attrs.label = `我方付结算路径()`;
      this.zdArr[3].formArr[3].attrs.label = `对手方付结算路径()`;
      this.handleCancelForm_ForexSpot();
      this.formData = {
        underlying: null,
        tradeDate: '',
        tradeTime: '',
        transactionDate: '',
        transactionTime: '',
        folderName: '',
        cptyName: '',
        spotRate: '',
        quotationUnit: '',
        noDecimal1: '',
        noDecimal2: '',
        maturityDate: '',
        amount1: '',
        amount2: '',
        dealFlag: 'INTERBANK',
        sourceName: 'RCS',
      };
      this.getSystemDate_ForexSpot();
      this.$bus.$off('bujiselectRow');
    },
    handleCancelForm_ForexSpot() {
      this.$refs.publicForm.resetForm();
    },
    handleModal_ForexSpot(num) {
      this.handleType = num;
      const data = {
        ...this.formData,
        dbCheck: '',
        quotationUnit: '1',
        capturedAmount: '1',
        conflictFlag: 'true',
        internalTradeFlag: 'false',
        cptyType: '',
        ofEventType: '',
        versionType: 'baseVersion',
        typeOfEvent: 'BOOKING',
        siteCode: 'BANKZB',
        inputMode: 'G',
      };
      const formData = objAddPrefix(data, 'deal');
      this.$bus.$emit('bujiformData', formData);
    },
    // get汇率
    getdcsPairInfo_ForexSpot(val) {
      this.$apis.getdcsPairInfo({ pairCode: val }).then((res) => {
        const result = res.data.result;
        this.formData.quotationUnit = result.quotationUnit;
        this.formData.noDecimal1 = result.noDecimal1;
        this.formData.noDecimal2 = result.noDecimal2;
      });
    },
    // 设置ccy
    setccy1ccy2_ForexSpot(val) {
      this.ccy1 = val.substring(0, 3);
      this.ccy2 = val.substring(3, 6);
      this.zdArr[0].formArr[1].attrs.label = `金额(${this.ccy1})`;
      this.zdArr[0].formArr[3].attrs.label = `金额(${this.ccy2})`;
      this.zdArr[3].formArr[0].attrs.label = `我方收结算路径(${this.ccy1})`;
      this.zdArr[3].formArr[1].attrs.label = `对手方收结算路径(${this.ccy1})`;
      this.zdArr[3].formArr[2].attrs.label = `我方付结算路径(${this.ccy2})`;
      this.zdArr[3].formArr[3].attrs.label = `对手方付结算路径(${this.ccy2})`;
    },
    // 提交 新增
    handleSubmit_ForexSpot() {
      const data1 = {
        instrument: this.formData.instrument,
        product: this.formData.product,
        sourceName: this.formData.sourceName,
        errorShow: false,
      };
      this.$apis.getdcsDealNo(data1).then((response) => {
        this.formData.globalId = response.data.result;
        this.formData.dbCheck = '';
        this.formData.quotationUnit = '1';
        this.formData.capturedAmount = '1'; // 1 和 2怎么区分
        this.formData.conflictFlag = 'true';
        this.formData.internalTradeFlag = 'false';
        this.formData.cptyType = '';
        this.formData.ofEventType = '';
        this.formData.versionType = 'baseVersion';
        this.formData.typeOfEvent = 'BOOKING';
        this.formData.siteCode = 'BANKZB';
        this.formData.inputMode = 'G';
        const data = objAddPrefix(this.formData, 'deal');
        this.$apis.handledcsdealActionFlow(data).then((res) => {
          this.$message.success('操作成功');
          this.handleclearForm_ForexSpot();
        });
      });
    },
    // 预submit
    handlePreSubmit() {
      if (this.$refs.publicForm.submitForm()) {
        console.log('this.formData', this.formData);
        this.handleSubmitIsHoliday_ForexSpot();
      }
    },
    sureHoliday(val) {
      this.$alert(`【${val}】是节假日，确认执行?`, '提示', {
        confirmButtonText: '确定',
        showCancelButton: true,
        cancelButtonText: '取消',
        callback: (action) => {
          if (action === 'confirm') {
            this.handleSubmit_ForexSpot();
          }
        },
      });
    },
    // submit前判断是否是节假日
    async handleSubmitIsHoliday_ForexSpot() {
      const isH = await this.IsHoliday(this.formData.tradeDate);
      if (isH) {
        this.sureHoliday('起息日');
      } else {
        const isH1 = await this.IsHoliday(this.formData.maturityDate);
        if (isH1) {
          this.sureHoliday('交易日期');
        }
      }
    },
  },
};
