<template>
	<view>
		<uni-popup ref="popup" :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"
							:isShowHistory="isShowHistory" :clearResult="true" :headerType="headerType"></win-com-scan>
					</view>
				</view>
			</view>
		</uni-popup>
		<balance-select ref="balanceSelect" @onSelectItem='selectBalanceItem'></balance-select>
	</view>
	<comMessage ref="comMessage"></comMessage>
</template>

<script>
	import winComScan from '@/mycomponents/scan/winComScan.vue'
	import balanceSelect from '@/mycomponents/balance/balanceSelect.vue'
	import {
		getBalanceByManagementPrecisionByPacking,
	} from '@/common/balance.js';

	import {
		getBalanceByParams,
		getBasicItemByCode,
		getBalanceByFilter
	} from '@/api/request2.js';

	import {
		getListLocationAreaTypeDesc,
		checkDirectoryItemExist,
		getDirectoryItemArray,
		getLocationAreaTypeName,
		getInventoryStatusDesc,
		getListItemTypeDesc,
		getItemTypeInfo
	} from '@/common/directory.js';
	export default {
		name: 'winScanPack',
		emits: ["getBalance"],
		components: {
			winComScan,
			balanceSelect
		},
		props: {
			title: {
				type: String,
				default: '箱标签'
			},
			isShowHistory: {
				type: Boolean,
				default: false
			},
			headerType: {
				type: String,
				default: "HPQ,HMQ"
			},
			balanceFromInventoryStatuses: { //是否传fromInventoryStatuses
				type: Boolean,
				default: true
			},
			bussinessCode: {
				type: String,
				default: ''
			},
			verifyCategory: {
				type: Boolean,
				default: false
			},
			isCheckLocationBalance: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				scanResult: {},
				show: false,
				scanList: [],
				expand: false,
				showScanResult: {},
				expendIcon: 'arrow-down',
				fromLocationCode: '',
				fromLocation: '',
				fromLocationList: [],
				fromLocationAreaTypeList: [],
				toLocationAreaTypeList: [],
				locationOnFocus: false,
				businessType: {},
				inventoryStatus: [],
				managementPrecision: '',
				fromInventoryStatuses: [],
				itemTypesList: [],
				isCheck: false,
				resultData: {}
			}
		},
		created() {

		},
		methods: {
			openScanPopup(businessType) {
				this.businessType = businessType
				this.fromInventoryStatuses = getDirectoryItemArray(businessType.outInventoryStatuses)
				this.fromLocationAreaTypeList = getDirectoryItemArray(businessType.outAreaTypes)
				this.toLocationAreaTypeList = getDirectoryItemArray(businessType.inAreaTypes)
				this.itemTypesList = getDirectoryItemArray(businessType.itemTypes)
				this.$refs.popup.open('bottom')
			},

			getScanResult(result) {
				this.resultData = result;
				if (!result.package) {
					this.showErrorMessage(result.label.code + "包装信息为空")
					return;
				}
				this.getItemCodeType(result.package.itemCode, callBack => {
					if(this.isCheckLocationBalance){
						this.getToLocationBalance(this.resultData)
					}else {
						this.queryBalance(this.resultData)
					}

				})
			},

			//查询到目标库位的库存余额
			getToLocationBalance(result) {
				uni.showLoading({
					title: '查询中',
					mask: true
				})
				var filters = []
				if (result.package.parentNumber) {
					var packingNumber = result.package.parentNumber + "," + result.package.number;
					filters.push({
						column: "packingNumber",
						action: "in",
						value: packingNumber
					})
				} else {
					filters.push({
						column: "packingNumber",
						action: "==",
						value: result.package.number
					})
				}

				filters.push({
					column: "itemCode",
					action: "==",
					value: result.package.itemCode
				})
				filters.push({
					column: "batch",
					action: "==",
					value: result.package.batch
				})

				filters.push({
					column: "areaType",
					action: "in",
					value: this.toLocationAreaTypeList.join(',')
				})


				var params = {
					filters: filters,
					pageNo: 1,
					pageSize: 100,
				}
				getBalanceByFilter(params).then(res => {
					uni.hideLoading()
					if (res.data.list.length > 0) {
						this.showErrorMessage("包装在库位【" + res.data.list[0].locationCode + "】已有库存余额");
					} else {
						this.queryBalance(this.resultData);
					}
					// callback(res.data)
				}).catch(err => {
					this.showErrorMessage(err.message);
				})
			},

			queryBalance(result) {
				var params = {
					itemCode: result.package.itemCode,
					batch: result.label.batch,
					packingNumber: result.label.packingNumber,
					parentPackingNumber: result.package.parentNumber,
					inventoryStatus: this.fromInventoryStatuses,
					areaType: this.fromLocationAreaTypeList,
					bussinessCode: this.bussinessCode
				}
				uni.showLoading({
					title: '查询中',
					mask: true
				})
				getBalanceByParams(params).then(res => {
					uni.hideLoading()
					if (res.data.length == 0) {
						var status = getInventoryStatusDesc(params.inventoryStatus)
						var areaType = getListLocationAreaTypeDesc(params.areaType)
						var hint =
							"按物料号 [" + params.itemCode + "] <br>" +
							"包装号 [" + params.packingNumber + "] <br>" +
							"批次 [" + params.batch + "] <br>" +
							"状态 [" + status + "] <br>" +
							"库区 [" + areaType + "] <br>" +
							"未查找到库存余额"
						this.showErrorMessage(hint)
					} else if (res.data.length == 1) {

						result.balance = res.data[0]
						if (result.label.packingNumber != result.balance.packingNumber) {
							result.balance.lableQty = result.label.qty
						}
						this.$emit("getBalance", result)
						// this.closeScanPopup()
					} else {
						//多条记录
						this.$refs.balanceSelect.openPopup(res.data);
					}

				}).catch(error => {
					uni.hideLoading()
					this.showErrorMessage(error)
				})
			},

			getItemCodeType(itemCode, callBack) {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				getBasicItemByCode(itemCode).then(res => {
					if (res.data != null && res.data.list.length > 0) {
						var result = res.data.list[0];
						var status = result.available;
						var type = result.type;
						if (status == "TRUE") {
							if (checkDirectoryItemExist(this.itemTypesList, type)) {
								if (this.verifyCategory) {
									if (result.category == 'LCJ' || result.category == 'BJ') {
										callBack()
									} else {
										this.showErrorMessage("扫描物料的种类不是【量产件】或者【备件】")
									}
								} else {
									callBack()
								}
							} else {
								var hint = getListItemTypeDesc(this.itemTypesList);
								uni.hideLoading()
								this.showErrorMessage("扫描物料[" + itemCode + "]是[" +
									getItemTypeInfo(type).label + "],需要的物料类型是[" + hint + "]")
							}
						} else {
							uni.hideLoading()
							this.showErrorMessage('物料【' + itemCode + '】不可用');
						}
					} else {
						uni.hideLoading()
						this.showErrorMessage('未查找到物料【' + itemCode + '】');
					}
				}).catch(error => {
					uni.hideLoading();
					this.showErrorMessage(error)
				})
			},

			showErrorMessage(message) {
				this.losefocus()
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {
						if (this.$refs.comscan) {
							this.$refs.comscan.getfocus()
						}
					}
				});
			},

			selectBalanceItem(item) {
				this.resultData.balance = item
				this.$emit("getBalance", this.resultData)
				// this.closeScanPopup()
			},
			closeScanPopup() {
				this.losefocus();
				this.$refs.popup.close()
			},
			getfocus() {
				if (this.$refs.comscan) {
					this.$refs.comscan.getfocus()
				}
			},
			losefocus() {
				if (this.$refs.comscan) {
					this.$refs.comscan.losefocus()
				}
			}
		}
	}
</script>

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