<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="goodsMnemonic" labelWidth="150rpx">
					<u-input v-model="dataForm.goodsMnemonic" 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 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"  :checked="item.isChcked" @click="handleData(item2)" />
						<view class="goods-card-content">
							<view class="goods-card-content-item">
								<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.sid, item2.id)"></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>
	export default {
		data() {
			return {
				dataForm: {
					sourceOrderNo: "",
					companyMnemonic: "",
					goodsMnemonic: "",
					pageNum: 1,
					pageSize: 10
				},
				allableQty:0,
				allactualQty:0,
				dataList: [],
				sid:'',
				query: {
					current: 1,
					size: 20
				},
				bgGoodNoList:[],
				bgSpecificationsList:[],
				loadStatus: 'loadmore',
				detailIds:[],
			}
		},
		onLoad() {
			this.loadList();
		},
		//上拉刷新(当数据距离底部100时触发，距离在pages.json配置)
		onReachBottom() {
			this.loadStatus = "loading";
			setTimeout(() => {
				this.dataForm.pageNum += 1;
				this.loadList();
			}, 100);
		},
		methods: {
			handleData(item){
				item.isChcked=!item.isChcked
				// console.log(item.isChcked);
				if(item.isChcked){
					if(this.bgGoodNoList.length==0){
						this.detailIds=[]
						this.allableQty=0
						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+=item.actualQty
					}else{
						if(item.bgGoodNo!=this.bgGoodNoList[0] || item.bgSpecifications!=this.bgSpecificationsList[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=>{
						item1.businInOrderDVos.forEach(item2=>{
							if(item2.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=[]
						this.bgSpecificationsList=[]
					}
				} 
				
			},
			startReceive(){
					this.$u.route({
						url: 'pages/receive/receiveHomeWorks/receiveHomeWork',
						type: 'navigateTo',
						params: {
							sid: this.sid,
							detailId:this.detailIds[0],
							detailIds: JSON.stringify(this.detailIds),
							allableQty:this.allableQty,
							allactualQty:this.allactualQty
						}
					})
			},
			loadList() {
				this.$u.api.recevie.pageOrder(this.dataForm).then(res => {
					console.log(res);
					if (!res.rows || res.rows.length == 0) {
						if (this.dataForm.sourceOrderNo !== "") {
							this.$u.toast("扫描单号" + this.dataForm.sourceOrderNo + "不存在任务");
						}
						this.loadStatus = "nomore";
						return;
					}
					
					// console.log(res.rows);
					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(sid, detailId) {
				this.$u.route({
					url: 'pages/receive/receiveHomeWork/receiveHomeWork',
					type: 'navigateTo',
					params: {
						sid: sid,
						detailId: detailId
					}
				})
			},
			searchClick() {
				this.dataList = [];
				this.dataForm.pageNum = 1;
				this.loadList();
			},
		}
	}
</script>

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