<template>
	<view :style="colorStyle">
		<view class='flash-sale'>
			<view class='header'>
				<img mode="widthFix" class="presellBg" :src="picUrl" alt="">
			</view>
			<view class="main-count">
				<view class="nav acea-row row-around row-middle">
					<view v-for="(item,index) in timeList" :key='index' @tap='settimeList(item.key)' class='item' :class="active == index + 1?'on':''">
						{{item.name}}
					</view>
				</view>
				<view class='list' v-if="presellList.length">
					<block v-for="(item,index) in presellList" :key='index'>
						<view class='item acea-row row-between-wrapper' @tap='goDetails(item)'>
							<view class='pictrue'>
								<image :src='item.image'></image>
							</view>
							<view class='text'>
								<view class='name line1'>{{item.store_name}}</view>
								<view class='booking'>
									<text v-if="active != 1">已预定{{item.sales ? item.sales : 0}}{{item.unit_name}}</text>
									<text v-else>暂未开始</text>
								</view>
								<view class="acea-row" v-if="item.coupon && item.coupon.constructor !== Array">
									<view class="coupon">
										满{{item.coupon.use_min_price}}减{{item.coupon.coupon_price}}
									</view>
								</view>
								<view class="bottom">
									<view class="progress">
										<view class='presell-price'>
											<text class="presell-text">预售价</text>
											<text class="price">¥ <text>{{ item.price }}</text></text>
										</view>
										<text class="iconfont icon-yushouanniu"></text>
										<view v-if="active != 1" class='order-btn'>{{ active === 2  ? '立即预定' : '已结束' }}
										</view>
										<view v-else class="unStartBtn">
											<text>开售时间</text>
											<view>{{ item.presale_start_time | dateFormat }}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</block>
				</view>
				<view class='loadingicon acea-row row-center-wrapper' v-if="presellList.length">
					<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
				</view>
				<view class='noCommodity' v-if="presellList.length==0 && page > 1">
					<view class='emptyBox'>
						<image :src="imgHost + '/statics/images/no-thing.png'"></image>
						<view class="tips">暂无商品，去看点别的吧</view>
					</view>
				</view>
			</view>
		</view>
		<home v-if="navigation"></home>
	</view>
</template>

<script>
	import {
		getAdvancellList
	} from '@/api/activity';
	import dayjs from '@/plugin/dayjs/dayjs.min.js';
	import home from '@/components/home/index.vue'
	import colors from "@/mixins/color";
	import {HTTP_REQUEST_URL} from '@/config/app';
	export default {
		components: {
			home
		},
		filters: {
			dateFormat: function(value) {
				return dayjs(value * 1000).format('MM/DD HH:mm');
			}
		},
		mixins: [colors],
		data() {
			return {
				presellList: [],
				timeList: [{
					name: '未开始',
					key: 1
				}, {
					name: '正在进行',
					key: 2
				}, {
					name: '已结束',
					key: 3
				}, ],
				active: 2,
				page: 1,
				limit: 8,
				loading: false,
				loadend: false,
				loadTitle: '加载更多',
				picUrl: '', //头部图片
				imgHost:HTTP_REQUEST_URL
			}
		},
		onLoad() {
			this.getPresellProductList();
			this.colorData();
			let picList = [
				'data:image/jpg;base64,[encoded data, 71332 characters]',
				'data:image/jpg;base64,[encoded data, 63904 characters]',
				'data:image/jpg;base64,[encoded data, 61548 characters]',
				'data:image/jpg;base64,[encoded data, 61192 characters]',
				'data:image/jpg;base64,[encoded data, 59356 characters]',
				'data:image/png;base64,[encoded data, 119100 characters]'
			]
			setTimeout(()=>{
				switch (this.colorNum) {
					case 1:
						this.picUrl = picList[0]
						break;
					case 2:
						this.picUrl = picList[1]
						break;
					case 3:
						this.picUrl = picList[2]
						break;
					case 4:
						this.picUrl = picList[3]
						break;
					case 5:
						this.picUrl = picList[4]
						break;
					case 6:
						this.picUrl = picList[5]
						break;	
					default:
						this.picUrl = picList[2]
						break
				}
			},1)
		},
		onShow(){
			uni.removeStorageSync('form_type_cart');
		},
		methods: {
			getPresellProductList: function() {
				let that = this;
				let data = {
					page: that.page,
					limit: that.limit,
					time_type: that.active
				};
				if (that.loading) return;
				if (that.loadend) return;
				that.loading = true;
				that.loadTitle = '';
				getAdvancellList(data).then(res => {
					let list = res.data.list;
					let loadend = list.length < that.limit;
					that.presellList = that.$util.SplitArray(list, that.presellList);
					that.$set(that, 'presellList', that.presellList);
					that.loadend = loadend;
					that.loadTitle = loadend ? '没有更多内容啦~' : '加载更多';
					that.page = that.page + 1;
					that.loading = false;
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			},
			settimeList(key) {
				let that = this;
				that.active = key;
				that.loadend = false;
				that.page = 1;
				that.presellList = [];
				that.getPresellProductList();
			},
			goDetails(item) {
				uni.navigateTo({
					url: '/pages/goods_details/index?id=' + item.id
				})
			}
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.getPresellProductList();
		}
	}
</script>

<style lang="scss">
	page {
		background-color: var(--view-theme);
	}
	
	.loadingicon{
		color: #fff;
		.loading{
			color: #fff;
		}
	}

	.noCommodity {
		background-color: #fff;
		width: 690rpx;
		height: 600rpx;
		border-radius: 16rpx;
		margin: 16rpx auto 0 auto;
		.emptyBox{
			text-align: center;
			margin-top: 40rpx;
			.tips{
				color: #aaa;
				font-size: 26rpx;
			}
			image {
				width: 414rpx;
				height: 304rpx;
			}
		}
	}

	.flash-sale {
		height: 100vh;
		background-color: var(--view-theme);
	}

	.flash-sale .header {
		width: 100%;
	}

	.flash-sale .main-count {
		position: relative;
		margin-top: -150rpx;
        background-color: var(--view-theme);
	}

	.flash-sale .header .presellBg {
		width: 100%;
	}

	.flash-sale .nav {
		margin: 0 30rpx;
		width: 690rpx;
		background-color: #fff;
		border-radius: 16rpx;
		line-height: 80rpx;
		height: 80rpx;
		.item{
			position: relative;
			font-size: 28rpx;
			color: #AAAAAA;
			&.on{
				color: var(--view-theme);
				&::after {
					display: inline-block;
					width: 100%;
					height: 4rpx;
					background: var(--view-theme);
					position: absolute;
					left: 0;
					bottom: 10rpx;
					border-radius: 2rpx;
					content: '';
				}
			}
		}

	}

	.flash-sale .list {
		margin-top: 24rpx;
	}

	.flash-sale .list .item {
		width: 690rpx;
		margin: 0 auto 20rpx auto;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx 25rpx;
	}

	.flash-sale .list .item .pictrue {
		width: 230rpx;
		height: 230rpx;
		border-radius: 10rpx;
	}

	.flash-sale .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.flash-sale .list .item .text {
		width: 380rpx;
		height: 230rpx;
		font-size: 30rpx;
		color: #333;
		position: relative;
	}
	
	.flash-sale .list .item .text .coupon {
		height: 40rpx;
		padding: 0 20rpx;
		color: var(--view-theme);
		background-color: var(--view-minorColorT);
		font-size: 24rpx;
		line-height: 40rpx;
		position: relative;
		margin-top: 6rpx;
	}
	
	.flash-sale .list .item .text .coupon:before {
		content: ' ';
		position: absolute;
		width: 7rpx;
		height: 10rpx;
		border-radius: 0 7rpx 7rpx 0;
		background-color: #fff;
		bottom: 50%;
		left: -3rpx;
		margin-bottom: -6rpx;
		border-left-color: #fff
	}
	
	.flash-sale .list .item .text .coupon:after {
		content: ' ';
		position: absolute;
		width: 7rpx;
		height: 10rpx;
		border-radius: 7rpx 0 0 7rpx;
		background-color: #fff;
		right: -3rpx;
		bottom: 50%;
		margin-bottom: -6rpx;
		border-right-color: #fff
	}

	.flash-sale .list .item .text .name {
		width: 100%;
		color: #282828;
		font-weight: bold;
		font-size: 28rpx;
	}

	.flash-sale .list .item .text .booking {
		font-size: 24rpx;
		color: #999;
		margin-top: 4rpx;
	}
	
	.flash-sale .list .item .text .bottom{
		position: absolute;
		bottom: 0;
		left:0;
	}

	.flash-sale .list .item .text .progress {
		width: 392rpx;
		height: 80rpx;
		overflow: hidden;
		position: relative;
		display: flex;
		align-items: center;
		.icon-yushouanniu{
			position: absolute;
			right: 82rpx;
			width: 44%;
			font-size: 92rpx;
			line-height: 80rpx;
			height: 80rpx;
			z-index: 98;
			color: var(--view-theme);
		}
		.presell-price {
			float: left;
			width: 55%;
			text-align: center;
			line-height: 15px;
			padding: 8rpx 0;
			background-image: url(~pages/activity/static/preBtnLeft.png);
			background-size: 100%;
			z-index: 99;
			height: 80rpx;
			position: absolute;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			.presell-text {
				display: block;
				color: #E93323;
				font-size: 20rpx;
				margin-bottom: 5rpx;
			}

			.price {
				font-size: 26rpx;
				color: #E93323;

				text {
					font-weight: bold;
				}
			}
		}

		.order-btn {
			width: 50%;
			text-align: center;
			color: #FFFFFF;
			font-size: 26rpx;
			line-height: 80rpx;
			z-index: 999;
			position: absolute;
			right: 8rpx;
		}

		.unStartBtn {
			position: absolute;
			right: 8rpx;
			width: 50%;
			text-align: center;
			color: #FFFFFF;
			font-size: 20rpx;
			z-index: 999;
			padding: 4rpx 0;
			text {
				font-size: 22rpx;
			}
		}
	}
</style>
