<template>
	<view>
		<view class="inner-box">
			<!-- 收货人 -->
			<view class="address-box" v-if="address==null" @click="openAddress()">
				<view class="left">
					<view class="name-box">
						<text class="text name">添加收货地址</text>
					</view>
				</view>
				<view class="arrow-box">
					<image src="/static/images/arrow_gray.png" mode=""></image>
				</view>
			</view>
			<view class="address-box" v-if="address!=null" @click="openAddress()">
				<view class="left">
					<view class="name-box">
						<text class="text name">{{address.realName}}</text>
						<text class="text" v-if="address.phone!=null">{{address.phone}}</text>
					</view>
					<view class="address">
						{{address.province}}{{address.city}}{{address.district}}{{address.detail}}
					</view>
				</view>
				<view class="arrow-box">
					<image src="/static/images/arrow_gray.png" mode=""></image>
				</view>
			</view>

			<!-- 积分 -->
			<view class="price-info">
				<view class="price-info-title">价格明细</view>
				<view class="points">
					<view class="left">
						<text class="text">商品总价</text>
					</view>
					<view class="right" style="align-items: baseline;">
						<text class="price-info-unit">¥</text>
						<text class="price-info-num">{{(orderData.payPrice||0).toFixed(2)}}</text>
					</view>
				</view>
				<view class="points" @click="openCoupon()">
					<view class="left">
						<text class="text">优惠券</text>
					</view>
					<view class="right">
						<text class="text">{{couponText}}</text>
						<image src="/static/images/arrow4.png" mode=""></image>
					</view>
				</view>
				<view class="points">
					<view class="left">
						<text class="text">运费</text>
					</view>
					<view class="right">
						<text class="text"
							v-if="address!=null">{{orderData.payDelivery==null||orderData.payDelivery==0?'免运费':orderData.payDelivery.toFixed(2)}}</text>
						<text class="text" v-if="address==null">--</text>
					</view>
				</view>
				<!-- <view class="points">
					<view class="left">
						<text class="text">服务费</text>
					</view>
					<view class="right">
						<text class="text">{{price.serviceFee.toFixed(2)}}</text>
					</view>
				</view> -->
				<view class="points">
					<view class="left">
						<text class="text">合计</text>
					</view>
					<view class="right" style="align-items: baseline;">
						<text class="price-info-unit">¥</text>
						<text class="price-info-num">{{(orderData.totalPrice||0).toFixed(2)}}</text>
					</view>
				</view>
			</view>

		</view>
		<view class="btn-foot">
			<view class="right">
				<view class="total">
					<text class="label">合计：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num">{{(orderData.totalPrice||0).toFixed(2)}}</text>
					</view>
				</view>
				<view class="btn" @click="submitOrder">提交订单</view>
			</view>
		</view>

		<popupBottom ref="popup" :visible.sync="couponVisible" title=" " bgColor="#f5f5f5" radius="30" maxHeight="60%">
			<view class="coupon" style="height:650rpx;">
				<div class="coupon-list" v-if="couponsList.length > 0">
					<div class="item acea-row row-center-wrapper" v-for="(item, index) in couponsList" :key="index">
						<div class="money">
							<image v-if="item.status==0" class="img" src="../../static/images/coupon1.png"
								mode="widthFix"></image>
							<image v-if="item.status!=0" class="img" src="../../static/images/coupon2.png"
								mode="widthFix"></image>
							<div style="z-index: 999;">
								￥<span class="num">{{ item.couponPrice }}</span>
							</div>
							<div class="pic-num">满{{ item.useMinPrice }}元可用</div>
						</div>
						<div class="text">
							<div class="condition line1">
								{{ item.couponTitle }}
							</div>
							<div class="data acea-row row-between-wrapper">
								<div>{{ item.limitTime }}到期</div>
								<div class="bnt bg-color-red" @click="couponSelect(item)">选择</div>
							</div>
						</div>
					</div>
				</div>
				<view v-if="couponsList.length == 0" class="no-data-box">
					<image src="/static/images/no_data.png" mode="aspectFit"></image>
					<view class="empty-title">暂无数据</view>
				</view>
			</view>

		</popupBottom>
	</view>
</template>

<script>
	import {
		createliveOrder, // 创建订单
		userAddr, // 获取用户收货地址
		cartOrder, //购物车订单
		computed
		// checked //获取购物车选中商品
	} from "@/api/order.js"
	import popupBottom from '@/components/px-popup-bottom/px-popup-bottom.vue'
	export default {
		components: {
			popupBottom
		},
		data() {
			return {
				orderData: {},
				cityId: null,
				// priceSum: null,
				cartsSelect: [],
				address: null,
				addressId: null,
				totalNum: null,
				orderKey: null,
				// price: null,
				liveId: null,
				orderList: [],
				userInfo: null,
				temps: [],
				couponUserId: null,
				couponText: "请选择",
				couponsList: [],
				couponVisible: false,
				carts: [],
				checked: false,
				// type: null,
				cartIds: null,
				confirmParam: [],

			}
		},

		onLoad(options) {
			// uni.$on('updateAddress', this.handleAddressUpdate);
			console.log("确认订单", options)
			this.orderKey = options.orderKey;
			this.liveId = options.liveId
			this.productId = options.productId
			this.totalNum = Number(options.totalNum) || 0
		},

		mounted() {
			this.getUserAddr()

			uni.$on('updateAddress', (e) => {
				this.address = e;
				this.addressId = e.addressId;
			});
		},
		onUnload() {
			// 页面卸载时移除监听，避免内存泄漏
			uni.$off('updateAddress', this.handleAddressUpdate);
		},
		beforeDestroy() {
			// 移除事件监听，避免内存泄漏
			uni.$off('updateAddress');
		},
		methods: {
			handleAddressUpdate(item) {
				console.log('接收到地址数据:', item);
				// 在这里处理地址数据
				this.cityId = item.cityId;
				this.address = item;
				this.addressId = item.addressId;
				this.computedOrder()
				// 更新页面显示或其他操作
			},
			// 获取用户收货地址
			getUserAddr() {
				userAddr().then(res => {
						if (res.code == 200) {
							console.log("用户收货地址>>>>", res.data)
							this.address = res.data;
							if (res.data && res.data.cityId) {
								this.cityId = res.data.cityId;
							}
							this.computedOrder();
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);
			},

			// 查询创建订单信息
			computedOrder() {
				let data = {
					cityId: this.cityId || '',
					totalNum: this.totalNum,
					productId: this.productId,
					orderKey: this.orderKey,
				}
				computed(data).then(res => {
						if (res.code == 200) {
							console.log("查询创建订单信息>>>>", res.data)
							this.orderData = res.data
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);
			},


			// 创建订单
			createLiveOrder() {

				let data = {
					cityId: this.cityId,
					liveId: this.liveId,
					orderKey: this.orderKey,
					userName: this.address.realName,
					userPhone: this.address.phone,
					userAddress: this.address.province + ' ' + this.address.city + ' ' + this.address.district + ' ' +
						this.address
						.detail,
					cartId: "5",
					productId: this.productId,
					totalNum: this.totalNum,

				}

				return createliveOrder(data).then(res => { // 添加 return
					if (res.code == 200) {
						this.orderList = res.order;
						return res.order; // 返回订单数据供后续使用
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						throw new Error(res.msg); // 抛出错误以便 submitOrder 捕获
					}
				});
			},
			getWeixinOrderTemps: function() {
				getWeixinOrderTemps().then(
					res => {
						if (res.code == 200) {
							this.temps = res.temp
						} else {

						}
					},
					rej => {}
				);
			},
			couponSelect(item) {
				this.couponText = "-¥" + (item.couponPrice || 0).toFixed(2);
				this.couponUserId = item.id;
				this.couponVisible = false;
				this.computed();
			},
			openCoupon() {
				let that = this;
				var data = {
					couponType: 2,
					useMinPrice: this.orderData.payPrice
				};
				getMyEnableCouponList(data).then(res => {
					this.couponVisible = true;
					that.couponsList = res.data
				})
			},
			integralChange(e) {
				this.form.useIntegral = e ? 1 : 0
				this.computed()
			},
			openAddress() {
				uni.navigateTo({
					url: '/pages_user/user/address'
				})
			},
			// 提交订单
			async submitOrder() {
				try {
					if (this.orderKey == null) {
						uni.showToast({
							icon: 'none',
							title: '订单KEY不存在',
						});
						return;
					}
					if (this.address == null) {
						uni.showToast({
							icon: 'none',
							title: '收货地址不能为空',
						});
						return;
					}
					let orderList;
					orderList = await this.createLiveOrder(); // 等待订单数据返回
					console.log("orderList>>", orderList)
					const orderListStr = encodeURIComponent(JSON.stringify(orderList));
					uni.navigateTo({
						url: `/pages_shopping/live/paymentOrder?orderList=${orderListStr}`
					});
				} catch (error) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
				}
			},

		}
	}
</script>

<style lang="scss">
	.inner-box {
		padding: 20upx 20upx 140upx;

		.address-box {
			margin-bottom: 20rpx;
			box-sizing: border-box;
			min-height: 171upx;
			background: #FFFFFF;
			border-radius: 16upx;
			background-repeat: no-repeat;
			background-size: 100% 30upx;
			background-position: left bottom;
			padding: 38upx 30upx 36upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				width: 92%;

				.name-box {
					display: flex;
					align-items: center;

					.text {
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #111111;
						line-height: 1;

						&.name {
							margin-right: 30upx;
						}
					}
				}

				.address {
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
					line-height: 42upx;
					text-align: left;
					margin-top: 23upx;
				}
			}

			.arrow-box {
				width: 12upx;
				height: 23upx;
				display: flex;
				align-items: cenetr;
				justify-content: cenetr;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.shopbox {
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-bottom: 20rpx;

			.points {
				padding: 0 !important;
			}

			.remarks {
				padding: 0 !important;
			}
		}

		.shopbox-name {
			padding: 30rpx 30rpx 0 30rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 30rpx;
			color: #111;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
		}

		.goods-list {
			// margin-top: 20upx;
			padding: 0 30upx;
			background-color: #FFFFFF;
			border-radius: 16upx;

			.item {
				padding: 30upx 0;
				border-bottom: 1px solid #EDEEEF;
				display: flex;
				align-items: center;

				.img-box {
					width: 160upx;
					height: 160upx;
					margin-right: 30upx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.info-box {
					width: calc(100% - 190upx);
					height: 160upx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.name-box {
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #111111;
						line-height: 40upx;

						.tag {
							display: inline-block;
							padding: 0 6upx;
							height: 30upx;
							background: linear-gradient(90deg, #66b2ef 0%, #0bb3f2 100%);
							border-radius: 4upx;
							margin-right: 10upx;
							font-size: 22upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FFFFFF;
							line-height: 30upx;
							float: left;
							margin-top: 7upx;
						}
					}

					.spec {
						margin-top: 10upx;
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 1;
					}

					.price-num {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.price {
							display: flex;
							align-items: flex-end;

							.unit {
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
								line-height: 1.2;
								margin-right: 4upx;
							}

							.num {
								font-size: 32upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
								line-height: 1;
							}
						}

						.num {
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							line-height: 1;
						}
					}
				}
			}

			.sub-total {
				height: 88upx;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.label {
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
				}

				.price {
					display: flex;
					align-items: flex-end;

					.unit {
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #FF6633;
						line-height: 1.2;
						margin-right: 4upx;
					}

					.num {
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1;
					}
				}
			}
		}

		.price-info {
			background: #FFFFFF;
			border-radius: 16upx;

			&-title {
				padding: 30rpx 30rpx 20rpx 30rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #111;
			}

			&-unit {
				font-size: 24rpx;
			}

			&-num {
				font-size: 28rpx;
			}
		}

		.points {
			height: 88upx;
			padding: 0 30upx;
			background: #FFFFFF;
			border-radius: 16upx;

			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				image {
					width: 28upx;
					height: 28upx;
					margin-right: 20upx;
				}

				.text {
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}
			}

			.right {
				display: flex;
				align-items: center;

				.text {
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;

				}

				image {
					margin-left: 15upx;
					width: 14upx;
					height: 24upx;
				}
			}
		}

		.remarks {
			// height: 88upx;
			padding: 0 30upx;
			background: #FFFFFF;
			border-radius: 16upx;
			margin-top: 20upx;
			display: flex;
			align-items: center;

			input {
				width: 100%;
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
			}

			.input {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}
		}
	}


	.btn-foot {
		box-sizing: border-box;
		width: 100%;
		height: 121upx;
		background: #FFFFFF;
		padding: 16upx 30upx 16upx 60upx;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;

		.right {
			display: flex;
			align-items: center;

			.total {
				display: flex;
				align-items: flex-end;
				margin-right: 36upx;

				.label {
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					line-height: 1.5;
				}

				.price {
					display: flex;
					align-items: flex-end;

					.unit {
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1.2;
						margin-right: 10upx;
					}

					.num {
						font-size: 50upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1;
					}
				}
			}

			.btn {
				width: 200upx;
				height: 88upx;
				line-height: 88upx;
				text-align: center;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				background: #0bb3f2;
				border-radius: 44upx;
			}
		}
	}
</style>