<template>
	<view>
		<view class="lf-p-t-20 head" v-if="tab_list.length">
			<u-tabs :list="tab_list" :is-scroll="true" :current="current" @change="tabChange"></u-tabs>
		</view>
		<lf-nocontent v-else></lf-nocontent>
		<swiper :style="{height: 'calc('+ windowHeight +'px - 60rpx)', width: '750rpx'}" :current="current" @change="swiperChange">
			<swiper-item v-for="(tabItem, tabIndex) in tab_list" :key="tabIndex">
				<scroll-view class="com" :scroll-y="true" :refresher-enabled="true" :refresher-triggered="tabItem.isRefresher" @scrolltolower="onScrolltolower" @refresherrefresh="onRefresherrefresh">
					<view class="lf-m-t-20"></view>
					<lf-waterfall :list="tabItem.list" ref="uWaterfall"></lf-waterfall>
					<view class="loading-more lf-m-b-10">
						<text :class="{'loading-more-text': tabItem.loadingClass}" v-if="tabItem.list.length">{{tabItem.loadingText}}</text>
						<lf-nocontent v-else></lf-nocontent>
					<!-- 	<view> 
							{{tabItem.list.length}}
						</view> -->
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import lfWaterfall from '@/components/lf-waterfall-recom/lf-waterfall.vue';
	export default {
		data(){
			return {
				tab_list: [],
				current: 0,
				windowHeight: 0,
				show_count: 0
			}
		},
		components: {
			lfWaterfall
		},
		onShow(){
			this.windowHeight = getApp().globalData.windowHeight;
			if(this.show_count >= 1){
				this.clearTabItem();
				this.getData();
			}else{
				this.getCategory();
			}
			this.show_count++;
		},
		methods: {
			getCategory(options = {}) {
				this.$http(this.API.API_CATEGORYLIST).then(res => {
					let res_list = res.data || [];
					let tab_list = res_list.map(item => {
						return {
							id: item.id,
							name: item.name,
							list: [],
							isRefresher: false,
							loadingClass: true,
							loadingText: '正在加载中',
							page: 1,
							isPage: true
						}
					});
					this.tab_list = tab_list;
					this.getData()
				}).catch(err => {
					
				})
			},
			getData() {
				let tab_item = this.tab_list[this.current];
				console.log('页数',tab_item.page)
				if(this.$shared.isValueType(tab_item) == 'undefined') return;
				this.$http(this.API.API_ADVICELIST,{page: tab_item.page,category_id: tab_item.id}).then(res => {
					let isPage = res.data.next_page_url == null?false:true;
					tab_item.isPage = isPage;
					if(!isPage){
						tab_item.loadingClass = false;
						tab_item.loadingText = '没有更多数据啦~';
					}
					tab_item.isRefresher = false;
					if(tab_item.page == 1){
						tab_item.list = res.data.data;
					}else{
						tab_item.list.push(...res.data.data);
					}
				}).catch(err => {
					
				})
			},
			tabChange(index){
				this.current = index;
				if(this.tab_list[index].list.length <= 0){
					this.getData(); // tab下没有数据，请求第一页
				}
			},
			// 滑块下标值变化
			swiperChange(event){
				this.current = event.detail.current;
				if(event.detail.source == '') return; // 如果是被动出发，没有事件类型则不做处理
				if(this.tab_list[event.detail.current].list.length <= 0){
					this.getData(); // tab下没有数据，请求第一页
				}
			},
			// 页面触底，加载下一页
			onScrolltolower(){
				let tab_item = this.tab_list[this.current];
				if(tab_item.isPage){
					tab_item.page = tab_item.page + 1;
					this.getData();
				}
			},
			// scroll-view 下拉刷新
			onRefresherrefresh(){
				this.$u.throttle(() => {
					this.clearTabItem();
					this.getData();
				}, 200);
			},
			clearTabItem(){
				let tab_item = this.tab_list[this.current];
				tab_item.page = 1;
				tab_item.isPage = true;
				tab_item.isRefresher = true;
				tab_item.loadingClass = true;
				tab_item.loadingText = '正在加载中';
				tab_item.list = [];
				this.$set(this.tab_list, this.current, tab_item);
				this.$refs.uWaterfall[this.current].clear();
			}
		}
	}
</script>

<style>
	page{
		background-color: #F6F6F6;
	}
</style>
<style lang="scss" scoped="scoped">
	.head{
		background-color: #FFFFFF;
	}
	.com{
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding: 0rpx 32rpx;
	}
</style>
