<template>
	<view class="container all-type-list-wrapper">
		<second-hand ref="productList"
						  :search-status="true"
						  @operationConfim="hanldOperationConfim"
						  >
		</second-hand>
		<!--底部选择模态层弹窗组件 -->
		<view class="popup spec"  :class="specClass"  @touchmove.stop.prevent="discard" @tap="hideSpec" >
			<!-- 遮罩层 -->
			<view class="mask"></view>
			<view class="layer" @tap.stop="discard" :style="{paddingBottom :isIphoneX ? '68rpx' : '36rpx',bottom:isIphoneX ?'-370rpx' : '-360rpx'}">
				<view class="content">
					<view class="layer-smimg">
						<image :src="handleData.mainImage" mode=""></image>
					</view>
					<view class="layer-nunbox">
						<view class="layer-nunbox-t">
							<view class="layer-nunbox-text">数量：</view>
							<view class="number-box">
								<view  class="iconfont icon-jianhao" :class="[isQuantity?'disabled':'']" @click="changeCountSub()"></view>
								<input class="btn-input" type="number" v-model="number" maxlength='6'  @blur="changeNumber($event)">
								<view  class="iconfont icon-jiahao"  :class="[isStock?'disabled':'']" @click="changeCountAdd()"></view>
							</view>
						</view>
						<view class="layer-nunbox-b">
							<view class="text">单价：
								<text class="p sm">￥</text>
								<text class="p bg">{{buyRetailPrice.toFixed(2)}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="btn">
					<view class="button add" @click="toConfirmation">立即购买</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import secondHand from '@/components/cm-module/listTemplate/secondHand'
	export default{
		components:{
			secondHand
		},
		data(){
			return{
				clubID:'',	//机构ID
				serviceProviderId:'',//协销ID
				serverUrl: '',
				emptyText: '',
				lastPageType: '',
				lastPageVal: '',
				isIphoneX:this.$store.state.isIphoneX,
				specClass: '',//规格弹窗css类，控制开关动画
				specClasszuhe:'',//组合加购物车弹窗
				specClassjieti:'',//阶梯展示弹窗
				ladderPriceList:{},
				handleData:{},
				isQuantity:false,
				isStock:false,
				number:1,
				buyRetailPrice:0,
				productKind:0,
				productTotalNum:0,
				productTotalAmount:0,
				buyNumRangeShow:'',//起订量范围
				buyPrice:'',//起订量范围价格
				buyRetailPriceStep:1,
				secondProductType:'',
			}
		},
		onLoad() {

		},
		methods:{
			getClubProductNum(){
				this.SellerService.GetSellerProductNum({clubId:this.clubID,serviceProviderId:this.serviceProviderId}).then(response =>{
					this.$refs.productList.cartQuantity = response.data
				})
			},
			hanldOperationConfim(data){//显示选择数量确认弹窗
				console.log(this.handleData)
				this.handleData = data
					this.number = 1
					this.buyRetailPrice = Number(data.price1);
					this.secondProductType = data.secondProductType
					this.specClass = 'show';
			},
			hideSpec() {//关闭选择数量确认弹窗
				this.specClass = 'hide';
				setTimeout(() => {
					this.specClass = 'none';
				}, 200);
			},
			changeCountAdd(){//popup弹窗数量增加按钮
				if(this.handleData.secondHandType == 2||this.handleData.secondHandType == 3){
					// if(this.number >= this.handleData.stock){
					// 	this.number= this.handleData.stock
					// 	this.isStock =true
					// 	this.$util.msg(`购买数量最高为${this.handleData.stock}`,2000);
					// 	return
					// }else{
						this.number++
						this.isQuantity =false
					// }
					this.buyRetailPrice = this.number*this.handleData.price1
				}else{
					this.isStock =true
					this.$util.msg(`该商品购买数量只能为1`,2000);
				}
			},
			changeCountSub(){//popup弹窗数量减按钮
				if(this.handleData.secondHandType == 2||this.handleData.secondHandType == 3){
					// if(this.number <= 1){
					// 	this.number= 1
					// 	this.isQuantity =true
					// 	this.$util.msg(`购买数量最低为1`,2000);
					// 	return
					// }else{
						this.number--
						this.isQuantity =false
					// }
					this.buyRetailPrice = this.number*this.handleData.price1
				}else{
					this.isQuantity =true
					this.$util.msg(`该商品购买数量只能为1`,2000);
				}
			 },
			// changeNumber(e){
			// 	let _value = e.detail.value;
			// 	if(!this.$api.isNumber(_value)){
			// 		this.number = this.minBuyNumber
			// 	}else if(_value < this.minBuyNumber){
			// 		this.$util.msg(`该商品最小起订量为${this.minBuyNumber}`,2000);
			// 		this.number = this.minBuyNumber
			// 	}else if( _value % this.minBuyNumber !=0 ){
			// 		this.$util.msg(`购买量必须为起订量的整数倍`,2000);
			// 		this.number = this.minBuyNumber
			// 	}else{
			// 		this.number = e.detail.value
			// 	}
			// },
			changeNumber(e){
				let _value = e.detail.value;
				if(!this.$api.isNumber(_value)){
					this.number = 1
				}else if(_value <= 1){
					this.$util.msg(`该商品最小起订量为1`,2000);
					this.number = 1
				}else{
					this.number = e.detail.value
				}
				this.buyRetailPrice = this.number*this.handleData.price1
			},
			clearProduct(){//成功加入购物车后清空所选值
				this.handleData.productKind= 0;
				this.handleData.productTotalNum = 0;
				this.handleData.productTotalAmount = 0;
				this.handleData.combinationProductList.forEach(item =>{
					item.initProductNum = 0;
				})
			},
			toConfirmation(){//跳转确认订单页面
				this.specClass = 'hide';
				let cartPramsData={
						clubId:this.clubID,
						allPrice:this.buyRetailPrice,
						allCount:this.number,
						productID:this.handleData.productID,
						productCount:this.number
				}
				this.$api.navigateTo(`/seller/pages/order/create-order?type=prodcut&data=${JSON.stringify({data:cartPramsData})}`)
				setTimeout(() => {
					this.specClass = 'none';
				}, 200);
			},
			hanldToCartPage(){
				this.$api.navigateTo('/seller/pages/cart/cart')
			},
			discard(){
				//丢弃
			}
		},
		onShow() {
			this.$api.getComStorage('orderUserInfo').then((resolve) =>{
				this.clubID = resolve.clubID
			})
			this.$api.getStorage().then((resolve) =>{
				this.serviceProviderId = resolve.serviceProviderID
				this.getClubProductNum()
			})
		},
	}
</script>

<style lang="scss">
	// 阶梯价格弹窗
	.jieti_box{
		width: 596rpx;
		// height: 496rpx;
		top: 10%;
		right: 50%;
		transform: translate(50%,50%);
		position: absolute;
		background:#FFFFFF;
		color: #000;
		font-size: 28rpx;
		border-radius: 10rpx;
		text-align: center;
		line-height: 84rpx;
		padding: 20rpx;
		z-index: 99999;
	}
	.jieti_box_one{
		// width: 548rpx;
		overflow: hidden;
		border: 2rpx solid #F5F5F5;
		border-bottom: 0;
		margin: auto;
	}
	.jieti_left{
		width: 298rpx;
		float: left;
		}
	.jieti_left view{
		height: 84rpx;
		border-bottom:2rpx solid #F5F5F5 ;
		border-right:2rpx solid #F5F5F5  ;
	}
	.ladder-a{
		letter-spacing: 4rpx;
	}
	.jieti_right{
			width: 294rpx;
			float: right;
		}
	.jieti_right view{
			height: 84rpx;
			border-bottom:2rpx solid #F5F5F5;
		}
	.addbtn{
			height: 84rpx;
			background: linear-gradient(135deg,rgba(242,143,49,1) 0%,rgba(225,86,22,1) 100%);
			text-align: center;
			font-size: 28rpx;
			color: #fff;
			line-height: 84rpx;
			border-radius: 10rpx;
			margin: 20rpx auto
		}
	.zuhe-content{
		display: inline-block;
	}
	.number-left{
		float: left;
	}
	.number-right{
		margin-left: 30rpx;
		float: left;
	}
	.number-left text{
		margin-left: 30rpx;
	}
	.number-right-text{
		color: #FF2A2A;
	}
	page {
		background: $sub-bg-color;
		.all-type-list-wrapper {
			display: flex;
			flex-direction: column;
		}
	}
	/* 加入购物模态层*/
	@keyframes showPopup {
		0% {
			opacity: 0;
		}
		100% {
			opacity: 1;
		}
	}
	@keyframes hidePopup {
		0% {
			opacity: 1;
		}
		100% {
			opacity: 0;
		}
	}
	@keyframes showLayer {
		0% {
			transform: translateY(0);
		}
		100% {
			transform: translateY(-100%);
		}
	}
	@keyframes hideLayer {
		0% {
			transform: translateY(-100%);
		}
		100% {
			transform: translateY(0);
		}
	}
	@keyframes showAmnation {
		0% {
			top: -12rpx;
			opacity: 0;
		}
		50% {
			top: -60rpx;
			opacity: 1;
		}
		100% {
			top: -100rpx;
			opacity: 0;
		}
	}
	@keyframes hideAmnation {
		0% {
			top: -100rpx;
			opacity: 0;
		}
		100% {
			top: -12rpx;
			opacity: 0;
		}
	}
	.popup {
		position: fixed;
		top: 0;
		width: 100%;
		height: 100%;
		z-index: 999;
		display: none;
		.mask{
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 21;
			background-color: rgba(0, 0, 0, 0.6);
		}
		.layer {
			position: fixed;
			z-index: 22;
			bottom: -360rpx;
			width: 702rpx;
			padding: 24rpx 24rpx 36rpx 24rpx;
			// height: 236rpx;
			border-radius: 20rpx 20rpx 0 0;
			background-color: #fff;
			display: flex;
			flex-wrap: wrap;
			align-content: space-between;
			.content {
				width: 100%;
				margin-top: 20rpx;
			}
			.btn {
				width: 100%;
				// height: 88rpx;
				display: flex;
				.button {
					width: 702rpx;
					height: 88rpx;
					color: #fff;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: $font-size-28;
					border-radius: 44rpx;
					background: $btn-confirm;
					margin-top: 20rpx;
				}
			}
		}
		.layer-nunbox-m{
			font-size: 28rpx;
		}
		&.show {
			display: block;
			.mask{
				animation: showPopup 0.2s linear both;
			}
			.layer {
				animation: showLayer 0.2s linear both;
			}
		}
		&.hide {
			display: block;
			.mask{
				animation: hidePopup 0.2s linear both;
			}

			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}
		&.none {
			display: none;
		}
		&.service {
			.row {
				margin: 30upx 0;
				.title {
					font-size: 30upx;
					margin: 10upx 0;
				}
				.description {
					font-size: 28upx;
					color: #999;
				}
			}
		}
		.layer-smimg{
			width: 158rpx;
			height: 158rpx;
			float: left;
			border-radius: 10rpx;
			margin-right: 24rpx;
			image{
				width: 158rpx;
				height: 158rpx;
				border-radius: 10rpx;
			}
		}
		.layer-nunbox{
			justify-content: space-between;
			align-items: center;
			width: 510rpx;
			height: 88rpx;
			padding: 10rpx 0 0 0;
			float: left;
			.layer-nunbox-t{
				width: 100%;
				height:44rpx;
				position:relative;
				display: flex;
				margin-top: 10rpx;
				.layer-nunbox-text{
					line-height: 44rpx;
					font-size: $font-size-28;
				}
				.number-box{
					display: flex;
					justify-content: center;
					align-items: center;
					border: 2rpx solid #ffe6dc;
					border-radius: 30rpx;
					height: 48rpx;
					margin-left: 20rpx;
					.iconfont{
						font-size: $font-size-24;
						padding:0 18rpx;
						color: #999999;
						text-align: center;
						line-height: 48rpx;
						font-weight: bold;
						background: #fef6f3;
						&.icon-jianhao{
							border-radius: 30rpx 0 0 30rpx;
						}
						&.icon-jiahao{
							border-radius: 0 30rpx 30rpx 0;
						}
					}
					.btn-input{
						width: 80rpx;
						height: 48rpx;
						line-height: 48rpx;
						background: #FFFFFF;
						border-radius: 4rpx;
						text-align: center;
						font-size: $font-size-28;
					}
				}
				.product-step{
					position: absolute;
					left: 45rpx;
					bottom: 0;
					height: 44rpx;
					background: #FFFFFF;
				}
			}
			.layer-nunbox-b{
				width: 100%;
				height:44rpx;
				margin-top: 13rpx;
			}
			.text{
				line-height: 44rpx;
				font-size: $font-size-28;
				.p{
					color: #FF2A2A;
				}
				.p:first-child{
					margin-left: 30rpx;
				}
				.p.sm{
					font-size: $font-size-24;
				}
			}
		}
	}

	.zuhe .layer{
		bottom: -270rpx;
		height: 200rpx;
	}
</style>
