<template>
	<view class="content">
		<view class="top-fixed">
			<!-- 搜索框 -->
			<view class="search-cont">
				<view class="inner">
					<image class="icon-search" src="https://kntobs.jnmyunl.com/shop/images/search.png" mode=""></image>
					<input type="text" @confirm="goSearch"   :value="form.productName" 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://kntobs.jnmyunl.com/shop/images/search1.png" mode=""></image>
					<image @click="showChange(1)" v-if="showType==2"  src="https://kntobs.jnmyunl.com/shop/images/search2.png" mode=""></image>
					<image @click="openCart()"   src="https://kntobs.jnmyunl.com/shop/images/carts.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" @click="searchChange('2')">
					<text class="label">价格</text>
					<view class="sort-img">
						<image v-if="form.priceOrder==null||form.priceOrder=='desc'" src="https://kntobs.jnmyunl.com/shop/images/price_arrow_up.png" mode="" @click="priceUp(true)"></image>
						<image v-if="form.priceOrder=='asc'" src="https://kntobs.jnmyunl.com/shop/images/price_arrow_up2.png" mode="" @click="priceUp(false)"></image>
						<image v-if="form.priceOrder==null||form.priceOrder=='asc'" src="https://kntobs.jnmyunl.com/shop/images/price_arrow_down.png" mode="" @click="priceDown(true)"></image>
						<image v-if="form.priceOrder=='desc'" src="https://kntobs.jnmyunl.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://kntobs.jnmyunl.com/shop/images/price_arrow_up.png" mode="" @click="saleUp(true)"></image>
						<image v-if="form.salesOrder=='asc'" src="https://kntobs.jnmyunl.com/shop/images/price_arrow_up2.png" mode="" @click="saleUp(false)"></image>
						<image v-if="form.salesOrder==null||form.salesOrder=='asc'" src="https://kntobs.jnmyunl.com/shop/images/price_arrow_down.png" mode="" @click="saleDown(true)"></image>
						<image v-if="form.salesOrder=='desc'" src="https://kntobs.jnmyunl.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="190rpx" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="medic-list" v-if="showType==1">
				<view v-for="(item,index) in dataList" :key="index" class="item" @click="showDetail(item)">
					<view class="img-box">
						<image :src="item.image" mode="aspectFit"></image>
					</view>
					<view class="info-box">
						<view class="title ellipsis2">{{item.productName}}</view>
						<view class="intro ellipsis">{{item.productInfo}}</view>
						<view class="prce-num">
							<view class="price">
								<text class="unit">¥</text>
								<text class="num">{{item.price.toFixed(2)}} </text>
							</view>
							<view class="cart-img" @click="navgetTo('../shopping/cart')">
								<view class="sale">已售 {{item.sales}} {{item.unitName}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="goods-list" v-if="showType==2">
				<view class="item" v-for="(item,index) in dataList" :key="index" @click="showDetail(item)">
					<view class="img-box">
						<image :src="item.image" mode="aspectFill"></image>
					</view>
					<view class="info-box">
						<view class="title ellipsis2">{{item.productName}}</view>
						<view class="price-box">
							<view class="now">
								<text class="unit">¥</text>
								<text class="num">{{item.price.toFixed(2)}}</text>
							</view>
							<view class="old">¥{{item.otPrice.toFixed(2)}}</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {getSalesProducts} from '@/api/companyOrder'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				showType:1,
				form:{
					defaultOrder:'desc',
					newOrder:null,
					priceOrder:null,
					salesOrder:null,
					productName:"",
					token:''
				},
				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://kntobs.jnmyunl.com/shop/images/no_data.png',
						tip: '暂无数据'
					},
					textNoMore:"已经到底了",
				},
				// 列表数据
				dataList: [],
			};
		},
		onLoad(option) {
			if(option.key!=undefined){
				this.form.productName = option.key
			}
		},
		methods:{
			goSearch(e) {
				this.form.productName=e.detail.value;
				this.mescroll.resetUpScroll();
			},
			searchChange(type){
				if(type==1){
					this.form.defaultOrder="desc";
					this.form.priceOrder=null;
					this.form.salesOrder=null;
					this.form.newOrder=null;
				}
				else if(type==2){
					this.form.defaultOrder=null;
					if(this.form.priceOrder==null){
						this.form.priceOrder="asc"
					}
					else if(this.form.priceOrder=="asc"){
						this.form.priceOrder="desc"
					}
					else if(this.form.priceOrder=="desc"){
						this.form.priceOrder=null;
					}
					this.form.salesOrder=null;
					this.form.newOrder=null;
				}
				else if(type==3){
					this.form.defaultOrder=null;
					this.form.priceOrder=null;
					if(this.form.salesOrder==null){
						this.form.salesOrder="asc"
					}
					else if(this.form.salesOrder=="asc"){
						this.form.salesOrder="desc"
					}
					else if(this.form.salesOrder=="desc"){
						this.form.salesOrder=null;
					}
					this.form.newOrder=null;
					
				}
				else if(type==4){
					this.form.newOrder="desc";
					this.form.defaultOrder=null;
					this.form.priceOrder=null;
					this.form.salesOrder=null;
				}
				this.mescroll.resetUpScroll();
			},
			showChange(type){
				this.showType=type;
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				this.form.page=page.num;
				this.form.pageSize=page.size;
				this.form.token=uni.getStorageSync('CompanyUserToken');
				getSalesProducts(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();
					}
				});
			},
			// 价格升序是否选中
			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
				})
			},
			openCart() {
				uni.navigateTo({
					url: './cart'
				})
			}
		}
	}
</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{
			height: 40upx;
			display: flex;
			align-items: center;
			image{
				margin-left: 10upx;
				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: #2BC7B9;
			}
			.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;
				}
			}
		}
	}
	.medic-list{
		padding: 20upx;
		.item{
			box-sizing: border-box;
			min-height: 285upx;
			background: #FFFFFF;
			border: 4upx solid #FFFFFF;
			border-radius: 16upx;
			margin-bottom: 20upx;
			padding: 40upx 30upx;
			display: flex;
			.img-box{
				width: 200upx;
				height: 200upx;
				margin-right: 30upx;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.info-box{
				width: calc(100% - 210upx);
				.title{
					font-size: 32upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					line-height: 40rpx;
					height: 80rpx;
				}
				.intro{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					line-height: 1;
					margin-top: 26upx;
				}
			 
				.prce-num{
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 30upx;
					.price{
						display: flex;
						align-items: flex-end;
						.unit{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #FF6633;
							line-height: 1.2;
							margin-right: 4upx;
						}
						.num{
							font-size: 36upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FF6633;
							line-height: 1;
						}
					}
					.cart-img{
						.sale{
							font-size: 20upx;
							font-family: PingFang SC;
							color: #999999;
						}
						 
					}
				}
			}
		}
	}
	.goods-list{
		padding: 20upx;
		display: flex;
		flex-wrap: wrap;
		.item{
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			width: 345rpx;
			background: #FFFFFF;
			box-shadow: 0px 0px 10rpx 4rpx rgba(199, 199, 199, 0.22);
			border-radius: 20rpx;
			overflow: hidden;
			&:nth-child(2n) {
				margin-right: 0;
			}
			.img-box{
				width: 100%;
				height: 334upx;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.info-box{
				box-sizing: border-box;
				height: 182upx;
				padding: 20upx 20upx 30upx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.title{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					line-height: 40upx;
				}
				.price-box{
					display: flex;
					align-items: flex-end;
					.now{
						display: flex;
						align-items: flex-end;
						margin-right: 20upx;
						.unit{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #FF6633;
							line-height: 1.2;
							margin-right: 4upx;
						}
						.num{
							font-size: 36upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FF6633;
							line-height: 1;
						}
					}
					.old{
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						text-decoration: line-through;
						color: #BBBBBB;
						line-height: 1.1;
					}
				}
			}
		}
	}
</style>
