<template>
	<view class="u-wrap">
		<view class="u-search-box" @click="goSearch">
			<view class="u-search-inner">
				<u-icon name="search" color="#909399" :size="28"></u-icon>
				<text class="u-search-text">搜索</text>
			</view>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
					:class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box"
				@scroll="rightScroll">
				<view class="page-view">
					<view class="class-item">
						<view class="item-box" v-for="(item1, index1) in tabbarList.children" :key="index1">
							<view class="item-title">
								<text>{{item1.name}}</text>
							</view>
							<view class="item-container">
								<view class="thumb-box" @click="goodsDetail(item2)"
									v-for="(item2, index2) in item1.children" :key="index2">
									<image class="item-menu-image" :src="item2.image?item2.image.external_url:''"
										mode=""></image>
									<view class="item-menu-name">{{item2.name}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<addShuiyin />
	</view>
</template>
<script>
	import * as cstegory from '@/api/category'
	// import classifyData from '@/common/classify.data.js';
	export default {
		data() {
			return {
				current: 0, // 预设当前项的值
				tabbar: [],
				tabbarList: [],
				scrollRightTop: 0,
				oldScrollTop: 0,
			}
		},
		onLoad() {
			this.getGoodsTypeList();
		},
		onReady() {
			this.getMenuItemTop()
		},
		methods: {
			goodsDetail(item) {
				console.log(item)
				// 跳转商品详情
				uni.navigateTo({
					url: '/pages/goods/list?categoryId=' + item.category_id
				})
			},
			goSearch() {
				uni.navigateTo({
					url: '/pages/search/index'
				})
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				this.tabbarList = []
				this.current = index;
				this.scrollRightTop = this.oldScrollTop;
				this.tabbarList = this.tabbar[index];
				this.$nextTick(function() {
					this.getMenuItemTop()
				})

			},
			getGoodsTypeList() {
				const that = this;
				return new Promise((resolve, reject) => {
					cstegory.list({
							// is_hot: 0
						})
						.then(res => {
							// 合并新数据
							console.log(res, '分类数据')
							that.tabbar = res.data.list;
							that.tabbarList = res.data.list[0]
						})
						.catch(reject)
				})
			},
			// 右边菜单滚动
			async rightScroll(e) {
				this.oldScrollTop = e.detail.scrollTop;
			},
			// 获取右边菜单每个item到顶部的距离
			getMenuItemTop() {
				let that = this
				new Promise(resolve => {
					let selectorQuery = uni.createSelectorQuery();
					selectorQuery.selectAll('.class-item').boundingClientRect((rects) => {
						rects.forEach((rect) => {
							that.scrollRightTop = rect.top - rects[0].top
							resolve();
						})
					}).exec()
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: #ffffff;
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 14rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #F21A1C;
		font-weight: 600;
		// background: #fff;
	}

	// .u-tab-item-active::before {
	// 	content: "";
	// 	position: absolute;
	// 	border-left: 4px solid #F21A1C;
	// 	height: 32rpx;
	// 	left: 0;
	// 	top: 39rpx;
	// }

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 0 16rpx 16rpx;
		overflow: hidden;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 20rpx;
		border-radius: 8rpx;
	}

	.class-item:last-child {
		min-height: 100vh;
	}

	.item-box {
		margin-top: 30rpx;

		&:first-child {
			margin-top: 0;
		}
	}

	.item-title {
		font-size: 28rpx;
		color: $u-main-color;
		font-weight: bold;
		padding: 10rpx;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 28rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
		margin-bottom: 10rpx;
	}
</style>
