import { getPreSettlementDetail, creatOrder } from './service';
import history from '../../utils/history';
import { tradeResult } from '../../service/common';
Component({
  data: {
    preSettlementDetail: {},
    // 预结算详情内容
    inpatientId: '',
    // 住院记录id
    useMedicare: false,
    // 使用医保结算，如果使用医保结算需要医保电子凭证授权进行预结算
    payAuthNo: '',
    // 医保线上支付授权编码
    repay: '' // 是true为补交，false是退款

  },

  didMount() {
    // 获取参数住院IDhospitalDistrictId
    const optionsValue = JSON.parse(JSON.stringify(this.$page.data.query));
    const {
      inpatientId,
      medicareAble,
      payAuthNo,
      repay
    } = optionsValue;
    console.log(repay);
    this.setData({
      inpatientId,
      payAuthNo,
      useMedicare: medicareAble,
      repay
    }, () => {
      // 获取预结算详情内容
      this.getPreSettlementDetailInfo();
    });
  },

  methods: {
    /**
       * 获取预结算详情内容
       */
    async getPreSettlementDetailInfo() {
      const {
        inpatientId
      } = this.data;
      const params = {
        inpatientId
      };
      const preSettlementDetail = await getPreSettlementDetail(params);
      this.setData({
        preSettlementDetail
      });
    },

    /**
     * 立即结算 - 获取交易号/唤起收银台参数
     */
    async onImmediateSettlement() {
      const {
        inpatientId,
        useMedicare,
        payAuthNo
      } = this.data;
      const params = {
        inpatientId,
        useMedicare,
        payAuthNo
      };
      const resData = await creatOrder(params);
      const {
        orderStr,
        status
      } = resData || {}; // 如果生成订单失败，toast提示

      if (status === 'SUCCESS') {
        // 无须支付/有退款
        const settlementType = 'supportMedicalPreSettlement';
        history.push({
          query: {
            settlementType,
            inpatientId
          },
          title: '出院结算',
          pageType: 'discharge-settlement'
        });
      }

      if (status === 'WAIT_BUYER_PAY') {
        // 医保需支付
        // 医保需支付
        if (!orderStr) {
          my.showToast({
            type: 'fail',
            content: '生成结算订单失败！',
            duration: 2000
          });
          return;
        } // 如果生成订单成功，唤起支付(预授权orderStr)


        my.tradePay({
          orderStr,
          success: async res => {
            // const { resultCode } = res;
            // // 支付成功， 跳转至出院结算结果页
            // if (resultCode === '9000') {
            //   // 跳转类型 supportMedicalPreSettlement - 支持医保，预结算-补交
            //   const settlementType = 'supportMedicalPreSettlement';
            //   history.push({
            //     query: {
            //       settlementType,
            //       inpatientId,
            //     },
            //     title: '出院结算',
            //     pageType: 'discharge-settlement',
            //   });
            //   return;
            // }
            // 4000 订单处理失败
            // 6001 用途中途取消支付
            // 6002 网络链接出错
            if (res.resultCode === '4000' || res.resultCode === '6002' || res.resultCode === '6001') {
              // 支付失败
              my.showToast({
                type: 'fail',
                content: res.memo || '订单支付失败'
              });
            } else {
              // 其他情况调用接口确认
              const payRes = await tradeResult({
                tradeNo: resData.tradeNo,
                resultCode: res.resultCode
              });

              if (payRes.status === 'TRADE_SUCCESS') {
                // 跳转类型 supportMedicalPreSettlement - 支持医保，预结算-补交
                const settlementType = 'supportMedicalPreSettlement';
                history.push({
                  query: {
                    settlementType,
                    inpatientId
                  },
                  title: '出院结算',
                  pageType: 'discharge-settlement'
                });
              } else {
                my.showToast({
                  type: 'fail',
                  content: res.memo || '订单支付失败'
                });
              }
            }
          }
        });
      }
    }

  }
});