<template>
	<view class="bg">
		<view class="order-goods-container" v-for="(item,i) in orderList" :key="i">
			<view class="top-box1" >{{ item.pInfo.title }}</view>
			<view class="tickets-box">
				<view class="w-full num-box">
					<view class="flex-between num-left">
						<view class="left-title text-overflow">{{item.sInfo.sku_name}}</view>
						<view class="left-price">{{item.sInfo.price / 100}}</view>
						<view class="num-right">
							<view :class="['btn-num',item.sInfo.buyNum<=1?'disabled':'']"   @click="delNumber(item.sInfo)" >-</view>
							<view class="num-span">{{ item.sInfo.buyNum }}</view>
							<view class="btn-num"  @click="addNumber(item.sInfo)" >+</view>
						</view>
					</view>
					<view style="padding: 30rpx 0 0rpx;" class="num-subtitle" @click="showpopRule(true, item.sInfo)">
						<view style="padding-left:4rpx">预订须知 ></view>
					</view>
				</view>
				
			</view>
		</view>
		
		<view class="people-box" >
			<view class="people-box-title">出行人信息</view>
			<view style="padding: 42rpx 0 10rpx;">
				<view class="people-box-sku">
					<view class="w-full flex flex-items-center"  style="padding-bottom: 33rpx;font-size: 29rpx;">
						<view style="width: 170rpx;font-weight: bold;font-size: 29rpx;" class="flex-shrink-0">联系电话</view>
						<view class="flex flex-between flex-1 w-1rpx" >
							<input class="input" type="text" placeholder="请输入手机号" v-model="phone" />
							<uni-icons v-if="phone&&phone.length>0"
								type="closeempty" size="14" @click="clearTel()"></uni-icons>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 优惠券 -->
		<view @click="goOrderCoupon" class="tickets-container flex-between top-line" v-if="!isShoppingCart">
			<view class="order-title">优惠券</view>
			<view class="coupon-btn" v-if="coupon==''">
				<view class="select">选择优惠券</view>
				<uni-icons style="height: 42rpx;" color="#999999" type="right" size="18"></uni-icons>
			</view>
			<div class="coupon-price" v-else>
				<span v-if="coupon.activity.fold == 0">-￥{{coupon.activity.money/100}}</span>
				<span v-else>-{{coupon.activity.fold*10}}%</span>
				<span style="margin:0 31rpx 0 8rpx;color: #6C7A94;">></span>
			</div>
		 </view>
		
		<view class="btn-list">
			<view class="price-box">
				<view class="text">合计:</view>
				<view class="price">{{ total() }}</view>
			</view>
			<view class="btn" @click="order()">去支付</view>
		</view>
		
		
		<!-- 预订须知的弹窗 -->
		<uni-popup ref="popupRule" type="bottom" :safe-area="false">
			<view class="popup-content-date flex-column flex" >
				<view class="popup-content-title flex">
					<view class="flex-1 w-1rpx text-overflow">
						{{skuInfo.sku_name}}
					</view>
					<img src="https://static.ticket.sz-trip.com/taizhou/images/cha.png" @click="showpopRule(null)"
						style="width: 20rpx;height: 20rpx;" class="flex-shrink-0">
				</view>	
				<view class="content flex-1 h-1rpx no-scrollbar" v-if="skuInfo && skuInfo.sku_model">
					<view class="detail-content" v-html="formateRichText(skuInfo.sku_model.bookinfo)"></view>
				</view>
			</view>
		</uni-popup>
	

	</view>
</template>

<script>

	export default {
		components: {},
		data() {
			return {
				isShoppingCart: false,
				// 下单产品
				orderList: [],
				phone: '',
				coupon:'',
				
				skuInfo: {}, // 预定须知当前点击的sku
			}
		},
		onLoad(options) {
			if (options.isShoppingCart) {
				this.isShoppingCart = options.isShoppingCart
			}
			// this.getList();
			this.$store.commit("choseCoupon", "");
			this.initPageData()
		},
		
		onShow() {
			if (!this.isShoppingCart) {
				this.coupon = this.$store.state.user.coupon
			}
		},

		methods: {
			goOrderCoupon () {
				let allPrice = 0
				let skuIds= []
				this.orderList.forEach(v=>{
					allPrice+= v.sInfo.price*v.sInfo.buyNum
					if (v.sInfo.buyNum>0) {
						skuIds.push(v.sInfo.id)
					}
				})
				uni.navigateTo({
					url: `/subPackages/order/orderCoupon?allprice=${allPrice}&sku_ids=${skuIds.join(',')}`
				})
			},
			
			// 获取最大优惠券
			async getMaxCouponData () {
				// 购物车不需要
				if (this.isShoppingCart) {
					return 
				}
				let allPrice = 0
				let skuIds= []
				this.orderList.forEach(v=>{
					allPrice+= v.sInfo.price*v.sInfo.buyNum
					if (v.sInfo.buyNum>0) {
						skuIds.push(v.sInfo.id)
					}
				})
				let param = {sku_ids: skuIds.join(','), money: allPrice}
				let res = await this.getMaxCoupon(param)
				if (res.id) {
					this.coupon = res
				}
			},
			
			
			initPageData () {
				let data = uni.getStorageSync('foodOrder');
				try{
					data = JSON.parse(data)
					this.orderList = data
					console.log(this.orderList)
					
				} catch(e){
					console.log(e)
				}
				this.getMaxCouponData()
			},

			clearTel () {this.phone = ''},
			
			// 减少数量
			delNumber(skuItem) {
				if (skuItem.buyNum <= 1) {
					return
				}
				skuItem.buyNum -= 1
				this.$store.commit("choseCoupon","");
				this.coupon = ''

			},
			// 增加数量
			addNumber(skuItem) {
				skuItem.buyNum += 1
				this.$store.commit("choseCoupon","");
				this.coupon = ''
			
			},
			// 计算总价
			total() {
				let price = 0
				let allPrice = 0
				this.orderList.forEach(v=>{
					allPrice+= v.sInfo.price*v.sInfo.buyNum
				})
				this.allprice = allPrice
				if (this.coupon) {
					if (this.coupon.activity.fold == 0) {
						if (this.coupon.activity.money>allPrice) {
							price =0
						}else{
							price = allPrice  - (this.coupon.activity.money)
						}
						
					} else{
						price = allPrice - allPrice * (this.coupon.activity.fold*10/100)
					}
				} else {
					price = allPrice
				}
				return price < 0 ? 0 : (price/100).toFixed(2)
			},
			
	
			
			// 预定须知
			showpopRule(flag, item) {
				if (item) {
					this.skuInfo = item
				} 
				if (flag) {
					this.$refs.popupRule.open('bottom');
				} else {
					this.$refs.popupRule.close();
				}
			},
			

			
			/*---------------------------价格日历-----------------------------------*/
			order() {
				let goods = []
				// 设置参数
				let canSubmit = true
				this.orderList.filter(v=>v.sInfo.buyNum>0).forEach(item=>{
					let sku = item.sInfo
					let param = {
						type: item.pInfo.type,
						product_id: item.pInfo.id,
						sku_id: sku.id,
						product_num: sku.buyNum,
						remark: sku.remark,
						phone: this.phone,
					}
					goods.push(param)
				})
				
			
				let data = {
					product_list: goods,
					coupon_id: this.coupon ? this.coupon.id : "",
				}
				console.log('data数据',data);
				this.Post({
					method: 'POST',
					data: JSON.stringify(data)
				}, '/api/order/create').then(res => {
					if (res.code == 1) {

						console.log(res.data.order_id);
						let order_id = res.data.order_id
						this.$store.commit("choseCoupon", "");
						this.Post({
							order_id: order_id,
							type: "miniprogram",
							platform: 'miniprogram'
						}, '/api/pay/unify').then(res => {
							if (res.data) {
								uni.requestPayment({
									nonceStr: res.data.nonceStr,
									package: res.data.package,
									paySign: res.data.paySign,
									signType: res.data.signType,
									timeStamp: res.data.timeStamp,
									success: () => {
										this.getSubscribeMessage()
									},
									fail() {
										uni.navigateTo({
											url: '/subPackages/order/trades'
										})
									}
								})
							}
						})
					}
				})
			}
			
		}
	}
</script>

<style scoped lang="scss">
	*{
		box-sizing: border-box;
	}
	.bg{
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 200rpx;
		background-color: rgb(247, 247, 247);
	}
	
	.top-box1 {
	    width: 100%;
	    background: #FFFFFF;
	    border-radius: 13rpx;
	    padding: 28rpx 20rpx;
	    font-weight: bold;
	    font-size: 33rpx;
	    color: #000000;
	}

.tickets-box{
	background: #FFFFFF;
	border-radius: 13rpx;
	width: 100%;
	margin: 20rpx 0;
	
	.num-box {
	    display: flex;
	    justify-content: space-between;
		flex-direction: column;
		padding: 30rpx 20rpx 40rpx;
	
	    .num-left {
	        width: 100%;
	        box-sizing: border-box;
	        display: flex;
	        justify-content: space-between;
	        .left-title {
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 31rpx;
				color: #000000;
				width: 450rpx;
	        }
	        .left-price {
	            font-weight: bold;
	            font-size: 36rpx;
	            color: #D62828;
				padding-right: 27rpx;
	        }
	        .left-price::before {
	            font-size: 24rpx;
	            content: '￥';
	        }
	
	
	    }
		.num-subtitle {
			display: flex;
			align-items: center;
			width: 100%;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 23rpx;
			color: #6A8A27;
		}
	    .num-right {
			display: flex;
			align-items: center;
			font-weight: bold;
			font-size: 29rpx;
			color: #000000;
			text-align: center;
	        .num-span {
	            width: 67rpx;
	            line-height: 63rpx;
	        }
	
	        .btn-num {
	            width: 46rpx;
	            height: 46rpx;
	            line-height: 40rpx;
	            background: #6A8A27;
	            border-radius: 50%;
	            font-weight: 500;
	            font-size: 45rpx;
	            font-size: 34rpx;
	            color: #FFFFFF;
	        }
			.btn-num.disabled{
				color: #999999;
				background: white;
				border: 1px solid #999;
			}
	    }
	}
	
	
	.buyMore{
		height: 118rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 31rpx;
		color: #000000;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding:0 20rpx;
		
		.buyMore-btn{
			height: 47rpx;
			width: 148rpx;
			text-align: center;
			border-radius: 23rpx;
			border: 1px solid #515150;
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 25rpx;
			color: #515150;
			line-height: 45rpx;
		}
	}
}

.people-box{
	width: 100%;
	background: #FFFFFF;
	border-radius: 13rpx;
	.people-box-title{
		font-family: PingFangSC;
		font-weight: 600;
		font-size: 31rpx;
		color: #000000;
		padding: 42rpx 18rpx;
		border-bottom: 1px solid #CCCCCC;
	}
	

	.people-box-sku{
		width: 100%;
		background: #FFFFFF;
		border-radius: 18rpx;
		padding: 0rpx 20rpx;
		
		.sku-title{
			font-weight: 400;
			font-size: 29rpx;
			color: #060001;
			padding-bottom: 25rpx;
		}
		
		.person-info{
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-weight: 400;
			font-size: 27rpx;
			color: #000000;
			margin-bottom: 33rpx;
			.person-info-detail{
				font-weight: 400;
				font-size: 24rpx;
				color: #666666;
				width: 400rpx;
			}
		}
		
		.person-need{
			width: 100%;
			height: 80rpx;
			background: #F2F2F2;
			border-radius: 13rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 27rpx;
			color: #515150;
			padding: 0 20rpx;
		}
		.person-item-more{
			width: 110rpx;
			height: 73rpx;
			background: #F2F2F2;
			border-radius: 11rpx;
			text-align: center;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 29rpx;
			color: #515150;
			line-height: 73rpx;
			position: absolute;
			right: 0;
			top: 0;
		}
		
	}
}


	.tickets-container {
		width: 100%;
		background: #fff;
		height: 120rpx;
		margin-top: 22rpx;
		.order-title {
			margin: 31rpx 0 31rpx 30rpx;
			font-size: 31rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #000000;
		}

		.coupon-price {
			color:#DD0000;
			font-size: 30rpx;
			font-weight: bold;
		}
	}

.btn-list {
	width: 100%;
	height: 166rpx;
	background: #ffffff;
	box-shadow: 0rpx -3rpx 9rpx 1rpx rgba(227, 229, 232, 0.5);
	display: flex;
	position: fixed;
	bottom: 0;
	left: 0;
	right:0;
	padding: 20rpx 50rpx;
	align-items: center;
	justify-content: space-between;
	.btn {
		width: 250rpx;
		height: 80rpx;
		background: #6A8A27;
		border-radius: 11rpx;
		text-align: center;
		line-height: 80rpx;

		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
	}
	.price-box {
		display: flex;
		align-items: baseline;
		.text {
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #393b3e;
		}
		.price {
			margin-left: 15rpx;
			font-size: 48rpx;
			font-weight: bold;
			color: #C3282E;
			&:before {
				content: '￥';
				display: inline-block;
				color: #C3282E;
				font-size: 24rpx;
			}
		}
		.post-text {
			margin-left: 15rpx;
			color: #C3282E;
			font-size: 24rpx;
		}
	}
}

	.popup-content-date {
		background-color: white;
		padding: 0rpx 28rpx 166rpx;
		height: 70vh;
		border-radius: 20rpx 20rpx 0 0 ;
		.popup-content-title{
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 37rpx;
			color: #000000;
			padding: 39rpx 0;
			display: flex;
			align-items: center;
			border-bottom: 1px solid #CCCCCC;
		}
		// 预定须知
		.content{
			padding-top: 48rpx;
			overflow-y: auto;
		}
		
		.order-popup-detail{
			.sku-title{
				padding: 48rpx 0 26rpx;
				font-family: PingFangSC;
				font-weight: 500;
				font-size: 31rpx;
				color: #000000;
			}
		}
		
		.dateMore{
				width: 120rpx;
				height: 133rpx;
				background: white;
				border-radius: 10rpx;
				background: #FFFFFF;
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 27rpx;
				color: #6A8A27;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				flex-shrink: 0;
				padding: 6rpx 0;
				position: absolute;
				bottom: 0;
				right: 0;
			}
			
			.date-content{
				width: 100%;
				display: flex;
				overflow-y: auto;
				position: relative;
				padding-right: 140rpx;
				
				.item{
					width: 120rpx;
					height: 133rpx;
					border-radius: 10rpx;
					margin-right: 24rpx;
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 27rpx;
					color: #000;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-around;
					flex-shrink: 0;
					padding: 6rpx 0;
					background: #F5F5F5;
				}
				.item.active{
					background: #6A8A27;
					color: white;
					.price{color: white;}
				}
				.item.disabled{
					background: #F5F5F5;
					color: #999999;
				}
				
				.price{
					color: #EE3E3B;
				}
				
			}
			.date-content::-webkit-scrollbar{
				display: none;
			}
		}
	
	
	.coupon-btn {
		color: #999999;;
		display: flex;
		align-items: center;
		.select {
			display: block;
			width: 153rpx;
			height: 40rpx;
			background: #6A8A27;
			border-radius: 9rpx;
			font-weight: 500;
			font-size: 24rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 40rpx;
			font-family: PingFang SC;
			margin-right: 20rpx;
	
		}
	}
</style>
