<template>
	<!-- 父组件使用 -->
	<view :class="['qn-page-' + theme]">
		<!-- 空白页 -->
		<Aempty text="您还没有收藏的商品哦～～" src="https://onlineimg.qianniao.vip/favor.png" v-if="shoppingList.length === 0"></Aempty>
		<block v-if="shoppingList.length > 0">
			<view class="cate-view clearfix">
				<view class="float_left tabs-view">
					<u-tabs :active-color="primaryColor" :bar-style="{ height: '6rpx' }" :list="college_list" name="categoryName" :current="tab_current" @change="tabtap"></u-tabs>
				</view>
				<view class="float_right type-view" @click="changeStyle"><text class="ibonfont" :class="[is_list ? 'ibonfenlei1' : 'ibonfenlei2']"></text></view>
			</view>
			<scroll-view scroll-y class="goods-scroll">
				<block v-if="shoppingList.length > 0">
					<u-waterfall v-model="shoppingList" ref="uWaterfall" v-if="!is_list">
						<template v-slot:left="{ leftList }">
							<block v-for="(item, index) in leftList" :key="index"><GoodsItem :isList="is_list" :item="item" @addCart="addCard(item.id)"></GoodsItem></block>
						</template>
						<template v-slot:right="{ rightList }">
							<block v-for="(item, index) in rightList" :key="index"><GoodsItem :isList="is_list" :item="item" @addCart="addCard(item.id)"></GoodsItem></block>
						</template>
					</u-waterfall>
					<!-- 循环item出来为对象 -->
					<view v-else :class="[is_list ? 'goods-li' : 'goods-li-inline']" v-for="(item, index) in shoppingList" :key="index">
						<GoodsItem :isList="is_list" :item="item" @addCart="addCard(item.id)"></GoodsItem>
					</view>
				</block>
			</scroll-view>
		</block>
		<AddCardModel :selAddress="now_sel_address" @close="is_add_show = false" :isShow="is_add_show" @change="cardModelPopChange" :goodsId="goods_id" />
	</view>
</template>

<script>
import AddCardModel from '@/components/AddCardModel';
import GoodsItem from '@/components/GoodsItem.vue';

export default {
	components: {
		GoodsItem,
		AddCardModel
	},
	data() {
		return {
			tab_current: 0,
			is_list: false,
			now_sel_address: {},
			is_add_show: false,
			goods_id: 0,
			college_list: [],
			shoppingList: [],
			categoryName: '',
			page: 1,
			pageSize: 10,
			loading_status: 'loadmore'
		};
	},
	// 下拉刷新
	onPullDownRefresh() {
		this.normalList();
	},
	async onShow(options) {
		await this.normalList();
	},
	methods: {
		// 切换商品列表展示样式
		changeStyle() {
			this.is_list = !this.is_list;
		},
		cardModelPopChange(obj) {
			if (!obj.show) {
				this.is_add_show = false;
			}
		},
		addCard(id) {
			this.goods_id = id;
			this.is_add_show = true;
		},
		// 获取商品列表
		normalList() {
			this.loading_status = 'loading';
			this.$u.api.normalList().then(({ data }) => {
				uni.stopPullDownRefresh();
				this.loading_status = 'loadmore';
				this.college_list = data;
				if (data.length) {
					let shoppingList = [];
					data.forEach(item => {
						shoppingList = shoppingList.concat(item.data);
					});
					this.shoppingList = shoppingList;
					this.college_list.unshift({
						categoryName: '全部',
						data: shoppingList
					});
				}
			});
		},
		tabtap(current) {
			this.tab_current = current;
			const item = this.college_list[current];
			this.shoppingList = item.data;
			this.categoryName = item.categoryName;
		}
	}
};
</script>

<style lang="scss">
.cate-view {
	background-color: #ffffff;
	.tabs-view {
		width: 620rpx;
	}
	.type-view {
		width: 80rpx;
		text-align: center;
		line-height: 90upx;
		height: 90upx;
		border-left: 1px solid #f5f5f5;
		.ibonfont {
			font-size: 36upx;
			color: #909399;
		}
	}
}

.goods-scroll {
	height: calc(100% - 90rpx);
	.goods-li {
		margin: 24rpx 32rpx;
	}
	.goods-li-inline {
		display: inline-block;
	}
}
</style>
