<template>
    <view class="list">
        <view class="top">商品推荐</view>
		<view>
			<!-- <u-tabs-swiper ref="uTabs" :list="classifyList" name="gc_name" :is-scroll="true" active-color="#FF780F" :current="current" font-size="26" :show-bar="false" @change="tabsChange" height="60"></u-tabs-swiper> -->
			<u-tabs :list="classifyList" name="gc_name" :is-scroll="true" :current="current" @change="tabsChange" active-color="#FF780F" :show-bar="false" height="60" font-size="24"  inactive-color="#333333"></u-tabs>
		</view>
		<swiper class="swiper-box" :current="swiperCurrent" @animationfinish="animationfinish" :style="{height: swiperHeight}">
			<swiper-item class="swiper-item" v-for="(_, index) in classifyList" :key="index">
				<view class="goods-item">
					<item v-for="item in goodsList[index]" :key="item.goods_id" :info="item"></item>
				</view>
				<u-empty text="暂无商品" mode="list" color="#000" margin-top="20" v-if="!goodsList[index] || !goodsList[index].length"></u-empty>
			</swiper-item>
		</swiper>
		<!-- 加载更多 -->
		<u-loadmore class="load-size" :status="loadStatus" bgColor="#FFF" margin-top="20" margin-bottom="20" @loadmore="loadMore"></u-loadmore>
    </view>
</template>
<script>
import item from "./item";
export default {
	name:"list",
	data() {
		return {
			// pageSize: 12,
			current: -1,
			swiperCurrent: 0,
			goodsList: [],
			classifyList: [],
			swiperHeight: '',
			page: 1, // 从 1 开始
			loadStatus: 'loadmore',
			timer: true,
		}
	},
    components: {
		item,
	},
	watch: {
		current(index) {
			this.page = 1;
			// this.goodsList = [];
			const id = this.classifyList[index].gc_id;
			this.getGoodsRecommend({gc_id: id});
		}
	},
	created() {
		this.getGoodsClassRecommend();
	},
	methods: {
		loadMore(page) {
			if(!this.timer) return false;
			this.loadStatus = "loading";
			this.page++;
			this.getGoodsRecommend({
				gc_id: this.classifyList[this.current].gc_id, 
				page: this.page, 
				reload: false,
			}).then(length => {
				// console.log(length);
				if(length == 0) {
					this.page--;
					this.loadStatus = 'nomore';
				} else {
					this.loadStatus = 'loadmore';
				}
			}).catch(() => {
				this.loadStatus = "nomore";
				this.page--;
			})
		},
		getGoodsClassRecommend() {
			this.$u.api.getGoodsClassRecommend().then(res => {
				if (res.errCode == 0) {
					// 初始化 current
					this.current = 0;
					this.classifyList = res.data.gc_recommend;
				}
			})
		},
		async getGoodsRecommend({ page = this.page, gc_id, reload = true } = {}) {
			const res = await this.$u.api.getGoodsRecommend({
				page: page,
				gc_id: gc_id,
			})
			// this.swiperCurrent = this.current;
			if (res.errCode == 0) {
				this.timer = true;
				if(reload) this.goodsList[this.current] = res.data.goodsList;
				else this.goodsList[this.current].push(...res.data.goodsList);
				// console.log(this.goodsList);
				this.setSwiperHeight();
			}
			this.$forceUpdate();
			return res.data.goodsList.length;
		},
		setSwiperHeight() {
			// height: 480rpx, margin-bottom: 26rpx
			const height = Math.ceil(this.goodsList[this.current].length / 2) * (480 + 26);
			this.swiperHeight = height == 0 ? '230rpx' : height + 'rpx';
			// console.log(this.swiperHeight);
		},
		// tabs通知swiper切换
		tabsChange(index) {
			this.current = index;
			this.swiperCurrent = this.current;
			// this.getGoodsRecommend({ gc_id: this.classifyList[this.current].gc_id });
		},
		// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
		// swiper滑动结束，分别设置tabs和swiper的状态
		animationfinish(e) {
			let current = e.detail.current;
			this.current = current;
			this.swiperCurrent = current;
			// this.getGoodsRecommend({ gc_id: this.classifyList[this.current].gc_id });
		},
	}
}
</script>
<style lang="scss" scoped>
.list{
	background-color: #ffffff;
	padding: 30rpx {
		top: 0;
	};
    .top {
        font-size: 30rpx;
        height: 74rpx;
        line-height: 88rpx;
        text-align: center;
        color: #333;
    }
    .swiper-box {
        height: 100%;
        padding-top: 20rpx;
        .swiper-item {
            // height: 100%;
            .goods-item {
            	display: flex;
            	flex-wrap: wrap;
            	justify-content: space-between;
                width: 100%;
                // height: 100%;
                overflow-y: scroll;
            }
        }
    }
}
</style>