<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="24" :show-bar="false" @change="tabsChange" height="60"></u-tabs-swiper>
		</view>
		<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish" :style="{height: swiperHeight}">
			<swiper-item class="swiper-item" v-for="(_, index) in classifyList" :key="index">
				<view class="goods-item" v-if="goodsList.length">
					<item v-for="item in goodsList" :key="item.goods_id" :info="item"></item>
				</view>
				<u-empty text="暂无商品" mode="list" color="#000" margin-top="20" v-else></u-empty>
			</swiper-item>
		</swiper>
		<!-- 加载更多 -->
		<u-loadmore :status="loadStatus" bgColor="#FFF" margin-top="20" margin-bottom="20" @loadmore="loadMore" v-if="goodsList.length>=pageSize"></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 = res.data.goodsList;
				else this.goodsList.push(...res.data.goodsList);
				// console.log(this.goodsList);
				this.setSwiperHeight();
			}
			return res.data.goodsList.length;
		},
		setSwiperHeight() {
			// height: 230px, margin-bottom: 13
			const height = Math.ceil(this.goodsList.length / 2) * (540 + 26);
			this.swiperHeight = height == 0 ? '230rpx' : height + 'rpx';
			// this.swiperHeight = Math.ceil(this.goodsList.length / 2) * (270 + 13) + 'px';
		},
		// tabs通知swiper切换
		tabsChange(index) {
			this.current = index;
			// this.getGoodsRecommend({ gc_id: this.classifyList[this.current].gc_id });
		},
		// swiper-item左右移动，通知tabs的滑块跟随移动
		transition(e) {
			let dx = e.detail.dx;
			this.$refs.uTabs.setDx(dx);
		},
		// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
		// swiper滑动结束，分别设置tabs和swiper的状态
		animationfinish(e) {
			let current = e.detail.current;
			this.$refs.uTabs.setFinishCurrent(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%;
        margin-bottom: 10rpx;
        .swiper-item {
            // height: 100%;
            .goods-item {
            	display: flex;
            	flex-wrap: wrap;
            	justify-content: space-between;
                width: 100%;
                // height: 100%;
                overflow-y: scroll;
            }
        }
    }
}
</style>