<template>
	<view class="content">
		<view class="" style="height: 20rpx;"></view>
		<template v-for="item in flist">
			<view class="cate-item"  v-if="item.pic" @click="navto('/pages/product/list?fid='+item.id)">
				<image :src="item.pic" mode="widthFix"></image>
			</view>
		</template>
	</view>
</template>
<script>
	import { getCategoryList,getProducts } from '@/api/product.js';
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 9,
				flist: [],
				height: '',
				currentIndex: 0,
				list: [],
				page: 1,
				limit: 20,
				loadingType: 'more',
				loaded: false,
			};
		},
		onLoad() {
			this.loadData();
		},
		// 监听导航栏输入框点击事件
		onNavigationBarSearchInputClicked(e) {
			uni.navigateTo({
				url: '/pages/product/search'
			});
		},
		onReady(res) {
			
		},
		methods: {
			leftClick(index) {
				this.currentIndex = index
				this.getProducts('reload')
			},
			getProducts(type) {
				let index = this.flist[this.currentIndex].id
				if(type == 'reload') {
					this.list = []
					this.page = 1
					this.loadingType = 'more'
					this.loaded = false
				}
				if(this.loadingType == 'loading' ) {
					return
				}
				if( this.loadingType == 'noMore') {
					
					if(this.currentIndex < (this.flist.length - 1)) {
						console.log(this.currentIndex)
						this.currentIndex++
						this.list = []
						this.page = 1
						this.loadingType = 'more'
						this.loaded = false
						this.getProducts()
					}else {
						console.log('ddd')
					}
					
				}
				this.loadingType = 'loading'
				getProducts({
					cid: index
				}).then(res => {
					this.list = this.list.concat(res.data)
					if(this.limit == res.data.length) {
						this.loadingType = 'more'
					}else {
						this.loadingType = 'noMore'
					}
					this.loaded = true
				})
			},
			// 载入数据
			async loadData() {
				let obj = this;
				getCategoryList({})
					.then(({ data }) => {
						obj.flist = data.map(function(s) {
							return s;
						});
						// obj.getProducts()
					})
					.catch(err => {
						console.log(err);
					});
			},
			//一级分类点击
			tabtap(item) {
				console.log(item);
				// 判断有没有初始化页面高度对象数据
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				// 获取当前点击的id
				this.currentId = item.id;
				console.log(item.top);
				this.tabScrollTop = item.top;
				console.log(this.tabScrollTop);
			},
			//右侧栏滚动
			asideScroll(e) {
				// 判断有没有初始化页面高度对象数据
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let box = 0; //列表包裹框高度初始化
				let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
				// 查询当前页面对象
				let view = uni.createSelectorQuery().select('.content');
				view.fields(
					{
						id: true,
						dataset: true,
						rect: true,
						size: true,
						scrollOffset: true
					},
					function(e) {
						// 保存包裹框高度
						box = e.height;
					}
				).exec();
				// 获取所有距离顶部大于滚轮距离页面高度的所有分类
				let tabs = this.flist.filter(item =>( item.top-10) <= scrollTop).reverse();
				if (tabs.length > 0) {
					// 判断是否已经到达滚轮底部
					if (box + scrollTop + bottom >= e.detail.scrollHeight) {
						this.currentId = this.flist[this.flist.length - 1].id;
					} else {
						this.currentId = tabs[0].id;
					}
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.flist.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
				});
			},
			navto(url) {
				uni.navigateTo({
					url
				})
			}
		}
	};
</script>
<style lang="scss">
	page {
		height: auto;
		min-height: 100%;
		background-color: #fff;
	}
	.cate-item {
		width: 717rpx;
		margin:0 auto 30rpx;
		image {
			width: 100%;
		}
	}
</style>