<template>
	<page-meta root-font-size="18px"></page-meta>
	<view class="">
		<view class="top_card">
			<view class="top_wrap" v-if="details.length>0">
				<view class="top_card" v-if="details[0].positionCode">

					<view class="device-detail">
						<view class="list-style nopad">
							<view class="ljh_box nopad">
								<view class="tit_ljh uni-flex">
									<text class="font_xl text_black text_bold">位置码 : {{details[0].positionCode}}</text>
								</view>

							</view>
						</view>
					</view>

				</view>
			</view>
		</view>


		<scroll-view scroll-y="true" @scroll="scroll" class="scroll-detail">

			<view class="detail-list margin_top" v-for="(item, index) in details" :key="item.id">


				<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="70">库位</uni-th>
										<uni-th width="240" align="center">
											<view class="text_packingCode">
												{{ item.toLocationCode }}
											</view>
										</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="70">叫料数量</text></uni-th>
										<uni-th width="240" align="center">
											<view class="text_packingCode"
												style="display: flex;flex-direction: row;justify-content:center;align-items: center;">
												{{item.notFinishQty}}
											</view>
										</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="70">单位</uni-th>
										<uni-th width="240" align="center">
											<view class="text_packingCode">
												{{ item.uom }}
											</view>
										</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="70">标包数</uni-th>
										<uni-th width="240" align="center">
											<view class="text_black">{{item.stdPackQty }}</view>
										</uni-th>
									</uni-tr>
								</uni-table>

							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<div class="new_bot_box">
			<view class="new_btn_bot bot_pos uni-flex">
				<button class="new_save_btn " style="width: 100%;" @click="submit()">执行</button>
			</view>
		</div>
		<comMessage ref="comMessage" @afterCloseCommitMessage='closeCommitMessage()'></comMessage>
	</view>
</template>

<script>
	import {
		getSparePartRequestDetail,
		sparePartRequestHandle,
		getSparePartRequestJobByRequest,
		isSparePartHasNewJob
	} from '@/api/index.js';
	import {
		showConfirmMsg,
		goHome,
		navigateBack
	} from '@/common/basic.js';
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import comJobScanDetail from '@/mycomponents/comjob/comJobScanDetail.vue'

	export default {
		components: {
			comMessage,
			comJobScanDetail,
		},
		data() {
			return {
				id: "",
				datacontent: {},
				details: [],
				jobStatus: "",
			}
		},
		onLoad(param) {
			this.id = param.id;
			this.getDetail();
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			} else if (e.index === 1) {
				window.location.reload();
			}
		},
		mounted() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: "#5A7CF3 !important"
			})
		},
		methods: {

			//加载零件信息
			getDetail() {
				let that = this;
				uni.showLoading({
					title:"加载中",
					mask:false
				})
				getSparePartRequestDetail(that.id)
					.then(item => {
						uni.hideLoading()
						console.log('item', item);
						that.datacontent = item;
						that.jobStatus = item.jobStatus
						that.details = item.details;
					})
					.catch(err => {
						uni.hideLoading()
						this.showMessage('未查找到详细信息')
					});
			},


			getScanCount() {
				this.scanCount = this.datacontent.details.filter(r => r.scaned).length;
			},

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

			submit() {
				showConfirmMsg("是否确认执行?", callback => {
					if (callback) {
						this.finsh();
					} 
				})
				
			},

		async finsh() {
				uni.showLoading({
					title:"提交中",
					mask:true
				})
				try{
					var jobList = await getSparePartRequestJobByRequest(this.datacontent.number)
					var handleResult = await sparePartRequestHandle(this.datacontent.id)
					
					var isHaveNewJobList=[];
					jobList.forEach(res=>{
						isHaveNewJobList.push(res.number);
					})
					var isHaveNewJob = await isSparePartHasNewJob(this.datacontent.number,isHaveNewJobList)
					uni.hideLoading()
					if(isHaveNewJob.includes("无任务")){
						this.showMessage(isHaveNewJob)
					}else {
						this.showMessage(isHaveNewJob)
						setTimeout(res=>{
							navigateBack(1)
						},1000)
					}
				}catch(error){
					uni.hideLoading()
					this.showMessage(error)
				}
			},

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

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


			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},

		}
	}
</script>

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