<template>
	<view class="center">
		<!-- 顶部logo and 搜索 start-->
		<view class="top-search flex">
			<view class="top-logo">
				<image src="../../static/icon/logo.png" mode="widthFix"></image>
			</view>
			<view class="search-box flex" @click="clickSearch()">
				<image class="search" src="../../static/icon/search-h.png" mode=""></image>
				<view class="search-font">输入关键词搜索</view>
			</view>
		</view>
		<!-- 顶部logo and 搜索 end-->
		<view class="jg"></view>
		<!-- 轮播图 start -->
		<swiper class="top-swiper" autoplay="true" duration="400" interval="5000" @change="swiperChange">
			<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item"
				@click="bannerNavToUrl(item)">
				<image :src="item.pic" />
			</swiper-item>
		</swiper>
		<!-- 轮播图 end -->
		<view class="swiper-btm flex">
			<view class="btm-item flex">
				<image src="../../static/icon/index-gou.png" mode=""></image>
				<view class="">自用购物返利</view>
			</view>
			<view class="btm-item flex">
				<image src="../../static/icon/index-gou.png" mode=""></image>
				<view class="">分享好友赚钱</view>
			</view>
			<view class="btm-item flex">
				<image src="../../static/icon/index-gou.png" mode=""></image>
				<view class="">超值正品优惠</view>
			</view>
		</view>
		<!-- 预约 start-->
		<view class="yuyue-wrap">
			<view class="yuyue-top flex">
				<image src="../../static/img/yuyue-top.png" mode="heightFix"></image>
			</view>
			<view class="yuyue-item"  v-for="item in changCiList">
				<view class="item-top">
					<image src="../../static/img/yuyue.png" mode=""></image>
					<view class="item-top-info flex">
						<view class="">
							{{item.name}}<text>/大家都在买</text>
						</view>
						<view class="goto">
							GO>
						</view>
					</view>
				</view>
				<view class="item-info flex">
					<view class="item-img">
						<image :src="item.image" mode=""></image>
					</view>
					<view class="item-val flex">
						<view class="item-status">
							距离本场开始
						</view>
						<view class="item-djs">
							
							<uni-countdown
							v-if="!item.djs.over"
								color="#C6914E"
								background-color="#F6EFDF"
								:show-day="false"
								:day="item.djs.day"
								:hour="item.djs.hours"
								:minute="item.djs.minutes"
								:second="item.djs.seconds"
							></uni-countdown>
							<view class="" v-else style="color: #dc262b;">
								预约已结束
							</view>
						</view>
						<view class="item-base">
							开始时间 {{item.radd_time}} 结束时间 {{item.rend_time }}
						</view>
						<view class="item-btn" @click="yuyue(item)" :class="{'yuyue': !item.djs.over || item.sta == 2 }">
							{{item.sta == 2 ? '立即进场': (item.djs.over ? '已结束': '立即预约')}}
						</view>
					</view>
					
				</view>
			</view>
		</view>
		<!-- 预约 end-->
		<!-- 预约弹窗 start-->
		<uni-popup ref="popupyy" type="bottom">
			<view class="popupyy-wrap">
				<view class="popupyy-top">
					预约活动
				</view>
				<view class="popupyy-tit">
					本次预约冻结广告值
				</view>
				<view class="popupyy-val">
					{{ yuyueInfo.advert }}
				</view>
				<view class="" style="margin-top: 50rpx;">
					消费账户不低于{{yuyueInfo.lowest }}
				</view>
				<view class="" style="color: #FF4C4C;margin-top: 15rpx;">
					广告值不低于{{yuyueInfo.minimum }}
				</view>
				<view class="popupyy-btn" @click="yuyueOk()">
					确认预约
				</view>
			</view>
		</uni-popup>
		<!-- 预约弹窗 end-->
		<!-- 预约成功弹窗 start-->
		<uni-popup ref="popupyyok" type="center">
			<view class="popupyyok-wrap">
				<image src="../../static/img/yuyueok.png" mode="widthFix"></image>
				<view class="" style="font-size: 35rpx;font-weight: bold;color: #333333;">
					预约成功！
				</view>
				<view class="" style="padding: 20rpx 80rpx;color: #999999;font-size: 26rpx;">
					恭喜您预约活动成功，请您调好闹钟，准时参与！
				</view>
				<view class="btn" @click="$refs.popupyyok.close()">
					确定
				</view>
			</view>
		</uni-popup >
	</view>
</template>

<script>
	import { timeComputed } from '@/utils/rocessor.js'
	import {
		tabbar1
	} from '@/utils/tabbar.js';
	import {
		loadIndexs,
		getChangCi,
		subscribe,
		advance
	} from '@/api/index.js';
	import uniCountdown from '@/components/uni-countdown/uni-countdown.vue';
	import { mapState, mapMutations } from 'vuex';
	export default {
		components: {
			uniCountdown
		},
		data() {
			return {
				current: 0,
				tabbar: tabbar1,
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				dataList: '',
				carouselList: [], //轮播图列表
				text: ['恭喜黑白灰用户拼团成功￥353元！', '恭喜黑白灰用户拼团成功￥353元！'],
				stopTime: {
					stopTimeH: 0,
					stopTimeM: 24,
					stopTimeS: 59
				},
				bzb: 30,
				changCiList: [],
				yuyueInfo: {}
			};
		},
		onLoad() {
			this.loadData();
			
		},
		onShow() {
			this.getChangCi()
		},
		computed: {
			...mapState('user',['userInfo'])
		},
		methods: {
			loadData() {
				loadIndexs({})
					.then(({
						data
					}) => {
						let goods = data.info;
						this.dataList = goods.bastList; //精品推荐
						this.carouselList = data.banner;
					})
					.catch(e => {});
			},
			goDetails(e) {
				console.log(e, '123456');
				uni.navigateTo({
					url: '/pages/index/messageInfo'
				});
			},
			nav(url) {
				uni.navigateTo({
					url,
					fail() {
						uni.switchTab({
							url
						});
					}
				});
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			// 點擊搜索框
			clickSearch() {
				uni.navigateTo({
					url: '/pages/product/search'
				});
			},

			//详情页
			navToDetailPage(item) {
				let id = item.id;
				uni.navigateTo({
					url: '/pages/product/product?id=' + id
				});
			},
			// 打开预约弹窗
			yuyue(item) {

				if(item.sta == 2) {
					// 进场
					advance({
						id: item.id
					}).then(res => {
						console.log(res)
						uni.navigateTo({
							url: '/pages/index/yuyuechang?id=' + item.id
						})
					})
				}else {
					if(item.djs.over) {
						return
					}
					this.yuyueInfo = item
					this.$refs.popupyy.open()
				}
				
			},
			// 确定预约
			yuyueOk() {
				let obj = this
				// 判断广告值
				if(obj.yuyueInfo.minimum*1 > obj.userInfo.aid_val*1) {
					return obj.$api.msg('您当前广告值不足')
				}
				// 判断消费账户
				if(obj.yuyueInfo.lowest*1 > obj.userInfo.now_money*1) {
					return obj.$api.msg('您当前消费账户不足')
				}
				subscribe({
					id: obj.yuyueInfo.id
				}).then(res => {
					console.log(res,'确定预约')
					obj.$refs.popupyy.close()
					obj.$refs.popupyyok.open()
				})
				
			},
			// 获取场次信息
			getChangCi() {
				let obj = this
				getChangCi({
					page: 1,
					limit: 10
				}).then(res => {
					console.log(res,'获取场次信息')
					
					let arr = res.data.map(e => {
						e.djs = timeComputed(e.time)
						return e
					})
					console.log(arr)
					obj.changCiList = arr.reverse()
				})
			}
		}
	};
</script>

<style lang="scss">
	page,
	.center {
		min-height: 100%;
		height: auto;
		background: #ededed;
	}
	// 顶部搜索
	.top-search {
		height: 80rpx;
		padding: 0 20rpx;
		background-color: #fff;
		.top-logo {
			width: 50rpx;
			// height: 50rpx;
			margin-right: 10rpx;

			image {
				width: 48rpx;
			}

		}

		.search-box {
			justify-content: center;
			width: 698rpx;
			height: 60rpx;
			background: #EEEEEE;
			// box-shadow: 0px 10rpx 20rpx 0px rgba(4, 114, 69, 0.22);
			border-radius: 30rpx;

			.search {
				width: 34rpx;
				height: 34rpx;
			}

			.search-font {
				margin-left: 14rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #CBCBCB;
			}
		}
	}
	// 顶部轮播图
	.top-swiper {
		width: 750rpx;
		height: 360rpx;
		// margin: 20rpx 0 0;
		image {
			width: 750rpx;
			height: 360rpx;
		}
	}
	.swiper-btm {
		height: 60rpx;
		width: 750rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		font-size: 21rpx;
		font-weight: 500;
		color: #333333;
		.btm-item {
			flex-grow: 1;
			justify-content: center;
			image {
				width: 25rpx;
				height: 25rpx;
				margin-right: 14rpx;
			}
		}
	}
	.jg {
		height: 20rpx;
		background-color: #fff;
	}
	.yuyue-wrap {
		background-color: #fff;
		.yuyue-top {
			height: 117rpx;
			justify-content: center;
			image {
				display: inline-block;
				height: 31rpx;
				margin: auto;
			}
		}
		.yuyue-item {
			width: 690rpx;
			margin: auto;
			
			.item-top {
				width: 690rpx;
				height: 80rpx;
				border-radius: 18rpx;
				// background-color: #bfa;
				position: relative;
				image {
					width: 690rpx;
					height: 80rpx;
					// position: relative;
				}
				.item-top-info {
					width: 690rpx;
					height: 80rpx;
					position: absolute;
					top: 0;
					left: 0;
					font-size: 33rpx;
					font-weight: bold;
					color: #C6914E;
					padding-left: 35rpx;
					padding-right: 25rpx;
					text {
						font-size: 22rpx;
						font-weight: 500;
						color: #C6914E;
					}
					.goto {
						width: 60rpx;
						line-height: 26rpx;
						background: #C6914E;
						border-radius: 12rpx;
						font-size: 18rpx;
						font-weight: 500;
						color: #FFFFFF;
						text-align: center;
					}
				}
			}
			.item-info {
				width: 690rpx;
				height: 252rpx;
				margin-top: -20rpx;
				background: #FFFFFF;
				box-shadow: 0px 0px 20rpx 0px rgba(50, 50, 52, 0.06);
				border-radius: 0px 0px 20rpx 20rpx;
				padding: 0 25rpx;
				margin-bottom: 20rpx;
				.item-img {
					width: 134rpx;
					height: 133rpx;
					background-color: red;
					flex-shrink: 0;
					image {
						width: 134rpx;
						height: 133rpx;
					}
				}
				.item-val {
					flex-grow: 1;
					height: 100%;
					// background-color: #009688;
					position: relative;
					flex-direction: column;
					align-items: flex-start;
					padding-left: 25rpx;
					justify-content: center;
					.item-djs {
						margin: 20rpx 0;
						height: 42rpx;
					}
					.item-status {
						font-size: 26rpx;
						font-weight: bold;
						color: #333333;
					}
					.item-base {
						font-size: 25rpx;
						font-weight: 500;
						color: #666666;
					}
					.item-btn {
						width: 159rpx;
						height: 59rpx;
						line-height: 59rpx;
						background: #CCCCCC;
						border-radius: 10rpx;
						position: absolute;
						right: 0;
						top: 0;
						bottom: 0;
						margin: auto;
						text-align: center;
					}
					.yuyue {
						background-color: #303030;
						color: #F8DABA;
					}
				}
				
			}
		}
	}
	.popupyy-wrap {
		width: 750rpx;
		height: 745rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0px 0px;
		text-align: center;
		font-size: 32rpx;
		color: #999999;
		font-weight: 500;
		padding-top: 60rpx;
		.popupyy-top {
			font-size: 36rpx;
			font-weight: 500;
			color: #333333;
			position: relative;
			&::before {
				content: '';
				width: 138rpx;
				height: 1rpx;
				background: #CBCBCB;
				position: absolute;
				top: 0;
				bottom: 0;
				left: 124rpx;
				margin: auto;
			}
			&::after {
				content: '';
				width: 138rpx;
				height: 1rpx;
				background: #CBCBCB;
				position: absolute;
				top: 0;
				bottom: 0;
				right: 124rpx;
				margin: auto;
			}
		}
		.popupyy-tit {
			font-size: 32rpx;
			font-weight: 500;
			color: #999999;
			margin-top: 70rpx;
		}
		.popupyy-val {
			font-size: 67rpx;
			font-weight: bold;
			color: #333333;
			margin-top: 60rpx;
		}
		.popupyy-btn {
			width: 680rpx;
			line-height: 100rpx;
			background: #303030;
			border-radius: 10rpx;
			font-size: 34rpx;
			font-weight: 500;
			color: #F8DABA;
			margin: 20rpx auto 0;
		}
	}
	.popupyyok-wrap {
		height: 707rpx;
		width: 551rpx;
		background-color: #fff;
		border-radius: 20rpx;
		text-align: center;
		image {
			width: 100%;
		}
		.btn {
			width: 295rpx;
			line-height: 69rpx;
			border-radius: 34rpx;
			background: #303030;
			color: #F8DABA;
			font-size: 35rpx;
			color: #F8DABA;
			margin: auto;
		}
	}
</style>
