<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> -->
					<text class="old-price" v-if="type">赠送：{{(item.price*1*pUser.recharge_resumption*0.01).toFixed(0)}}复投积分</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 { passUser } from '@/api/zero.js'
	import empty from '@/components/empty.vue'
	import {
		getBargainList,
		getProducts,
		goodsDetail,
		poster,
	} from '@/api/product.js';
	export default {
		components: {
			empty
		},
		data() {
			return {
				is_pack: 0,
				goodList: [], //商品列表
				loadingType: 'more',
				page: 1,
				limit: 10,
				type: 0,
				loaded: false,
				pUser: {},
				is_zero: 0,
			}
		},
		onLoad(opt) {
			if (opt.type) {
				this.type = opt.type
				
			}
			if(opt.tit) {
				uni.setNavigationBarTitle({
					title: opt.tit
				})
			}
			if(opt.is_pack) {
				this.is_pack = opt.is_pack
			}
			if(opt.is_zero) {
				this.is_zero = opt.is_zero
			}
			this.getGoodList()
		},
		onShow() {
			this.passUser()
		},
		onReachBottom() {
			
			this.getGoodList()
		},
		onPullDownRefresh() {
			this.getGoodList('down')
		},
		methods: {
			passUser() {
				passUser().then(res => {
					this.pUser = res.data
				})
			},
			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: obj.is_pack,
						is_zero: obj.is_zero,
						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;
						color: $base-color;
						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>