<template>
	<view class="bg" v-if="info">
		<view class="swipe-box">
			<swiper class="swiper" :autoplay="true" :interval="3000" :duration="1000" 
			@change="swiperChange" circular>
				<swiper-item v-for="(item, index) in info.listimg" :key="item.id">
					<view class="swiper-item">
						<image class="item-img" :src="showImg(item)" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
			
			<view class="swiper-pointer">
				<view :class="['cricle',swiperCurrent==i?'active':'']" v-for="(item,i) in info.listimg" :key="i"></view>
			</view>
		</view>
		
		<view class="detail-container">
			<view class="common-container info-container">
				<view class="flex-between">
					<view class="info-title text-overflowRows">{{info.title}}</view>
				</view>
				<view class="tags flex text-overflow" style="overflow: hidden" v-if="info.display_tags">
					<view class="tag" v-for="(tagItem, tagIndex) in info.display_tags.split(',').slice(0, 2)" 
					:key="tagIndex">
						<text>{{ tagItem }}</text>
						<view class="tip"></view>
					</view>
				</view>
				
				<view class="flex" style="margin: 30rpx 0;">
					<text class="flex-shrink-0">开园时间：</text>
					<text class="flex-1 w-1rpx">{{info.open_time}}</text>
				</view>
				
				<view class="flex-between" style="align-items: flex-start;">
					<view class="flex flex-1 w-1rpx">
						<text class="flex-shrink-0">地址：</text>
						<text class="flex-1 w-1rpx">{{info.address}}</text>
					</view>
					<view @click="goMap" class="flex-shrink-0">
						<img style="width: 34.67rpx;height: 34.67rpx;" src="https://static.ticket.sz-trip.com/uploads/20250610/aefe5ce619ba00f6f1906d229effd686.png">
					</view>
				</view>
			</view>
			

			<view class="w-full flex" v-if="sku.length>0">
				<scroll-view class="left-container no-scrollbar" scroll-y scroll-with-animation>
					<view :class="['type-item',current ==i?'active':'']" 
						v-for="(item,i) in sku" :key="i" @click="changeType(item,i)">
						{{item.title}}
					</view>
				</scroll-view>
				
				<scroll-view class="right-container no-scrollbar" scroll-y scroll-with-animation>
					<view style="height: 20rpx;"></view>
					<view class="scenic-list" v-for="(item, index) in sku" :key="index">
						<view class="scenic-title">{{item.title}}</view>
						<view class="scenic-item " v-for="(itemSku, indexSku) in item.sku" :key="indexSku">
							<view class="title text-overflow">{{ itemSku.sku_name }}</view>
							<view class="flex flex-1 h-1rpx flex-between">
								<view class="w-1rpx flex-1">
									<view class="tags-box">
										<view class="tags text-overflow" v-if="itemSku.display_tags">
											{{ itemSku.display_tags.split(',').join(" | ") }}
										</view>
									</view>
												
									<view style="color: #FF944C;" class="notice" @click="showSkuInfo(itemSku, item)">
										预订须知 >>
									</view>
								</view>
								<view class="item-right  flex-shrink-0">
									<view class="price">{{showNoPriceNew(itemSku.price)}}</view>
									<view class="btn" @click="changeSku(itemSku, item)">预订</view>
								</view>
							</view>
							
						</view>
					</view>
					<view style="height: 20rpx;"></view>
				</scroll-view>
			
			</view>
		
			<view class="common-container" style="padding: 30rpx 20rpx;background: white;">
				<view class="box-title">景点简介</view>
				<view class="" id="cpts" v-html="formateRichText(info.feature_content)"></view>
			</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.title}}
					</view>
					<img src="https://static.ticket.sz-trip.com/taizhou/images/cha.png" @click="closePopupRule"
						style="width: 20rpx;height: 20rpx;" class="flex-shrink-0">
				</view>	
				<view class="content flex-1 h-1rpx no-scrollbar">
					<view class="detail-content" v-html="formateRichText(skuInfo.reserve_content)"></view>
				</view>
			</view>
		</uni-popup>
		
		<!-- 预定弹窗 -->
		<uni-popup ref="popup" type="bottom" :safe-area="false">
			<view class="popup-content-date" >
				<view class="popup-content-title flex">
					<view class="flex-1 w-1rpx text-overflow">
						{{skuInfo.title}}
					</view>
					<img src="https://static.ticket.sz-trip.com/taizhou/images/cha.png" @click="closePopup"
						style="width: 31rpx;height: 31rpx;" class="flex-shrink-0">
				</view>				
				<view class="order-popup-detail">
					<view class="relative">
						<view class="sku-title">使用日期</view>
						<view class="date-content">
							<view :class="['item', item.store>0?'':'disabled',seldDateIndex===index?'active':'']" 
							v-for="(item,index) in allSeldDate" :key="index"
							@click="clickTab(item,index)">
								<view>{{ShowDateDay(new Date(item.date).getDay())}}</view>
								<view>{{item.date.slice(-5)}}</view>
								<view class="price" v-if="item.store > 0">￥{{showNoPriceNew(item.money)}}</view>
								<view v-else>不可定</view>
							</view>
						</view>
						<view class="dateMore" @click="openCalendar">
							<view style="width: 55rpx;">更多日期</view>
							<view style="padding-left: 9rpx;">></view>
						</view>
					</view>
					
					<view v-if="timesArr.length > 0 && skuInfo.is_time_stock">
						<view class="sku-title">选择场次</view>
						<view class="time-box">
							<view v-for="(item,index) in timesArr" :key="index" 
							:class="['time-item',{'time-disable': item.stock_number < 1, 'time-active': item.stock_number > 0 && index == seldTimeIndex}]" 
							@click="changeTime(item,index)">
								{{ item.start_time }}-{{ item.end_time }}
								{{item.stock_number < 10 ? (item.stock_number === -1 ? '不可定' : item.stock_number === 0 ? '无票' : '(余票' + item.stock_number + ')') : '有票'}}
							</view>
						</view>
					</view>
				</view>
				<view class="sku-bottom">
					<view class="flex" style="align-items: baseline;">
					    合计：<view class="bottom-price">{{allSeldDate[seldDateIndex].money / 100 || 0}}</view>
					</view>
					<view class="bottom-btn" @click="addBuyCard">
							下一步
					</view>
				</view>
			</view>
			
		</uni-popup>
		
		<uni-calendar ref="calendar" class="uni-calendar--hook" :clear-date="true" 
		:date="allSeldDate[seldDateIndex].date" :insert="false"  :startDate="calendarParam.startDate"
		:endDate="calendarParam.endDate"  @confirm="confirmCalendar" :selected="calendarParam.selected"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				headImg: null,
				swiperCurrent: 0,
				
				id: null,
				info: null,
				
				current: 0,
				
				sku: [],
				skuInfo: {}, // 预定门票
				selectGoods: {}, // 预定的商品
				minSeldDate: new Date().Format('yyyy-MM-dd'),
				maxSeldDate: new Date((new Date()).getFullYear(), (new Date()).getMonth() + 3, 0).Format('yyyy-MM-dd'),
				allSeldDate: [],
				seldDateIndex: 0,
				timesArr: [],
				seldTimeIndex: -1,
				calendarParam: {
					stratDate:'',endDate: '', selected: []
				},
				
			}
		},
		onShow(options) {
			
		},
		onLoad(options) {
			this.id = options.id;
			this.getInfo();
			// this.getGoodsList()
		},	
		methods: {
			swiperChange (e) {
				this.swiperCurrent = e.detail.current
			},
			// 获取景点信息
			getInfo() {
				this.Post({id: this.id},'/api/scene/detail').then(res => {
					this.info = res.data
					this.sku = this.info.product || []
				});
			},

			goMap () {
				if (!this.info.lat || !this.info.lon) {
					uni.showToast({
						title: '暂未配置地理位置',
						icon: 'none'
					})
					return
				}
				
				uni.openLocation({
					latitude: Number(this.info.lat),
					longitude: Number(this.info.lon),
					name: this.info.title,
					address: this.info.address,
					success: function () {
						console.log('success');
					}
				});
			},
			
			changeType (item,i) {
				
			},
			
			showSkuInfo (itemSku,goods) {
				this.skuInfo = itemSku
				this.selectGoods = goods
				console.log(itemSku,goods)
				this.openPopRule()
			},
			
			// 选择列表规格
			changeSku(itemSku,goods) {
				this.skuInfo = itemSku
				this.selectGoods = goods
				// 期票类型
				// if(this.skuInfo.ticket_type == 2) {
				// 	this.addBuyCard()
				// }else {
				// 	this.getDays(itemSku,goods)
				// }
				this.getPriceCal(itemSku,goods)
			},
			openCalendar () {
				this.calendarParam = {
					startDate: (this.allSeldDate.find(v=>v.store>0) || {}).date,
					endDate: (this.allSeldDate[this.allSeldDate.findLastIndex(v=>v.store>0)]||{}).date,
					selected: this.allSeldDate.filter(v=>v.store>0).map(v=>{
						return {
							date: v.date,
							info: '￥'+this.showNoPriceNew(v.money),
							notNeedDot:true,
						}
					})
				}
				this.$refs.calendar.open();
			},
			
			// 获取价格日历列表
			getPriceCal(itemSku,goods) {
				this.Post({
					specifications_id: itemSku.id,
					goods_id: goods.id,
					limit: 60
				}, '/api/goods/getPriceCalendarListBySpecifications').then(res => {
					this.allSeldDate = res.data || []
					this.seldDateIndex = this.allSeldDate.findIndex(item => item.store != 0)
					this.getTimeStock(this.allSeldDate[this.seldDateIndex].date)
					this.openPop()
				})
			},
			// 获取规格分时库存
			getTimeStock(date) {
				this.Post({
					specifications_id: this.skuInfo.id,
					date: date,
				}, '/api/goods/getTimeStock').then(res => {
					if (Array.isArray(res.data)) {
						this.timesArr = res.data || []
						this.seldTimeIndex = -1
					}
					if (res.data.length > 0) {
						this.seldTimeIndex = this.timesArr.findIndex(item => item.stock_number > 0)
					}
				})
			},
			
			
			// 选择日期
			clickTab(item, index) {
				if (item.store>0) {
					this.seldDateIndex = index
					this.getTimeStock(item.date)
				}
				
			},
			// 选择时段
			changeTime(item, index) {
				if(item.stock_number > 0) {
					this.seldTimeIndex = index
				}
			},
			
			confirmCalendar (val) {
				let index = this.allSeldDate.findIndex(v=>v.date == val.fulldate)
				if (index>=0) {
					this.clickTab(this.allSeldDate[index],index)
				}
			},

			addBuyCard() {
				let that = this;
				// 如果是分时，未选择分时
				console.log(this.skuInfo)
				if(that.skuInfo.is_time_stock && that.seldTimeIndex < 0) {
					uni.showToast({title:'请选择分时',icon:'none'})
					return;
				}
				
				let param = {
					sInfo: this.skuInfo,
					pInfo: this.selectGoods,
					minSeldDate: this.minSeldDate,
					maxSeldDate: this.maxSeldDate,
					calendarParam: this.calendarParam,
					allSeldDate: this.allSeldDate,
					seldDateIndex: this.seldDateIndex,
					timesArr: this.timesArr,
					seldTimeIndex: this.seldTimeIndex,
				};
				uni.setStorageSync('ticketOrder', JSON.stringify(param));
				// that.gotoBuy(pInfo, sInfo);
				uni.navigateTo({
					url:'/subPackages/ticketBooking/order',
				})
			},
					

			gotoBuy(pInfo, sInfo) {
				console.log(pInfo, sInfo);
				let that = this;
				if (
					(pInfo.flag != 1 || sInfo.flag !== "on" || sInfo.stock <= 0) &&
					sInfo.is_third_stock == 0
				)
				return;

						let params = {
							pInfo: pInfo,
							sInfo: sInfo,
							minSeldDate: this.minSeldDate,
							maxSeldDate: this.maxSeldDate,
							calendarParam: this.calendarParam,
							allSeldDate: this.allSeldDate,
							seldDateIndex: this.seldDateIndex,
							timesArr: this.timesArr,
							seldTimeIndex: this.seldTimeIndex,
						};
						if (that.detail.allowance) {
							sInfo.allowance_data = that.detail.allowance;
							let spread_price = Number(
								sInfo.allowance_data.discount_limit_price -
								sInfo.allowance_data.user_used_price
							);
							if (spread_price > 0) {
								if (
									spread_price <
									(sInfo.allowance_data.discount_rate / 100) *
									sInfo.price
								)
									sInfo.allowance_price = spread_price;
								else
									sInfo.allowance_price =
									(sInfo.allowance_data.discount_rate / 100) *
									sInfo.price;
							}
						}
			},

			closePopup() {
				this.$refs.popup.close()
			},
			openPop(){
				this.$refs.popup.open()
			},
			closePopupRule() {
				this.$refs.popupRule.close()
			},
			openPopRule(){
				this.$refs.popupRule.open()
			},
			// 价格格式
			showNoPriceNew(price) {
				if (price && price > 0) {
					return (price / 100)
				} else {
					return '0'
				}
			},
			
	
			
			getTime(time) {
				let m = parseInt(time / 60);
				let s = time % 60;
				return this.towNum(m) + ':' + this.towNum(s);
			},
			towNum(num) {
				if(num >= 10) {
					return num;
				}else {
					return '0' + num;
				}
			},
			
		}
	}
</script>

<style scoped lang="scss">
	*{
		box-sizing: border-box;
	}
	::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}
	.bg{
		min-height: 100vh;
		background: #F8F8F8;
	}
	
	.swipe-box {
		height: 413rpx;
		position: relative;
		.swiper {
			height: 413rpx;
			position: relative;
		
			.swiper-item {
				width: 100%;
				height: 413rpx;
		
				.item-img {
					width: 750rpx;
					height: 413rpx;
				}
			}
		}
		
		.swiper-pointer{
			position: absolute;
			right: 10rpx;
			bottom: 40rpx;
			display: flex;
		}
		.cricle{
			width: 14rpx;
			height: 14rpx;
			background: rgba(255,255,255,0.3);
			border-radius: 50%;
			margin-left: 10rpx;
		}
		.cricle.active{
			background: white;
		}
	}
	
	.detail-container{
		width: 100%;
		z-index: 2;
		padding: 26rpx 0;
		position: relative;
		top: -52rpx;
	}
	
	.common-container{
		background: white;
		border-radius: 20rpx;
		margin-bottom: 30rpx;
		padding: 22rpx 26rpx;
	}
	
	.info-container{
		font-family: PingFang;
		font-weight: 500;
		font-size: 27rpx;
		color: #666;
		.info-title{
			width: 100%;
			font-family: PingFang SC;
			font-size: 33rpx;
			height: 80rpx;
			font-weight: bold;
			color: #000;
		}
		
		.tags .tag {
		    margin-right: 20rpx;
		    font-size: 24rpx;
		    color: #6A8A27;
			display: inline-block;
			position: relative;
			.tip{
				position: absolute;
				left: 0;
				bottom: 4rpx;
				width: 100%;
				background: #EFF7DF;
				height: 8rpx;
				z-index: 2;
			}
			text{
				position: relative;
				z-index: 5;
			}
		    
		}
	}
	
	.left-container{
		width: 147rpx;
		flex-shrink: 0;
		.type-item{
			font-weight: 500;
			font-size: 24rpx;
			color: #000000;
			width: 100%;
			height: 133rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			text-align: center;
		}
		.type-item.active{
			font-weight: bold;
			font-size: 24rpx;
			color: #6A8A27;
			background: #FFFFFF;
		}
	}
	.right-container{
		flex: 1;
		width: 1rpx;
		background: white;
		max-height: 1000rpx;
	}
	
	
	.box-title {
		font-weight: normal;
		font-size: 35rpx;
		color: #6A8A27;
	}
	
	.address-icon{
		margin-right: 9rpx;
		width: 26rpx;
		height: 26rpx;
	}
	
	
	.scenic-list{
		padding: 10rpx 10rpx 0;
		.scenic-title{
			font-weight: bold;
			font-size: 35rpx;
			color: #000000;
			padding-bottom: 20rpx;
		}
		.scenic-item {
			width: 100%;
			height: 187rpx;
			background: rgba(239, 247, 223, 0.5);
			border-radius: 13rpx;
			margin-bottom: 20rpx;
		    padding: 19rpx 22rpx 10rpx;
			display: flex;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 23rpx;
			color: #666666;
			flex-direction: column;
			justify-content: space-between;
		
			.title {
			    width: 100%;
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 31rpx;
				color: #000000;
			}
			.tags-box {
			    overflow: hidden;
				padding-bottom: 17rpx;
				padding-left: 6rpx;
				font-weight: 500;
				font-size: 23rpx;
				color: #666666;
			}
			.item-right {
			    flex-shrink: 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
			.price {
				font-family: PingFangSC;
				font-weight: 500;
				font-size: 36rpx;
				color: #D62828;
				font-weight: bold;
				&::before {
				    display: inline-block;
				    content: "￥";
				    font-size: 24rpx;
				}
			}
			
			
			.btn {
				width: 93rpx;
				height: 80rpx;
				background: #6A8A27;
				border-radius: 13rpx;
			    text-align: center;
			    line-height: 80rpx;
			   font-weight: 500;
			   font-size: 31rpx;
			   color: #FFFFFF;
			   margin-left: 12rpx;
			}
		}
	}

    .bottom {
		.notice {
			font-weight: 400;
			color: #0B898E;
		}
	}
	
	.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;
			border: 1px solid #000000;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 27rpx;
			color: #111;
			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;
				border: 1px solid #000000;
				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;
			}
			.item.active{
				background: #515150;
				color: white;
				border: 1px solid #12293C;
				.price{color: white;}
			}
			.item.disabled{
				color: #666;
				border-color: #CCC;
			}
			
			.price{
				color: #C3282E;
			}
			
		}
		.date-content::-webkit-scrollbar{
			display: none;
		}
	}
	
	.time-box {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		max-height: 340rpx;
		overflow-y: auto;

		.time-item {
			width: 48%;
			height: 60rpx;
			border-radius: 10rpx;
			text-align: center;
			font-size: 34rpx;
			margin-bottom: 23rpx;
			font-family: PingFangSC;
			font-weight: 400;
			font-size: 25rpx;
			border: 1px solid #333333;
			line-height: 58rpx;
			color: #000;
		}
		.time-active {
			background: #515150;
			color: white;
			border: 1px solid #12293C;
		}
		.time-disable {
			color: #666666;
			border-color: #CCC;
		}
	}
	.sku-bottom {
		width: 100%;
		height: 166rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -3rpx 8rpx 0rpx rgba(71,71,71,0.1);
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 50rpx 50rpx 85rpx;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #393B3E;
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;

		.bottom-price {
			font-size: 48rpx;
			font-weight: bold;
			color: #C3282E;
		}
		.bottom-price::before {
			font-size: 24rpx;
			content: '￥';
		}

		.bottom-btn {
			width: 250rpx;
			height: 80rpx;
			background: #C3282E;
			border-radius: 40rpx;
			font-size: 32rpx;
			text-align: center;
			font-weight: bold;
			color: #FFFFFF;
			line-height: 78rpx;
		}
	}
	
	.address-title{
		display: flex;
		align-items: flex-start;
		.address-detail{
			font-size: 27rpx;
			color: #666666;
			padding-right: 40rpx;
		}
	}

	/deep/ .uni-calendar-item--extra{
		color: #333 !important;
	}
	/deep/ .uni-calendar-item--isDay{
		background: #525251 !important;
		border-radius: 50%;
		
		.uni-calendar-item--extra{
			color: white !important;
		}
	}
	
	/deep/ .uni-calendar-item--checked{
		background: #525251 !important;
		border-radius: 50%;
		
		.uni-calendar-item--extra{
			color: white !important;
		}
	}

</style>
