import { tradeNoQuery, triggerPay } from '../../utils/service';
import { history } from '../../utils';
import monitor from '../../utils/alipayLogger';
Component({
  props: {
    data: {
      cardNum: '',
      balance: ''
    }
  },
  data: {
    balance: '0',
    // 余额
    amount: '',
    loading: false
  },

  didMount() {
    my.setNavigationBar({
      title: '充值页面'
    });
  },

  methods: {
    async toResult({
      success,
      subTitle
    }) {
      await this.setData({
        loading: false
      });
      history.openPage({
        pageType: 'recharge-result',
        data: {
          success,
          subTitle
        }
      });
    },

    setAmount(e) {
      this.setData({
        amount: e.detail.value
      });
    },

    // 唤起支付弹窗
    async tradeNoQuery() {
      // 生成订单号
      if (Number.isNaN(Number(this.data.amount)) || this.data.amount <= 0) {
        my.showToast({
          type: 'none',
          content: '金额要大于0'
        });
        return;
      }

      if (this.data.loading) {
        return;
      }

      await this.setData({
        loading: true
      });
      const {
        cardNum
      } = this.props.data;
      const [, tradeRes] = await tradeNoQuery({
        type: 2,
        idNum: cardNum,
        amount: this.data.amount
      });

      if (tradeRes) {
        monitor.api({
          api: '门诊缴费',
          success: true,
          c1: 'taSR_YL',
          time: 2000
        }); // res为：商家订单号

        my.tradePay({
          // 调用统一收单交易创建接口（alipay.trade.create），获得返回字段支付宝交易号trade_no
          tradeNO: tradeRes.tradeNo,
          success: payRes => {
            // 4000 订单处理失败
            // 6001 用途中途取消支付
            // 6002 网络链接出错
            if (payRes.resultCode === '4000' || payRes.resultCode === '6002') {
              // 支付失败
              this.toResult({
                success: false
              });
            } else if (payRes.resultCode === '6001') {
              // 取消支付
              this.toResult({
                success: false,
                subTitle: '取消支付'
              });
            } else {
              // 其他情况调用接口确认
              this.triggerPay(tradeRes.outTradeNo, tradeRes.tradeNo, payRes.resultCode);
            }
          },
          fail: res => {
            // 订单支付异常
            this.toResult({
              success: false,
              subTitle: res.message
            });
          }
        });
      } else {
        monitor.api({
          api: '门诊缴费',
          success: false,
          c1: 'taSR_YL',
          time: 2000
        });
      }
    },

    async triggerPay(outTradeNo, tradeNo, resultCode) {
      const [err, confirmRes] = await triggerPay({
        outTradeNo,
        tradeNo,
        resultCode
      });

      if (err) {
        this.toResult({
          success: false
        });
        return;
      } // 交易成功


      if (confirmRes && confirmRes.status === 'TRADE_SUCCESS') {
        // 已经成功
        this.toResult({
          success: true,
          subTitle: `流水号：${tradeNo}`
        });
      } else {
        // 其他情况视为异常
        this.toResult({
          success: false
        });
      }
    }

  }
});