<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}">
						<!-- :name="item.name" -->
						<!-- <checkbox v-if="" class="pad-checked" ref="testDom"  :checked="item2.isChcked" @click="handleData(item2,index)" /> -->
						<u-checkbox-group class="pad-checked" @change="checkboxGroupChange(item2)">
									<u-checkbox 
										
										@change="handleData(item2,index)"
										v-model="item2.isChcked" 
									></u-checkbox>
						</u-checkbox-group>
						<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
				},
				k:0,
				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){
			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,
				// bgGoodNo:item.bgGoodNo,
				bgGoodName:item.bgGoodName,
				// bgSpecifications:item.bgSpecifications,
				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);
			}

		},
		checkboxGroupChange(item2){
			if(this.k == 1){
				item2.isChcked=!item2.isChcked
				this.k = 0
			}
		},
		//选择打对勾
		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)  {
				// let includeBgGoodNo = this.bgGoodNoList.includes(item.bgGoodNo) 
				// let includeBgSpecifications = this.bgSpecificationsList.includes(item.bgSpecifications)
					if( _.orderNoList[0].bgGoodNo === item.bgGoodNo && _.orderNoList[0].bgSpecifications  === item.bgSpecifications){
						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.actualQty-=item.ableQty;
						itemData.detailIds.forEach((it1,ind2)=>{
							if(it1 === item.id){
								itemData.detailIds.splice(ind2,1)
								itemData.ableQty = _.allableQty
								itemData.actualQty= _.allableQty
								if(itemData.detailIds.length === 0 ){
									_.orderNoList.splice(index,1);
								}
							}
						})
					}
				})
	
			} 
		},
		
		
		startReceive(){
				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=[]
			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,
				bgGoodName:item.bgGoodName,
				actualQty:0,
				packageQty:0
			};
			// {orderNo: item.orderNo,id:item.sid,ableQty:item.ableQty,allactualQty:Number(item.actualQty),
			// detailIds:this.detailIds,actualQty:0,packageQty:0}
			// _.orderNoList.push(tempData);
			this.orderNoList1.push(tempData)
			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>
