<template>
	<view class="good-list">
		<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
			<!-- 空白页 -->
			<!-- #ifdef H5 -->
			<empty src="../../static/error/emptyMyCart.png"
				v-if="navList[tabCurrentIndex].loaded === true && navList[tabCurrentIndex].orderList.length === 0">
			</empty>
			<!-- #endif -->
			<!-- #ifndef H5 -->
			<empty src="../static/error/emptyMyCart.png"
				v-if="navList[tabCurrentIndex].loaded === true && navList[tabCurrentIndex].orderList.length === 0">
			</empty>
			<!-- #endif -->
			<view class="good flex" v-for="item in navList[tabCurrentIndex].orderList">
				<image :src="item.product.image" mode="" class="good-image"></image>
				<view class="right">
					<view class="good-name ">
						<view class="clamp2">
							{{item.product.store_name}}
						</view>
					</view>
					<view class="good-key">
						{{item.product.keyword}}
					</view>
					<view class="good-price">
						剩余<text class="num">{{item.certificate_num}}</text>张
					</view>
				</view>
			</view>
			<uni-load-more :status="navList[tabCurrentIndex].loadingType"></uni-load-more>
		</scroll-view>

	</view>
</template>

<script>
	import {
		use_certificate
	} from '@/api/water.js';
	export default {
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
					state: 0,
					text: '全部',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}]
			};
		},
		computed: {
			allNumber() {
				const item = this.navList[0].orderList;
				let num = 0;
				for (let i = 0; i < item.length; i++) {
					num += item[i].certificate_num
				}
				return num
			}
		},
		onReachBottom() {
			this.getGoodList();
		},
		onLoad: function(option) {
			this.getGoodList();
		},
		methods: {
			// 返回退回
			back() {
				uni.reLaunch({
					url: '/pages/home/user'
				})
			},
			// 加载数据
			getGoodList(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				console.log(navItem, '数据');
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				use_certificate({
							page: navItem.page,
							limit: navItem.limit
						})
					.then(({
						data
					}) => {
						let arr = data.data
						navItem.orderList = navItem.orderList.concat(arr);
						navItem.page++;
						if (navItem.limit == arr.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						this.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e);
					});
			},
		}
	};
</script>


<style lang="scss">
	.good-list {
		width: 750rpx;
		height: 100%;

		.good {
			background: #FFFFFF;
			box-shadow: 0px 0px 20px 0px rgba(50, 50, 52, 0.06);
			width: 100%;
			border-radius: 14rpx;
			margin-bottom: 20rpx;
			position: relative;
			padding: 20rpx;

			.good-image {
				width: 180rpx;
				height: 180rpx;
				background-color: #eee;
				border-radius: 10rpx;
				flex-shrink: 0;

			}

			.right {
				height: 180rpx;
				position: relative;

				.good-name {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
					padding-left: 20rpx;
				}

				.good-key {
					font-size: 22rpx;
					font-weight: 500;
					color: #999999;
					padding-left: 20rpx
				}

				.good-price {
					font-size: 28rpx;
					font-weight: bold;
					position: absolute;
					bottom: 0rpx;
					padding: 0 20rpx;
					left: 0;
					right: 0rpx;
					.num{
						color:$color-red;
					}
				}
			}
		}
	}

	.list-scroll-content {
		height: calc(100% - 200px - var(--status-bar-height));
		padding: 20rpx;
	}

</style>
