<template>
	<view>
		<u-navbar back-text="返回" title="预留领用直接出库" :background="background"></u-navbar>
		<u-form ref="testForm">
			<u-form-item :border-bottom="false">
				<p>单据:</p>
				<u-select v-model="dlistShowA" mode="single-column" value-name="id" :list="dlistA" @confirm="selConfirmA" confirm-text="确认选择" />
				<u-input
					:border="true"
					v-model="shipRequestName"
					placeholder="请选择单据"
					:focus="focusShipRequest"
					@confirm="checkShipRequest"
					style="background: #ffffff; margin-right: 10rpx"
				/>
				<u-button @click="selectShipRequest">选择单号</u-button>
			</u-form-item>

			<u-form-item :border-bottom="false">
				<p>订单行项:</p>
				<u-select v-model="dlistShowB" mode="single-column" value-name="id" :list="dlistB" @confirm="selConfirmB" confirm-text="确认选择" />
				<u-input
					:border="true"
					v-model="shipRequestNameDetail"
					placeholder="请选择行项"
					:focus="focusShipRequestDetail"
					@confirm="checkShipRequestDetail"
					style="background: #ffffff; margin-right: 10rpx"
				/>
				<u-button @click="selectShipRequestDetail">选择订单行项</u-button>
			</u-form-item>

			<u-form-item :border-bottom="false">
				<p>栈板:</p>
				<u-input
					:border="true"
					v-model="palletName"
					placeholder="请扫描栈板"
					:focus="focusPalletName"
					@confirm="SearchPalletInfo"
					style="background: #ffffff; margin-right: 10rpx"
				/>
			</u-form-item>
		</u-form>
		<u-card style="border: 1rpx solid #0081ff" :show-head="false" :full="true" :body-style="{ height: '320rpx' }">
			<view slot="body">
				<u-row>
					<u-col span="10">
						<view>单据类型：{{ SHIPREQUESTDETAILTYPE || '' }}</view>
					</u-col>
					<u-col span="10">
						<view>ERP工厂：{{ erpFactoryName || '' }}</view>
					</u-col>
					<u-col span="10">
						<view>ERP库位：{{ erpLocationName || '' }}</view>
					</u-col>
					<u-col span="10">
						<view>fgcode：{{ productSpec || '' }}</view>
					</u-col>
					<u-col span="10">
						<view>等级：{{ PackingGrade || '' }}</view>
					</u-col>
					<u-col span="10">
						<view>请求数量：{{ requestQuantity || '0' }}</view>
					</u-col>
					<u-col span="10">
						<view>出库数量：{{ assignedQuantity || '0' }}</view>
					</u-col>
				</u-row>
			</view>
		</u-card>

		<view>
			<uni-table ref="table" :loading="loading" border stripe type="false">
				<uni-tr>
					<uni-th align="center">fgcode</uni-th>
					<uni-th align="center">等级</uni-th>
					<uni-th align="center">panel数量</uni-th>
					<uni-th align="center">box数量</uni-th>
					<uni-th align="center">checkincode</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in PalletInfo" :key="index" @row-click="rowclick(item)" :style="{ backgroundColor: '' }">
					<uni-td align="center">{{ item.PRODUCTSPECNAME }}</uni-td>
					<uni-td align="center">{{ item.PACKINGGRADE }}</uni-td>
					<uni-td align="center">{{ item.PRODUCTQUANTITY }}</uni-td>
					<uni-td align="center">{{ item.SUBPACKINGQUANTITY }}</uni-td>
					<uni-td align="center">{{ item.CHECKINCODE }}</uni-td>
				</uni-tr>
			</uni-table>
			<u-button :loading="Comloading" @click="ReserveStockOut" style="background: #0081ff; color: #ffffff">确认出库</u-button>
		</view>
	</view>
</template>

<script>
import { QueryInfo, ReserveStockOutPallet } from '../../../../common/api.js';
export default {
	data() {
		return {
			background: {
				backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
			},
			myStyle: {
				background: '#007aff'
			},
			Comloading: false,
			//下拉框属性---------------------------------------
			dlistShowA: false, //是否显示单据选择框
			dlistShowB: false, //是否显示单据行项选择框
			dlistA: [], //选择单据内容
			dlistB: [], //选择单据行项内容
			shipRequestName: '', //text绑定的订单号
			shipRequestNameDetail: '', //text绑定的订单行项
			//焦点-----------------------------------------------
			focusShipRequest: true, //订单焦点
			focusShipRequestDetail: false, //订单行项焦点
			focusPalletName: false, //pallet焦点
			//pallet扫码和信息列表-------------------------------
			shipRequestInfo: [], //校验订单时查询订单信息
			shipRequestDetailInfo: [], //校验订单行项时查询订单详细信息
			palletName: '', //text绑定的栈板号
			PalletInfo: [], //pallet查询信息显示在表格
			//卡片绑定信息--------------------------------------
			erpFactoryName: '', //ERP工厂
			erpLocationName: '', //ERP库位
			SHIPREQUESTDETAILTYPE: '', //单据类型
			productSpec: '', //fgcode
			PackingGrade: '', //等级
			requestQuantity: '', //请求数量
			assignedQuantity: '', //绑定数量
			//查询-----------------------------------------------
			Queryaram: {
				queryID: '',
				version: '',
				BINDV: {}
			},
			stockOut: {
				SITENAME: '',
				USERNAME: '',
				SHIPREQUESTNAME: '',
				SHIPREQUESTDETAILNAME: '',
				PRODUCTPACKINGNAME: ''
			}
		};
	},
	methods: {
		//焦点初始化，让页面所有的焦点同时只能存在一个
		initfocus() {
			this.focusShipRequest = true;
			this.focusShipRequestDetail = false;
			this.focusPalletName = false;
		},
		//获取焦点的公共方法
		getfocus(nextfocus) {
			this.initfocus();
			this[nextfocus]; //不能用this.nextfocus
			this.$nextTick(() => {
				this[nextfocus] = true;
			});
		},
		//弹出的选择器的值赋值给单号输入框----------
		selConfirmA(e) {
			this.shipRequestName = e[0].value;
			this.checkShipRequest();
		},
		selConfirmB(e) {
			this.shipRequestNameDetail = e[0].value;
			this.checkShipRequestDetail();
		},
		//查询选择器的订单号
		selectShipRequest() {
			this.dlistA = []; //清空下拉选择，否则会重复添加
			let _param = this.Queryaram;
			_param.queryID = 'GetShipRequestList';
			_param.version = 'YYL06';
			_param.BINDV.SITENAME = uni.getStorageSync('SITENAME');
			_param.BINDV.SHIPREQUESTNAME = '%' + this.shipRequestName.trim() + '%';
			QueryInfo({
				param: JSON.stringify(_param)
			})
				.then((res) => {
					if (res.DATA != undefined) {
						for (var i = 0; i <= res.DATA.length; i++) {
							this.dlistA.push({
								id: res.DATA[i].SHIPREQUESTNAME,
								label: res.DATA[i].SHIPREQUESTNAME
							});
							this.dlistShowA = true;
						}
					} else {
						this.$showMessage('未查询到单据明细');
						this.shipRequestName = ''; //清空text
					}
				})
				.catch((err) => {
					this.$showMessage(err);
					return;
				});
		},
		//查询选择器的订单行项
		selectShipRequestDetail() {
			this.dlistB = []; //清空下拉选择，否则会重复添加
			let _param = this.Queryaram;
			_param.queryID = 'GetShipRequestList';
			_param.version = 'xx009';
			_param.BINDV.SITENAME = uni.getStorageSync('SITENAME');
			_param.BINDV.SHIPREQUESTNAME = this.shipRequestName.trim();
			QueryInfo({
				param: JSON.stringify(_param)
			})
				.then((res) => {
					if (res.DATA != undefined) {
						//查询订单信息
						this.erpFactoryName = '', //赋值前先清空
						this.erpLocationName = '',
						this.SHIPREQUESTDETAILTYPE = '',
						this.shipRequestInfo = res.DATA;
						this.erpFactoryName = this.shipRequestInfo[0]['ERPFACTORYNAME'];
						this.erpLocationName = this.shipRequestInfo[0]['ERPLOCATIONNAME'];
						this.SHIPREQUESTDETAILTYPE = this.shipRequestInfo[0]['SHIPREQUESTDETAILTYPE'];
						for (var i = 0; i <= this.shipRequestInfo.length; i++) {
							this.dlistB.push({
								id: res.DATA[i].SHIPREQUESTDETAILNAME,
								label: res.DATA[i].SHIPREQUESTDETAILNAME
							});
							this.dlistShowB = true;
						}
					} else {
						this.$showMessage('未查询到单据行项');
						this.shipRequestNameDetail = ''; //清空text
					}
				})
				.catch((err) => {
					this.$showMessage(err);
					return;
				});
		},
		//查询订单信息是否为空，不为空就跳转到订单行项
		checkShipRequest() {
			if (this.shipRequestName == null || '' == this.shipRequestName.trim()) {
				this.$showMessage('订单为空，请确认', 'error');
				return;
			}
			this.cleardetailInfo(); //清空订单明细以及pallet信息
			this.getfocus('focusShipRequestDetail');
		},
		//校验订单行项信息
		checkShipRequestDetail() {
			if (this.shipRequestNameDetail == null || '' == this.shipRequestNameDetail.trim()) {
				this.$showMessage('订单为空，请确认', 'error');
				return;
			}
			let _param = this.Queryaram;
			_param.queryID = 'GetShipRequestList';
			_param.version = 'xx010';
			_param.BINDV.SITENAME = uni.getStorageSync('SITENAME');
			_param.BINDV.SHIPREQUESTNAME = this.shipRequestName.trim();
			_param.BINDV.SHIPREQUESTDETAILNAME = this.shipRequestNameDetail.trim();
			QueryInfo({
				param: JSON.stringify(_param)
			})
				.then((res) => {
					this.shipRequestDetailInfo = res.DATA;
					//将行项信息清空，然后进行赋值
					this.productSpec = '';
					this.PackingGrade = '';
					this.requestQuantity = '';
					this.assignedQuantity = '';
					this.productSpec = this.shipRequestDetailInfo[0]['PRODUCTSPECNAME'];
					this.PackingGrade = this.shipRequestDetailInfo[0]['GRADE'];
					this.requestQuantity = this.shipRequestDetailInfo[0]['REQUESTQUANTITY'];
					this.assignedQuantity = this.shipRequestDetailInfo[0]['ASSIGNEDQUANTITY'];
					if (
						this.SHIPREQUESTDETAILTYPE != 'S30028' &&
						this.SHIPREQUESTDETAILTYPE != 'S30029' &&
						this.SHIPREQUESTDETAILTYPE != 'S30629' 
					) {
						this.$showMessage('单据类型不正确:' + this.SHIPREQUESTDETAILTYPE);
						this.shipRequestName = '';
						this.shipRequestNameDetail = ''; //清空text
						this.productSpec = '';
						this.PackingGrade = '';
						this.requestQuantity = '';
						this.assignedQuantity = '';
						return;
					}
					if (this.requestQuantity == this.assignedQuantity) {
						this.$showMessage('该订单行项目已完全出库');
						this.shipRequestNameDetail = ''; //清空text
						this.productSpec = '';
						this.PackingGrade = '';
						this.requestQuantity = '';
						this.assignedQuantity = '';
						this.getfocus('focusShipRequestDetail');
					} else {
						this.getfocus('focusPalletName');
					}
				})
				.catch((err) => {
					this.$showMessage(err);
					return;
				});
		},
		//扫描pallet回车后查询pallet信息到表格
		SearchPalletInfo() {
			let _param = this.Queryaram;
			_param.queryID = 'GetPalletRow';
			_param.version = 'YYL03';
			_param.BINDV.SITENAME = uni.getStorageSync('SITENAME');
			_param.BINDV.PRODUCTPACKINGNAME = this.palletName.trim();
			QueryInfo({
				param: JSON.stringify(_param)
			})
				.then((res) => {
					if (res.DATA != undefined) {
						this.PalletInfo = res.DATA;
						//校验pallet信息是否符合标准
						let leaveCount = this.requestQuantity - this.assignedQuantity;
						if (parseInt(this.PalletInfo[0]['PRODUCTQUANTITY']) > leaveCount) {
							this.$showMessage('该托数量超过该订单行项剩余数量，无法拣配:' + this.palletName);
							this.PalletInfo = [];
							this.palletName = '';
							return;
						}
						if (this.PalletInfo[0]['ERPFACTORYNAME'] != this.erpFactoryName.trim()) {
							this.$showMessage('该托ERP工厂与订单不匹配:' + this.palletName);
							this.PalletInfo = [];
							this.palletName = '';
							return;
						}
						if (this.PalletInfo[0]['ERPLOCATIONNAME'] != this.erpLocationName.trim()) {
							this.$showMessage('该托ERP库位与订单不匹配:' + this.palletName);
							this.PalletInfo = [];
							this.palletName = '';
							return;
						}
						if (this.PalletInfo[0]['PRODUCTSPECNAME'] != this.productSpec.trim()) {
							this.$showMessage('该托fgcode与订单不匹配:' + this.palletName);
							this.PalletInfo = [];
							this.palletName = '';
							return;
						}
						// if(this.PalletInfo[0]['PACKINGGRADE']!=this.PackingGrade.trim()){
						// 	this.$showMessage('该托等级与订单不匹配:'+this.palletName);
						// 	this.PalletInfo=[];
						// 	this.palletName='';
						// 	return;
						// }
					} else {
						this.$showMessage(' 是否被冻结和拣配,或者托盘不在库:' + this.palletName);
						this.PalletInfo = [];
						this.palletName = '';
						return;
					}
				})
				.catch((err) => {
					this.$showMessage(err);
					this.PalletInfo = [];
					this.palletName = '';
					return;
				});
		},
		//确认出库
		ReserveStockOut() {
			if (this.shipRequestName == '') {
				this.$showMessage('订单号不能为空');
				return;
			}
			if (this.shipRequestNameDetail == '') {
				this.$showMessage('订单行项不能为空');
				return;
			}
			if (this.palletName == '') {
				this.$showMessage('未扫入Pallet,请扫入Pallet信息');
				return;
			}
			if (
				this.SHIPREQUESTDETAILTYPE != 'S30028' &&
				this.SHIPREQUESTDETAILTYPE != 'S30029' &&
				this.SHIPREQUESTDETAILTYPE != 'S30629' 
			) {
				this.$showMessage('单据类型不正确,请确认该单据是否在该页面出:' + this.SHIPREQUESTDETAILTYPE);
				this.cleardetailInfo();
				return;
			}
			this.Comloading = true;
			this.stockOut.SITENAME = uni.getStorageSync('SITENAME'); //现地
			this.stockOut.USERNAME = uni.getStorageSync('username'); //操作人
			this.stockOut.SHIPREQUESTNAME = this.shipRequestName;
			this.stockOut.SHIPREQUESTDETAILNAME = this.shipRequestNameDetail;
			this.stockOut.PRODUCTPACKINGNAME = this.palletName;
			//发送出库请求
			ReserveStockOutPallet({
				param: JSON.stringify(this.stockOut)
			})
				.then((res) => {
					if (res['status'] == 'success') {
						this.$showMessage(res['message'][0]); //后端返回的信息
						this.cleardetailInfo();
						this.$playSuccess(); //成功提示音
						this.getfocus('focusShipRequestDetail'); //焦点聚焦到条码
					} else {
						this.$showMessage(res['message'][0]);
						this.$playFail(); //提示音
					}
					this.Comloading = false;
				})
				.catch((err) => {
					this.Comloading = false;
					console.log(err);
					//调用失败的话报错提示
					this.$showMessage(err);
					this.$palyFail();
				});
		},
		//清空pallet信息和明细信息
		cleardetailInfo() {
			this.palletName = '';
			this.shipRequestNameDetail = '';
			this.productSpec = '';
			this.PackingGrade = '';
			this.requestQuantity = '';
			this.assignedQuantity = '';
			this.PalletInfo = []; //清空查询信息
			this.shipRequestDetailInfo = [];
		}
	}
};
</script>

<style></style>