<!-- 发料任务详情 -->
<template>
	<page-meta root-font-size="16px"></page-meta>
	<view class="">
		<view class="top_card">
			<com-job-scan-detail :jobContent="datacontent" :allCount="allCount" :scanCount="scanCount">
			</com-job-scan-detail>
		</view>
		<scroll-view :scroll-top="scrollTop" scroll-y="true" @scrolltoupper="upper" @scrolltolower="lower"
			@scroll="scroll" class="scroll-detail">

			<view class="detail-list margin_top" v-for="(item, index) in details" :key="item.id">
				<!-- <comDeliverBoardDetail :dataContent="item" :isShowPacking="true"></comDeliverBoardDetail> -->
				<view class="detail-content">
					<view class="choose_main">
						<view class="ljh_box">
							<view class="tit_ljh">{{ item.itemCode }}</view>
							<view class="ljh_left desc_ljh">
								<view class="font_xs text_lightblue">{{ item.itemName }}</view>
								<view class="font_xs text_lightblue">{{ item.itemDesc1 }}</view>
							</view>
						</view>
						<view class="list_form">
							<view class="uni-container">
								<uni-table style="overflow-x: hidden;">
									<uni-tr>
										<!-- <uni-th width="90"></uni-th>
										<uni-th width="100" align="center">推荐</uni-th>
										<uni-th width="100" align="center">实际</uni-th> -->
										<uni-th width="50"></uni-th>
										<uni-th width="120" align="center">推荐</uni-th>
										<uni-th width="120" align="center">实际</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="50">器具码</uni-th>
										<uni-th width="120" align="center">
											<view class="text_packingCode">
												{{ item.recommendContainerCode }}
											</view>
										</uni-th>
										<uni-th width="120" align="center">
											<view v-if="item.scaned" class="text_packingCode">
												{{ item.containerCode }}
											</view>
										</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="50">数量</text></uni-th>
										<uni-th width="120" align="center">
											<!-- <text class="text_black">{{item.recommendQty}}({{item.uom}})</text> -->
										</uni-th>
										<uni-th width="120" align="center">
											<view v-if="item.scaned"
												style="display: flex;flex-direction: row;justify-content:center;align-items: center;">
				
												<com-number-box ref="comNumberBox" v-model="item.handledQty"
													:max="99999" :min="0" style='width: 100px;'
													@change="qtyChanged($event,item,index)">
												</com-number-box>
												<text class="text_black">({{item.uom}})</text>
											</view>
										</uni-th>
									</uni-tr>
								</uni-table>
							</view>
						</view>
					</view>
					<view class="choose_marked" v-if="item.scaned">
						<image src="@/static/image_marked.svg"></image>
					</view>
					<view class="fr" >
						<button class="btn_single" hover-class="btn_single_after" @click="unPacking(item)">拆箱</button>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="uni-flex uni-row new_btn_bot">
			<button class="new_clear_btn btn_double" @click="clear()">清空</button>
			<button class="new_save_btn btn_double" @click="submit()">提交</button>
		</view>
		<com-balance ref='issueitems' @selectedItem='selectedBalanceItem'></com-balance>
		<win-scan-button @goScan='openScanPopup'></win-scan-button>
		<winScanByContainer ref="scanPopup" @getScanResult='getScanResult'></winScanByContainer>
		<comUnPacking ref='comUnPacking'
			@getvalue="getUnPackingCount" title="拆箱">
		</comUnPacking>
		<com-message ref="comMessage" @afterCloseCommitMessage='closeCommitMessage()'
			@afterCloseScanMessage='closeScanMessage'></com-message>
	</view>
</template>

<script>
	import {
		getDeliverJobDetail,
		takeDeliverBoardJob,
		cancelTakeDeliverBoardJob,
		finshIssueJob,
		issueBalances
	} from '@/api/index.js';
	import {
		getJobStatuStyle,
		getJobStatuDesc,
		showConfirmMsg,
		goHome,
		compareDesc,
		compareStr,
		getCurrDateTime,
		navigateBack
	} from '@/common/basic.js';
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winScanByContainer from '@/mycomponents/wincom/winScanByContainer.vue'
	import comBalance from '@/mycomponents/common/comBalance.vue'
	import comJobScanDetail from '@/mycomponents/comjob/comJobScanDetail.vue'
	import comDeliverBoardDetail from '@/mycomponents/coms/comDeliverBoardDetail.vue'
	import comNumberBox from '@/mycomponents/common/comNumberBox.vue';
	import comUnPacking from '@/mycomponents/coms/comUnPacking.vue'
	
	export default {
		components: {
			comMessage,
			winScanButton,
			winScanByContainer,
			comBalance,
			comJobScanDetail,
			comDeliverBoardDetail,
			comNumberBox,
			comUnPacking
		},
		data() {
			return {
				id: "",
				datacontent: {},
				details: {},
				toLocation: '',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				ispending: false,
				balancesItem: null,
				currentItem: null,
				allCount: 0,
				scanCount: 0,
				jobStatus: ""

			}
		},
		onLoad(param) {
			this.id = param.id;
			if (param.jobStatus == 1) {
				this.receive((callback => {
					this.getDetail();
				}));
			} else {
				this.getDetail();
			}
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			} else if (e.index === 1) {
				window.location.reload();
			}
		},

		onBackPress(e) {
			//已经接收但是没提交任务
			if (e.from == 'backbutton') {
				if (this.jobStatus == 2) {
					//取消承接任务	
					cancelTakeDeliverBoardJob(this.id).then(res => {
						uni.navigateBack();
					}).catch(error => {
						uni.navigateBack();
					})
				} else {
					uni.navigateBack();
				}
				return true;
			}
		},

		mounted() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: "#5A7CF3 !important"
			})
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			//加载零件信息
			getDetail() {
				let that = this;
				if (that.id == undefined) {
					return;
				}
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let params = {
					id: that.id,
				};
				getDeliverJobDetail(params)
					.then(item => {
						console.log('item', item);
						that.datacontent = item;
						that.details = item.details;
						that.details[0].recommendContainerCode = "C19"
						that.jobStatus = item.jobStatus
						if (that.details != null) {
							that.details.forEach(
								r => {
									r.scaned = false;
									r.scanDate = new Date()
								}
							);
							that.ispending = item.jobStatus === 2;
							// if (that.toLocation === '') {
							// 	that.toLocation = item.details[0].requestLocationCode;
							// }
							this.allCount = that.details.length;
							this.scanCount = 0;

						}
						uni.hideLoading();
					})
					.catch(err => {
						this.showMessage('未查找到详细信息')

						uni.hideLoading();
					});
			},

			getScanResult(result) {
				//零件是否已经被扫描过
				let item = this.details.find(r => r.recommendContainerCode == result.containerCode);
				if (item == undefined) {
					this.showMessage("器具码【" + result.containerCode + "】不在列表中")
				} else {
					if (item.scaned) {
						this.showMessage("器具码【" + result.containerCode + "】已经扫描")
					} else {
						if (this.allCount == this.scanCount) {
							this.showMessage("零件已经全部扫描完成");
							return;
						}
						item.containerCode = result.containerCode;
						item.scaned = true;
						item.handleQty = 10;
						this.calcScanCount()
						this.$forceUpdate();
					}
				}
			},

			// //接收
			receive(callback) {
				let params = {
					id: this.id
				};
				takeDeliverBoardJob(params)
					.then(res => {
						callback(true);
					})
					.catch(err => {
						this.showMessage(err.message);
						callback(false);
					});
			},
			calcScanCount() {
				this.scanCount = this.details.filter(r => r.scaned === true).length;
				this.closeScanPopup();
			},

			submit() {
				this.calcScanCount()
				if (this.scanCount != this.allCount) {
					this.showMessage('当前扫描数量【'+this.scanCount+"】,总数量为【"+this.allCount+"】,未全部扫描");
					return;
				}
				uni.showLoading({
					title: "提交中...",
					mask: true
				});
				let params = that.datacontent;
				// finshIssueJob(that.id, params)
				// 	.then(res => {
				// 		uni.hideLoading();
				// 		if (res != null) {
				// 			that.showCommitSuccessMessage();
				// 			that.backJobList();
				// 		}
				// 	})
				// 	.catch(err => {
				// 		that.showMessage(err.message);
				// 		uni.hideLoading();
				// 	});
			},

			//返回任务列表页
			backJobList() {
				navigateBack(1)
			},

			clear() {
				this.scanCount = 0;
				this.getDetail();
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			showScanMessage(message) {
				this.$refs.comMessage.showScanMessage(message);
			},

			closeScanMessage() {
				this.$refs.scanPopup.getfocus();
			},

			calcScanCount() {
				this.scanCount = this.details.filter(r => r.scaned === true).length;
				this.closeScanPopup();
			},

			showCommitSuccessMessage() {
				this.$refs.comMessage.showCommitSuccess();
			},

			closeCommitMessage() {
				this.backJobList(0); //点关闭直接返回列表
				uni.hideLoading();
			},

			closeScanPopup() {
				if (this.allCount == this.scanCount) {
					this.$refs.scanPopup.closeScanPopup();
				}
			},

			closeScanMessage() {
				this.scanPopupGetFocus();
			},

			scanPopupGetFocus() {
				this.$refs.scanPopup.getfocus();
			},

			scrollToTop() {
				let that = this;
				// 解决view层不同步的问题
				that.scrollTop = that.old.scrollTop
				this.$nextTick(function() {
					that.scrollTop = 0
				});
			},

			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.detail.scrollTop;
			},
			qtyChanged(value, item, index) {
				if (value <= 0) {
					this.showMessage('发料数量不能小于或等于0')
					this.$refs['comNumberBox_' + index][0].setValue(item.handledQty);
				} 
			},

		}
	}
</script>

<style scoped lang="scss">
</style>
