<template>
	<view class="container">
		<custom-navbar title="中奖详情" :show-back="true" title-color="#000" background-color="#FFFFFF"></custom-navbar>
		<view class="activity-page">
			<!-- 活动信息模块 -->
			<view class="section">
				<view class="section-title">活动信息</view>
				<view class="info-row mb-30">
					<view class="label">活动名称</view>
					<view class="value">{{ recordDetails.activity_name }}</view>

				</view>
				<view class="info-row mb-30">
					<view class="label">活动编号</view>
					<view class="value">{{ recordDetails.activity_id }}</view>
				</view>
				<view class="info-row">
					<view class="label">中奖编号</view>
					<view class="value">{{ recordDetails.lottery_num }}</view>
				</view>
			</view>

			<!-- 中奖信息模块 -->
			<view class="section">
				<view class="section-title">
					中奖信息
				</view>
				<view class="prize-card">
					<image class="prize-img" :src="recordDetails.prize_img" mode="aspectFill"></image>
					<view class="prize-desc">
						<view class="prize-name flex-row-center">
							{{ recordDetails.prize_name }}
							<view class="prize-name-price flex-row-center" v-if="recordDetails.type==3">
								<view class="prize-name-price-text">价值¥ {{ formatPriceNumber(recordDetails.price) }}</view>
								
								<image class="prize-name-price-img" src="/static/images/background/lotteryBj14.png" mode="aspectFill"></image>
							</view>
						</view>
						<view class="prize-time">中奖时间: {{ recordDetails.lottery_time }}</view>
						<view class="prize-way flex-row-center-center">兑奖方式: {{ recordDetails.type==2?"线上发放":"邮寄"}}
						</view>
					</view>
				</view>
				<view class="bottom-bar">
					<view class="expire-time">{{ recordDetails.claim_deadline }}前兑换</view>
					<view class="address-btn flex-row-center-center">
						<text>{{ getType }}</text>
					</view>
				</view>
			</view>

			<!-- 收货地址模块 -->
			<view class="section" v-if="recordDetails.type==3">
				<view class="section-title">收货地址信息</view>
				<view class="address-row">
					<view class="address-label">收货地址</view>
					<view class="address-tip flex-row-center" @click="goToAddress">
						<text
							:class="{addressText:recordDetails.claim_address || address.server_address}">{{ recordDetails.claim_address || address.server_address || "请填写收货地址"}}</text>
						<image class="address-tip-icon" src="/static/images/icons/right_gray5.png"></image>
					</view>
				</view>
			</view>
			<view class="section" v-if="recordDetails.type==3 && recordDetails.send_time ">
				<view class="section-title">发货物流信息</view>
				<view class="section-text1 mb-30">发放时间：{{ recordDetails.send_time }}</view>
				<view class="flex-row-center-center mb-30" @click="imgPreview(recordDetails.send_img)">
					<image class="section-img" :src="recordDetails.send_img" mode="aspectFill"></image>
				</view>

				<view class="section-text2">备注：<text style="color: #E8101E;">{{ recordDetails.send_remark }}</text>
				</view>
			</view>


			<!-- 金额 -->
			<view class="section" v-if="recordDetails.type==2">
				<view class="section-title">领取金额</view>
				<view class="price-row">
					<view class="price-label">红包金额</view>
					<view class="price-tip" @click="goToAddress">
						<text>{{ recordDetails.price}}元</text>
					</view>
				</view>
			</view>
		</view>

		<view class="container-footer">
			<view class="container-footer-btn flex-row-center-center" :class="{nobtn:!address.server_address}"
				v-if="recordDetails.type==3 && recordDetails.issue==1 && !recordDetails.claim_address"
				@click="claimPhysical">确认地址</view>
			<view class="container-footer-btn flex-row-center-center" v-if="recordDetails.type==2" @click="goWxPage">
				{{ recordDetails.issue==1?"领取":"返回礼品页" }}
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	import {
		requestMerchantTransfer
	} from "@/uni_modules/uni-requestMerchantTransfer";
	// #endif
	import request from "@/utils/request";
	export default {
		name: 'ActivityPage',
		data() {
			return {
				isSubmit: false,
				address: {},
				id: null,
				recordDetails: {},
				openid: null,
			}
		},
		async onLoad(options) {
			await this.getyhopenid()
			if (options.id) {
				this.id = options.id
				this.getRecordDetails()
			}
		},
		computed: {
			getType() {
				let list = [{
						id: 0,
						text: "未中奖",
					},
					{
						id: 1,
						text: "未发放",
					},
					{
						id: 2,
						text: "已发放",
					},
					{
						id: 3,
						text: "已失效",
					},
				];
				let name = list.find((it) => it.id == this.recordDetails.issue)?.text;
				return name;
			}
		},
		onShow() {
			console.log("show")
			if (this.id) {
				this.getRecordDetails()
			}
		},
		methods: {
			async getyhopenid() {
				await request.post("/withdrawal/getyhopenid").then((res) => {
					if (res.code == 200) {
						if (res.data) {
							this.openid = res.data
						}
					}
				});
			},
			getOpenId() {
				console.log("调用登录接口获取openid");
				uni.login({
					provider: "weixin",
					onlyAuthorize: true,
					success: (loginRes) => {
						console.log("loginRes:", loginRes);
						const code = loginRes.code;
						if (code) {

							request
								.post("/user/user/getOpenidApp", {
									code
								})
								.then((res) => {
									if (res.code == 200) {
										console.log("获取openid成功：", res);

										if (res.data.openid) {
											this.openid = res.data.openid
											this.goWxPage();
										}
									}
								})
								.catch((err) => {
									console.error("获取openid失败：", err);
								});
						}
					},
					fail: function(err) {
						console.error("登录接口调用失败：", err);
					},
				});
			},
			goWxPage() {
				if (this.isSubmit) {
					return
				}
				if (this.recordDetails.issue != 1) {
					const pages = getCurrentPages()
					const prevPage = pages[pages.length - 2]
					if (prevPage) {
						uni.navigateBack()
					}
					return
				}
				const openid = this.openid;
				if (!openid) {
					this.getOpenId();
					return;
				}
				const id = this.id;
				console.log(
					"开始发起企业付款，openid:",
					openid,
					" id:",
					id
				);
				this.isSubmit = true
				request
					.post("/user/lottery/claimRedPacket", {
						id,
						openid: openid
					})
					.then((wxPayRes) => {
						console.log("wxpay res:", wxPayRes);
						if (wxPayRes.code != 200) {
							uni.showToast({
								title: "领取失败，请稍后重试",
								icon: "none",
							});
							this.isSubmit = false
							return;
						}
						requestMerchantTransfer({
							mchId: wxPayRes.data.mch_id,
							appId: wxPayRes.data.appid,
							package: wxPayRes.data.package_info,
							success: (res) => {
								console.log("requestMerchantTransfer ", res);
								this.isSubmit = false
							},
							fail: (res) => {
								console.log(res.errMsg, '失败');
								this.isSubmit = false
							}
						});
					})
					.catch((err) => {
						this.isSubmit = false
						console.error("wxpay err:", err);
					});
			},
			checkSystemType() {
				try {
					const systemInfo = uni.getSystemInfoSync();
					const {
						platform,
						system
					} = systemInfo;
					let systemType = "";

					// 优先通过 system 字段判断鸿蒙（兼容模式下 platform 仍为 android，但 system 含 HarmonyOS）
					if (system.includes("HarmonyOS")) {
						systemType = "harmony"; // 鸿蒙系统（兼容/原生）
						// 进一步区分鸿蒙原生 vs 安卓兼容模式
						if (platform === "harmony") {
							systemType = "harmony_original"; // 纯鸿蒙原生模式
						} else if (platform === "android") {
							systemType = "harmony_android"; // 鸿蒙安卓兼容模式
						}
					} else if (platform === "android") {
						systemType = "android"; // 纯安卓系统（非鸿蒙）
					} else if (platform === "ios") {
						systemType = "ios"; // iOS 系统
					} else {
						systemType = "other"; // 小程序/PC/其他系统
					}

					//   const { isHarmony, systemType } = this.checkSystemType();
					//   if (isHarmony) {
					//     console.log("当前是鸿蒙系统");
					//     if (systemType === "harmony_original") {
					//       console.log("纯鸿蒙原生模式");
					//     } else if (systemType === "harmony_android") {
					//       console.log("鸿蒙安卓兼容模式");
					//     }
					//   } else if (systemType === "android") {
					//     console.log("当前是纯安卓系统");
					//   } else if (systemType === "ios") {
					//     console.log("当前是iOS系统");
					//   }

					return {
						systemType, // 系统类型：harmony/harmony_original/harmony_android/android/ios/other
						systemInfo, // 完整系统信息（含版本、设备型号等）
						isHarmony: system.includes("HarmonyOS"), // 是否为鸿蒙系统（布尔值）
					};
				} catch (err) {
					console.error("检测系统类型失败", err);
					return {
						systemType: "unknown",
						isHarmony: false
					};
				}
			},
			imgPreview(url) {
				uni.previewImage({
					urls: [url]
				})
			},
			getRecordDetails() {
				request.post("/user/lottery/recordDetails", {
					id: this.id
				}).then((res) => {
					if (res.code == 200) {
						this.recordDetails = res.data
					}
				});
			},
			// 选择地址
			goToAddress() {
				if (this.recordDetails.claim_address) {
					return
				}
				uni.navigateTo({
					url: "/pages/address/list"
				});
			},
			addReservation(item) {
				this.address = item
				console.log(1111, this.address)
			},
			claimPhysical() {
				if (!this.address.server_address) {
					uni.showToast({
						title: "请选择地址",
						icon: "none",
					});
					return
				}
				request.post("/user/lottery/claimPhysical", {
					id: this.id,
					claim_name: this.address.name,
					claim_phone: this.address.call_phone,
					claim_address: this.address.server_address
				}).then((res) => {
					if (res.code == 200) {
						const pages = getCurrentPages()
						const prevPage = pages[pages.length - 2]
						if (prevPage) {
							if (prevPage.$vm && typeof prevPage.$vm.saveSuccess === "function") {
								prevPage.$vm.saveSuccess(); // 存在则调用
							}
							uni.navigateBack()
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
					}
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: "none",
					});
				});
			}
		}
	}
</script>

<style lang="less">
	/* 页面整体样式 */
	.activity-page {
		padding: 20rpx;
		padding-bottom: 170rpx;
	}

	/* 模块通用样式 */
	.section {
		padding: 30rpx 20rpx;
		background: #FFFFFF;
		margin-bottom: 20rpx;
		border-radius: 20rpx;

		.section-title {
			font-weight: 500;
			font-size: 32rpx;
			color: #3D3D3D;
			line-height: 39rpx;
			text-align: left;
			font-style: normal;
			margin-bottom: 40rpx;


		}

		.section-text1 {
			font-weight: 400;
			font-size: 26rpx;
			color: #333333;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
		}

		.section-text2 {
			font-weight: 400;
			font-size: 26rpx;
			color: #333333;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
		}

		.section-img {
			width: 400rpx;
			height: 400rpx;
		}
	}

	/* 信息行通用样式 */
	.info-row {
		display: flex;
		justify-content: space-between;
		font-size: 28rpx;
		color: #666;

		.label {
			font-weight: 400;
			font-size: 26rpx;
			color: #999999;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
		}

		.value {
			font-weight: 400;
			font-size: 26rpx;
			color: #333333;
			line-height: 34rpx;
			text-align: right;
			font-style: normal;
		}


	}

	/* 中奖信息卡片 */
	.prize-card {
		display: flex;
		margin-bottom: 16rpx;

		.prize-img {
			width: 150rpx;
			height: 150rpx;
			border-radius: 10rpx;
			margin-right: 26rpx;
		}

		.prize-desc {
			flex: 1;

			.prize-name {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
				margin-bottom: 26rpx;

				.prize-name-price {
					
					margin-left: 6rpx;
					height: 36rpx;
					.prize-name-price-text{
						box-sizing: border-box;
						padding: 0 0rpx 0 10rpx;
						font-weight: 500;
						font-size: 20rpx;
						color: #BC7105;
						line-height: 36rpx;
						text-align: left;
						font-style: normal;
						white-space: nowrap;
						position: relative;
						overflow: auto;
						border: 1px solid rgba(250, 193, 107, 0.36);
						border-right: none;
						border-radius: 6rpx 0rpx 0  6rpx;
						background: linear-gradient(
						    to bottom right, 
						    rgba(254, 228, 182, 0.36), 
						    rgba(255, 242, 215, 0.36), 
						    rgba(254, 228, 182, 0.36), 
						    rgba(255, 242, 215, 0.36)
						  );
					}
					.prize-name-price-img{
						height: 36rpx;
						width: 14rpx;
						// position: absolute;
						// right: 0;
						// top: 0;
					}
					// background-image: url('/static/images/background/lotteryBj14.png');
					//  background-size: cover;
					//     /* 关键修改2：同时设置x和y轴的位置，确保图片垂直居中且右对齐 */
					//     background-position: right center;
					//     background-repeat: no-repeat;
				}
			}

			.prize-time {
				font-weight: 400;
				font-size: 24rpx;
				color: #666666;
				line-height: 33rpx;
				text-align: left;
				font-style: normal;
				margin-bottom: 14rpx;
			}

			.prize-way {
				height: 37rpx;
				padding: 0 8rpx;
				border-radius: 10rpx;
				border: 1rpx solid rgba(210, 87, 85, 0.5);
				font-weight: 400;
				font-size: 20rpx;
				color: #DB2B28;
				text-align: left;
				font-style: normal;
				width: fit-content;
			}
		}
	}

	/* 有效期+状态行 */
	// 底部栏（截止时间 + 按钮）
	.bottom-bar {
		padding-top: 22rpx;
		border-top: 1rpx solid #F1F1F1;
		margin-top: 22rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		// 截止时间
		.expire-time {
			font-weight: 400;
			font-size: 24rpx;
			color: #666666;
			line-height: 33rpx;
			text-align: left;
			font-style: normal;
		}

		// 填写地址按钮
		.address-btn {
			height: 47rpx;
			padding: 0 20rpx;
			background: #F43642;
			border-radius: 24rpx;
			font-weight: 400;
			font-size: 24rpx;
			color: #FFFFFF;
			text-align: left;
			font-style: normal;

			.address-btn-img {
				width: 25rpx;
				height: 25rpx;
				margin-right: 8rpx;
			}
		}
	}

	/* 收货地址行 */
	.address-row {
		display: flex;
		justify-content: space-between;

		.address-label {
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
		}

		.address-tip {
			font-weight: 400;
			font-size: 26rpx;
			color: #CCCCCC;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
			max-width: 500rpx;

			.addressText {
				color: #333;
			}

			.address-tip-icon {
				width: 11rpx;
				height: 20rpx;
				margin-left: 10rpx;
			}
		}
	}


	.price-row {
		display: flex;
		justify-content: space-between;

		.price-label {
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
		}

		.price-tip {
			font-weight: 400;
			font-size: 26rpx;
			color: #E8101E;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
		}
	}

	.container-footer {
		padding: 40rpx;
		background: #FFFFFF;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;

		.container-footer-btn {
			width: 670rpx;
			height: 86rpx;
			border-radius: 100rpx;
			background: #E8101E;
			font-weight: 400;
			font-size: 34rpx;
			color: #FFFFFF;
			text-align: center;
			font-style: normal;
		}

		.nobtn {
			background: rgba(232, 16, 30, 0.4);
		}
	}
</style>