<template>
	<view class="container cart clearfix"> 
		<view class="container-cart-main">
			<view class="container-cart">
				<view class="cart-content" :style="{paddingBottom :isIphoneX ? '130rpx' : '100rpx'}">
					<view class="goods-title">请选择合适的商品数量进行发货</view>
					<view class="goods-list">
						<view 	class="goods-pros" v-for="(pros,idx) in productsList" :key="idx" >
							<view class="goods-pros-t"  @click.stop="ischeck(pros)">
								<!--选择商品-->
								<view class="checkbox-box">
									<button class="checkbox iconfont" :class="[pros.checked ?'icon-gouxuanl':'icon-weigouxuan']"></button>
								</view>
								<view class="pros-img"><image :src="pros.productImage ? pros.productImage:''" alt="" /></view>
								<view class="pros-product">
									<view class="producttitle">{{pros.name}}</view>
									<view class="productspec">规格:{{pros.productUnit ? pros.productUnit : ''}}</view>
									<view class="productspec">商品编码:{{pros.productNo ? pros.productNo : ''}}</view>
									<view class="product-view">
										<view class="view-num">数量：{{pros.num+pros.presentNum}}</view>
									</view>
									<view class="product-view">
										<view class="view-num">已发货：{{pros.shipmentsNum}}</view>
										<view class="view-num">未发货：{{pros.notOutStore-pros.actualCancelNum}}</view>
									</view>
									<view class="product-view">
										<view class="view-num">已退货：{{pros.returnedNum}}</view>
										<view class="view-num">已取消：{{pros.actualCancelNum}}</view>
									</view>
								</view>	
							</view>
							<view class="goods-pros-b">
								<view class="productprice">
									<view class="text">本次发货</view>
									<view class="count">
										<view class="number-box">
											<view  class="iconfont icon-jianhao" :class="[pros.isReduceNum ? 'disabled':'']" @click="changeCountSub(pros)"></view>
											<input class="btn-input" type="number" maxlength='4' v-model="pros.shipmentCount" @blur="changeNnmber($event,pros)">
											<view  class="iconfont icon-jiahao"  :class="[pros.isPlusNum ?'disabled':'']" @click="changeCountAdd(pros)"></view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>	
				</view>
				<!-- 脚部菜单 -->
				<view class="footer" :style="{paddingBottom :isIphoneX ? '68rpx' : '0rpx'}">
					<view class="footer-le">
						<view class="foot-check checkbox-box" @tap.stop="checkAll()">
							<button class="checkbox iconfont" :class="[isCheckAll?'icon-gouxuan':'icon-weigouxuan']"></button> 
							<view class="text">全选</view>
						</view>
					</view>
					<view class="footer-ri" >
						<view class="btn" @tap="toConfirmDeliver">去发货</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import authorize from '@/common/config/authorize.js'
	import { mapState,mapMutations } from 'vuex';
	
	export default{
		data(){
			return{
				CustomBar:this.CustomBar,// 顶部导航栏高度
				isIphoneX:this.$store.state.isIphoneX,
				userID:'',
				shopOrderId:'',
				alertType:'',
				productsList:[],//发货的商品
				setGoodData:'', //提交发货的商品
				isCheckAll:false,//是否全选
				isModallayer:false,
				isDisabled: false, // 供应商/店铺全选是否禁用状态
				isNoConfim:false,
			}
		},
		onLoad(option){
			console.log(option)
			this.shopOrderId = option.shopOrderId
			this.initShopOrderShipmentsInfo();
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods:{
			initShopOrderShipmentsInfo(){//初始化购物车 index:1
				this.ShopService.ShopOrderShipmentsInfo({ shopOrderId : this.shopOrderId }).then(response =>{
					let orderProductList = response.data.orderProductList
					let elObject = {}
					let isReduceNum = false
					let creatProductList = []
					orderProductList.forEach(el =>{
						let uninNum = el.notOutStore - el.actualCancelNum//未发货数量
						if(uninNum == 1){
							isReduceNum = true
						}else{
							isReduceNum = false
						}
						elObject = {
							shipmentCount:uninNum,
							isReduceNum:isReduceNum,
							checked:false
						}
						creatProductList.push(Object.assign({},el,elObject))
					})
					this.productsList = creatProductList
					console.log(this.productsList)
				}).catch(error =>{
					this.$util.msg(error.msg,2000);
				})
				
			},		
			ischeck(pro){//为未选中的时候改变为true，反之为true
				pro.checked = !pro.checked;
				this.updateCheckAllBtn();
			},
			updateCheckAllBtn() {// 全选勾选判断
				let goodsCheckedLength = 0,
					productsList = this.productsList;
				productsList.forEach(item => {
					if(item.checked) { goodsCheckedLength++; }
				})
				this.isCheckAll = goodsCheckedLength === productsList.length;
			},
			updateBothCheckBtn() {
				this.productsList.forEach((item)=>{
					item.checked = this.isCheckAll ;
				})
			},
			checkAll(){//全选方法内调用方法
			    this.isCheckAll = !this.isCheckAll;
				this.updateBothCheckBtn();     
			},
			changeCountAdd(pros){//商品数量加加
				let uninNum = pros.notOutStore-pros.actualCancelNum//未发货数量
				if(pros.shipmentCount == uninNum){
					this.isNoneNum =true
					return
				}else{
					pros.shipmentCount++
					this.isNoneNum =false
				}
			},
			changeCountSub(pros){//商品数量减减
				let uninNum = pros.notOutStore-pros.actualCancelNum//未发货数量
				if(pros.shipmentCount == 1){
					pros.shipmentCount = pros.shipmentCount
					return
				}else{
					pros.shipmentCount--
				}
			},
			changeNnmber(e,pros){//输入商品数量更新
				let uninNum = pros.notOutStore-pros.actualCancelNum//未发货数量
				let _value = e.detail.value;
				if(!this.$api.isNumber(_value) || uninNum == 1){
					pros.shipmentCount = uninNum
				}else if(_value > uninNum){	
					pros.shipmentCount = uninNum
				}else{
					pros.shipmentCount = e.detail.value
				}
			},
			toConfirmDeliver(){//添加物流页面
				let setProductList=[];
				this.productsList.forEach(el=>{
					if(el.checked){
						let elObject = {
								orderProductId:el.orderProductID.toString(),
								num:el.shipmentCount.toString()
						}
						setProductList.push(elObject)
					}
				})
				if(setProductList == ''){
					this.$util.msg("请先选择商品~",2000);
					return
				}else{
					this.$api.navigateTo(`/supplier/pages/deliver/add-logistics?shopOrderId=${this.shopOrderId}&data=${JSON.stringify(setProductList)}`)
				}
			},
		},
		onPullDownRefresh() {//下拉刷新
			// this.initShopOrderShipmentsInfo()
			// uni.stopPullDownRefresh()
		},
		onShow(){
			
		},
	}
</script>

<style lang="scss">
	page{
		background: #f7f7f7;
		height: auto;
	}
	.cart-content{
		position: relative;
	}
	.goods-title{
		width: 702rpx;
		padding: 0 24rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: left;
		font-size: $font-size-28;
		color: $color-system;
		background-color: rgba(225, 86, 22, 0.17);
	}
	.checkbox-box{
		display: flex;
		align-items: center;
		.checkbox{
			display: flex;
			margin: 0;
			padding: 0;
			display: flex;
			flex-direction: column;
			align-items: center;
			box-sizing: border-box;
			text-align: center;
			text-decoration: none;
			border-radius: 0;
			-webkit-tap-highlight-color: transparent;
			overflow: hidden;
			background-color:#FFFFFF;
			font-size: 36rpx;
			color:$color-system;
		}
		&.disabled{
			.checkbox{
				color:#999999
			}
		}
		.text{
			font-size: $font-size-24;
			margin-left: 10rpx;
		}
	}
	.goods-list{
		width: 100%;
		height: auto;
		background-color: #F7F7F7;
		margin-top: 24rpx;
		.goods-item{
			width: 702rpx;
			padding: 0 24rpx;
			background: #FFFFFF;
			margin-bottom: 24rpx;
		}
		.goods-pros{
			width: 702rpx;
			padding: 0 24rpx;
			background: #FFFFFF;
			margin-bottom: 24rpx;
		}	
		.goods-pros-t{
			display: flex;
			width: 100%;
			height: auto;
			padding:20rpx 0;
			.checkbox-box{
				padding: 10rpx;
			}
			.pros-img{
				width: 210rpx;
				height: 210rpx;
				border-radius: 10rpx;
				margin:0 20rpx;
				border:1px solid #f3f3f3;
				image{
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
		.goods-pros-b{
			width:622rpx;
			height: 80rpx;
			margin-left: 84rpx;
			border-top: 1px solid #F7F7F7;
			position: relative;
			.productprice{
				height: 48rpx;
				width: 100%;
				margin-top: 15rpx;
				.text{
					line-height: 48rpx;
					float: left;
					color: $text-color;
					font-size: $font-size-28;
				}
				.count{
					height: 100%;
					float: right;
					position: relative;
					&.show{
						display: block;
					}
					&.none{
						display: none;
					}
					.number-box{
						display: flex;
						justify-content: center;
						align-items: center;
						.iconfont{
							font-size: $font-size-24;
							padding:0 20rpx;
							color: $text-color;
							text-align: center;
							line-height: 48rpx;
							font-weight: bold;
							&.disabled{
								color: #999999;
							}
						}
						.btn-input{
							width: 62rpx;
							height: 48rpx;
							line-height: 48rpx;
							background: #F8F8F8;
							border-radius: 4rpx;
							text-align: center;
							font-size: $font-size-24;
						}
					}
					.uni-numbox{
						position: absolute;
						left: 45rpx;
						bottom: 0;
						.uni-numbox-minus, .uni-numbox-plus{
							width: 50rpx;
							line-height: 40rpx;
						}
						.uni-numbox-value {
							font-size: $font-size-28;
							width: 60rpx;
						}
					}
				}
			}
		}
		.pros-product{
			width: 402rpx;
			height: 100%;
			line-height: 36rpx;
			font-size: $font-size-28;	
			position: relative;
			.producttitle{
				width: 100%;
				display: inline-block;
				height: auto;							
				text-overflow:ellipsis;
				display: -webkit-box;
				word-break: break-all;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
				margin-bottom: 8rpx;
			}
			.productspec{
				height: 44rpx;
				color: #999999;
				line-height: 44rpx;
				font-size: $font-size-26;
			}
			.product-view{
				width: 100%;
				height: auto;
				display: flex;
				.view-num{
					flex: 1;
					text-align: left;
					font-size: $font-size-26;
					color: #666666;
					line-height: 44rpx;
				}
			}
		}
	}
	.footer{
		width: 100%;
		background-color: #FFFFFF;
		height: 110rpx;
		position: fixed;
		bottom: 0rpx;
		z-index: 100;
		.footer-le{
			width: 490rpx;
			height: 100%;
			padding:0 30rpx;
			float: left;
			.text{
				font-weight: bold;
			}
			.foot-check{
				width: 100rpx;
				float: left;
				line-height: 110rpx;
				font-size: $font-size-24;
				.checkbox{
					width: 40rpx;
					text-align: center;
				}
				.text{
					width: 60rpx;
					float: right;
				}
			}
			.foot-check-delbtn{
				float: left;
				.delBtn{
					margin: 0;
					padding: 0;
					display: flex;
					flex-direction: column;
					align-items: center;
					box-sizing: border-box;
					font-size: $font-size-24;
					text-align: center;
					text-decoration: none;
					border-radius: 0;
					-webkit-tap-highlight-color: transparent;
					overflow: hidden;
					background-color:#FFFFFF;
					color: #FF2A2A;
					padding: 0 24rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					line-height: 110rpx;
					font-weight: bold;
					&.none{
						display: none;	
					}
				}
			}
			.sum{
				font-size: $font-size-28;
				line-height: 110rpx;
				color: $text-color;
				display: flex;
				justify-content: flex-end;
				.money{
					color: #FF2A2A;
				}
				.money-sign{
					font-size: $font-size-24;
					color: #FF2A2A;
				}
			}
		}
		.footer-ri{
			width: 200rpx;
			height: 100%;
			background:linear-gradient(135deg,rgba(242,143,49,1) 0%,rgba(225,86,22,1) 100%);
			float: right;
			display: flex;
			justify-content: space-between;
			align-items: center;
			z-index: 999;
			&.none{
				display: none;
			}
			.btn{
				width: 200rpx;
				height: 100%;
				font-size: $font-size-28;
				line-height: 110rpx;
				color: #FFFFFF;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
</style>