<template>
	<view class="container">
		<view class="top">
			<!-- <image class="top-bg" src="../../static/img/top-bg.png" mode=""></image> -->
			<view class="top-main flex">
				<view class="search-box flex" @click="clickSearch()">
					<image class="search" src="../../static/img/search-h.png" mode=""></image>
					<view class="search-font">输入关键词搜索</view>
				</view>
			</view>
			<swiper class="carousel" 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 class="img" :src="item.pic" mode="scaleToFill" />
				</swiper-item>
			</swiper>
		</view>
		<view class="box">
			<view class="box-title">
				<view class="left flex">
					<view class="item" @click="changeList(0)" :class="{action:checked==0}">
						艺绘发售
					</view>
					<view class="item margin-l-30" @click="changeList(1)" :class="{action:checked==1}">
						盲盒发售
					</view>
				</view>
			</view>
			<view v-show="checked==0" class="box-content" v-for="(item,index) in dataList[0].list" @click="buy(item)">
				<view class="img">
					<image :src="item.image" mode=""></image>
				</view>

				<view class="content-title">
					<view class="title">
						{{item.store_name}}
					</view>
					<view>
						<view class="text">
							艺绘官方 发行
						</view>
						<view class="text flex">
							<view class="flex">
								<view class="textStock border-radius-10 flex">
									<view class="tip">
										限量
									</view>
									<view class="num">
										{{item.stock}}
									</view>
								</view>
								<view class="tip border-radius-10 margin-l-10">
									限购{{item.buy_limit}}份
								</view>
							</view>
							<view class="money">
								￥{{item.price}}
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore v-show="checked==0" :status="dataList[0].list.loadType" />
			<view v-show="checked==1" class="box-content" v-for="(item,index) in dataList[1].list" @click="buyMh(item)">
				<view class="img">
					<image :src="item.pic" mode=""></image>
				</view>
				<view class="content-title">
					<view class="title">
						{{item.name}}
					</view>
					<view class="text">
						艺绘官方 发行
					</view>
					<view class="text">
						￥{{item.price}}
					</view>
				</view>
			</view>
			<u-loadmore v-show="checked==1" :status="dataList[1].list.loadType" />
		</view>
	</view>
</template>

<script>
	import {
		getProducts,
		mysteryList
	} from '@/api/product.js';
	import {
		loadIndexs
	} from '@/api/index.js'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				checked: 0, //当前选中的出售对象
				isSc: 2,
				carouselList: [], //轮播列表
				dataList: [{
					list: [], //艺发售
					page: 1,
					limit: 1,
					loadType: 'loadmore',
				}, {
					list: [], //盲盒发售
					page: 1,
					limit: 1,
					loadType: 'loadmore',
				}]

			};
		},
		computed: {
			...mapState(['loginInterceptor', 'baseURL']),
			...mapState('user', ['hasLogin', 'userInfo'])
		},
		onLoad: function(option) {
			this.loadIndex()
		},
		onShow: function() {
			this.getProduct()
		},
		onReachBottom() {
			this.getProduct()
		},
		methods: {
			...mapMutations(['setLat', 'setLon']),
			loadIndex() {
				let obj = this
				loadIndexs().then(res => {
					obj.carouselList = res.data.banner
				})
			},
			// 切换选中的需要显示的列表数据
			changeList(ind) {
				// 判断是否重复点击
				if (this.checked == ind) {
					return
				}
				this.checked = ind;
				this.getProduct()
			},
			// 更新数据
			getProduct() {
				let obj = this;
				let item = obj.dataList[obj.checked];
				if (item.loadType === 'loading') {
					//防止重复加载
					return;
				}
				if (item.loadType === 'nomore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				item.loadType = 'loading';
				// 判断是否为艺绘商品
				if (obj.checked == 0) {
					this.getBaseYh(item)
				}
				// 判断是否盲盒
				if (obj.checked == 1) {
					this.mysteryList(item)
				}

			},
			// 盲盒列表
			mysteryList(item) {
				mysteryList({
					page: item.page,
					limit: item.limit
				}).then(res => {
					let arr = res.data.list.map((e) => {
						return e
					})
					item.list = item.list.concat(arr);
					item.page++;
					if (item.limit == arr.length) {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						item.loadType = 'loadmore';
						return;
					} else {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						item.loadType = 'nomore';
					}
				})
			},
			// 获取艺商品
			getBaseYh(item) {
				getProducts({
					page: item.page,
					limit: item.limit
				}).then(res => {
					let arr = res.data.map((e) => {
						let time = new Date(e.sell_time * 1000);
						e.cmy_pay_time = time.getFullYear() + '年' + (time.getMonth() + 1) + '月' + time
							.getDate() + '日' + time.getHours() + '时' + time.getMinutes() + '分' + time
							.getSeconds() + '秒'
						return e
					})
					item.list = item.list.concat(arr);
					item.page++;

					if (item.limit == arr.length) {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						item.loadType = 'loadmore';
						return;
					} else {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						item.loadType = 'nomore';
					}
				})
			},
			// 普通商品
			buy(item) {
				console.log(item, 'res');
				uni.navigateTo({
					url: '/pages/product/product?id=' + item.id + '&isSc=' + this.isSc
				})
			},
			// 盲盒
			buyMh(item) {
				uni.navigateTo({
					url: '/pages/product/productMh?id=' + item.id
				})
			},
			navTo(url) {
				uni.switchTab({
					url
				})
			},
			// 點擊搜索框
			clickSearch() {
				uni.navigateTo({
					url: '/pages/product/search'
				});
			},
			swiperChange() {}



		}
	};
</script>

<style lang="scss">
	page {
		width: 750rpx;
		min-height: 100%;
		// background: #111111;
	}

	.carousel {
		width: 750rpx;
		height: 375rpx;

		.carousel-item {
			height: 100%;
			width: 100%;

			.img {
				height: 100%;
				width: 100%;
			}
		}
	}

	.top {
		position: relative;
		width: 100%;

		.top-bg {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			width: 100%;
			height: 100%;
		}

		.top-main {
			position: relative;
			z-index: 2;
			padding: 30rpx;

			.search-box {
				justify-content: flex-start;
				width: 698rpx;
				height: 60rpx;
				background: #1d1c21;
				border-radius: 10rpx;
				padding-left: 20rpx;

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

				.search-font {
					margin-left: 14rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #a4a4a4;
				}
			}
		}
	}

	.box {
		padding: 30rpx;

		.box-title {
			margin-top: 20rpx;
			display: flex;
			justify-content: space-between;
			margin-bottom: 50rpx;

			.left {
				font-size: 34rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;

				.action {
					color: #FDD58A;
				}
			}

			.right {

				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 120rpx;
			}

		}

		.box-content {
			margin-bottom: 50rpx;
			background-color: rgb(15, 15, 15);
			border-radius: 20rpx;
			overflow: hidden;

			.img {
				width: 690rpx;
				height: 690rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.content-title {
				padding: 30rpx 20rpx;
				border-bottom-left-radius: 10rpx;
				border-bottom-right-radius: 10rpx;
				background-color: rgb(29, 28, 33);

				.title {
					font-size: 39rpx;
					font-weight: bold;
					color: #FFFFFF;
					margin-bottom: 20rpx;

				}

				.text {
					margin: 10rpx 0;
					color: #FFFFFF;
					font-size: 28rpx;
					flex-grow: 1;
					.textStock {
						overflow: hidden;
					}

					.num {
						font-size: 18rpx;
						padding: 10rpx;
						color: #9F570E;
						background-color: #4D3414;
					}
					.money{
						font-size: 40rpx;
					}
					.tip {
						font-size: 20rpx;
						color: #9F570E;
						padding: 10rpx;
						line-height: 1;
						background: $bgBaseBg;
					}
				}
			}
		}
	}

	.popup-box {
		width: 522rpx;
		height: 605rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		position: relative;

		.img {
			position: relative;
			top: -56rpx;
			left: 0;
			width: 522rpx;
			height: 132rpx;
			display: flex;
			justify-content: center;

			image {
				border-radius: 20rpx 20rpx 0 0;
				width: 450rpx;
				height: 132rpx;
			}
		}

		.mian {
			margin-top: -44rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			// padding: 32rpx 32rpx;
			background-color: #ffffff;
			border-radius: 0 0 20rpx 20rpx;
			text-align: center;

			.delivery {
				font-size: 40rpx;
				color: #333333;
				display: flex;
				align-items: center;
				flex-direction: column;

				.title {}

				image {
					margin-top: 48rpx;
					width: 172rpx;
					height: 160rpx;
				}
			}

			.nocancel {
				font-size: 32rpx;
				color: #333333;
				margin-top: 14rpx;
			}

			.comfirm-box {
				margin-top: 52rpx;
				display: flex;
				// margin-bottom: 32rpx;

				// justify-content: space-around;
				.cancel {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 197rpx;
					height: 74rpx;
					border: 1px solid #dcc786;
					border-radius: 38rpx;

					font-size: 32rpx;
					color: #605128;
				}

				.comfirm {
					margin-left: 32rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 197rpx;
					height: 74rpx;
					background: linear-gradient(-90deg, #d1ba77 0%, #f7e8ad 100%);
					border-radius: 38px;
					font-size: 32rpx;
					color: #605128;
				}
			}
		}
	}

	.tongz {
		width: 690rpx;
		height: 70rpx;
		margin: 32rpx auto 0;
		padding: 18rpx 30rpx 18rpx 24rpx;
		align-items: center;
		position: relative;

		.tongz-bg {
			position: absolute;
			top: 0;
			right: 0;
			left: 0;
			width: 690rpx;
			height: 70rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.tongz-left {
			width: 640rpx;

			.image-left {
				width: 28rpx;
				height: 34rpx;
			}

			.tongz-font {
				margin-left: 22rpx;
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #0f253a;
			}
		}

		.tongz-right {
			position: relative;
			z-index: 11;
			width: 12rpx;
			height: 26rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
