<template>
	<el-layout>
		<el-navbar v-model:top="navTop" :placeholder="true" :fixed="true" :border="false" title="商品列表"></el-navbar>
		
		<view class="search" :style="{ top: navTop + 'px' }">
			<view class="top">
				<view class="search-wrap" @click.stop="navigateTo('pages/search/index')">
					<view class="search-box">
						<uni-icons type="search" size="18" color="#6d6d6d"></uni-icons>
						<text>搜索商品</text>
					</view>
				</view>
				<view class="menu">
					<view @click.stop="onMenu('list')" v-if="type == 'grid'" class="iconfont icon-grid"></view>
					<view @click.stop="onMenu('grid')" v-if="type == 'list'" class="iconfont icon-list"></view>
				</view>
			</view>
			<view class="bottom">
				<view class="item-box" :class="{ active: sort == 'all' }" @click.stop="onSort('all')">
					<text>综合</text>
				</view>
				<view class="item-box" :class="{ active: sort == 'sales' }" @click.stop="onSort('sales')">
					<text>销量</text>
				</view>
				<view class="item-box" :class="{ active: sort == 'price' }" @click.stop="onSort('price')">
					<text>价格</text>
					<view class="menu">
						<view class="icon up" :class="{ active: sort == 'price' && !sortPrice }">
							<text class="iconfont icon-arrow-top"></text>
						</view>
						<view class="icon down" :class="{ active: sort == 'price' && sortPrice }">
							<text class="iconfont icon-arrow-bottom"></text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="width: 100%; height: 82px;"></view>
		
		<el-refresh ref="refreshRef" @refresh="refresh" @scrolltolower="scrolltolower" :scrollHeight="getScrollHeight">
			
			<view class="wrapper">
				<view class="goods" :class="{ 'on': type == 'list' }">
					
					<view class="goods-list" :class="{ 'on': type == 'list' }">
						
						<view 
						class="goods-item" 
						:class="{ 'on': type == 'list' }" 
						v-for="(item,index) in list" 
						:key="index"
						@click="navigateTo(`/pages/goods/view?id=${item.id}`)"
						>
							<view class="thumb-image">
								<image :src="item.image"></image>
							</view>
							<view class="goods-text">
								<view class="goods-name">{{ item.name }}</view>
								<view class="price">
									<view><text>￥</text>{{ item.sell_price }}</view>
								</view>
								<view class="unit">
									已售 {{ item.sale }} {{ item.unit || "件" }}
								</view>
							</view>
						</view>
						
					</view>
					
				</view>
				
			</view>
			
		</el-refresh>
		
	</el-layout>
</template>

<script setup>
	import { onLoad } from "@dcloudio/uni-app";
	import { nextTick,ref,computed,getCurrentInstance } from "vue";
	import { inArray } from "@/el-ui/libs/check";
	import { navigateTo } from "@/el-ui/libs/navigate";
	import { getClientRect } from "@/el-ui/libs/style";
	
	const catID       = ref(0);
	const refreshRef  = ref();
	const navTop 	  = ref(0);
	onLoad((option)=>{
		catID.value = option.id || 0;
		nextTick(()=>{
			init();
		})
	});
	
	const list = ref([]);
	const init = async ()=>{
		refreshRef.value?.resetPage();
	}
	
	const getScrollHeight = computed(()=>{
		const { windowHeight } = uni.getSystemInfoSync();
		return (windowHeight - navTop.value);
	})
	
	/**
	 * 下拉刷新
	 */
	const refresh = ()=>{
		init();
	}
	
	/**
	 * 上拉加载
	 */
	const scrolltolower = (data)=>{
		if(refreshRef.value.isMore()) {
			refreshRef.value.loadingPage();
	
			setTimeout(() => {
				uni.$http.get("goods.index/index",{ cat_id: catID.value, page: data.page, type: sort.value, order: sortPrice.value ? 'desc' : 'asc' }).then(res=>{
					if(res.code == 1){
						if(data.page == 1) list.value = [];
						refreshRef.value.setData(res.data.total,res.data.list.length);
						list.value = list.value.concat(res.data.list);
					}else if(res.code == -1){
						if(data.page == 1 && res.data.list.length <= 0) list.value = [];
						refreshRef.value.emptyPage();  
					}
				});
			}, 100)
		}
	}
	
	const sort = ref("all");
	const sortPrice = ref(false);
	const onSort = (value)=>{
		sort.value = value;
		sortPrice.value = value == 'price' ? !sortPrice.value : true;
		if(value != 'price'){
			sortPrice.value = true;
		}
		
		init();
	}
	
	const type = ref("grid");
	const onMenu = (value)=>{
		type.value = value;
	}
	
</script>

<style lang="scss" scoped>
	.search {
		width: 100%;
		position: fixed;
		top: 0;
		background-color: #fff;
		z-index: 99999;
		.top {
			display: flex;
			align-items: center;
			.search-wrap{
				border-bottom: 0;
				background: #fff;
				border-radius: 50rpx;
				overflow: hidden;
				font-size: 28rpx;
				color: #6d6d6d;
				box-sizing: border-box;
				height: 64rpx;
				line-height: 64rpx;
				flex: 1;
				margin: 20rpx;
				.search-box {
					width: 100%;
					font-size: 28rpx;
					background: #f7f7f7;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
			.menu {
				width: 60rpx;
				height: 60rpx;
				line-height: 60rpx;
				view {
					font-size: 46rpx;
					color: #6d6d6d;
				}
			}
		}
		.bottom {
			display: flex;
			padding: 0rpx 0 10rpx 0;
			.item-box {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				height: 50rpx;
				font-size: 28rpx;
				color: #333;
				&.active {
					color: #fa2209;
				}
				.menu {
					margin-left: 15rpx;
					.icon {
						&.active {
							text {
								color: #fa2209;
							}
						}
						&.up {
							margin-bottom: -22rpx;
						}
						&.down {
							margin-top: -22rpx;
						}
					}
					text {
						color: #333;
						font-size: 20rpx;
					}
				}
			}
		}
	}
	
	.wrapper {
		.goods {
			padding: 0 20rpx 30rpx 20rpx;
			margin-top: 20rpx;

			.goods-list {
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				align-items: center;
				justify-content: space-between;
				.goods-item {
					display: flex;
					flex-direction: column;
					justify-content: center;
					width: 345rpx;
					border-radius: 10rpx;
					overflow: hidden;
					margin-bottom: 20rpx;
					background-color: #fff;
					&.on {
						width: 100%;
						padding: 15px 0;
						flex-direction: row;
						justify-content: space-between;
						.thumb-image {
							width: 180rpx;
							height: 180rpx;
							margin-left: 20rpx;
							image {
								height: 180rpx;
							}
						}
						.goods-text {
							width: 500rpx;
						}
					}
					.thumb-image {
						width: 100%;
						display: flex;
						justify-content: center;
						image { 
							width: 100%;
							height: 345rpx;
						}
					}
					.goods-name {
						margin-top: 15rpx;
						font-size: 28rpx; 
						padding: 0 20rpx; 
						height: 80rpx; 
						display: -webkit-box;
						overflow: hidden;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}
					.price {
						display: flex;
						flex-direction: row;
						padding: 15rpx 20rpx; 
						view {
							&:nth-child(1){
								font-size: 32rpx;
								color: red;
								text {
									font-size: 24rpx;
								}
							}
						}
					}
					.unit {
						padding: 0rpx 20rpx 20rpx 20rpx; 
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}
	}
</style>