<template>
  <u-popup :show="popupShow" mode="bottom" @close="popupShow = false" class="u_popup_bottom">
    <view class="bg_color_fff u_popup_bottom">
      <view class="u_popup_bottom_desc border-bottom gray font_size25 text_align_center">请选择付款方式</view>
      <view class="padding30 border-bottom text_align_center" @click="popupShow = false;pay(0)">微信支付</view>
      <!-- <view class="padding30 border-bottom text_align_center" @click="popupShow = false;pay(1)">
        余额支付
        <text class="color_price"> ( {{appStore.userInfo?.rechargeBalance}} {{appStore.moneyUnit}} )</text>
      </view> -->
      <view class="padding30 text_align_center" @click="popupShow = false">取消</view>
    </view>
  </u-popup>
</template>
<script setup>
import { ref } from 'vue'
import { createOrder } from "@/api/order.js";
import { wxPay } from "@/utils/util.js";
import { useAppStore } from "@/stores/app";
const appStore = useAppStore();
defineExpose({
  open
});
const emit = defineEmits(['reloadList']);
const popupShow = ref(false);
const orderId = ref(null);
const payType = ref('');//支付方式 0微信 1余额
function open(e) {
  popupShow.value = true;
  orderId.value = e.orderId;
}

async function pay(payMethod){
  console.log('pay',payMethod,orderId.value);
  payType.value = payMethod;
  const res = await createOrder({
    payMethod:payType.value,//支付方式 0微信 1余额
    orderId:orderId.value
  });
  orderId.value = res.data.orderId;
  // 余额支付
  if(payType.value==1){
    //1成功 0失败；
    const paySuccess = res.data.paySuccess;
    orderStatusQuery({isSuccess:paySuccess?1:0});
  }else{ 
    const payInfo = res.data.payData.prepayWithRequestPaymentResponse;
    wxPay({
      timeStamp:payInfo.timeStamp,
      nonceStr:payInfo.nonceStr,
      packageVal:payInfo.packageVal,
      signType:payInfo.signType,
      paySign:payInfo.paySign,
    },orderStatusQuery);
  }
}
function orderStatusQuery({isSuccess}){
 emit('reloadList');
 to_success_pay({isSuccess});
}
function to_success_pay({isSuccess}){
	console.log('to_success_pay',isSuccess);
	uni.navigateTo({
    url: `/pages/recharge/success_pay?orderId=${orderId.value}&isSuccess=${isSuccess}&payMethod=${payType.value}`
  })
}
</script>
<style lang="scss">
.u_popup_bottom{
	.u-popup__content{
		border-radius: 30rpx 30rpx 0 0;
	}
  .u_popup_bottom_desc{
		padding: 50rpx 50rpx 30rpx;
	}
}
</style>
<style lang="scss" scoped>
.u_popup_bottom{
}
</style>
