<template>
	<view class="content flex">
		<view class="title">
			扫描箱号条形码
		</view>
		<u-toast ref="uToast"></u-toast>
		<view class="box-scan">
			<uni-easyinput ref="inputScan"  prefixIcon="search" v-model="info.EnchaseId" @click="search" @clear="cleanItem"
			 :focus="isFocus" @blur="isFocus = false" placeholder="请扫描">
			</uni-easyinput>
			<!-- <view style="margin: 10px;
			margin-left: 0;">{{info.FEntity}}</view>
			<view style="margin: 10px;
			margin-left: 0;">{{info.BillType}}</view>
			<view style="margin: 10px;
			margin-left: 0;">{{info.EnchaseId}}</view> -->
			
		</view>
		<view class="form-btn flex">
			<button @click="back" class="cu-btn bg-gray lg shadow" style="font-size: 40rpx;">上一步</button>
			<button @click="cleanItem" class="cu-btn bg-gray lg shadow" style="font-size: 40rpx;">清空</button>
			<button @click="next" class="cu-btn bg-blue lg shadow" style="font-size: 40rpx;">下一步</button>
		</view>
	</view>
</template>

<script>
	import {
		GetClothesEnchaseByQRCode } from "../../api/enchase";
	export default {
		data() {
			return {
				isFocus: true,
				info: {
					PrdOrgId: '',
					BillType: '',
					EnchaseId: '',
					WorkShopId: '',
					OrderNo: '',
					BoxNo: '',
					MatchNo: '',
					boxInvNo: '',
					EnchaseTypeId: '',
					EnchaseType: '',
					Note: '',
					FEntity: [],
				},
			};
		},
		onLoad() {
			this.info.BillType = uni.getStorageSync('box_invType');
			this.info.PrdOrgId = uni.getStorageSync('box_org');
			this.info.WorkShopId = uni.getStorageSync('box_workshopCode');
			this.info.EnchaseType = uni.getStorageSync('box_packType');
			this.info.EnchaseTypeId = uni.getStorageSync('box_packTypeId');
			
			
			
		},
		onReady() {
			//自动聚焦
			
		},
		methods: {
			//自动聚焦
			focusPoint() {
				console.log("执行了聚焦")
				this.info.EnchaseId = ""
				this.isFocus = false;
				this.$nextTick(() => {
					this.info.EnchaseId = ""
					this.isFocus = true;
				});
			},
			cleanItem() {
				this.info.EnchaseId = '',
				this.focusPoint()
					this.$modal.msgSuccess('已清空！');
			},
			back() {
				uni.reLaunch({
					url: "/pages/index/index"
				})
			},

			async next() {
				//统一校验
				if (!this.info.EnchaseId) {
					this.$modal.msgError("箱号不能为空！");
					this.focusPoint()
					return
				}
				//按计划装箱
				if ("AJHZX" == this.info.BillType) {
					console.log("按计划装箱校验----")
					//调用接口获得装箱单据编号
					let boxInfo = '';
					try{
						const res = await GetClothesEnchaseByQRCode(this.info.EnchaseId)
						boxInfo = res
					}catch(e){
						console.error('GetClothesEnchaseByQRCode', error);
					}
					console.log(boxInfo)
					if (!boxInfo ||JSON.stringify(boxInfo) === '{}') {
						this.$refs.uToast.show({
							message: '不存在的计划单！',
							type: 'error',
							icon: true
						});
						this.focusPoint()
						return
					}
					

					if (boxInfo.FEnchaseBillNo.trim()) {
						this.$refs.uToast.show({
							message: '已经装箱，请重新扫描其它箱号条形码！',
							type: 'error',
							icon: true
						});
						this.focusPoint()
						return
					}
					this.info.EnchaseId = boxInfo.FEnchaseId
					this.info.BoxNo = boxInfo.FBoxNum
					this.info.PrdOrgId = boxInfo.FOrgId
					this.info.WorkShopId = boxInfo.FWorkShopNum
					this.info.OrderNo = boxInfo.FOrderNo
					this.info.FCustId = boxInfo.FCustId
					this.info.FPlanQty = boxInfo.FPlanQty
					this.info.MatchNo = boxInfo.FMatchNo
					this.info.EnchaseTypeId = boxInfo.FEnchaseTypeId
					this.info.EnchaseType = boxInfo.FEnchaseTypeName
					this.info.Note = boxInfo.FNote
					this.info.FEntity = boxInfo.subDetail
					console.log("按计划装箱校验----" + JSON.stringify(this
						.info))
				}

				// 校验成功进入服装扫描页面
				this.$tab.navigateTo('/pages/index/scan-clothing?Info=' + encodeURIComponent(JSON.stringify(this
					.info)))

			},
			search() {
				// this.info.EnchaseId = '240423135230-00001'
				// console.log(this.info.EnchaseId)
				let that = this
				if(this.info.EnchaseId === ''){
					uni.showToast({
						title: '请扫描条码',
						icon: 'none',
						duration: 3000
					})
				}
				//扫描箱号
				// uni.scanCode({
				// 	onlyFromCamera: true,
				// 	success: function(res) {
				// 		that.info.EnchaseId = res.result
				// 		console.log('条码类型：' + res.scanType);
				// 		console.log('条码内容：' + res.result);
				// 	}
				// });


			},
			

		},

	}
</script>

<style lang="scss">
	.content {
		flex-direction: column;

		.title {
			text-align: center;
			padding-top: 20%;
			font-size: 85rpx;
			color: #8a8a8a;
		}

		// .form {
		// 	height: 60rpx;
		// 	margin: 0 10%;
		// 	border-bottom: 5rpx solid #245cd5;

		// }
		.box-scan {
			height: 60rpx;
			margin: 15% 10%;
			border-bottom: 5rpx solid #245cd5;
		}

		.form-btn {
			margin: 50rpx;
			justify-content: space-around;
		}
	}
</style>