<template>
	<view class="content">
		<empty v-if="loaded && goodList.length == 0"></empty>
		<view class="good flex" v-for="item in goodList" @click="type==3?chooseGood(item):navTo('/pages/product/product?id=' + item.id)">
			<view class="good-img">
				<image :src="item.image" mode=""></image>
			</view>
			<view class="good-info flex" v-if="type != 3">
				<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" v-if="type!=3">直降{{item.ot_price*1 - item.price*1}}元</text>
					</view>
					<view class="new-price flex">
						<view class="">
							¥{{item.price}}
						</view>
						<view class="good-tip">
							{{type==3?'确认': '立即购买'}}
						</view>
					</view>
				</view>
			</view>
			<view class="good-info flex" v-if="type == 3">
				<view class="good-name clamp2">
					{{item.title}}
				</view>
				<view class="good-price">
					<view class="new-price flex">
						<view class="stock">
							库存：{{item.stock}}
						</view>
						<view class="good-tip">
							{{type==3?'确认': '立即购买'}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType" v-if="!(loaded && goodList.length == 0)"></uni-load-more>
	</view>
</template>

<script>
	import {getProducts,getGoods } from '@/api/product.js'
	export default {
		data() {
			return {
				id: 0,
				goodList: [],
				page: 1,
				limit: 10,
				loadingType: 'more',
				loaded:false,
				type:0,//0->变压器 1->租电
			}
		},
		onLoad(opt) {
			if(opt.type) {
				this.type = opt.type
			}
			if(opt.id) {
				this.id = opt.id
			}
			if(this.type == 1) {
				uni.setNavigationBarTitle({
					title:'租电'
				})
			}
			if(this.type == 2) {
				uni.setNavigationBarTitle({
					title:'升压器'
				})
			}
			if(this.type == 3) {
				uni.setNavigationBarTitle({
					title:'选择商品'
				})
			}
			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
							})
						}
					})
				}
			},
			chooseGood(item) {
				let prepage = this.$api.prePage();
				prepage.selctGood = item
				uni.navigateBack()
			},
			getGoodList() {
				let that = this
				if (that.loadingType == 'loading' || that.loadingType == 'noMore') {
					return
				}
				that.loadingType = 'loading'
				let qdata = {
					page: that.page,
					limit: that.limit,
					is_battery: that.type == 1? 1:0,
					is_booster: that.type == 2? 1: 0
				}
				if(that.type == 3 ) {
					getGoods({
						store_id:that.id
					}).then(res => {
						that.goodList = res.data
						that.loadingType = 'noMore'
					})
				}else {
					getProducts(qdata).then(res => {
						that.goodList = that.goodList.concat(res.data)
						that.page++
						if (that.limit == res.data.length) {
							that.loadingType = 'more'
						} else {
							that.loadingType = 'noMore'
						}
						that.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;
					.stock {
						color: #333333;
						font-size: 28rpx;
					}
					.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>