<template>
	<view class="container">
		<!-- 内容区域 -->
		<view class="content">
			<!-- 支付结果状态 -->
			<view class="result-section">
				<!-- 状态图标 -->
				<view class="result-icon" :class="resultIconClass">
					<text class="icon-text">{{ resultIcon }}</text>
				</view>

				<!-- 状态文字 -->
				<text class="result-text">{{ resultText }}</text>

				<!-- 成功状态显示积分信息 -->
				<view v-if="isSuccess" class="points-info">
					<text class="points-value">{{ pointsValue }}</text>
					<!-- <text class="points-unit">{{ resultData.creditsTypeName || '积分' }}</text> -->
				</view>

				<!-- 失败状态显示失败原因 -->
				<view v-if="!isSuccess" class="failure-info">
					<text class="failure-reason">{{ failureReason }}</text>
				</view>

				<!-- 支付方式信息 - 仅成功状态显示 -->
				<view v-if="isSuccess" class="payment-info">
					<view class="payment-row">
						<text class="payment-label">支付方式</text>
						<text class="payment-value">积分兑换</text>
					</view>
				</view>
			</view>

			<!-- 底部操作按钮 -->
			<view class="action-section">
				<button class="action-btn" @click="handleAction">{{ actionButtonText }}</button>
			</view>

			<!-- 活动banner -->
			<view class="vip-active-area" v-if="pageCfgStore?.contentCfg?.activeInfo?.length > 0">
				<swiper class="vip-active-swiper" :indicator-dots="pageCfgStore?.contentCfg?.activeInfo?.length > 1"
					:autoplay="true" :circular="true" indicator-color="#dfddd9" indicator-active-color="#b27c1e"
					:indicator-top="32">
					<swiper-item v-for="(item, index) in pageCfgStore?.contentCfg?.activeInfo" :key="index">
						<image class="vip-active-img"
							:src="item?.bgUrl?.includes('http') ? item?.bgUrl : $baseUrl + `${item?.bgUrl}`"
							mode="aspectFit" @click="handleVipActiveClick(index, item)" />
					</swiper-item>
				</swiper>
			</view>
		</view>

		<!-- 客服按钮 -->
		<view class="service-btn" @click="handleServiceClick">
			<image class="service-icon" :src="$baseUrl + 'homepage/Q3Res/settlementCenter_serviceBtn.png'"
				mode="aspectFit"></image>
		</view>
	</view>
</template>

<script>
import { useHomeStore } from '@/stores/home';
import { usePageCfgStore } from '@/stores/pageCfg';
import { jump, JumpType } from '../../utils';

export default {
	data() {
		return {
			// 支付结果数据 - 可根据实际需要修改
			resultData: {
				isSuccess: true, // true: 成功, false: 失败
				pointsValue: '',
				failureReason: '',
				orderId: '', // 订单ID，用于跳转订单详情
				// 新增字段
				goodsName: '',
				goodsImage: '',
				goodsSpec: '',
				quantity: 1,
				creditsTypeName: '积分',
				priceData: '',
				gid: '',
				skuld: '',
				buy_num: 1
			},
			// 商店信息
			homeStore: null,
			// 页面配置信息
			pageCfgStore: null
		}
	},
	computed: {
		// 是否成功
		isSuccess() {
			return this.resultData.isSuccess;
		},

		// 结果图标
		resultIcon() {
			return this.isSuccess ? '✓' : '✕';
		},

		// 结果图标样式类
		resultIconClass() {
			return this.isSuccess ? 'icon-success' : 'icon-failure';
		},

		// 结果文字
		resultText() {
			return this.isSuccess ? '兑换成功' : '兑换失败';
		},

		// 积分值
		pointsValue() {
			return this.resultData.pointsValue;
		},

		// 失败原因
		failureReason() {
			return this.resultData.failureReason;
		},

		// 操作按钮文字
		actionButtonText() {
			return this.isSuccess ? '查看订单' : '返回首页';
		}
	},
	onLoad(options) {
		console.log('支付结果页面接收到的参数:', options);

		// 初始化stores
		this.homeStore = useHomeStore();
		this.pageCfgStore = usePageCfgStore();
		// 尝试获取页面配置数据
		if (this.pageCfgStore && typeof this.pageCfgStore.fetchContentCfg === 'function') {
			this.pageCfgStore.fetchContentCfg();
		}

		// 处理页面参数
		if (options.isSuccess !== undefined) {
			this.resultData.isSuccess = options.isSuccess === 'true';
		}
		if (options.pointsValue) {
			this.resultData.pointsValue = decodeURIComponent(options.pointsValue);
		}
		if (options.failureReason) {
			this.resultData.failureReason = decodeURIComponent(options.failureReason);
		}
		// 处理订单ID，添加详细日志
		if (options.orderId || options.orderNo) {
			this.resultData.orderId = options.orderId || options.orderNo;
			console.log('接收到的订单ID:', this.resultData.orderId);
		} else {
			console.warn('未接收到订单ID，options:', options);
		}

		// 保存其他参数供后续使用
		this.resultData.goodsName = options.goodsName || '';
		this.resultData.goodsImage = options.goodsImage || '';
		this.resultData.goodsSpec = options.goodsSpec || '';
		this.resultData.quantity = parseInt(options.quantity) || 1;
		this.resultData.creditsTypeName = decodeURIComponent(options.creditsTypeName || '积分');
		this.resultData.priceData = options.priceData || '';
		this.resultData.gid = options.gid || '';
		this.resultData.skuld = options.skuld || '';
		this.resultData.buy_num = parseInt(options.buy_num) || 1;

		console.log('支付结果数据:', this.resultData);
	},
	methods: {
		// 处理操作按钮点击
		handleAction() {
			if (this.isSuccess) {
				// 查看订单 - 跳转到订单详情页面
				if (this.resultData.orderId) {
					console.log('跳转到订单详情页，订单ID:', this.resultData.orderId);
					// 确保订单ID被正确编码
					const encodedOrderId = encodeURIComponent(this.resultData.orderId);
					uni.navigateTo({
						url: `/v3/orderDetail/orderDetail?orderId=${encodedOrderId}`
					});
				} else {
					console.log('没有订单ID，跳转到订单列表页');
					// 如果没有订单ID，跳转到订单列表页面
					uni.navigateTo({
						url: '/v3/orderList/orderList'
					});
				}
			} else {
				// 返回首页
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}
		},

		// 设置支付结果 - 供外部调用
		setPaymentResult(isSuccess, pointsValue = '', failureReason = '', orderId = '') {
			this.resultData.isSuccess = isSuccess;
			this.resultData.pointsValue = pointsValue;
			this.resultData.failureReason = failureReason;
			this.resultData.orderId = orderId;
		},

		// 处理客服按钮点击
		handleServiceClick() {
			console.log('客服点击');
			const homeStore = useHomeStore();
			const {
				memberId,
				mobile,
				openId,
				unionId
			} = homeStore.homeInfo;
			const customerUrl =
				`https://intelcc-user.icsoc.net/?channelKey=45839e0505554f8c8aea3c7b6259b049&init=1&crmld=${memberId}&mobile=${mobile}&openId=${openId}&unionId=${unionId}`;
			jump({
				type: JumpType.H5,
				url: customerUrl
			});
		},

		// 处理活动banner点击
		handleVipActiveClick(index, item) {
			console.log('活动banner点击:', item);
			
			// 只保留跳转功能，从item.link获取跳转参数
			const url = item?.link?.url || item?.linkUrl || '';
			const type = item?.link?.type || (item?.linkType === 'h5' ? JumpType.H5 : JumpType.PAGE);
			const extra = item?.link?.extra;

			jump({
				type: type,
				url: url,
				extra: extra
			})
		}
	}
}
</script>

<style scoped>
.container {
	width: 100%;
	min-height: 100vh;
	background-color: #f5f5f5;
	box-sizing: border-box;
}


/* 内容区域 */
.content {
	padding: 0;
	box-sizing: border-box;
}

/* 支付结果状态 */
.result-section {
	background-color: #fff;
	padding: 80rpx 30rpx 40rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	text-align: center;
}

.result-icon {
	width: 120rpx;
	height: 120rpx;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-bottom: 30rpx;
}

.result-icon.icon-success {
	background-color: #d3a458;
}

.result-icon.icon-failure {
	background-color: #d3a458;
}

.icon-text {
	font-size: 60rpx;
	color: #fff;
	font-weight: bold;
}

.result-text {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 20rpx;
}

.points-info {
	display: flex;
	align-items: baseline;
	margin-bottom: 30rpx;
}

.points-value {
	font-size: 48rpx;
	font-weight: bold;
	color: #d3a458;
}

.points-unit {
	font-size: 32rpx;
	color: #d3a458;
	margin-left: 10rpx;
}

.failure-info {
	margin-bottom: 30rpx;
}

.failure-reason {
	font-size: 28rpx;
	color: #999;
}

.payment-info {
	width: 100%;
}

.payment-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 0;
}

.payment-label {
	font-size: 28rpx;
	color: #999;
}

.payment-value {
	font-size: 28rpx;
	color: #999;
}

/* 操作按钮区域 */
.action-section {
	padding: 40rpx 30rpx;
	background-color: #fff;
}

.action-btn {
	width: 100%;
	height: 88rpx;
	border-radius: 44rpx;
	font-size: 32rpx;
	font-weight: bold;
	background-color: #fff;
	color: #333;
	border: 1rpx solid #ddd;
}

/* 活动banner区域 */
.vip-active-area {
	padding: 20rpx 30rpx;
	background-color: #fff;
}

.vip-active-swiper {
	height: 220rpx;
	width: 100%;
}

.vip-active-img {
	height: 220rpx;
	width: 100%;
	border-radius: 15rpx;
}

/* 客服按钮 */
.service-btn {
	position: fixed;
	bottom: 200rpx;
	right: 30rpx;
	width: 120rpx;
	height: 120rpx;
	background-color: #d3a458;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 100;
}

.service-icon {
	width: 100%;
	height: 100%;
}
</style>
