<template>
	<view class="bg">
		<view class="swipe-box">
			<swiper class="swiper" :autoplay="true" :interval="3000" :duration="1000" circular indicator-dots indicator-color="rgba(255,255,255,.5)" indicator-active-color="#fff">
				<swiper-item v-for="(item, index) in info.list_images.split(',')" :key="item.id">
					<view class="swiper-item">
						<image class="item-img" :src="showImg(item)" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</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 class="collect" @click="collect">
						<image :src="showImg('/uploads/20240827/8a55a8936b9324fa1c7b85c2da9c015b.png')" mode="" v-if="info.is_collect == 0"></image>
						<image v-else :src="showImg('/uploads/20240827/6bf73216f19c756961496031f8aed053.png')" mode="" ></image>
						<view>收藏</view>
					</view>
				</view>
				<view class="flex-between time-container">
					<view class="flex flex-1 flex-shrink-0 flex-items-center">
						<image class="address-icon flex-shrink-0" :src="showImg('/uploads/20240827/3d357e6e562de9395f373dc380a790a7.png')" mode=""></image>
						<view class="title text-overflowRows">
							<text style="margin-right: 28rpx;">营业时间</text>
							{{info.open_description}}
						</view>
					</view>
					<view></view>
				</view>
				<view class="flex-between">
					<view class="flex flex-1 flex-shrink-0 flex-items-center">
						<image class="address-icon flex-shrink-0" :src="showImg('/uploads/20240827/3d357e6e562de9395f373dc380a790a7.png')" mode=""></image>
						<view class="title text-overflowRows">
							<text style="margin-right: 28rpx;">景区地址</text>
							{{info.address}}
						</view>
					</view>
					<view>
						<img style="width: 30rpx;height: 30rpx;" :src="showImg('/uploads/20240827/5b19517f2a630f3a766ea03ac621a3be.png')">
					</view>
				</view>
			</view>
			
			<view class="box-title" v-if="sku.length>0">门票预定</view>
			<view class="common-container" v-if="sku.length>0">
				<view class="scenic-list" v-for="(item, index) in sku" :key="index">
					<view class="list-title text-overflow">{{ item.title }}</view>
					<view class="scenic-item com-flex-tao" v-for="(itemSku, indexSku) in item.specifications" :key="indexSku">
						<view class="w-1rpx flex-1">
							<view class="title text-overflow">{{ itemSku.title }}</view>
							<view class="tags-box">
								<view class="tags text-overflow" v-if="itemSku.specifications_new_tag">
									<view v-for="(tagSku,tagSkuIndex) in itemSku.specifications_new_tag.split(',').slice(0, 2)" 
									:key="tagSkuIndex">{{ tagSku }}</view>
								</view>
							</view>
							<view class="bottom com-flex-tao ">
								<view class="notice" @click="showSkuInfo(item, itemSku)">
									预订须知 >
								</view>
								<view></view>
							</view>
						</view>
						<view class="item-right com-flex-tao flex-shrink-0">
							<view class="price">
								{{showNoPriceNew(itemSku.price)}}
							</view>
							<view class="btn" @click="changeSku(itemSku, item)">
								预订
							</view>
						</view>
					</view>
				</view>
			</view>
		
			<view class="box-title">介绍</view>
			<view class="common-container">
				<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" >
				<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: 31rpx;height: 31rpx;" class="flex-shrink-0">
				</view>	
				<view class="content">
					<view class="detail-content" v-html="skuInfo.info"></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>
					<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>更多日期</view>
							<view>></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,
				
				id: null,
				
				info: {list_images:'', times_list_info: {start:'',end:''}},
				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) {
			this.headImg = 'https://tongli.sz-trip.com/uploads/20240826/8653c32761e01ee683505eddba1ae22b.png'
		},
		onLoad(options) {
			this.id = options.id;
			this.getInfo();
			this.getGoodsList()
		},
		methods: {
			// 获取景点信息
			getInfo() {
				this.Post({id: this.id},'/api/scenic/getScenicById').then(res => {
					if (res.data.flag == 0) {
						uni.showToast({title: '商品不存在或已下架',icon: 'none'})
						setTimeout(() => {this.goBack()}, 2000)
					}
					let info = res.data;
					try {
						info.times_list_info = JSON.parse(info.times_list)[0]
					} catch(e) {
						console.log(e)
						info.times_list_info = {start:'',end:''}
					}
					this.info = info
					console.log(info)
				});
			},
			// 根据景点id获取商品列表
			getGoodsList(){
				this.Post({
					scenic_id: this.id
				},'/api/scenic/getGoodsByScenicId').then(res => {
					this.sku = res.data || []
				})
			},
			
			// 收藏 2景点
			collect() {
				this.Post({type: 2,id: this.id},'/api/scenic/collect').then(res => {
					if (res) {
						uni.showToast({title: res.msg,icon: 'none'});
						this.info.is_collect = !this.info.is_collect
					}
				});
			},
			
			
			showSkuInfo (itemSku,goods) {
				this.skuInfo = itemSku
				this.selectGoods = 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 (res.data.length > 0) {
						this.timesArr = res.data || []
						this.seldTimeIndex = this.timesArr.findIndex(item => item.stock_number > 0)
					}
				})
			},
			
			
			// 选择日期
			clickTab(item, index) {
				this.seldDateIndex = index
				this.getTimeStock(item.date)
			},
			// 选择时段
			changeTime(item, index) {
				if(item.store > 0) {
					this.seldTimeIndex = index
				}
			},
			
			confirmCalendar (val) {
				let index = this.allSeldDate.find(v=>v.date == val.fulldate)
				if (index) {
					this.clickTab({date:val.fulldate},index)
				}
			},

			addBuyCard() {
				let that = this;
				// 如果是分时，未选择分时
				if(that.skuInfo.is_time_stock && that.seldTimeIndex < 0) {
					uni.showToast({title:'请选择分时',icon:'none'})
					return;
				}
				
					
				this.selectGoods.specifications.forEach(v=>{v.buyNum = 0;v.selPeople = []})
				
				let orderSkuIndex = this.selectGoods.specifications.findIndex(v=>v.id == that.skuInfo.id)
				let item = this.selectGoods.specifications.splice(orderSkuIndex, 1)[0]; // 将购买的数据放在第一位
				item.buyNum = 1
				item.selPeople = [
					{
						id:null,id_number:null,name:null,
						tel:null,user_id:null,age:null,
					},
				]
				this.selectGoods.specifications.unshift(item);
				
				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,
				}
				
				// that.gotoBuy(pInfo, sInfo);
				uni.navigateTo({
					url:'/subPackages/ticketBooking/order',
					success() {
						uni.$emit("updateDataByConnect", {msgType:'updateTicketBookingOrder',data:param})
					}
				})
			},
					

			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;
				// that.buryPoint("scene_order");
				// that.checkIsCanBuy(that.detail.title,
				// 	function() {
						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'
				}
			},
			
		}
	}
</script>

<style scoped lang="scss">
	*{
		box-sizing: border-box;
	}
	.bg{
		min-height: 100vh;
		background: #F8F8F8;
	}
	
	.swipe-box {
		height: 484rpx;
		position: relative;
	
		.swiper-item-num {
			width: 90rpx;
			height: 40rpx;
			background: rgba(0, 0, 0, 0.5);
			border-radius: 20rpx;
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #ffffff;
			text-align: center;
			line-height: 40rpx;
			position: absolute;
			right: 30rpx;
			bottom: 50rpx;
		}
	}
	
	.swiper {
		height: 484rpx;
		position: relative;
	
		.swiper-item {
			width: 100%;
			height: 484rpx;
	
			.item-img {
				width: 750rpx;
				height: 484rpx;
			}
		}
	}
	
	
	.detail-container{
		width: 100%;
		z-index: 2;
		padding: 26rpx;
		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: #000000;
		.time-container{
			padding: 22rpx 0;
		}
		
		.info-title{
			width: 550rpx;
			font-family: PingFang SC;
			font-size: 31rpx;
			height: 80rpx;
		}
		.collect{
			font-size: 23rpx;
			image{
				width: 48rpx;
				height: 48rpx;
			}
		}
	}
	
	.box-title {
		margin: 52rpx 0 26rpx 12rpx;
		font-family: PingFang;
		font-weight: bold;
		font-size: 37rpx;
		color: #000000;
	}
	
	.address-icon{
		margin-right: 9rpx;
		width: 26rpx;
		height: 26rpx;
	}
	
	.scenic-list{
		.list-title {
			padding-bottom: 20rpx;
		}
		.scenic-item {
		    /* border-top: 1px solid #d9d9d9; */
		    padding: 20rpx;
		    text-align: left;
		    background: rgba(11, 137, 142, .06);
		    border-radius: 20rpx;
		    margin-bottom: 20rpx;
			display: flex;
			
			.title {
			    font-weight: bold;
			    color: #333333;
			    width: 100%;
			}
			.tags-box {
			    overflow: hidden;
				.tags {
					display: flex;
					flex-wrap: nowrap;
				    flex: 1;
					font-weight: 400;
					color: #666666;
				}
				 .tags view::after {
					content: '丨'
				}
				.tags view:last-of-type:after {
					display: none;
				}
			}
			.item-right {
			    width: 100rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: space-between;
			}
			.price {
			    font-size: 30rpx;
			    font-weight: 400;
			    color: #8d8d8d;
				font-weight: 500;
				color: #d62828;
			}
			.price::before {
			    display: inline-block;
			    content: "￥";
			    font-size: 24rpx;
			    font-weight: 400;
			    color: #d62828;
			}
			
			.btn {
			    width: 100rpx;
			    height: 60rpx;
			    background: #0B898E;
			    border-radius: 20rpx;
			    text-align: center;
			    line-height: 58rpx;
			    font-weight: 500;
			    color: #FFFFFF;
			}
		}
	}

    .bottom {
		.notice {
			font-weight: 400;
			color: #0B898E;
		}
	}
	
	.popup-content-date {
		background-color: white;
		padding: 0rpx 39rpx 51rpx 39rpx;
		height: auto;
		border-radius: 20rpx 20rpx 0 0 ;
		.popup-content-title{
			padding: 20rpx 0;
		}
		
		.dateMore{
			width: 120rpx;
			height: 180rpx;
			background: #fff;
			font-weight: 400;
			font-size: 32rpx;
			padding-left: 15rpx;
			color: #0b898e;
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			right: 0;
			bottom: 0;
		}
		
		.date-content{
			width: 100%;
			display: flex;
			overflow-y: auto;
			position: relative;
			padding-right: 140rpx;
			
			.item{
				min-width: 160rpx;
				height: 180rpx;
				background: #f5f5f5;
				border-radius: 10rpx;
				margin-right: 20rpx;
				font-weight: 500;
				font-size: 32rpx;
				color: #000;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: space-around;
			}
			.item.active{
				background: #0b898e;
				color: #fff;
			}
			.item.disabled{
				color: #999;
			}
			
		}
		.date-content::-webkit-scrollbar{
			display: none;
		}
	}
	
	.time-box {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.time-item {
			width: 48%;
			background: #F5F5F5;
			border-radius: 10rpx;
			text-align: center;
			font-weight: 400;
			font-size: 34rpx;
			color: #000000;
			margin-bottom: 20rpx;
			padding: 5rpx;
		}
		.time-active {
			background: #0B898E;
			color: #fff;
		}
		.time-disable {
			color: #999999;
		}
	}
	.sku-bottom {
		width: 100%;
		height: 120rpx;
		background: #FFFFFF;
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		font-weight: 400;
		font-size: 36rpx;
		color: #393B3E;
		line-height: 80rpx;

		.bottom-price {
			font-weight: 500;
			font-size: 50rpx;
			color: #D62828;
		}
		.bottom-price::before {
			font-size: 27rpx;
			content: '￥';
		}

		.bottom-btn {
			width: 360rpx;
			background: #D62828;
			border-radius: 40rpx;
			text-align: center;
			font-weight: bold;
			font-size: 42rpx;
			color: #FFFFFF;
		}
	}
</style>
