<template>
	<uni-popup ref="popup">
		<view class="popup_box">
			<view class="pop_title">
				扫描信息
				<text class="fr" @click="closeScanPopup()">关闭</text>
			</view>
			<view class="pop_tab">
				<view class="uni-steps">
					<com-steps-custom :options="list" :active="active"></com-steps-custom>
				</view>
				<view class="tab_info tab_notitle">
					<win-com-scan ref="scanContainerCode" v-if="active==0" @getScanResult="getScanContainerResult"
						:placeholder="scanPlaceholder" :clearResult='true'>
					</win-com-scan>
					<win-scan-tj-mes v-else @getScanResult="getScanItems" :placeholder="scanPlaceholder"></win-scan-tj-mes>
				</view>
			</view>
		</view>
		<comMessage ref="comMessage"></comMessage>
	</uni-popup>
</template>

<script>
	import comStepsCustom from '@/mycomponents/common/comStepsCustom.vue'
	import winComScan from '@/mycomponents/wincom/winComScan.vue'
	import WinScanTjMes from '@/mycomponents/wincom/WinScanTjMes.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import moment from 'moment'
	import {
		getprodlineitemAsync,
		getContainerAsync,
		checkContainerOrderAsync,
	} from '@/api/index.js';
	export default {
		name: 'comScanTjReceipt',
		components: {
			comStepsCustom,
			winComScan,
			WinScanTjMes,
			comMessage,
		},
		data() {
			return {
				active: 0,
				list: [{
					title: '器具编码',
					num: "1"
				}, {
					title: '上层门板二维码',
					num: "2"
				}, {
					title: '下层门板二维码',
					num: "3"
				}],
				scanPlaceholder: '请扫描器具编码',
				containerCode: '',
				produceDate: '',
				prodLine: {},
				scanPosition: 'F' //F前 R后
			};
		},
		props: {

		},
		mounted() {

		},
		onPullDownRefresh() {

		},
		methods: {
			openScanPopup(containerCode, containerItem, postion) {
				this.containerCode = containerCode;
				this.containerItem = containerItem;
				if (postion == 'F') {
					this.active = 2;
					this.scanPosition = 'R'
					this.scanPlaceholder = '请扫描下层门板二维码';
				} else if (containerCode != '' ) {
					this.active = 1;
					this.scanPosition = 'F'
					this.scanPlaceholder = '请扫描上层门板二维码';
				} else if (containerCode == '') {
					this.active = 0;
					this.scanPlaceholder = '请扫描器具编码';
				}

				this.$refs.popup.open('bottom')
			},
			closeScanPopup() {
				this.$refs.popup.close()
			},

			//扫描器具号
			async getScanContainerResult(result) {
				if (result.data.code === undefined) {
					this.showMessage('未扫描到器具号');
				} else {
					let code = result.data.code;
					await this.getContainerCode(code);
					this.$emit('getContainerResult', code, this.containerItem, this.prodLine)
					this.active = 1;
					this.scanPosition = 'F'; //上层门板
					this.scanPlaceholder = '请扫描上层门板二维码';
					return;
				}
			},

			async getContainerCode(code) {
				let that = this;
				that.containerCode = code;
				uni.showLoading({
					title: '正在解析中...'
				})
				//按序收货
				if (that.isOrderReceipt) {
					try {
						let orderRes = await checkContainerOrderAsync(code);
						if (orderRes.error != null && orderRes.error.validationErrors != null) {
							let error = '';
							for (var i = 1; i <= orderRes.error.validationErrors.length; i++) {
								let item = orderRes.error.validationErrors[i - 1];
								error += i + '.' + item.message;
							}
							that.showMessage(error);
							that.clearContainerCode();
							uni.hideLoading();
							return;
						}
					} catch (ex) {
						that.showMessage('获取排序异常' + ex.message);
						uni.hideLoading();
						return;
					}
				} else {
					//1.检查器具号是否存在
					let container = await getContainerAsync(code);
					if (container != "") {
						that.showMessage('器具编号为[' + code + ']的器具已收货，无法重复收货');
						that.clearContainerCode();
						uni.hideLoading();
						return;
					}
				}

				//2.检查器具格式是否正确
				let containerItem = that.checkContainerFormat(code)
				if (containerItem == null) {
					uni.hideLoading();
					return;
					that.clearContainerCode();
				} else {
					that.containerItem = containerItem;
				}
				//3.获取生产线是否存在
				console.log('prodLineCode', that.containerItem.prodLineCode)
				let prodItem = await getprodlineitemAsync(that.containerItem.prodLineCode);
				if (prodItem.error != null) {
					that.showMessage(prodItem.error.message);
					uni.hideLoading();
					uni.hideLoading();
					return;
				} else {
					that.prodLine = {
						prodLineCode: prodItem.code,
						productLocation: prodItem.productLocation,
						rawLocation: prodItem.rawLocation,
						workShop: prodItem.workshopCode
					};
					uni.hideLoading();
				}
				uni.hideLoading();
			},

			//检验器具格式
			checkContainerFormat(code) {
				console.log('code', code);
				//P-20220703-254L-0004
				let containerItem = {
					prefix: '', //前缀
					produceDate: '', //日期
					prodLineCode: '', //生产线
					projectNo: '', //项目号
					postion: '', //位置
					order: '' //流水
				};

				//校验器具格式			
				let arrayItems = code.split('-');

				//1.前缀
				let str0 = arrayItems[0];
				if (!/^([a-zA-Z])$/.test(str0)) {
					this.showMessage('器具前缀[' + str0 + ']格式错误');
					return null;
				} else {
					containerItem.prefix = str0; //前缀
				}

				//2.日期
				let str1 = arrayItems[1];
				let producedate = moment(str1).format("YYYY-MM-DD");
				if (producedate == 'Invalid date') {
					this.showMessage('器具日期格式[' + str1 + ']错误');
					return null;
				} else {
					this.produceDate = producedate;
					containerItem.produceDate = producedate; //前缀
				}

				let prodLineCode = arrayItems[2]; //生产线
				if (prodLineCode == null || prodLineCode == '') {
					this.showMessage('器具中未解析到生产线');
					return null;
				} else {
					containerItem.prodLineCode = prodLineCode; //前缀
				}

				let projectno = prodLineCode.substring(0, prodLineCode.length - 1) //项目号
				let postion = prodLineCode.substring(prodLineCode.length - 1, prodLineCode.length);
				if (postion != 'L' && postion != 'R') {
					this.showMessage('[' + prodLineCode + ']中未体现出左右(LR)位置');
					return null;
				} else {
					containerItem.projectNo = projectno;
					containerItem.postion = postion;
				}
				//流水
				let str3 = arrayItems[3];
				if (!/^[0-9]{7}$/.test(str3)) {
					this.showMessage('器具流水[' + str3 + ']格式错误');
					return null;
				} else {
					containerItem.order = str3;
				}
				return containerItem;
			},

			async getProdlineItem(prodLineCode) {
				try {
					let res = await getprodlineitemAsync(prodLineCode);
					if (res === null) {
						this.showMessage('未查找到生产线:' + prodLineCode);
					} else {
						return res;
					}
				} catch (err) {
					this.showMessage('查询生产线失败' + err.message);
					uni.hideLoading();
				}
			},

			//扫描完工二维码
			getScanItems(result) {
				let that = this;
				let items = result.items;
				let availItems = items.filter(r => r.itemCode != 'N' && r.projectNo != 'N' && r.produceNo != 'N' && r
					.lot != 'N');
				if (availItems.length > 0) {
					let item = availItems[0];
					let firstPosition = item.position;
					let count = availItems.filter(r => r.position != firstPosition).length;
					if (count > 0) {
						this.showMessage('门板位置信息不一致,请重新扫描');
						return;
					}

					let p = firstPosition.substring(1, 2) //左右位置
					if (p != this.containerItem.postion) {
						this.showMessage('门板位置信息与托盘左右位置信息不一致,请重新扫描');
						return;
					}

					let p1 = firstPosition.substring(0, 1) //前后位置
					if (this.scanPosition != p1) {
						if (this.scanPosition == 'F') //上层
						{
							this.showMessage('请扫描上层门板二维码');
						} else {
							this.showMessage('请扫描下层门板二维码');
						}
						return;
					}

					if (firstPosition === 'FL' || firstPosition === 'FR') { //上层门板
						this.$emit('getDoorResult', this.scanPosition, items)
						this.active = 2;
						this.scanPosition = 'R'; //上层门板
						this.scanPlaceholder = '请扫描下层门板二维码';
					} else if (firstPosition === 'RL' || firstPosition === 'RR') { //后门板
						this.$emit('getDoorResult', this.scanPosition, items)
						this.closeScanPopup();
					} else {
						this.showMessage('门板位置信息错误,请重新扫描');
					}
				} else {
					this.showMessage('未解析到有效的门板信息,请重新扫描');
					return;
				}
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
		}
	};
</script>

<style>
</style>
