<template>
	<view class="page-wraper">
		<view class="" v-if='detailSource.length==0'>
			<com-blank-view @goScan='goScan()'></com-blank-view>
		</view>
		<view v-else class="page-wraper">
			<view class="page-main">
				<scroll-view scroll-y="true" class="">
					<view v-for="(toLocation, index) in detailSource">
						<com-issue-detail-card ref='comIssueDetailCard' :dataContent="toLocation"
							@updateData='updateData'>
						</com-issue-detail-card>
					</view>
				</scroll-view>
				<button class="btn_add" @click="goScan()">+去添加</button>
			</view>

			<div class="btn_bottom">
				<view class="" style="display: flex;flex-direction: row;">
					<view class="">
						<button class="btn_commit" hover-class="btn_commit_after" @click="submit()">提交</button>
					</view>
				</view>
			</div>

			<win-scan-button @goScan='openScanDetailPopup'></win-scan-button>
			<com-scan-issue-pack ref="comScanIssuePack" @closeScan='closeScanPopup' @updateData='updateData'>
			</com-scan-issue-pack>
			<comMessage ref="comMessage"></comMessage>
		</view>
	</view>
	<com-issue-request-popup ref="comIssueRequestPopup" @confirm='requestConfirm'></com-issue-request-popup>
</template>

<script>
	// import {
	// 	getIssueJobDetail,
	// } from '@/api/request.js';

	import {
		goHome,
		getRemoveOption,
		getISODateTime
	} from '@/common/basic.js';

	import {
		getDataSource
	} from '@/pages/issue/js/issue.js';

	import comBlankView from '@/mycomponents/common/comBlankView.vue'
	import comIssueRequestPopup from '@/pages/issue/coms/comIssueRequestPopup.vue'
	import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
	import winScanButton from '@/mycomponents/scan/winScanButton.vue'
	import comIssueDetailCard from '@/pages/issue/coms/comIssueDetailCard.vue'
	import comScanIssuePack from '@/pages/issue/coms/comScanIssuePack.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'

	export default {
		name: 'issueDetail',
		components: {
			comBlankView,
			comIssueRequestPopup,
			jobDetailPopup,
			winScanButton,
			comIssueDetailCard,
			comScanIssuePack,
			comMessage
		},
		data() {
			return {
				id: '1689889453461090304',
				jobContent: {}, //任务内容
				detailList: [], //接口返回的任务details
				detailSource: [], //绑定在页面上的数据源
				detailOptions: [],
				scanOptions: [],
				requestList: [] ,//需求信息
				fromType:""
			}
		},
		mounted() {
			this.goScan()
		},
		onLoad(option) {
			this.fromType = option.fromType
			if(this.fromType=="requestType"){
				updateTitle("发料申请")
			}else {
				updateTitle("发料记录")
			}
		},
		methods: {
			goScan() {
				this.$refs.comIssueRequestPopup.openRequestPopup(this.detailSource[0]);
			},

			//确定需求信息
			requestConfirm(action, item) {
				//调用接口查询推荐信息
				this.getDetail(action, item);
			},

			getDetail(action, item) {
				var that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				getIssueJobDetail(this.id).then(res => {
					if (res.data[0].details.length > 0) {
						that.detailList = res.data[0].details;
						if (action == 'update') //更新需求信息,将原来的零件节点删除
						{
							let index = that.detailSource[0].Items.findIndex(r => r.itemCode == item.itemCode);
							if (index >= 0) {
								that.detailSource[0].Items.splice(index, 1);
							}
						}
						that.detailSource = getDataSource(that.detailSource, that.detailList)
						that.resizeCollapse();
					}
					uni.hideLoading();
				}).catch(error => {
					uni.hideLoading()
					this.showErrorMessage(error)
				})
			},


			updateData(record) {
				let requestLocation = this.detailSource.find(r => r.toLocationCode == record.toLocationCode);
				let item = requestLocation.Items.find(r => r.itemCode == record.itemCode);
				let itemHandleQty = 0;
				if (item != undefined) {
					item.Locations.forEach(l => {
						let batch = l.Batchs.find(b => (b.packingNumber == record.packingNumber || b
								.packingNumber == '') && b.batch ==
							record.batch);

						let handleQty = 0;
						batch.Records.forEach(res => {
							handleQty += Number(res.qty)
						})
						batch.handleQty = handleQty;
						itemHandleQty += handleQty;
					})
				}
				// item.handleQty=itemHandleQty;
			},

			openScanDetailPopup() {
				var datacontent = {}
				//克隆对象，深度克隆，防止双向绑定同一个变量
				// Object.assign(datacontent, this.detailSource);
				this.$refs.comScanIssuePack.openScanPopup(this.detailSource, this.jobContent);
			},

			closeScanPopup() {
				this.resizeCollapse();
			},

			resizeCollapse() {
				this.$nextTick(r => {
					this.$refs.comIssueDetailCard.forEach(r => {
						r.resizeCollapse();
					})
				});
			},
			submit(){
				if(this.fromType=="requestType"){
					
				}else {
					
				}
				
			}
		}
	}
</script>

<style>
</style>
