<!-- 首页 -->
<template>
	<view>
		<!-- 搜索框 -->
		<view class="search_box">
			<view class="search_box_border">
				<u-input placeholder="搜索优质产品" placeholder-style="color: #969696" prefixIcon="search"
					prefixIconStyle="font-size: 22px;color: #909399;margin-left:30rpx" readonly @tap="toSearch()">
				</u-input>
			</view>
		</view>
		<view class="content">
			<!-- 分类模块 -->
			<view class="classify">
				<u-grid :border="false" col="5">
					<u-grid-item v-for="(listItem,listIndex) in classifyList" :key="listIndex"
						@click="toClassify(listItem.id)">
						<u--image :src="imgUrl+listItem.icon" width="86rpx" height="86rpx" :lazy-load="true">
						</u--image>
						<!-- <u-icon size="86rpx" :name="listItem.icon"></u-icon> -->
						<text class="grid-text">{{listItem.name}}</text>
					</u-grid-item>
				</u-grid>
			</view>
			<!-- 分类模块下的轮播图 -->
			<view>
				<u-swiper :list="swiperList" keyName="img" indicatorMode="line" radius="0" circular
					@change="e => currentNum = e.current"
					@click="handleSwiperList(currentNum==''?0:currentNum,swiperList)"></u-swiper>
			</view>

			<!-- 选项卡: 上架时间和点赞量 -->
			<view>
				<u-tabs :list="tabList" lineColor="#15CA65" scrollable="false"
					activeStyle="color:#15CA65;font-weight: bold;" :current="tabCurrent" @change="tabChange">
				</u-tabs>
			</view>
			<!-- 产品 -->
			<productsByTime v-if="tabCurrent==0 && initStart" ref="productsByTime" @getTimeLength="getTimeLength">
			</productsByTime>
			<productsByThumb v-if="tabCurrent==1 && initStart" ref="productsByThumb" @getThumbLength="getThumbLength">
			</productsByThumb>
			<view style="font-size: 24rpx;color: #A3A3A3;text-align: center;padding: 26rpx 0;"
				v-show="reachBottomLength===0">
				没有更多数据了</view>
		</view>
	</view>
</template>
<script>
	import {
		serverHost
	} from '@/service/request.js'
	export default {
		data() {
			return {
				classifyList: [],
				tabCurrent: 0,
				tabList: [{
						name: '上架时间'
					},
					{
						name: '点赞量'
					},
				],
				imgUrl: '',
				initStart: false,
				swiperList: [],
				reachBottomLength: '',
				currentNum: ''
			}
		},
		onReachBottom() {
			if (this.tabCurrent == 0 && this.reachBottomLength !== 0) {
				this.$refs.productsByTime.pageNum++
				this.$refs.productsByTime.getProductsByTime()
			}
			if (this.tabCurrent == 1 && this.reachBottomLength !== 0) {
				this.$refs.productsByThumb.pageNum++
				this.$refs.productsByThumb.getProductsByThumb()
			}
		},
		created() {
			this.getImgUrl()
		},
		methods: {
			init() {
				this.imgUrl = uni.getStorageSync('img_url')
				this.getCategories()
				this.getBanner()
				wx.showShareMenu({
					withShareTicket: true,
					//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
					menus: ["shareAppMessage", "shareTimeline"]
				})
			},
			//分享到微信好友
			onShareAppMessage(res) {
				return {
					title: '食瞳',
					type: 0,
					path: '/pages/index/index?id=' + distSource,
					summary: "",
					imageUrl: "https://pupil.hchyun.com/uploads/product/zhanwei02.png"
				}
			},
			//分享到朋友圈
			onShareTimeline(res) {
				return {
					title: '食瞳',
					type: 0,
					query: 'id=' + distSource,
					summary: "",
					imageUrl: "https://pupil.hchyun.com/uploads/product/zhanwei02.png"
				}
			},

			//获取按上架时间排列的产品列表length
			getTimeLength(e) {
				this.reachBottomLength = e
			},
			//获取按点赞量排列的产品列表length
			getThumbLength(e) {
				this.reachBottomLength = e
			},
			//点击上架时间和点赞量切换页面
			tabChange(data) {
				this.tabCurrent = data.index
				if (data.index == 0) {
					this.$refs.productsByTime.getProductsByTime()
				} else if (data.index == 1) {
					this.$refs.productsByThumb.getProductsByThumb()
				}
			},
			//获取一级分类
			getCategories() {
				this.$apiServe.getCategories().then(res => {
					this.classifyList = res.data.data
				}).finally(_ => {})
			},
			//获取首页轮播图
			getBanner() {
				this.$apiServe.getBanner().then(res => {
					let banner = res.data.data
					if (banner) {
						for (const item of banner) {
							item.img = this.imgUrl + item.img
						}
						this.swiperList = res.data.data
					}
				}).finally(_ => {})
			},
			//点击轮播图
			handleSwiperList(currentNum, swiperList) {
				swiperList[currentNum].link = swiperList[currentNum].link.replace(/\s*/g, "")
				uni.navigateTo({
					url: '/' + swiperList[currentNum].link
				})
			},
			//点击搜索框跳转到搜索页面
			toSearch() {
				uni.navigateTo({
					url: '../../packageSearch/search/search'
				})
			},
			//点击一级分类跳转到分类页
			toClassify(id) {
				uni.navigateTo({
					url: '../../packageSearch/goods-category-search/category-index?categoryId=' + id
				})
			},
			//获取图片url
			getImgUrl() {
				this.$apiServe.getImgUrl().then(res => {
					try {
						uni.setStorageSync('img_url', res.data.data.img_url);
						this.initStart = true
						this.init()
					} catch (e) {
						// error
					}
				}).finally(_ => {

				})
			}
		}
	}
</script>
<style lang="scss">
	// 搜索框样式
	.search_box {
		background: linear-gradient(135deg, #12CA64 0%, #1ACA67 100%);

		.search_box_border {
			width: 94.6%;
			height: 124rpx;
			background: linear-gradient(320deg, #36E182 0%, #26DD76 100%, #26DD76 100%);
			margin: 0 auto;
			box-shadow: 0 -1px 3px 0 #31B96D;
			border-radius: 38rpx 38rpx 0 0;

			.u-border {
				border: none;
				padding-top: 16px !important;

				.u-input__content {
					height: 64rpx;
					border-radius: 32rpx;
					background-color: #fff;

					.u-input__content__field-wrapper__field {
						font-size: 26rpx !important;
					}
				}
			}
		}
	}

	.content {

		//分类模块
		.classify {
			height: 350rpx;
			background: #FFFFFF;
			border-radius: 24rpx;

			box-shadow: 0px 2px 4px 0px rgba(206, 206, 206, 0.5);

			.u-grid {
				padding-top: 30rpx;

				.grid-text {
					padding-top: 8rpx;
					font-size: 24rpx;
				}

				.u-grid-item {
					padding-bottom: 38rpx;
				}
			}
		}

		//轮播图
		.u-swiper__wrapper__item__wrapper {
			margin-top: 20rpx;
			height: 290rpx;
		}

		.u-swiper-indicator__wrapper {
			display: none !important;
		}

		// 选项卡
		.u-tabs__wrapper__nav {
			background-color: #FFFFFF;

			.u-tabs__wrapper__nav__item {
				padding: 0 !important;
				flex: 1;
			}
		}


	}
</style>
