<template>
	<view class="app">
		<view class="card">
			<view class="items" :style="{background:listcolor[index % listcolor.length]}" @tap='goProductList(item)'
				v-for="(item,index) in goodsCategoryList" :key="index">
				<image :src="item.goodsCategoryPicture" mode=""></image>
				<view class="titles" style="font-size:42rpx">
					{{item.goodsCategoryName}}
				</view>
			</view>
			<view style="width:43%" v-if="goodsCategoryList.length%2!=0">
			</view>
		</view>
	</view>
</template>
<script>
	import {
		goodsCategoryList
	} from '@/api/shopping/shopping.js';
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				pageNum: 1, //页
				pageSize: 10, //列
				total: 0, //list总长度
				goodsCategoryList: [], //商品列表list
				listcolor: ['#E69874', '#09C87E', '#E4B958', '#6592DC', '#EF6A80'] //颜色随动
			};
		},
		//1.分享给朋友
		onShareAppMessage(res) {
			let pages = getCurrentPages();
			let url = pages[pages.length - 1].$page.fullPath
			return {
				title: '泉医到家',
				path: url,
			}
		},
		//2.分享到朋友圈
		onShareTimeline(res) {
			let pages = getCurrentPages();
			let url = pages[pages.length - 1].$page.fullPath
			return {
				title: '泉医到家',
				path: url,
			}
		},
		//初始化加载
		onLoad(options) {
			this.pageNum = 1
			this.goodsCategory()
		},
		onShow() {
			let that = this
			this.pageNum = 1
			const value = uni.getStorageSync('Refresh');
			if (value) {
				that.goodsCategory();
			}
		},
		methods: {
			//跳转到商品列表页面
			goProductList(item) {
				uni.navigateTo({
					url: `/pagesB/ProductList/ProductList?title=${item.goodsCategoryName}&goodsCategoryId=${item.id}`
				})
			},
			//获取商城信息
			goodsCategory() {
				goodsCategoryList(this.pageSize, this.pageNum).then(res => {
					uni.removeStorageSync('Refresh');
					if (res.code == 200) {
						res.rows.forEach(e => {
							e.goodsCategoryPicture = baseurl + e.goodsCategoryPicture
						})
						this.goodsCategoryList = res.rows
						this.total = res.total
					}
				})
			}
		},
		onReachBottom() { //下滑加载
			if (this.goodsCategoryList.length >= this.total) {} else {
				this.pageNum++;
				goodsCategoryList(this.pageSize, this.pageNum).then(res => {
					res.rows.forEach(e => {
						e.goodsCategoryPicture = baseurl + e.goodsCategoryPicture
						this.goodsCategoryList.push(e)
					})
					this.total = res.total
				})
			}
		},
		onPullDownRefresh() { //下拉刷新
			this.pageNum = 1;
			goodsCategoryList(this.pageSize, this.pageNum).then(res => {
				res.rows.forEach(e => {
					e.goodsCategoryPicture = baseurl + e.goodsCategoryPicture
				})
				this.goodsCategoryList = res.rows
				this.total = res.total
			})
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		}
	}
</script>

<style lang="scss">
	.app {
		padding: 10rpx 0 0;
		background-color: #fff;

		.card {
			width: 100%;
			display: flex;
			justify-content: space-evenly;
			flex-wrap: wrap;

			.items {
				width: 45%;
				height: 360rpx;
				margin-bottom: 40rpx;
				z-index: 1;
				border-radius: 25rpx;
				position: relative;

				image {
					border-radius: 25rpx;
					width: 100%;
					height: 100%;
					z-index: -1;
				}

				.titles {
					width: 90%;
					font-size: 36rpx;
					color: #FCFCFC;
					position: absolute;
					top: 30rpx;
					left: 30rpx;
					z-index: 999;
				}
			}
		}
	}
</style>
