<template>
	<view class="sh-content">
		<view class="sh-search">
			<u-form labelPosition="left" :model="dataForm" class="sh-form">
				<u-form-item label="单据编号" prop="sourceOrderNo" labelWidth="150rpx">
					<u-input v-model="dataForm.sourceOrderNo" border style="background-color: #ffffff;"
						placeholder="请输入或扫描单号"></u-input>
				</u-form-item>
				<u-form-item label="供货单位" prop="companyMnemonic" labelWidth="150rpx">
					<u-input v-model="dataForm.companyMnemonic" border style="background-color: #ffffff;"
						placeholder="请输入名称/助记码"></u-input>
				</u-form-item>
				<u-form-item label="商品条码" prop="barCode" labelWidth="150rpx">
					<u-input v-model="dataForm.barCode" border style="background-color: #ffffff;"
						placeholder="请输入或扫描条码"></u-input>
				</u-form-item>
			</u-form>
			<view class="sh-sub-search">
				<u-button class="sh-search-button" @click="searchClick()">搜索</u-button>
			</view>
		</view>
		
		<view class="all-card">
			<view class="sh-card" v-for="(item, index) in dataList" :keys="item.id" :index="item.id"
				v-if="dataList.length > 0" >
				<view class="sh-card-title" :class="{'borderBottom':!item.businInOrderDVos.length}">
					<view class="receipt-number">
						<text class="receipt-number-title">单据编号</text>
						<text class="receipt-number-value">{{item.sourceOrderNo}}</text>
					</view>
					<view class="card-content">
						<view class="card-content-item">
							<text class="item-title">业主名称</text>
							<text class="item-value">{{item.consignorName}}</text>
						</view>	
					</view>	
					
					<view class="card-content">
						<view class="card-content-item">
							<text class="item-title">供货单位</text>
							<text class="item-value">{{item.companyName}}</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.ablelQty}}</text>
						</view>
					</view>
				</view>
				<view    class="sh-card-content" v-for="(item2, index2) in item.businInOrderDVos" :key="item2.id"
					:index="item2.id">
					<view class="pad-content" :class="{'sh-card-title-border':(index2 + 1) < item.businInOrderDVos.length}">
						<checkbox v-if="" class="pad-checked" ref="testDom"  :checked="item2.isChcked" @click="handleData(item2,index)" />
						<view class="goods-card-content">
							<view class="goods-card-content-item" style="width: 90%;">
								<text class="goods-title">商品名称</text>
								<text class="goods-value">{{item2.bgGoodName}}</text>
							</view>
						</view>
						<view class="titleValue">
							<span class="title">规 格：</span>
							<span class="value">{{item2.bgSpecifications}}</span>
						</view>
						<view class="detail-icon">
							<u-icon size="40" name="../../static/icon/bg-right.png"  @click="goodDetail(item2)"></u-icon>
						</view>
						<view class="goods-card-content">
							<view class="goods-card-content-item">
								<text class="goods-title">订单数量</text><text class="goods-value">{{item2.qty}}</text>
							</view>
							<view class="goods-card-content-item">
								<text class="goods-title">未收货数量</text><text class="goods-value">{{item2.ableQty}}</text>
							</view>
						</view>
					</view>
				</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="startReceive">开始收货</button>
		</view>
		
		
	</view>
</template>

<script>
import { nextTick } from "vue";
	export default {
		data() {
			return {
				dataForm: {
					sourceOrderNo: "",
					companyMnemonic: "",
					barCode: "",
					pageNum: 1,
					pageSize: 10
				},
				allableQty:0,
				allactualQty:0,
				dataList: [],
				orderNoList:[],
				sid:'',
				query: {
					current: 1,
					size: 20
				},
				bgGoodNoList:[],
				orderNoList1:[],
				orderNoListOne:[],
				bgSpecificationsList:[],
				loadStatus: 'loadmore',
				detailIds:[],
			}
		},
		onLoad() {
			this.loadList();
		},
		//上拉刷新(当数据距离底部100时触发，距离在pages.json配置)
		onReachBottom() {
			this.loadStatus = "loading";
			setTimeout(() => {
				this.dataForm.pageNum += 1;
				this.loadList();
			}, 100);
		},
	methods: {
		handleOrderNoOne(item){
			// //单个订单的数组
			// this.orderNoListOne.push(item.orderNo)
			// //货物列表
			// this.bgGoodNoList.push(item.bgGoodNo)
			// //规格列表
			// this.bgSpecificationsList.push(item.bgSpecifications)
			// // this.sid=item.sid
			// this.detailIds.push(item.id)
			// //总未收货
			// this.allableQty=item.ableQty
			// //实际收货
			// // this.allactualQty+=Number(item.actualQty)
			// //订单列表
			// // ableQty:,packageQty:
			// this.orderNoList.push({orderNo: item.orderNo,id:item.sid,ableQty:this.allableQty,
			// detailIds:this.detailIds,actualQty:0,packageQty:0})
			// console.log(this.orderNoList);
			let _ = this;
			let detailIds = [];
			detailIds.push(item.id);
			let tempData = {
				orderNo: item.orderNo,
				id:item.sid,
				ableQty:item.ableQty,
				detailIds:detailIds,
				bgGoodNo:item.bgGoodNo,
				bgSpecifications:item.bgSpecifications,
				actualQty:0,
				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.allableQty+=item.ableQty
			// 	this.allactualQty+=Number(item.actualQty)
			// 	this.orderNoList[indexOfTree].detailIds.push(item.id)
			// 	this.orderNoList[indexOfTree].ableQty = this.allableQty
			// }
			
			_.orderNoList.forEach((itemData,index) =>{
				if(itemData.orderNo !== item.orderNo){
					_.handleOrderNoOne(item);
				}else{
					itemData.ableQty += item.ableQty;
					itemData.detailIds.push(item.id);
				}
			})
		},
	
		//选择打对勾
		handleData(item,index){
			let _ = this;
			// testDom
			item.isChcked=!item.isChcked
			// let childStyleList = [...this.$refs.testDom[index].$el.children[0].children[0].classList]
			// console.log(childStyleList);
			if(item.isChcked === true){
				if(_.orderNoList.length === 0) {
					_.handleOrderNoOne(item);
				}
				//orderNoList大于1时
				else if(_.orderNoList.length > 0)  {
				// let includeBgGoodNo = this.bgGoodNoList.includes(item.bgGoodNo) 
				// let includeBgSpecifications = this.bgSpecificationsList.includes(item.bgSpecifications)
				// 	if(includeBgGoodNo && includeBgSpecifications){
				// 		this.handleOrderNoMulti(item)
				// 	}else{
				// 		this.$u.toast('所选非同商品或同规格')
				// 		// item.isChcked = false
				// 		this.$nextTick(()=>{
				// 			this.$refs.testDom[index].$el.children[0].children[0].className='uni-checkbox-input'
				// 		})
						
				// 	}
				// }
					_.orderNoList.forEach((itemData,index1) =>{
						if(itemData.bgGoodNo !== item.bgGoodNo || itemData.bgSpecifications !== item.bgSpecifications){
							_.$u.toast('所选非同商品或同规格')
							// item.isChcked = false;
							// _.$nextTick(()=>{
							// 	_.$refs.testDom[index].$el.children[0].children[0].className='uni-checkbox-input'
							// })
						}else{
							_.handleOrderNoMulti(item);
						}
					})
				}
					
			}
			//判断减时
			if(item.isChcked === false){
				// const indexOfTree = this.orderNoList.findIndex(item1=> (item1.orderNo===item.orderNo));
				// if(indexOfTree === -1){
				// // this.handleOrderNoOne(item)
				// }else{
				// 	this.allableQty-=item.ableQty
				// 	// this.allactualQty-=Number(item.actualQty)
				// 	this.orderNoList[indexOfTree].detailIds.forEach((it1,ind2)=>{
				// 		if(it1 === item.id){
				// 		 this.orderNoList[indexOfTree].detailIds.splice(ind2,1)
				// 		 this.orderNoList[indexOfTree].ableQty = this.allableQty
				// 		 if(this.orderNoList[indexOfTree].detailIds.length === 0 ){
				// 			 this.orderNoList.splice(indexOfTree,1)
				// 		  }
				// 		 }
				// 		})
					
				// }
				
				_.orderNoList.forEach((itemData,index) =>{
					if(itemData.orderNo === item.orderNo){
						itemData.allableQty-=item.ableQty;
						itemData.detailIds.forEach((it1,ind2)=>{
							if(it1 === item.id){
								itemData.detailIds.splice(ind2,1)
								itemData.ableQty = _.allableQty
								if(itemData.detailIds.length === 0 ){
									_.orderNoList.splice(index,1);
								}
							}
						})
					}
				})
	
			} 
		},
		
		
		startReceive(){
				// this.allableQty=0
				// this.allactualQty=0
				this.$u.route({
					url: 'pages/receive/receiveHomeWorks/receiveHomeWork',
					type: 'navigateTo',
					params: {
						sid: this.orderNoList[0].id,
						detailId:this.orderNoList[0].detailIds[0],
						detailIds: JSON.stringify(this.detailIds),
						allableQty:this.allableQty,
						allactualQty:Number(this.allactualQty),
						orderNoList:JSON.stringify(this.orderNoList)
					}
				})
				
		},
		loadList() {
			this.$u.api.recevie.pageOrder(this.dataForm).then(res => {
				if (!res.rows || res.rows.length == 0) {
					if (this.dataForm.sourceOrderNo !== "") {
						this.$u.toast("扫描单号" + this.dataForm.sourceOrderNo + "不存在任务");
					}
					this.loadStatus = "nomore";
					return;
				}
				
				res.rows.forEach(item=>{
					item.businInOrderDVos.forEach(item1=>{
						item1.isChcked=false 
					})
				})
				this.dataList = this.dataList.concat(res.rows);
				
				this.loadStatus = "loadmore";
			});
		},
		//sId：汇总id，detailId：明细id
		goodDetail(item) {
			this.orderNoList1=[]
			this.detailIds=[]
			this.detailIds.push(item.id)
			
			this.orderNoList1.push({orderNo: item.orderNo,id:item.sid,ableQty:item.ableQty,allactualQty:Number(item.actualQty),
			detailIds:this.detailIds,actualQty:0,packageQty:0})
			this.$u.route({
				url: 'pages/receive/receiveHomeWorks/receiveHomeWork',
				type: 'navigateTo',
				params: {
					sid: item.sid,
					detailId: item.id,
					detailIds: JSON.stringify(this.detailIds),
					allableQty:item.ableQty,
					allactualQty:Number(item.actualQty),
					orderNoList:JSON.stringify(this.orderNoList1)
				}
			})
		},
		searchClick() {
			this.dataList = [];
			this.dataForm.pageNum = 1;
			this.orderNoList=[]
			this.allableQty=0
			this.loadList();
		},
		}
	}
</script>

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