<template>
	<view class="content">
		<empty v-if="loaded && goodList.length == 0"></empty>
		<view class="good flex" v-for="item in goodList" @click="navTo('/pages/product/product?id=' + item.id)">
			<view class="good-img">
				<image :src="item.image" mode=""></image>
			</view>
			<view class="good-info flex">
				<view class="good-name clamp2">
					{{item.store_name}}
				</view>
				<view class="good-price">
					<view class="old-price" v-if="item.ot_price*1 > item.price*1">
						<text class="old-left">¥{{item.ot_price}}</text>
						<image src="../../static/icon/down.png" mode="widthFix"></image>
						<text class="old-right">直降{{item.ot_price*1 - item.price*1}}元</text>
					</view>
					<view class="new-price flex">
						<view class="">
							¥{{item.price}}
						</view>
						<view class="good-tip">
							立即购买
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType" v-if="!(loaded && goodList.length == 0)"></uni-load-more>
	</view>
</template>

<script>
	import {getProducts } from '@/api/product.js'
	export default {
		data() {
			return {
				goodList: [],
				page: 1,
				limit: 10,
				loadingType: 'more',
				loaded:false
			}
		},
		onLoad() {
			this.getGoodList()
		},
		onShow() {

		},
		onReachBottom() {
			this.getGoodList()
		},
		onReady() {

		},
		methods: {
			navTo(url) {
				if (url.indexOf('http') != -1) {
					window.location.href = url
				} else {
					uni.navigateTo({
						url,
						fail() {
							uni.switchTab({
								url
							})
						}
					})
				}
			},
			getGoodList() {
				let obj = this
				if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return
				}
				obj.loadingType = 'loading'
				getProducts({
					page: obj.page,
					limit: obj.limit
				}).then(res => {
					obj.goodList = obj.goodList.concat(res.data)
					obj.page++
					if (obj.limit == res.data.length) {
						obj.loadingType = 'more'
					} else {
						obj.loadingType = 'noMore'
					}
					obj.loaded = true
				})
			},
		}
	}
</script>

<style lang="scss">
	.content {
		padding-top: 20rpx;
	}
	.good {
		width: 690rpx;
		height: 276rpx;
		background: #FFFFFF;
		box-shadow: 0px 0px 20rpx 0px rgba(50, 50, 52, 0.06);
		border-radius: 10rpx;
		margin: auto;
		padding: 20rpx 15rpx;
		margin-bottom: 20rpx;
		&:last-of-type {
			margin-bottom: 0rpx;
		}
		
		.good-img {
			flex-shrink: 0;
			width: 236rpx;
			height: 236rpx;
			border-radius: 10rpx;
			margin-right: 22rpx;
		
			image {
				width: 236rpx;
				height: 236rpx;
				border-radius: 10rpx;
			}
		}
		
		.good-info {
			flex-grow: 1;
			height: 100%;
			flex-direction: column;
			justify-content: space-between;
			align-items: flex-start;
		
			.good-name {
				font-size: 32rpx;
				font-weight: bold;
				padding-top: 10rpx;
				color: #333333;
			}
		
			.good-price {
				width: 100%;
		
				image {
					width: 14rpx;
					margin: 0 6rpx 0 10rpx;
				}
		
				.old-price {
					.old-left {
						font-size: 26rpx;
						font-weight: 500;
						text-decoration: line-through;
						color: #999999;
					}
		
					.old-right {
						font-size: 24rpx;
						font-weight: bold;
						color: #B59467;
					}
				}
		
				.new-price {
					width: 100%;
					font-size: 36rpx;
					font-weight: bold;
					color: #FF4C4C;
					justify-content: space-between;
		
					.good-tip {
						width: 137rpx;
						height: 52rpx;
						background: $base-color;
						border-radius: 26rpx;
						position: relative;
						font-size: 26rpx;
						font-weight: 500;
						color: #FFFFFF;
						line-height: 52rpx;
						text-align: center;
					}
				}
			}
		}
	}
</style>