<template>
	<view>
		<uni-popup ref="popup" @maskClick="closeScanPopup()">
			<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="uni-flex uni-row" style="align-items: center;
					background-color: #fff; 
					margin-left: 20rpx;
					margin-right: 20rpx;
					border-radius: 8rpx;
					height: 30px;">
					<view class="uni-center" style="width: 25%; ">
						位置
					</view>
					<view class="" style="width: 75%;padding: 8rpx">
						<view class="uni-flex u-col-center uni-row" @click="showSelect">
							<view class="" style="margin-left: 20rpx;">
								{{positionInfo}}
							</view>
							<u-select v-model="show" mode="mutil-column-auto" :list="positionList"
								@confirm="confirmSelect"></u-select>
						</view>

					</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>
	</view>
	<com-message ref="comMessage" @afterClose="getfocus"></com-message>
</template>

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

	import {
		getWorkShopLineStation
	} from '@/api/request2.js';

	export default {
		name: 'winScanPack',
		emits: ["getResult", "close"],
		components: {
			winComScan,
			comMessage,
		},
		props: {
			title: {
				type: String,
				default: '箱标签'
			},
			isShowHistory: {
				type: Boolean,
				default: true
			},
			headerType: {
				type: String,
				default: 'HPQ,HMQ'
			}
		},
		data() {
			return {
				show: false,
				positionInfo: "请选择位置",
				positionList: [],
				productionLineCode: '',
				rawLocationCode: "",
				fgLocationCode: "",
				workshopCode: "",
				workStationCode: "",
				workShopName: "",
				productionLineName: "",
				workStationName: "",
				isEditPosition: true
			}
		},
		created() {

		},
		methods: {
			openScanPopup() {
				if (this.positionList.length == 0) {
					uni.showLoading({
						title: "数据加载中....",
						mask: true
					});
					getWorkShopLineStation().then(res => {
						uni.hideLoading()
						this.positionList = res.data
					}).catch(error => {
						uni.hideLoading()
						this.showErrorMessage(error)
					})
				}

				this.$refs.popup.open('bottom');
			},
			
			initData(){
				this.positionInfo= "请选择位置";
				this.positionList = [];
				this.productionLineCode = '';
				this.rawLocationCode = "";
				this.fgLocationCode = "";
				this.workshopCode = "";
				this.workStationCode ="";
				this.workShopName = "";
				this.productionLineName="";
				this.workStationName="";
				this.show = false;
				this.isEditPosition = true
			},

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

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

			cancelClick() {
				this.$refs.comscan.clearScanValue();
			},
			getScanResult(result) {
				if (this.positionInfo == "请选择位置") {
					this.showErrorMessage("请先选择位置")
					return
				}
				var param = {
					positionInfo: this.positionInfo,
					workshopCode: this.workshopCode,
					productionLineCode: this.productionLineCode,
					workStationCode: this.workStationCode,
					rawLocationCode: this.rawLocationCode,
					fgLocationCode: this.fgLocationCode,
				}
				if (result.success) {
					this.isEditPosition = false
					this.$emit("getResult", result, param);
				} else {
					this.showErrorMessage(result.message)
				}
			},

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

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


			showSelect() {
				if (this.isEditPosition) {
					this.show = !this.show
				}

			},
			confirmSelect(e) {
				this.positionInfo = e[0].label + "-" + e[1].label + "-" + e[2].label
				console.log("位置", this.positionInfo)
				this.workshopCode = e[0].value
				this.productionLineCode = e[1].value
				this.workStationCode = e[2].value
				this.workShopName = e[0].label
				this.productionLineName = e[1].label
				this.workStationName = e[2].label

				let shop = this.positionList.find(shop => shop.value == this.workshopCode);
				if (shop != undefined && shop.children != undefined) {
					let prodLine = shop.children.find(line => line.value == this.productionLineCode);
					if (prodLine != undefined && prodLine.children != undefined) {
						let station = prodLine.children.find(r => r.value == this.workStationCode);
						if (station.rawLocationCode == '' && station.rawLocationCode == null) {
							this.showErrorMessage(this.workStationName + "的原材料库位为空,请重新选择")
							return;
						} else {
							this.rawLocationCode = station.rawLocationCode;
							this.fgLocationCode = station.fgLocationCode;
						}
					} else {
						this.showErrorMessage("生产线-工位基础信息维护错误")
					}
				} else {
					this.showErrorMessage("车间-生产线基础信息维护错误")
				}
			},
		}
	}
</script>

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