<template>
    <div>
        <van-nav-bar title="订单信息" />
        <van-card class="my-card" v-for="(orderDetail, index) in orderDetails" :key="index" :num="orderDetail.productNumber"
            :price="orderDetail.price.toFixed(2)" :title="orderDetail.productName"
            :thumb="showSugarPhoto(orderDetail.productNo)" />
        <van-submit-bar :price="amount" button-color="#07c160" button-text="支付" @click="wxPay()" />
    </div>
</template>
  
<script>
import { useRoute } from "vue-router";
import { showFailToast, showSuccessToast, showToast, showDialog } from 'vant';
import { getOrderBySn } from '@/service/popPayment';
import { onMounted, ref } from "vue";


export default {
    name: 'popPayment',

    setup() {
        const route = useRoute();
        const sn = route.query.sn;
        const status = route.query.status;
        const order = ref(null);
        const orderDetails = ref([]);
        const amount = ref();
        // const price = ref();

        onMounted(async () => {
            if (status == 1) {
                showDialog({
                    message: '订单已支付',
                    theme: 'round-button',
                    confirmButtonColor: '#07c160',
                }).then(() => {
                    if (typeof window.WeixinJSBridge !== 'undefined') {
                        window.WeixinJSBridge.call('closeWindow');
                    } else {
                        window.close();
                    }
                })
            } else {
                await getOrder();
            }
        });
        const getOrder = async () => {
            const { data } = await getOrderBySn({ sn: sn });
            if (data.code === '00000') {
                order.value = data.data;
                orderDetails.value = data.data.orderDetails;
                console.log("order", order.value);
                console.log("orderDetails", orderDetails.value[0]);
                amount.value = order.value.price * 100;
                // console.log(amount.value);
                // price.value = order.value.price.toFixed(2);
            } else { showFailToast(data.message); }

        };
        const showSugarPhoto = (no) => {
            if (no == 'A99') {
                return require(`../assets/order/spunSugar/goods/A30.png`);
            }
            return require(`../assets/order/spunSugar/goods/${no}.png`);
        };
        const wxPay = () => {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                "appId": "wxd9c7c138a055ee66",     //公众号ID，由商户传入     
                "timeStamp": route.query.timeStamp,         //时间戳，自1970年以来的秒数     
                "nonceStr": route.query.nonceStr, //随机串     
                "package": route.query.package,
                "signType": "RSA",         //微信签名方式：     
                "paySign": route.query.paySign //微信签名 
            },
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        console.log("支付成功");
                        showSuccessToast('支付成功');
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        console.log("取消支付");
                        showToast('取消支付');
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        console.log("支付失败");
                        showFailToast('支付失败');
                    }
                }
            )
        };
        return {
            wxPay,
            getOrder,
            orderDetails,
            showSugarPhoto,
            // price,
            amount
        };
    }
};
</script>

<style>
.van-image__img {
    object-fit: contain !important;
}
</style>