<template>
	<view class="app">
		<view class="productlist">
			<view class="item" @tap='goCommodityDetails(item)' v-for="(item,index) in goodsList" :key="index">
				<image :src="baseurl+item.goodsPictureUrl" mode=""></image>
				<view class="title">
					{{item.goodsName}}
				</view>
				<view class="price">
					￥{{item.goodsPrice}}
				</view>
			</view>
			<view class="item" v-if="goodsList.length%2!=0">
			</view>
		</view>
	</view>
</template>

<script>
	import {
		goodsList
	} from '@/api/ProductList/ProductList.js';
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				baseurl: '',
				title: '医路优品', //导航栏标题
				pageNum: 1,
				pageSize: 10,
				goodsCategoryId: '', //请求值
				goodsList: [], //商品列表list
				total: 0, //list总长度
			};
		},
		onLoad(options) { //获取传值
			this.title = options.title //导航栏标题
			this.goodsCategoryId = options.goodsCategoryId //请求id
			this.baseurl = baseurl;
			this.goodsListinfo(this.pageSize, this.pageNum, options.goodsCategoryId)
		},
		methods: {
			// 查询商品列表
			goodsListinfo(pageSize, pageNum, goodsCategoryId) {
				goodsList(pageSize, pageNum, goodsCategoryId).then(res => {
					res.rows.forEach(e => {
						this.goodsList.push(e)
					})
					this.total = res.total
				})
			},
			//跳转详情页
			goCommodityDetails(item) {
				uni.navigateTo({
					url: `/pages/CommodityDetails/CommodityDetails?goodsPrice=${item.goodsPrice}&goodsInfoId=${item.goodsInfoId}&buySource=${'SHOPPING_MALL'}`
				})
			},
		},
		onReady() { //更改导航栏文字
			uni.setNavigationBarTitle({
				title: this.title,
			});
		},
		onReachBottom() { //下滑加载
			if (this.goodsList.length >= this.total) {} else {
				this.pageNum++;
				this.goodsListinfo(this.pageSize, this.pageNum, this.goodsCategoryId)
			}
		},
		onPullDownRefresh() { //下拉刷新
			this.pageNum = 1;
			goodsList(this.pageSize, this.pageNum, this.goodsCategoryId).then(res => {
				this.goodsList = res.rows
				this.total = res.total
			})
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		}
	}
</script>

<style lang="scss">
	.app {
		padding: 0;
		.productlist {
			width: 100%;
			margin: 0 auto;
			padding: 40rpx 0 0 0;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-evenly;

			.item {
				width: 45%;
				background: #FFFFFF;
				border-radius: 25rpx;
				padding-bottom: 10rpx;
				box-shadow: 0px 4rpx 8rpx 4rpx rgba(199, 200, 202, 0.8);
				margin-bottom: 40rpx;

				.price {
					font-size: 42rpx;
					color: #D43953;
					line-height: 79rpx;
					padding-left: 20rpx;
				}

				.title {
					font-size: 39rpx;
					color: #000000;
					line-height: 69rpx;
					padding-left: 20rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
				}

				image {
					width: 100%;
					height: 340rpx;
				}
			}
		}
	}
</style>
