<template>
	<view class="content">
		<view class="top-fixed">
			<!-- 搜索框 -->
			<view class="search-cont">
				<view class="inner">
					<image class="icon-search" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/search.png" mode=""></image>
					<input type="text" @confirm="goSearch"   :value="form.keyword" placeholder="输入产品名称" placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
				</view>
				<!-- <view class="icon-search">
					<image @click="showChange(2)" v-if="showType==1"  src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/search1.png" mode=""></image>
					<image @click="showChange(1)" v-if="showType==2"  src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/search2.png" mode=""></image>
				</view> -->
			</view>
			<!-- 排序框 -->
		<!-- 	<view class="sort-box">
				<view class="item" :class="form.defaultOrder=='desc'?'active':''" @click="searchChange('1')">
					<text class="label">默认</text>
				</view>
				<view class="item" :class="form.defaultOrder=='desc'?'active':''" @click="searchChange('1')">
					<text class="label">新品</text>
				</view>
				<view class="item" :class="form.defaultOrder=='desc'?'active':''" @click="searchChange('1')">
					<text class="label">默认</text>
				</view>
				<view class="item" @click="searchChange('2')">
					<text class="label">价格</text>
					<view class="sort-img">
						<image v-if="form.priceOrder==null||form.priceOrder=='desc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_up.png" mode="" @click="priceUp(true)"></image>
						<image v-if="form.priceOrder=='asc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_up2.png" mode="" @click="priceUp(false)"></image>
						<image v-if="form.priceOrder==null||form.priceOrder=='asc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_down.png" mode="" @click="priceDown(true)"></image>
						<image v-if="form.priceOrder=='desc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_down2.png" mode="" @click="priceDown(false)"></image>
					</view>
				</view>
				<view class="item" @click="searchChange('3')">
					<text class="label">销量</text>
					<view class="sort-img">
						<image v-if="form.salesOrder==null||form.salesOrder=='desc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_up.png" mode="" @click="saleUp(true)"></image>
						<image v-if="form.salesOrder=='asc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_up2.png" mode="" @click="saleUp(false)"></image>
						<image v-if="form.salesOrder==null||form.salesOrder=='asc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_down.png" mode="" @click="saleDown(true)"></image>
						<image v-if="form.salesOrder=='desc'" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/price_arrow_down2.png" mode="" @click="saleDown(false)"></image>
					</view>
				</view>
				<view class="item" :class="form.newOrder=='desc'?'active':''" @click="searchChange('4')">
					<text class="label">新品</text>
				</view>
			</view> -->
		</view>
	 
		<!-- 数据列表 -->
		<mescroll-body top="100rpx" bottom="40rpx" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="goods-grid">
				<view class="grid-item" v-for="(item, index) in dataList" :key="index">
					<GoodsCard :item="item" @goPage="showDetail"/>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import { getStoreProductList } from '@/api/companyOrder.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import GoodsCard from './components/GoodsCard.vue';
	export default {
		components: { GoodsCard },
		mixins: [MescrollMixin], 
		data() {
			return {
				form:{
					// defaultOrder:'desc',
					// newOrder:null,
					// priceOrder:null,
					// salesOrder:null,
					//position:1,
					productName:"",
					companyUserId:null,
					companyId:null
				},
				mescroll:null,
				// 上拉加载的配置
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						icon:'https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/no_data.png',
						tip: '暂无数据'
					},
					textNoMore:"已经到底了",
				},
				// 列表数据
				dataList: [],
				companyId:null,
				companyUserId:null,
			};
		},
		onLoad(option) {
			if(option.key!=undefined){
				this.form.productName = option.key
			}
			if(option.id != undefined){
				this.form.id= option.id
			}
			if(option.type!=undefined){
				this.searchChange(option.type)
			}
			this.companyId = option.companyId||'';
			this.companyUserId = option.companyUserId||'';
			this.form.companyId=option.companyId||'';
			this.form.companyUserId=option.companyUserId||'';
		},
		methods:{
			goSearch(e) {
				this.form.productName=e.detail.value;
				this.mescroll.resetUpScroll();
			},
			searchChange(type){
				
				this.mescroll.resetUpScroll();
			},
			showChange(type){
				this.showType=type;
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				// 与 newindex 瀑布流使用同一接口：getHomeGoods，id 为分类（0=全部）
				//联网加载数据
				var that = this;
				this.form.pageNum=page.num;
				this.form.pageSize=page.size;
				getStoreProductList(this.form).then(res => {
					if(res.code==200){
						//设置列表数据
						if (page.num == 1) {
							that.dataList = res.data.list; 
							
						} else {
							that.dataList = that.dataList.concat(res.data.list);
							 
						}
						that.mescroll.endBySize(res.data.list.length, res.data.total);
						
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				}).catch(() => {
				that.dataList = page.num == 1 ? [] : that.dataList
				that.mescroll.endErr()
			})
			},
			// 价格升序是否选中
			priceUp(value) {
				this.priceIsUp = value
				this.priceIsDown = false
			},
			// 价格降序是否选中
			priceDown(value) {
				this.priceIsDown = value
				this.priceIsUp = false
			},
			// 销量升序是否选中
			saleUp(value) {
				this.saleIsUp = value
				this.saleIsDown = false
			},
			// 销量降序是否选中
			saleDown(value) {
				this.saleIsDown = value
				this.saleIsUp = false
			},
			// 查看详情
			showDetail(item) {
				uni.navigateTo({
					url: './productDetails?productId='+item.productId+"&companyId="+this.companyId+"&companyUserId="+this.companyUserId
				})
			}
		}
	}
</script>

<style lang="scss">
	.top-fixed{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 10;
	}
	.search-cont{
		padding: 16upx 30upx;
		background-color: #FFFFFF;
		display:flex;
		align-items: center;
		justify-content: space-between;
		.inner{
			box-sizing: border-box;
			width: 100%;
			height: 72upx;
			background: #F7F7F7;
			border-radius: 36upx;
			display: flex;
			align-items: center;
			padding: 0 30upx;
			.icon-search{
				width: 28upx;
				height: 28upx;
				margin-right: 20upx;
			}
			input{
				height: 60upx;
				line-height: 60upx;
				flex: 1;
			}
			
		}
		.icon-search{
			margin-left: 10upx;
			width: 40upx;
			height: 40upx;
			image{
				width: 40upx;
				height: 40upx;
			}
			
		}
	}
	.sort-box{
		height: 88upx;
		background: #FFFFFF;
		padding: 0 100upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.item{
			display: flex;
			align-items: center;
			justify-content: center;
			&.active .label{
				color: #FF233C;
			}
			.label{
				font-size: 26upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
				line-height: 1;
			}
			.sort-img{
				height: 20upx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin: 3upx 0 0 10upx;
				image{
					width: 12upx;
					height: 8upx;
				}
			}
		}
	}
	.goods-grid {
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		// display: flex;
		// flex-wrap: wrap;
		// gap: 20rpx;
	}
	.goods-grid .grid-item {
		width: 100%;
	}
</style>
