<template>
	<view class="good-list">
		<view class="good flex" v-for="item in navList[tabCurrentIndex].orderList">
			<image :src="item.image" mode="" class="good-image"></image>
			<view class="right">
				<view class="good-name ">
					<view class="clamp2">
						{{item.store_name}}
					</view>
				</view>
				<view class="good-key">
					{{item.keyword}}
				</view>
				<view class="good-price flex">
					<view class="price">
						￥{{item.price}}/{{item.unit_name}}
					</view>
					<navigator :url="'/pages/water/waterDetail?id='+item.id">
						<view class="xl">
							立即购买
						</view>
					</navigator>
				</view>
			</view>
		</view>
		<uni-load-more :status="navList[tabCurrentIndex].loadingType"></uni-load-more>
	</view>
</template>

<script>
	import {
		waterList
	} from '@/api/water.js';
	export default {
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
					state: 0,
					text: '全部',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}]
			};
		},
		onReachBottom() {
			this.getGoodList();
		},
		onLoad: function(option) {
			this.getGoodList();
		},
		methods: {
			// 加载数据
			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';

				waterList({
						car_number: this.id,
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						let arr = data.data.map(e => {
							return e;
						});
						navItem.orderList = navItem.orderList.concat(arr);
						// console.log(navItem.orderList);
						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 {
		padding: 20rpx 28rpx;
		width: 750rpx;

		.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 {
					display: flex;
					justify-content: space-between;
					align-items: flex-end;
					font-size: 28rpx;
					font-weight: bold;
					color: #FF1A1A;
					position: absolute;
					bottom: 0rpx;
					padding: 0 20rpx;
					left: 0;
					right: 0rpx;

					.xl {
						background: linear-gradient(90deg, #3C82E6, #5395F5);
						border-radius: $font-base;
						padding: 10rpx 20rpx;
						color: #fff;
					}
				}
			}
		}
	}
</style>
