<template>
	<view class="payment-container">
		<!-- 收货地址 -->
		<view class="address-section">
			<view class="address-content" v-if="orderInfo">
				<view class="contact-info">
					<text class="name">{{orderInfo.name}}</text>
					<text class="phone">{{orderInfo.mobile}}</text>
				</view>
				<view class="address-detail">
					{{orderInfo.province}}{{orderInfo.city}}{{orderInfo.area}}{{orderInfo.address}}
				</view>
			</view>
		</view>

		<!-- 商品信息 -->
		<view class="goods-section">
			<view class="goods-title">商品信息</view>
			<view class="goods-item" v-for="(item, index) in orderDetails" :key="index">
				<image :src="item.productImage" mode="aspectFill" class="goods-img"></image>
				<view class="goods-info">
					<view class="goods-name">{{item.productName}}</view>
					<view class="goods-spec">{{item.skuName}}</view>
					<view class="price-num">
						<text class="price">￥{{item.unitPrice}}</text>
						<text class="num">x{{item.quantity}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 支付方式 -->
		<view class="pay-section">
			<view class="pay-title">支付方式</view>
			<view class="pay-method">
				<view class="method-item">
					<image src="/static/images/wxpay.png" mode="aspectFit" class="pay-icon"></image>
					<text>微信支付</text>
				</view>
			</view>
		</view>

		<!-- 订单金额信息 -->
		<view class="amount-section">
			<view class="amount-item">
				<text>商品总价</text>
				<text>￥{{orderInfo.paymentPrice}}</text>
			</view>
			<view class="amount-item total">
				<text>实付金额</text>
				<text class="total-price">￥{{orderInfo.paymentPrice}}</text>
			</view>
		</view>

		<!-- 底部支付按钮 -->
		<view class="footer">
			<view class="total-section">
				<text>实付金额：</text>
				<text class="total-price">￥{{orderInfo.paymentPrice}}</text>
			</view>
			<view class="pay-btn" @click="handlePay">
				立即支付
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getOrderPayDetail,
		getWxPayParams
	} from '@/config/api.js';

	export default {
		data() {
			return {
				orderId: '',
				orderInfo: null,
				orderDetails: []
			}
		},
		onLoad(options) {
			if (options.orderId) {
				this.orderId = options.orderId;
				this.getOrderDetail();
			} else {
				uni.showToast({
					title: '订单参数错误',
					icon: 'none'
				});
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			}
		},
		methods: {
			// 获取订单详情
			async getOrderDetail() {
				try {
					uni.showLoading({
						title: '加载中...'
					});
					
					const res = await getOrderPayDetail(this.orderId);
					if (res.code === 200 && res.data) {
						this.orderInfo = res.data.orderInfo;
						this.orderDetails = res.data.orderDetails;
						
						// 如果订单状态不是待付款，提示并返回
						if (this.orderInfo.status !== 1) {
							uni.showToast({
								title: '订单状态已改变',
								icon: 'none'
							});
							setTimeout(() => {
								uni.navigateBack();
							}, 1500);
						}
					} else {
						uni.showToast({
							title: res.msg || '获取订单信息失败',
							icon: 'none'
						});
					}
				} catch (e) {
					console.error('获取订单详情失败:', e);
					uni.showToast({
						title: '获取订单信息失败',
						icon: 'none'
					});
				} finally {
					uni.hideLoading();
				}
			},
			
			// 处理支付
			async handlePay() {
				if (!this.orderInfo) {
					return uni.showToast({
						title: '订单信息错误',
						icon: 'none'
					});
				}
				
				try {
					// 调用后端获取支付参数
					const payRes = await getWxPayParams(this.orderId);
					if (payRes.code !== 200 || !payRes.data) {
						throw new Error(payRes.msg || '获取支付参数失败');
					}
					
					// 调起微信支付
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: payRes.data.timestamp,
						nonceStr: payRes.data.noncestr,
						package: payRes.data.package,
						signType: payRes.data.signType,
						paySign: payRes.data.sign,
						success: (res) => {
							uni.showToast({
								title: '支付成功',
								icon: 'success'
							});
							// 支付成功后跳转到订单列表
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/order/list?status=2'
								});
							}, 1500);
						},
						fail: (err) => {
							console.error('支付失败:', err);
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							});
						}
					});
				} catch (e) {
					console.error('支付异常:', e);
					uni.showToast({
						title: e.message || '支付失败',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.payment-container {
		min-height: 100vh;
		background-color: #f8f8f8;
		padding-bottom: 120rpx;
	}
	
	.address-section {
		background-color: #fff;
		padding: 30rpx;
		margin-bottom: 20rpx;
		
		.address-content {
			.contact-info {
				margin-bottom: 10rpx;
				
				.name {
					margin-right: 20rpx;
					font-weight: bold;
				}
				
				.phone {
					color: #666;
				}
			}
			
			.address-detail {
				color: #333;
				font-size: 28rpx;
			}
		}
	}
	
	.goods-section {
		background-color: #fff;
		padding: 30rpx;
		margin-bottom: 20rpx;
		
		.goods-title {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}
		
		.goods-item {
			display: flex;
			margin-bottom: 20rpx;
			
			.goods-img {
				width: 160rpx;
				height: 160rpx;
				margin-right: 20rpx;
				border-radius: 8rpx;
			}
			
			.goods-info {
				flex: 1;
				
				.goods-name {
					font-size: 28rpx;
					margin-bottom: 10rpx;
				}
				
				.goods-spec {
					font-size: 24rpx;
					color: #666;
					margin-bottom: 10rpx;
				}
				
				.price-num {
					display: flex;
					justify-content: space-between;
					
					.price {
						color: #ff4d4f;
						font-weight: bold;
					}
					
					.num {
						color: #666;
					}
				}
			}
		}
	}
	
	.pay-section {
		background-color: #fff;
		padding: 30rpx;
		margin-bottom: 20rpx;
		
		.pay-title {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}
		
		.method-item {
			display: flex;
			align-items: center;
			
			.pay-icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}
		}
	}
	
	.amount-section {
		background-color: #fff;
		padding: 30rpx;
		
		.amount-item {
			display: flex;
			justify-content: space-between;
			margin-bottom: 20rpx;
			font-size: 28rpx;
			
			&.total {
				margin-top: 20rpx;
				padding-top: 20rpx;
				border-top: 1px solid #eee;
				
				.total-price {
					color: #ff4d4f;
					font-weight: bold;
					font-size: 32rpx;
				}
			}
		}
	}
	
	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 100rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);
		
		.total-section {
			flex: 1;
			
			.total-price {
				color: #ff4d4f;
				font-weight: bold;
				font-size: 36rpx;
			}
		}
		
		.pay-btn {
			width: 240rpx;
			height: 80rpx;
			background-color: #ff4d4f;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 40rpx;
			font-size: 32rpx;
		}
	}
</style> 