<template>
	<view class="sh-content">
		<view class="sh-search">
			<u-form labelPosition="left" :model="dataForm" class="sh-form">
				<u-form-item label="商品条码" prop="barCode" labelWidth="150rpx">
					<u-input v-model="dataForm.barCode" border style="background-color: #ffffff;"
						placeholder="请输入或扫描条码" @input="changeCode()"></u-input>
				</u-form-item>
			</u-form>

		</view>
    <!-- {{data}} -->
		<view class="sh-card-center">
			
		
		<view class="sh-card" v-for="(item, index) in dataList" :key="item.id" :index="item.id"
			v-if="dataList.length > 0">
			
		
			<view class="sh-card-title" >
				
				<u-checkbox-group class="pad-checked" @change="checkboxGroupChange(item)">
							<u-checkbox 
								
								@change="handleData(item,index)"
								v-model="item.isChcked" 
							></u-checkbox>
				</u-checkbox-group>
				<!-- <checkbox v-if="" class="pad-checked"  :checked="item.isChcked" @click="handleData(item)" /> -->
				
				<view class="sh-center">
				<view class="receipt-number">
					<text class="receipt-number-title">收货单号</text>
					<text class="receipt-number-value">{{item.orderNo}}</text>
				</view>
				<view class="card-content">
					<view class="card-content-item">
						<text class="item-title">商品条码</text>
						<text class="item-value">{{item.barCode}}</text>
					</view>	
				</view>
				<view class="card-content">
					<view class="card-content-item">
						<text class="item-title">商品编码</text>
						<text class="item-value">{{item.goodNo}}</text>
					</view>	
				</view>
				<view class="card-content"><view class="card-content-item">
						<text class="item-title">商品名称</text>
						<text class="item-value">{{item.goodName}}</text>
					</view></view>
				<view class="card-content">
					<view class="card-content-item">
						<text class="item-title">收货数量</text>
						<text class="item-value">{{item.qty}}</text>
					</view>
					<view class="card-content-item">
						<text class="item-title">未验数量</text>
						<text class="item-value">{{item.ableQty}}</text>
					</view>
				</view>
				</view>
				<u-icon size="40" name="../../static/icon/bg-right.png"  @click="goodDetail(item.sid, item.id)"></u-icon>
			</view>
		
    </view>
	
		<u-empty class="no-list" mode="list" v-else>
		</u-empty>
		<view class="loadmore-data">
			<u-loadmore :status="loadStatus"></u-loadmore>
		</view>
		</view>
		
		<view class="enter-button">
			<button class="smell-button" @click="startCheckAccept">开始验收</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataForm: {
					barCode: "" // 商品条码
				},
				dataList: [],
				refreshIfNeeded: false,
				sid:'',
				did:'',
				query: {
					pageNum: 1,
					pageSize: 10
				},
				loadStatus: 'loadmore',
				dicOptions: [],
				bgGoodNoList:[],
				orderNoList:[],
				detailIds:[]
			}
		},
		onPullDownRefresh() {
			this.query.pageNum = 1
			setTimeout(() => {
			        //结束下拉刷新
			      uni.stopPullDownRefresh ();
				  // this.allableQty=0
				  this.dataList=[]
				  this.bgGoodNoList=[]
				  this.getDataList();
			    }, 1000);
			
		},
		onLoad() {
			// uni.startPullDownRefresh()
			this.getDataList();
			this.getDic()
		},
		//上拉刷新(当数据距离底部100时触发，距离在pages.json配置)
		onReachBottom() {
			this.loadStatus = "loading";
			setTimeout(() => {
				//当前页 +1
				this.query.pageNum += 1;
				this.getDataList();
			}, 100);
		},
    onShow() {
		// uni.startPullDownRefresh()
        var pages = getCurrentPages(); // 获取当前页面栈
        var currentPage = pages[pages.length - 1]; // 当前页面
        // console.log(this.refreshIfNeeded)
        if ( currentPage._data.refreshIfNeeded) {
          currentPage._data.refreshIfNeeded = false;
            this.query.pageNum = 1
          this.getDataList();; // 调用列表重置刷新页面
          // this.mescroll.scrollTo(0,0); // 滚动页面置顶
        }
      },
		methods: {
			
			//选择打对勾
			handleData(item,index){
				let _ = this;
				// testDom
				item.isChcked=!item.isChcked
				if(item.isChcked === true){
					if(_.orderNoList.length === 0) {
						_.handleOrderNoOne(item);
					}
					//orderNoList大于1时
					else if(_.orderNoList.length > 0)  {
						if( _.orderNoList[0].goodNo === item.goodNo ){
							this.handleOrderNoMulti(item)
						}else{
							this.$u.toast('所选非同商品编码')
							this.k = 1
						}
					}
						
				}
				//判断减时
				if(item.isChcked === false){
					_.orderNoList.forEach((itemData,index) =>{
						if(itemData.orderNo === item.orderNo){
							itemData.allableQty-=item.ableQty;
							// itemData.allableQty-=item.ableQty;
							itemData.detailIds.forEach((it1,ind2)=>{
								if(it1 === item.id){
									itemData.detailIds.splice(ind2,1)
									itemData.ableQty = _.ableQty
									itemData.actualQty = _.ableQty
									if(itemData.detailIds.length === 0 ){
										_.orderNoList.splice(index,1);
									}
								}
							})
						}
					})
				
				} 
			},
			checkboxGroupChange(item2){
				if(this.k == 1){
					item2.isChcked=!item2.isChcked
					this.k = 0
				}
			},
			//单个
			handleOrderNoOne(item){
				let _ = this;
				let detailIds = [];
				detailIds.push(item.id);
				let tempData = {
					orderNo: item.orderNo,
					id:item.sid,
					ableQty:item.ableQty,
					detailIds:detailIds,
					goodNo:item.goodNo,
					bgGoodName:item.bgGoodName,
					actualQty:item.ableQty,
					packageQty:0
				};
				_.orderNoList.push(tempData);
			},
			//增加时多个选择
			handleOrderNoMulti(item){
				let _ = this;
				//判断orderList里订单是否重复
				const indexOfTree  = this.orderNoList.findIndex(item1=> item1.orderNo === item.orderNo);	
				if(indexOfTree === -1){
				this.handleOrderNoOne(item)
				}else{
					this.orderNoList[indexOfTree].ableQty += item.ableQty;
					this.orderNoList[indexOfTree].actualQty += item.ableQty;
					this.orderNoList[indexOfTree].detailIds.push(item.id);
				}
			
			},
			//选择
			// handleData(item){
			// 	item.isChcked=!item.isChcked
			// 	// console.log(item.isChcked);
			// 	console.log(item);
			// 	if(item.isChcked){
			// 		if(this.bgGoodNoList.length==0){
			// 			this.detailIds=[]
			// 			this.allableQty=0
			// 			this.bgGoodNoList.push(item.orderNo)
			// 			this.sid=item.sid
			// 			this.detailIds.push(item.id)
			// 			this.allableQty+=item.ableQty
						
			// 		}else{
			// 			if(item.orderNo!=this.bgGoodNoList[0] ){
			// 				// item.isChcked=!item.isChcked
			// 				this.$u.toast('所选非同收货单号')
							
			// 			}else{
			// 			this.detailIds.push(item.id)
			// 			this.allableQty+=item.ableQty
			// 			// this.allactualQty+=item.actualQty
			// 			}
			// 		}
			// 	}else{
			// 		let len=0
			// 		this.dataList.forEach(item1=>{
			// 			// console.log(item1);
			// 				if(item1.isChcked==true){
			// 					len++
			// 				}

			// 		})
			// 		this.detailIds.forEach((item1,index1)=>{
			// 			if(item1==item.id){
			// 			this.detailIds.splice(index1,1)
			// 			this.allableQty-=item.ableQty
			// 			// this.allactualQty-=item.actualQty
			// 			}
			// 		})
			// 		if(len==0){
			// 			this.bgGoodNoList=[]
			// 		}
			// 	} 
				
			// },
			startCheckAccept(){
					this.$u.route({
						url: 'pages/checkAccept/checkHomeworks',
						type: 'navigateTo',
						params: {
							did:this.orderNoList[0].detailIds[0],
							sid:this.orderNoList[0].id,
							// detailIds: JSON.stringify(this.detailIds),
							allableQty:this.ableQty,
							orderNoList: JSON.stringify(this.orderNoList)
							// allactualQty:this.allactualQty
						}
					})
			},
			getDic() {
				this.$u.api.getByDicType().then(res => {
					this.dicOptions = res.data;
				});
			},
			transUnit(unit) {
				for (let dic in this.dicOptions) {
					if (this.dicOptions[dic].dictValue === unit) {
						return this.dicOptions[dic].dictLabel;
					}
				}
			},
			changeCode() {
				this.query = {
					pageNum: 1,
					pageSize: 10
				}
				this.getDataList()
			},
			//初始化数据
			getDataList() {
				uni.stopPullDownRefresh();
				this.loadStatus = "loadmore";
				this.$u.api.acceptance.pageReceiving({
					...this.query,
					...this.dataForm
				}).then(res => {
					res.rows.forEach(item=>{
							item.isChcked=false 
					})
					if (!res.rows || res.rows.length == 0) {
						this.loadStatus = "nomore";
						return;
					}
					if (res.code === 200&&res.rows.length!==0) {
					this.dataList = [...this.dataList,...res.rows]
					}
					
				});
				this.$u.toast("数据列表已刷新");
			},
			goodDetail(sid, did) {
				this.$u.toast("你点击了商品详情");
				this.$u.route({
					url: 'pages/checkAccept/checkHomework',
					type: 'navigateTo',
					params: {
						sid,
						did
					}
				})
			},
			searchClick() {
				this.$u.toast("你点击了搜索");
				this.$u.route({
					url: 'pages/checkAccept/taskView',
					type: 'navigateTo'
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "./index.scss";
</style>
