<template>
	<view class="hot-list">
		<empty v-if="loaded && goodList.length == 0"></empty>
		<view class="good" v-for="item in goodList" @click="navto('/pages/product/product?id=' + item.id)">
			<image :src="item.image" mode="" class="good-img"></image>
			<view class="good-tit">
				{{item.store_name}}
			</view>
			<view class="good-cz flex">
				<view class="price">
					<text class="xy">¥</text> <text>{{item.price}}</text><text class="old-price">¥{{item.ot_price}}</text>
				</view>
				<view class="cz-btn">
					立即抢购
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType" v-if="!(loaded && goodList.length == 0)"></uni-load-more>
	</view>
</template>

<script>
	import empty from '@/components/empty.vue'
	import {
		getBargainList,
		getProducts,
		goodsDetail,
		poster,
	} from '@/api/product.js';
	export default {
		components: {
			empty
		},
		data() {
			return {
				goodList: [], //商品列表
				loadingType: 'more',
				page: 1,
				limit: 10,
				type: 0,
				loaded: false
			}
		},
		onLoad(opt) {
			if(opt.type) {
				this.type = opt.type
				uni.setNavigationBarTitle({
					title: opt.tit
				})
			}
			this.getGoodList()
		},
		onShow() {
			
		},
		onReachBottom() {
			this.getGoodList()
		},
		onPullDownRefresh() {
			this.getGoodList('down')
		},
		methods: {
			navto(url) {
				uni.navigateTo({
					url,
					fail() {
						uni.switchTab({
							url
						})
					}
				})
			
			},
			getGoodList(type) {
				let obj = this
				if(type == 'down') {
					obj.loadingType = 'more'
					obj.page = 1
				}
				if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return
				}
				obj.loadingType = 'loading'
				if(obj.type) {
					getProducts({
						page: obj.page,
						limit:obj.limit,
						cid: obj.type
					}).then(res => {
						if(type == 'down') {
							obj.goodList = []
							uni.stopPullDownRefresh();
						}
						obj.goodList = obj.goodList.concat(res.data)
						if (obj.limit == res.data.length) {
							obj.loadingType = 'more'
							obj.page++
						} else {
							obj.loadingType = 'noMore'
						}
						obj.loaded = true
					}).catch(err => {
						obj.loadingType = 'more'
						uni.stopPullDownRefresh();
					})
				}else {
					getProducts({
						is_pack: 1,
						page: obj.page,
						limit:obj.limit,
					}).then(res => {
						if(type == 'down') {
							obj.goodList = []
							uni.stopPullDownRefresh();
						}
						obj.goodList = obj.goodList.concat(res.data)
						if (obj.limit == res.data.length) {
							obj.loadingType = 'more'
							obj.page++
						} else {
							obj.loadingType = 'noMore'
						}
						obj.loaded = true
						
					}).catch(err => {
						obj.loadingType = 'more'
						uni.stopPullDownRefresh();
					})
				}
				
			},
		}
	}
</script>

<style lang="scss" scoped>
	.hot-list {
		// margin-top: -70rpx;
		padding-top: 20rpx;
		.good {
			width: 704rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin:0 auto 30rpx;
			.good-img {
				width: 704rpx;
				height: 330rpx;
				background: #D4D4E1;
				border-radius: 20rpx 20rpx 0rpx 0rpx;
			}
			.good-tit {
				padding:10rpx 20rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #000000;
			}
			.good-cz {
				padding: 0 20rpx 23rpx 23rpx;
				.price {
					font-size: 42rpx;
					font-weight: bold;
					color: $base-color;
					.xy {
						font-size: 24rpx;
					}
					.old-price {
						font-size: 26rpx;
						font-weight: 500;
						text-decoration: line-through;
						color: #999999;
						margin-left: 18rpx;
					}
				}
				.cz-btn {
					width: 172rpx;
					line-height: 60rpx;
					background: $base-color;
					border-radius: 10rpx;
					font-size: 26rpx;
					font-weight: 500;
					color: #FFFFFF;
					text-align: center;
				}
			}
		}
	}
</style>