<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="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/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.address}} 
					</view>
				</view>
				<view class="arrow-box">
					<image src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/images/arrow_gray.png" mode=""></image>
				</view>
			</view>
			<!-- 药品列表 -->
			<view class="goods-list">
				<view v-for="(item,index) in items" :key="index" class="item">
					<view class="img-box">
						<image :src="JSON.parse(item.jsonInfo).image==''?'https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/images/drug.svg':JSON.parse(item.jsonInfo).image" mode="aspectFill"></image>
					</view>
					<view class="info-box">
						<view>
							<view class="name-box ellipsis2">
								{{JSON.parse(item.jsonInfo).productName}}
							</view>
							<view class="spec ellipsis2">{{JSON.parse(item.jsonInfo).sku}}</view>
						</view>
						<view class="price-num">
							<view class="price">
								<!-- <text class="unit">¥</text> -->
								<!-- <text class="num">{{JSON.parse(item.jsonInfo).price.toFixed(2)}}</text> -->
							</view>
							<view class="num" v-if="order.orderType==1">x{{item.num}}</view>
						</view>
					</view>
				</view>
				<!-- 小计 -->
				<view class="sub-total">
					<text class="label">合计：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num" v-if="order!=null">{{order.totalPrice.toFixed(2)}}</text>
					</view>
				</view>
				 
			</view>
			<view class="other-info">
				<view class="title">支付明细</view>
				<view class="item">
					<view class="left">
						<text class="label">订单金额：</text>
					</view>
					<view class="right">
						<text class="text" v-if="order!=null">{{order.payPrice.toFixed(2)}}</text>
					</view>
				</view>
				<view class="item" v-if="order!=null&&(order.userCouponId==null||order.userCouponId==0)" @click.stop="openCoupon()">
					<view class="left">
						<text class="label">优惠券：</text>
					</view>
					<view class="right">
						<text class="text">{{couponText}}</text>
						<image v-if="userCouponId!=0" @click.stop="delCoupon()" class="ic-close" src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/images/close.png"></image>
						<image  class="ic-back" src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/images/arrow_gray.png"></image>
					</view>
				</view>
				<view class="item" >
					<view class="left">
						<text class="label">优惠金额：</text>
					</view>
					<view class="right">
						<text class="text">{{discountMoney.toFixed(2)}}</text>
					</view>
				</view>
				<view class="item">
					<view class="left">
						<text class="label">支付金额：</text>
					</view>
					<view class="right">
						<text class="text">{{payMoney.toFixed(2)}}</text>
					</view>
				</view>
			</view>
			<view class="pay-type">
				<view class="title">支付方式</view>
				<view class="item">
					<view class="left">
						<image src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/images/wecha_pay.png" mode=""></image>
						<text class="text">全款支付</text>
					</view>
					<label>
						<radio   value="" :checked="wxPay" color="#078C9D"></radio>
					</label>
				</view>
			</view>
			<!-- 备注 -->
			<view class="remarks" v-if="order!=null">
				<input type="text" v-model="order.remark" placeholder="备注留言（选填）" placeholder-class="input" />
			</view>
		</view>
		<u-popup :closeable="true" :show="couponShow" :round="10" mode="bottom" @close="close" @open="open">
			<view class="coupon">
				<view class="coupon-box" v-if="coupons.length>0" >
				   <view @click="couponSelect(item)" class="coupon-item" v-for="(item) in coupons">
				 	<view class="left" >
				 	  <image v-if="item.status==0" src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/imagesNew/0fdd75d89db84458886d38e615011048.png" mode="widthFix"></image>
				 	  <image v-if="item.status!=0" src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/imagesNew/d950e4c7af1e4464be1a7777e9dca576.png" mode="widthFix"></image>
				 	  <view style="z-index: 999;">
				 		￥<span class="num">{{item.price.toFixed(2)}}</span>
				 	  </view>
				 	  <view class="pic-num"  >满{{item.minPrice.toFixed(2)}}元可用</view>
				 	</view>
				 	<view class="right">
				 	  <view class="title">
				 		{{item.title}}
				 	  </view>
				 	  <view class="btns">
				 		<view  v-if="item.status==0||item.status==2" >{{item.limitTime}} 到期</view>
				 		<view  v-if="item.status==1">使用时间 {{item.useTime}}</view>
				 		<view class="bnt gray" v-if="item.status==1"  >已使用</view>
				 		<view class="bnt gray" v-if="item.status==2" >已过期</view>
				 		<view class="btn" v-if="item.status==0"  >使用</view>
				 	  </view>
				 	</view>
				   </view>
				</view>
				<view class="empty" v-if="coupons.length==0">
					<image src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/imagesNew/cf4a86b913a04341bb44e34bb4d37aa2.png"></image>
				</view>
			</view>
		</u-popup>
		<!-- 底部按钮 -->
		<view class="btn-box">
			<view class="btn" v-if="order!=null" @click="payOrder()">支付{{payMoney.toFixed(2)}}元</view>
		</view>
	</view>
</template>

<script>
	import {getMyEnableCouponList} from '@/api/user.js'
	import {getStoreOrderById,pay,compute} from '@/api/storeOrder'
	export default {
		data() {
			return {
				addressId:null,
				wxPay: true,
				address:null,
				orderId:null,
				order:null,
				items:null,
				couponText:"请选择优惠券",
				userCouponId:0,
				coupons:[],
				couponShow:false,
				payMoney:0,
				discountMoney:0
				
			}
		},
		onLoad(option) {
			this.orderId=option.orderId;
			var that=this;
			uni.$on('updateAddress', (e) => {
				that.addressId=e.addressId;
			 	that.address=e;
				that.address.address=e.province+e.city+e.district+e.detail
			})
		},
		onShow() {
			this.getStoreOrderById();
		},
		// onShareAppMessage(res) {
		// 	var that=this;
		// 	if(this.$isLogin()){
		// 		return {
		// 			title: "订单支付",
		// 			path: '/pages_order/storeOrderPay?orderId='+that.orderId,
		// 			imageUrl: 'https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/imagesNew/logo.jpg' //分享图标，路径可以是本地文件路径、代码包文件路径或者网络图片路径.支持PNG及JPG。显示图片长宽比是 5:4
		// 		}
		// 	}
			
		// },
		methods: {
			couponSelect(item){
				this.couponShow = false;
				//计算金额
				this.userCouponId=item.id;
				console.log(item)
				// this.couponText=item.price+"元优惠券"
				this.couponText= item.title
				this.compute()
			},
			open() {
			},
			close() {
				this.couponShow = false
			},
			openCoupon(){
				this.couponShow=true;
			},
			delCoupon(){
				this.userCouponId=0;
				this.couponText="请选择优惠券";
				this.compute();
			},
			compute(){
				var data={
					orderId:this.orderId,
					userCouponId:this.userCouponId,
					appId:getApp().globalData.appId
				}
				console.log(data)
				compute(data).then(
					res => {
						if(res.code==200){
							this.payMoney=res.data.payMoney;
							this.discountMoney=res.data.discountMoney;
							
						}
						else{
							this.userCouponId=0;
							this.couponText="请选择优惠券"
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
							this.$reLoginCheck(res.msg)
						}
					},
					err => {
					}
				);
			},
			getMyEnableCouponList(){
				let data={couponType:2,orderId:this.orderId}
				if(this.order.orderType == 2) {
					data={couponType:'2,4',orderId:this.orderId}
				}
				getMyEnableCouponList(data).then(
					res => {
						if(res.code==200){
							this.coupons=res.data;
						}
					},
					err => {
					}
				);
			},
			getStoreOrderById(){
				var data={orderId:this.orderId};
				console.log(data)
				getStoreOrderById(data).then(
					res => {
						if(res.code==200){
							this.order=res.order
							this.items=res.items
							if(this.order.userName!=null){
								this.address={
									realName:this.order.userName,
									phone:this.order.userPhone,
									address:this.order.userAddress,
								}
							}
							this.compute();
							this.getMyEnableCouponList();
						}else{
							 
						}
					},
					rej => {}
				);
			},
			openAddress(){
				uni.navigateTo({
					url: '/pages_user/address'
				})
			},
			payOrder(){
				var data = {
					userCouponId:this.userCouponId,
					addressId:this.addressId,
					remark:this.order.remark,
					orderId:this.order.orderId,
					appId:getApp().globalData.appId
				};
				var that=this;
				uni.showLoading();
				pay(data).then(
					res => {
						if(res.code==200){
							var temps=['wqKEklE_w5N19w9VAGY74TltPE6Ne3BaFObDD47uDEo']
							uni.requestSubscribeMessage({
								tmplIds: temps,
								success(e) {
									setTimeout(function(){
										uni.redirectTo({
											url:"/pages_order/storeOrderPayment?orderId="+that.order.orderId
										}) 
									},200);
								},
								fail(e) {
									setTimeout(function(){
										uni.redirectTo({
											url:"/pages_order/storeOrderPayment?orderId="+that.order.orderId
										}) 
									},200);
								}
							})
							
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
							this.$reLoginCheck(res.msg)
						}
					},
					rej => {}
				);
				
			}
		}
	}
</script>

<style lang="scss">
	.inner-box{
		padding: 20upx 20upx 140upx;
		.address-box{
			box-sizing: border-box;
			min-height: 171upx;
			background: #FFFFFF;
			border-radius: 16upx;
			background-image: url(https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/images/address_bg.png);
			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%;
				}
			}
		}
		.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, #078C9D 0%, #E2C99E 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;
					}
				}
			}
		}
		.other-info{
			margin-top: 20upx;
			background-color: #fff;
			border-radius: 20upx;
			overflow: hidden;
			padding: 0 30upx;
			.title{
				height: 80upx;
				line-height: 80upx;
				font-size: 30upx;
				color: #000;
				font-weight: bold;
				border-bottom: 2upx solid #eeeeee;
			}
			.item{
				height: 80upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				&:last-child{
					border-bottom: none;
				}
				.left{
					flex: 1;
					display: flex;
					align-items: center;
					.label{
						min-width: 140rpx;
						font-size: 28upx;
						color: #000;
					}
					.text{
						font-size: 28upx;
						color: #1b1b1b;
					}
				}
				.right{
					display: flex;
					align-items: center;
					justify-content: flex-end;
					.text{
						font-size: 28upx;
						color: #1b1b1b;
					}
					.ic-close{
						margin-left: 10rpx;
						width: 30rpx;
						height:30rpx;
					}
					.ic-back{
						margin-left: 10rpx;
						width: 15rpx;
						height:30rpx;
					}
				}
				.item-btn{
					max-width: 200rpx;
					padding: 0rpx 15rpx;
					height: 48upx;
					border-radius: 24upx;
					line-height: 48upx;
					font-size: 24upx;
					color: #000;
					border: 1upx solid #d8d8d8;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
		.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-box{
	 	height: 140upx;
	 	z-index: 9999;
	 	width: 100%;
	 	padding: 0rpx 30upx;
	 	position: fixed;
	 	bottom: 0;
	 	left: 0;
	 	box-sizing: border-box;
	 	background-color: #ffffff;
	 	display: flex;
	 	align-items: center;
	 	justify-content: center;
	 	.btn{
	 		width: 100%;
	 		height: 88upx;
	 		line-height: 88upx;
	 		text-align: center;
	 		font-size: 34upx;
	 		font-family: PingFang SC;
	 		font-weight: 400;
	 		color: #FFFFFF;
	 		background: #078C9D;
	 		border-radius: 10upx;
	 	}
	}
	.pay-type{
	 	box-sizing: border-box;
	 	height: 192upx;
	 	background: #FFFFFF;
	 	border-radius: 16upx;
	 	margin-top: 20upx;
	 	padding: 40upx 30upx;
	 	display: flex;
	 	flex-direction: column;
	 	justify-content: space-between;
	 	.title{
	 		font-size: 28upx;
	 		font-family: PingFang SC;
	 		font-weight: 500;
	 		color: #999999;
	 		line-height: 1;
	 	}
	 	.item{
	 		display: flex;
	 		align-items: center;
	 		justify-content: space-between;
	 		.left{
	 			display: flex;
	 			align-items: center;
	 			image{
	 				width: 44upx;
	 				height: 44upx;
	 				margin-right: 20upx;
	 			}
	 			.text{
	 				font-size: 30upx;
	 				font-family: PingFang SC;
	 				font-weight: bold;
	 				color: #222222;
	 				line-height: 1;
	 			}
	 		}
	 	}
	}
	.coupon{
		height: 100%;
		.empty{
			display: flex;
			align-items: center;
			justify-content: center;
			height: 650rpx;
			width: 100%;
			image{
				width: 280rpx;
				height: 200rpx;
			}
		}
	}
	.coupon-box{
		overflow-y: auto;
		padding: 80rpx 20rpx 80rpx;
		height: 650rpx;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: flex-start;
		box-sizing: border-box;
		.coupon-item{
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: flex-start;
			margin-bottom: 16rpx;
			height:170rpx;
			&:last-child{
				margin-bottom: 0rpx;
			}
			.left{
				 color: #fff;
				font-size: 36rpx;
				font-weight: bold;
				text-align: center;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				position: relative;
				width: 230rpx;
				image{
					position: absolute;
					width: 230rpx;
					height:170rpx;
					color: #fff;
				}
				.num{
					font-size: 40rpx;
				}
				.pic-num{
					font-size: 20rpx;
					z-index: 99;
				}
			}
			.right{
				
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: flex-start;
				height:170rpx;
				width: calc(100% - 230rpx);
				padding: 0 17rpx 0 24rpx;
				background-color: #fff;
				box-sizing: border-box;
				.title{
					width: 100%;
					font-size: 0.3 * 100rpx;
					color: #282828;
					height: 0.93 * 100rpx;
					line-height: 0.93 * 100rpx;
					border-bottom: 1px solid #f0f0f0;
				}
				.btns{
					display: flex;
					align-items: center;
					justify-content: space-between;
					width: 100%;
					font-size: 0.2 * 100rpx;
					color: #999;
					height: 0.76 * 100rpx;
					.btn{
						width: 1.36 * 100rpx;
						height: 0.44 * 100rpx;
						border-radius: 0.22 * 100rpx;
						font-size: 0.22 * 100rpx;
						color: #fff;
						text-align: center;
						line-height: 0.44 * 100rpx;
						background-color: #078C9D;
						.gray{
							 background-color: #ccc;
						}
					}
					
				}
			}
		}
		
	} 
</style>
 

