<template>
	<view class="page-wrapper">
		<view class="topBar" ref="top" :style="{ height: navHeight + 'px' }"></view>
		<view class="mp-search-box" :style="{
			  top: (customTop+2) + 'px',
			  height: customHeight + 'px',}"
		>
			<view class="wrapper" :style="{
			  height: (customHeight-4) + 'px',}"
		>
				<u-icon style="float: left;width:20px;" name="search" color="#A1A1A1" size="20"></u-icon>
				<input class="ser-input" type="text" placeholder="搜索工具ID、关键字" :style="{
					  height: (customHeight-6) + 'px',}"
				/>
			</view>
		</view>
		<view class="cateWrapper">
			<cate-list></cate-list>
		</view>
		
		 <view class="tabWrapper">
		 	<u-tabs :list="list1" @click="click"></u-tabs>
		 	<view class="line" style="top:84rpx;"></view>
		 </view>
		 
		<list-item></list-item>
	</view>
</template>

<script>
	import cateList from '@/components/cate-list.vue';
	import listItem from '@/components/list-item.vue';
	import { customTabar } from "@/common/js/mixins.js";
	export default {
		mixins: [customTabar],
		components: {
			listItem,
			cateList
		},
		data() {
			return {
				currentId: 1,
				list1: [{
					name: '全部',
				}, {
					name: '最新',
				}],
			}
		},
		onLoad(){
			this.loadData();
		},
		methods: {
			async loadData(){
				let list = await this.$api.json('cateList');
				list.forEach(item=>{
					if(!item.pid){
						this.flist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
					}else if(!item.picture){
						this.slist.push(item); //没有图的是2级分类
					}else{
						this.tlist.push(item); //3级分类
					}
				}) 
			},
			//一级分类点击
			tabtap(item){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				
				this.currentId = item.id;
				let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid){
				uni.navigateTo({
					url: `/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
				})
			}
		}
	}
</script>

<style lang='scss' scoped>
	.page-wrapper{
		height: 100%;
		padding-top: 150upx;
		background-color: #f8f8f8;
	}
	.topBar{
		@include topBar($height:160rpx);
	}
	.mp-search-box{
		width: 100%;
		/* padding: 20rpx 20rpx; */
		height: 120rpx;
		position: fixed;
		top: 62upx;
		left: 8rpx;
		z-index: 501;
		.wrapper{
			display: flex;
			width: 64vw;
			height: 60rpx;
			padding-left: 12rpx;
			background-color: #fff;	
			border: 4rpx solid #1f1f1f;
			border-radius: 40rpx;
			.ser-input{
				flex:1;
				height: 56rpx;
				line-height: 56rpx;
				text-align: left;
				font-size: 28rpx;
				color:$font-color-base;
				border-radius: 20px;
				background: rgba(255,255,255,.6);
			}
		}
		
	}
	
	::v-deep .u-tabs__wrapper__nav{
		background-color: #fff;
	}

	
</style>
