<template>
	<view>
		<uni-popup ref="popup" @change="change" @maskClick="closeScanPopup()">
			<view class="popup_box">
				<view class="pop_title uni-flex space-between">
					<view class="" style="font-size: 35rpx;">
						校验客户标签
					</view>

					<view class="">
						<image class="fr icons_scan_close" src="/static/icons/icons_scan_close.svg"
							@click="closeScanPopup()"></image>
					</view>
				</view>

				<view class="">
					<view class="" style="position: relative">
						<win-com-scan ref="comscanFgLabel" placeholder="WMS制品标签" @getResult="getScanFgResult"
							:isShowHistory="false" :clearResult="false" :headerType="headerType"></win-com-scan>
						<view class="uni-flex"
							style="position: absolute; color: #3C9CFF; font-weight: bold; font-size: 15px;  left: 10px;bottom:10px; z-index: 999;">
							{{itemCode}}
						</view>
					</view>
					<view class="">
						<win-com-scan-customer-label ref="comscanCustomerLabel" placeholder="客户标签"
							@getResult="getScanCustomerResult" :isShowHistory="false" :clearResult="false"
							:headerType="headerType"></win-com-scan-customer-label>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
	<comMessage ref="comMessage"></comMessage>
</template>

<script>
	import winComScan from '@/mycomponents/scan/winComScan.vue'
	import winComScanCustomerLabel from '@/mycomponents/scan/winComScanCustomerLabel.vue'

	import balanceSelect from '@/mycomponents/balance/balanceSelect.vue'
	import {
		getBalanceByManagementPrecision,
	} from '@/common/balance.js';

	import {
		getBasicLocationByCode,
		getCustomerItemList
	} from '@/api/request2.js';

	import {
		getListLocationAreaTypeDesc,
		checkDirectoryItemExist,
		getDirectoryItemArray,
		getLocationAreaTypeName,
		getInventoryStatusDesc
	} from '@/common/directory.js';
	export default {
		name: 'winScanPack',
		emits: ["getResult", "close", "getCountScanResult"],
		components: {
			winComScan,
			winComScanCustomerLabel
		},
		props: {
			title: {
				type: String,
				default: '箱标签'
			},
			headerType: {
				type: String,
				default: "HPQ,HMQ"
			},
		},
		data() {
			return {

				scanResult: {},
				show: false,
				scanList: [],
				expand: false,
				showScanResult: {},
				expendIcon: 'arrow-down',
				fromLocationCode: '',
				fromLocation: '',
				fromLocationList: [],
				fromLocationAreaTypeList: [],
				locationOnFocus: false,
				businessType: {},
				inventoryStatus: [],
				managementPrecision: '',
				fromInventoryStatuses: "",
				isCheck: false,
				itemCode: '',
				customerItem: null,
				customerCode: '',//客户代码
				packingNumber:''//箱码
			}
		},
		created() {

		},
		methods: {
			//直接扫描
			openScanPopupForType(customerCode) {
				
				this.customerCode = customerCode;
				if (this.fromLocationCode != '') {
					this.packGetFocus();
				} else {
					this.locationGetFocus();
				}
				this.fromInventoryStatuses = this.businessType.outInventoryStatuses
				this.inventoryStatus = getDirectoryItemArray(this.businessType.outInventoryStatuses); //出库库存状态
				this.fromLocationAreaTypeList = getDirectoryItemArray(this.businessType.fromLocationAreaTypeList); //出库库区
				setTimeout(res => {
					this.$refs.popup.open('bottom')
				}, 500)
			},

			//在任务中扫描
			openScanPopupForJob(fromLocationCode, fromLocationList, jobContent) {
				this.fromLocationCode = fromLocationCode;
				this.fromLocationList = fromLocationList;
				if (fromLocationCode != '') {
					this.packGetFocus();
				} else {
					if (this.fromLocationList.length == 0) {
						this.locationGetFocus();
					} else {
						this.fromLocationCode = this.fromLocationList[0];
					}
				}
				setTimeout(res => {
					this.$refs.popup.open('bottom')
				}, 500)
				this.fromInventoryStatuses = jobContent.outInventoryStatuses
				this.inventoryStatus = getDirectoryItemArray(jobContent.outInventoryStatuses); //出库库存状态; //出库库存状态
				this.fromLocationAreaTypeList = getDirectoryItemArray(jobContent.fromAreaTypes); //出库库存状态
			},

			closeScanPopup(content) {
				this.$refs.popup.close();
				this.$emit("close", '');
			},

			getScanFgResult(result) {
				console.log('getScanFgResult',result)
				this.itemCode = result.label.itemCode;
				this.packingNumber = result.package.number; //箱码

				var filters = []
				filters.push({
					column: "customerCode",
					action: "==",
					value: this.customerCode
				})
				filters.push({
					column: "itemCode",
					action: "==",
					value: this.itemCode
				})

				var params = {
					filters: filters,
					pageNo: 1,
					pageSize: 10,
				}


				getCustomerItemList(params).then(res => {
					console.log('getCustomerItemList', res)

					if (res.data != null && res.data.list.length > 0) {
						this.customerItem = res.data.list[0];
					} else {
						this.showErrorMessage('未查找到客户物料代码');
					}
				}).catch(error => {
					this.showErrorMessage(error);
				})


				this.customerGetFocus();
			},

			getScanCustomerResult(result) {
				console.log('getScanCustomerResult', result)
				if (this.customerItem != null) {
					// 客户物料代码
					if (this.customerItem.customerItemCode == result) {
						// 通过
						this.$emit("checkResult", {
							packingNumber:this.packingNumber ,// 箱码
							customerItemCode:this.customerItem.customerItemCode,//客户物料代码
							customerItemCode_reality:result, // 实际校验物料
							itemCode:this.itemCode,//物料代码
							pass:true
						});
						
					}else{
						// 不通过
						this.$emit("checkResult", {
							packingNumber:this.packingNumber ,// 箱码
							customerItemCode:this.customerItem.customerItemCode,//客户物料代码
							customerItemCode_reality:result, // 实际校验物料
							itemCode:this.itemCode,//物料代码
							pass:false
						});
					}
				}
			},

			afterCheck() {

			},

			packGetFocus() {
				if (this.$refs.comscanFgLabel != undefined) {
					this.$refs.comscanFgLabel.getfocus();
				}
			},

			packLoseFocus() {
				if (this.$refs.comscanFgLabel != undefined) {
					this.$refs.comscanFgLabel.losefocus();
				}
			},

			customerGetFocus() {
				if (this.$refs.comscanCustomerLabel != undefined) {
					this.$refs.comscanCustomerLabel.getfocus();
				}
			},

			customerLoseFocus() {
				if (this.$refs.comscanCustomerLabel != undefined) {
					this.$refs.comscanCustomerLabel.losefocus();
				}
			},

			locationGetFocus() {
				this.fromLocationCode = '';
				this.locationOnFocus = true;
			},


			showMessage(message, callback) {
				setTimeout(r => {
					this.packLoseFocus();
					this.$refs.comMessage.showMessage(message, callback);
				})
			},

			showErrorMessage(message, callback) {
				setTimeout(r => {
					this.packLoseFocus();
					this.$refs.comMessage.showErrorMessage(message, callback)
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view {
		overflow-y: scroll;
		height: auto;
		max-height: 300rpx;
	}
</style>