<template>
    <view class="points-detail">
        <view class="card">
            <view class="status">{{ order.pay_status_text }}</view>
            <view class="row">
                <text>订单编号</text>
                <text>{{ order.sn }}</text>
            </view>
            <view class="row">
                <text>积分套餐</text>
                <text>{{ order.product_name }}</text>
            </view>
            <view class="row">
                <text>到账积分</text>
                <text>{{ order.points }}</text>
            </view>
            <view class="row">
                <text>支付金额</text>
                <text class="amount">￥{{ order.order_amount }}</text>
            </view>
            <view v-if="order.pay_time" class="row">
                <text>支付时间</text>
                <text>{{ order.pay_time }}</text>
            </view>
        </view>

        <view v-if="order.pay_status === 0" class="pay-card">
            <view class="pay-title">请选择支付方式</view>
            <view class="pay-tips">支持支付宝、微信支付；使用币支付入口预留中</view>
            <u-button type="primary" shape="circle" @click="payState.showPay = true">立即支付</u-button>
        </view>

        <payment v-model:show="payState.showPay" v-model:show-check="payState.showCheck" :order-id="orderId"
            from="points_order" :redirect="payState.redirect" @success="handlePaySuccess" @fail="handlePayFail" />
    </view>
</template>

<script setup lang="ts">
import { onLoad, onShow } from '@dcloudio/uni-app'
import { reactive, ref } from 'vue'
import { pointsOrderDetail } from '@/api/points'

const orderId = ref(0)
const order = reactive<any>({})
const payState = reactive({
    showPay: false,
    showCheck: false,
    redirect: '/packages/pages/points_order_detail/points_order_detail'
})

const getDetail = async () => {
    const data = await pointsOrderDetail({ order_id: orderId.value })
    Object.assign(order, data)
}

const handlePaySuccess = async () => {
    payState.showPay = false
    payState.showCheck = false
    await getDetail()
    uni.$u.toast('支付成功')
}

const handlePayFail = () => {
    uni.$u.toast('支付失败')
}

onLoad((options: any) => {
    orderId.value = Number(options.order_id || 0)
    payState.redirect = `/packages/pages/points_order_detail/points_order_detail?order_id=${orderId.value}`
})

onShow(() => {
    if (orderId.value) getDetail()
})
</script>

<style lang="scss" scoped>
.points-detail {
    min-height: 100vh;
    padding: 24rpx;
    background: #f5f5f5;
}
.card,
.pay-card {
    padding: 32rpx;
    background: #fff;
    border-radius: 18rpx;
}
.status {
    margin-bottom: 24rpx;
    font-size: 36rpx;
    font-weight: 700;
    color: #185dff;
}
.row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx 0;
    font-size: 26rpx;
    color: #666;
    border-bottom: 1rpx solid #f2f2f2;
}
.row:last-child {
    border-bottom: 0;
}
.amount {
    color: #185dff;
    font-weight: 700;
}
.pay-card {
    margin-top: 24rpx;
}
.pay-title {
    font-size: 30rpx;
    font-weight: 700;
    color: #222;
}
.pay-tips {
    margin: 12rpx 0 28rpx;
    font-size: 24rpx;
    color: #999;
}
</style>
