<template>
	<view>
		<uni-popup ref="popup" @change="change" :mask-click="false">
			<view class="popup_box">
				<view class="pop_title uni-flex space-between">
					<view class="" style="font-size: 35rpx;">
						扫描{{title}}
					</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="">
						<win-com-scan ref="comscan" :placeholder="title" @getResult="getScanResult"
							:headerType="headerType" :isShowHistory="isShowHistory" :clearResult="true"></win-com-scan>
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- 模拟扫描功能 -->
		<win-com-scan v-show="false" ref="comscansimulate" @getResult="getScanResult" :headerType="headerType"
			:isShowHistory="false" :clearResult="true"></win-com-scan>
		<comMessage ref="comMessage" @afterClose="getfocus"></comMessage>
	</view>
</template>

<script>
	import winComScan from '@/mycomponents/scan/winComScan.vue'
	import {
		getBalanceByFilter
	} from '@/api/request2.js';

	export default {
		name: 'winScanPack',
		components: {
			winComScan,
		},
		props: {
			title: {
				type: String,
				default: '箱标签'
			},
			isShowHistory: {
				type: Boolean,
				default: true
			},
			headerType: {
				type: String,
				default: 'HPQ,HMQ'
			}
		},
		data() {
			return {
				show: false,
				businessType: null
			}
		},
		created() {

		},
		methods: {
			simulateScan(item) {
				this.$refs.comscansimulate.setItemCodeSimulate(item.copyContent)
				this.$refs.comscansimulate.clickScanMsg();
			},
			openScanPopup() {
				setTimeout(res => {
					this.$refs.popup.open('bottom')
				}, 200)
			},


			openScanPopupByBusinessType(businessType) {
				setTimeout(res => {
					this.$refs.popup.open('bottom')
					this.businessType = businessType;

				}, 200)
			},


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

			scanClick() {
				if (this.$refs.comscan) {
					this.$refs.comscan.clickScanMsg();
				}

			},

			cancelClick() {
				if (this.$refs.comscan) {
					this.$refs.comscan.clearScanValue();
				}
			},

			getScanResult(result) {
				if (result.success) {
					this.getBalance(result, res => {
						result.balance = res;
						this.$emit("getResult", result);
					})
				} else {
					this.showMessage(result.message)
				}
			},

			getParams(label) {
				let filters = [{
					column: "itemCode",
					action: "==",
					value: label.itemCode
				}, {
					column: "batch",
					action: "==",
					value: label.batch
				}];
				if (this.businessType.outInventoryStatuses != null) {
					filters.push({
						column: "inventoryStatus",
						action: "in",
						value: this.businessType.outInventoryStatuses
					})
				}
				if (this.businessType.outAreaTypes != null) {
					filters.push({
						column: "areaType",
						action: "in",
						value: this.businessType.outAreaTypes
					})
				}
				if (this.businessType.outAreaCodes != null) {
					filters.push({
						column: "areaCode",
						action: "in",
						value: this.businessType.outAreaCodes
					})
				}
				return filters;
			},

			getBalance(result, callback) {
				let filters = [];
				let label = result.label;
				let packageInfo = result.package;
				let comfilters = this.getParams(label);
				if (packageInfo.parentNumber !== null) {
					let packingNumber = packageInfo.parentNumber + "," + label.packingNumber;
					filters.push({
						column: "packingNumber",
						action: "in",
						value: packingNumber
					})
				} else {
					filters.push({
						column: "packingNumber",
						action: "==",
						value: label.packingNumber
					})
				}

				filters = filters.concat(comfilters);
				var params = {
					filters: filters,
					pageNo: 1,
					pageSize: 100,
				}
				getBalanceByFilter(params).then(res => {
					if (res.data.list.length > 0) {
						res.data.list.forEach(r => {
							if (packageInfo.parentNumber !== null) {
								r.parentPackingNumber = packageInfo.parentNumber
							} else {
								r.parentPackingNumber = packageInfo.number
							}
						})
						callback(res.data.list)
					} else {
						//如果扫描的是托标签，按托标签没查找到库存再按托标签下的子标签查询库存
						if (packageInfo.subList.length > 0) {
							let packparams = '';
							packageInfo.subList.forEach(pack => {
								packparams = packparams + pack.number + ','
							})
							packparams = packparams.trimEnd(',');
							filters = [];
							filters.push({
								column: "packingNumber",
								action: "in",
								value: packparams
							})
							filters = filters.concat(comfilters);
							params.filters = filters;
							getBalanceByFilter(params).then(res1 => {
								if (res1.data.list.length == 0) {
									this.showErrorMessage('未查找到该包装的库存余额', res => {
										this.packGetFocus();
									})
								} else {
									res1.data.list.forEach(r => r.parentPackingNumber = packageInfo.number)
									callback(res1.data.list)
								}
							}).catch(err => {
								this.showErrorMessage(err.message);
							})
						} else {
							this.showErrorMessage('未查找到该包装的库存余额', res => {
								this.packGetFocus();
							})
						}
					}
				}).catch(err => {
					this.showErrorMessage(err.message);
				})

			},

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

			losefocus() {
				if (this.$refs.comscan != undefined) {
					this.$refs.comscan.losefocus();
				}
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			change(e) {
				this.show = e.show
			},
			showErrorMessage(message) {
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {

					}
				});
			},
		}
	}
</script>

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