<template>
	<view class="page-wraper" style="background-color: #fff;">
		<view class="">
			<com-blank-view @goScan='openFg' v-if="!dataContent"></com-blank-view>
		</view>

		<view class="page-wraper" v-if="dataContent">
			<view class="" style="font-size: 35rpx;padding: 10rpx; padding-left: 15rpx;">
				生产计划：{{dataContent.number}}
			</view>
			<view class='split_line'></view>
			<view class="">
				<item-qty :dataContent="dataContent" :isShowBalanceQty="false"></item-qty>
			</view>
			<view class='split_line'></view>
			<view class="" style="margin-left: 20rpx;">
				<batch :batch="dataContent.batch"></batch>
				<view class="card_view ">
					<text style="color: #FFA500;padding: 5px;font-size: 35rpx;">包装规格</text>
					<text class="card_content ">{{dataContent.packUnit}}</text>
				</view>
			</view>
			<view class='split_line'></view>
			<view class="page-main">
				<uni-swipe-action>
					<view class="scan_view" v-for="(item, index) in dataContent.subList" :key="index">
						<uni-swipe-action-item :right-options="options" @click="swipeClick($event,item,index)">
							<view class="" style="margin-left: 40rpx; padding-top: 10rpx; padding-bottom: 10rpx;">
								<text style="font-size: 35rpx;color: #B66463;">唯一码</text>
								<text style="font-size: 35rpx;"> {{item.code}}</text>
							</view>
						</uni-swipe-action-item>

					</view>
				</uni-swipe-action>
			</view>
			<view class="page-footer">
				<view class="uni-flex u-col-center space-between padding_10"
					style="background-color:ghostwhite; width: 100%; ">
					<view class="">
						<view class="uni-flex uni-row">
						</view>

					</view>
					<view class=" uni-flex uni-row">
						<button class="btn_single_clear" style="margin-right: 50rpx;" hover-class="btn_clear_after"
							@click="clear">清空</button>
						<button class="btn_single_commit" hover-class="btn_commit_after" @click="commit">提交</button>
					</view>
				</view>
			</view>


		</view>
		<comReceiptPopup ref="comReceiptPopup" @confirm='requestConfirm'></comReceiptPopup>
		<win-scan-button v-if="dataContent" @goScan='openScanPopup'></win-scan-button>
		<win-scan-fg-label ref="scanPopup" @getResult='getScanResult' title='制品标签'></win-scan-fg-label>
		<comMessage ref="comMessage"></comMessage>
	</view>
</template>

<script>
	import {
		isCheckMesCode,
		planReceiptSubmit
	} from '@/api/request2.js';
	import {
		getRemoveOption
	} from '@/common/basic.js';
	import winScanButton from '@/mycomponents/scan/winScanButton.vue'
	import comBlankView from '@/mycomponents/common/comBlankView.vue'
	import comReceiptPopup from '@/pages/fg/coms/comReceiptPopup.vue'
	import itemQty from '@/mycomponents/item/itemQty.vue'
	import batch from '@/mycomponents/balance/batch.vue'
	import pack from '@/mycomponents/balance/pack.vue'
	import winScanFgLabel from "@/mycomponents/scan/winScanFgLabel.vue"
	export default {
		components: {
			comBlankView,
			comReceiptPopup,
			itemQty,
			batch,
			pack,
			winScanButton,
			winScanFgLabel
		},
		data() {
			return {
				dataContent: null,
				options: []

			};
		},

		onShow() {},
		onLoad() {
			this.options = getRemoveOption()
			this.openFg();
		},

		mounted() {

		},
		methods: {
			openFg() {
				setTimeout(res => {
					if (this.$refs.comReceiptPopup) {
						this.$refs.comReceiptPopup.openRequestPopup();
					}
				}, 600)

			},
			requestConfirm(result) {
				this.dataContent = {
					itemCode: result.itemCode,
					number: result.number, //计划单号
					handleQty: 0,
					qty: result.planQty,
					batch: result.batch,
					packUnit: "3031",
					uom: result.uom,
					subList: [{
							code: "1234"

						},
						{
							code: "12345"

						}

					]
				}
			},
			swipeClick(e, dataContent,index) {
				if (e.content.text == "移除") {
					this.dataContent.subList.splice(index,1);
				} 

			},
			commit() {
				planReceiptSubmit(params).then(res => {
					uni.hideLoading()
					if (res.data) {
						this.showCommitSuccessMessage(res.data)
					} else {
						this.showErrorMessage("提交失败[" + res.msg + "]")
					}
				}).catch(error => {
					uni.hideLoading()
					this.showErrorMessage(error)
				})
			},
			clear() {

			},
			openScanPopup() {
				var itemCode = this.dataContent.itemCode
				// itemCode = "015553147"
				console.log("物料", itemCode)
				this.$refs.scanPopup.openScanPopup(itemCode);
			},
			getScanResult(result) {
					let that = this;
				//TODO缺少零件号的对应关系
				//查询mes码是否完工
				isCheckMesCode(result.content).then(res=>{
					if(res.data){
						if (that.dataContent) {
							let list = that.dataContent.subList; //应该按零件号查找
							
							var itemIndex = list.findIndex(r =>
								r.itemCode == result.itemCode &&
								// r.productDate == result.productDate &&
								r.order == result.order);
								//不存在
							if (itemIndex == -1) {
								list.push(result);
								that.calcFgQty();
							} else {
								this.showErrorMessage("标签【" + result.content + '】已经扫描，是否移除', confirm => {
									list.splice(itemIndex, 1)
									that.calcFgQty();
								})
							}
							setTimeout(r => {
								this.scanPopupGetFocus();
							}, 500)
							
							
						}
					}
				}).catch(error=>{
					this.showErrorMessage(error)
				})
				
			},
			
			calcFgQty() {
				this.dataContent.handleQty =this.dataContent.subList.length;
				if (this.dataContent.handleQty == this.dataContent.qty) {
					this.commit()
				}
			},
			
			scanPopupGetFocus() {
				if (this.$refs.scanPopup) {
					this.$refs.scanPopup.getfocus();
				}
			},
			scanPopupLoseFocus() {
				if (this.$refs.scanPopup ) {
					this.$refs.scanPopup.losefocus();
				}
			},
			showCommitSuccessMessage(hint) {
				this.$refs.comMessage.showSuccessMessage(hint, res => {
					this.dataContent=null;
					this.openFg();
				})
			},
		}

	}
</script>

<style>
</style>