<template>
	<view>
		<u-navbar back-text="返回" title="其他入库" :background="background"></u-navbar>
		<u-form ref="testForm" style="margin: 10px;">
			<u-form-item :border-bottom="false">
				<p>单据:</p>
				<u-select v-model="dlistShow" mode="single-column" value-name="id" :list="dlist" @confirm="selConfirm"
					confirm-text="确认选择" />
				<u-input :border="true" v-model="ReceiveRequestName" placeholder="请选择单据" :focus="focusReceiveRequest"
					@confirm="checkReceiveRequest" style="background: #ffffff; margin-right: 10rpx" />
				<u-button @click="selectReceiveRequest" type="primary" size="mini">选择单号</u-button>
			</u-form-item>


			<u-form-item :border-bottom="false">
				<picker @change="bindPickerChange" :range="array">
					<label>选择行：</label>
					<label class="">{{array[index]}}</label>
				</picker>
			</u-form-item>

			<u-row>
				<u-col span="2"><label>料号：</label> </u-col>
				<u-col span="10"><label>{{MATERIALSPECNAME}}</label> </u-col>
			</u-row>
			<br />
			<u-row>
				<u-col span="2"><label>品名：</label> </u-col>
				<u-col span="10"><label>{{DESC_CN}}</label> </u-col>
			</u-row>
			<br />
			<u-row>
				<u-col span="2"><label>规格：</label> </u-col>
				<u-col span="10"><label>{{SPECNAME}}</label> </u-col>
			</u-row>
			<br />
			<u-row>
				<u-col span="2"><label>阶段：</label> </u-col>
				<u-col span="10"><label>{{PHASE}}</label> </u-col>
			</u-row>
			<br />
			<u-row>
				<u-col span="2"><label>单号：</label> </u-col>
				<u-col span="4"><label>{{ReceiveRequestNAME}}</label> </u-col>
				<u-col span="2"><label>行号：</label> </u-col>
				<u-col span="4"><label>{{ReceiveRequestDETAILNAME}}</label> </u-col>
			</u-row>
			<br />
			<u-row>
				<u-col span="2"><label>组织：</label> </u-col>
				<u-col span="4"><label>{{ERPFACTORY}}</label> </u-col>
				<u-col span="2"><label>仓库：</label> </u-col>
				<u-col span="4"><label>{{ERPLOCATION}}</label> </u-col>
			</u-row>
			<br />
			<u-row>
				<u-col span="3"><label>入库单：</label> </u-col>
				<u-col span="9"><label>{{RECEIVEACTNO}}</label> </u-col>
			</u-row>
			<u-card style="border: 1rpx solid #0081ff" :show-head="false" :full="true"
				:body-style="{ height: '100rpx' }">
				<view slot="body">
					<u-row>
						<u-col span="6">
							<view>应收数量：{{ REQUESTQUANTITY || '0' }}</view>
						</u-col>
						<u-col span="6">
							<view>接收数量：{{ RECEIVEDQUANTITY || '0' }}</view>
						</u-col>
					</u-row>
				</view>
			</u-card>
			<u-row>
				<u-col span="3">
					<p>货位条码:</p>
				</u-col>
				<u-col span="7">
					<u-input :border="true" v-model="LOCATIONNAME" placeholder="请扫描货位" style="background: #ffffff;" />
				</u-col>
				<u-col span="2">
					<u-button @click="clearLocationName" type="primary" size="mini">清空货位</u-button>
				</u-col>
			</u-row>
			<u-row>
				<u-col span="3">
					<p>来料条码:</p>
				</u-col>
				<u-col span="7">
					<u-input :border="true" v-model="MATERIALPACKINGNAME" @confirm="AnalyBarCode" placeholder="请扫描条码"
						:focus="focusPalletName" style="background: #ffffff;" />
				</u-col>
			</u-row>


		</u-form>

		<uni-table ref="BoxList" border stripe type="false">
			<uni-tr>
				<uni-th align="center">标签</uni-th>
				<uni-th align="center">批次</uni-th>
				<uni-th align="center">品名</uni-th>
				<uni-th align="center">料号</uni-th>
				<uni-th align="center">规格</uni-th>
				<uni-th align="center">阶段</uni-th>
				<uni-th align="center">数量</uni-th>
				<uni-th align="center">组织</uni-th>
				<uni-th align="center">仓库</uni-th>
				<uni-th align="center">货位</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in BoxListInfo" :key="index">
				<!-- :style="getStyle(index)"-->
				<!--:style="{ backgroundColor: item.color === 0 ? 'white' : 'blue' }" -->
				<uni-td align="center">{{ item.MATERIALPACKINGNAME}}</uni-td>
				<uni-td align="center">{{ item.CHARGE}}</uni-td>
				<uni-td align="center">{{ item.DESC_CN}}</uni-td>
				<uni-td align="center">{{ item.MATERIALSPECNAME }}</uni-td>
				<uni-td align="center">{{ item.SPECNAME }}</uni-td>
				<uni-td align="center">{{ item.PHASE }}</uni-td>
				<uni-td align="center">{{ item.MATERIALQUANTITY }}</uni-td>
				<uni-td align="center">{{ item.ERPFACTORY }}</uni-td>
				<uni-td align="center">{{ item.ERPLOCATION }}</uni-td>
				<uni-td align="center">{{ item.LOCATIONNAME }}</uni-td>
			</uni-tr>
		</uni-table>
		<u-button @click="CommitOtherInInvoice" style="background: #0081ff; color: #ffffff" :loading="loading">确认提交
		</u-button>
	</view>
</template>

<script>
	import {
		print
	} from '../../../../common/BluetoothPrint.js';
	
	export default {
		data() {
			return {
				array: ['请选择行号'],
				background: {
					backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				},
				index: 0,
				REQUESTQUANTITY: 0,
				RECEIVEDQUANTITY: 0,
				BOX_QTY: "",
				focusPalletName: false,
				ReceiveRequestName: "",
				ReceiveRequestNAME: '',
				ReceiveRequestDETAILNAME: "",
				focusReceiveRequest: "",
				dlist: [], //选择内容
				dlistShow: false, //是否显示选择框
				description: "描述",
				loading: false,
				BoxListInfo: [],
				jg: "",
				zpl: "",
				focusBoxQty: false,
				rangedata: [],
				DESC_CN: "",
				SPECNAME: "",
				MATERIALSPECNAME: "",
				PHASE: "",
				LOCATIONNAME: "",
				MATERIALPACKINGNAME: "",
				ReceiveRequestType: "",
				userId: "",
				ERPLOCATION: "",
				ERPFACTORY: "",
				RECEIVEACTNO: ""
			}
		},
		created() {
			let that = this
			uni.getStorage({
				key: "sitename",
				success(res) {
					that.siteName = res.data
				}
			})
			uni.getStorage({
				key: "userid",
				success(res) {
					that.userId = res.data
				}
			})
		},
		computed: {},
		methods: {
			selConfirm(e) {
				this.ReceiveRequestName = e[0].value;
				this.checkReceiveRequest();
			},

			bindPickerChange: function(e) { //改变的事件名
				console.log('picker发送选择改变，携带值为', e.target.value) // 用于输出改变索引值
				this.index = e.target.value //将数组改变索引赋给定义的index变量
				this.jg = this.array[this.index] //将array【改变索引】的值赋给定义的jg变量
				this.$MyRequest('/invoice/getOtherInLineInfo', {
					ReceiveRequestName: this.jg
				}).then(res => {
					if (res.data.success) {
						var _Arr = res.data.resultObj;
						for (let i = 0; i < _Arr.length; ++i) {
							this.DESC_CN = _Arr[i]['DESC_CN'];
							this.REQUESTQUANTITY = _Arr[i]['REQUESTQUANTITY'];
							this.RECEIVEDQUANTITY = _Arr[i]['RECEIVEDQUANTITY'];
							this.SPCNAME = _Arr[i]['SPCNAME'];
							this.MATERIALSPECNAME = _Arr[i]['MATERIALSPECNAME'];
							this.ReceiveRequestNAME = _Arr[i]['RECEIVEREQUESTNAME'];
							this.ReceiveRequestDETAILNAME = _Arr[i]['RECEIVEREQUESTDETAILNAME'];
							this.PHASE = _Arr[i]['PHASE'];
							this.ERPLOCATION = _Arr[i]['ERPLOCATION'];
							this.ERPFACTORY = _Arr[i]['ERPFACTORY'];
							this.RECEIVEACTNO=_Arr[i]['RECEIVEACTNO'];
						}
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			//查找待操作的调拨入库单集合
			selectReceiveRequest() {
				this.dlist = [];
				this.BoxListInfo = [];
				this.$MyRequest('/invoice/getOtherInList', {
					ReceiveRequestName: this.ReceiveRequestName,
					ReceiveRequestType: "ZK4A"
				}).then(res => {
					console.log(res.data)
					if (res.data.success) {
						console.log(res.data.resultObj.length)
						for (var i = 0; i <= res.data.resultObj.length; i++) {
							console.log(res.data.resultObj[i].ReceiveRequestNAME)
							this.dlist.push({
								id: res.data.resultObj[i].RECEIVEREQUESTNAME,
								label: res.data.resultObj[i].RECEIVEREQUESTNAME
							});
							console.log(this.dlist);
							this.dlistShow = true;
							//this.checkReceiveRequest();
						}
					} else {
						this.$showMessage(res.data.message)
					}

				}).catch(err => {
					this.$showMessage(err)
				})
			},
			checkReceiveRequest() {
				this.resetValue();
				this.$MyRequest('/invoice/getOtherInLine', {
					ReceiveRequestName: this.ReceiveRequestName,
					ReceiveRequestType: "ZK4A"
				}).then(res => {
					if (res.data.success) {
						var _Arr = res.data.resultObj;
						for (let i = 0; i < _Arr.length; ++i) {
							console.log(_Arr[i])
							this.array[i] = _Arr[i]['WL'];
						}
					}
					this.focusBarCode = true
					this.focusReceiveRequest = false
				}).catch(err => {
					this.$showMessage(err)
				});
				this.getBarCode();
			},
			//创建条码信息
			async createActNo() {
				await this.$MyRequest('/FGStockIn/createActNo', {
					//MATERIALPACKINGNAME: this.MATERIALPACKINGNAME
				}).then(res => {
					if (res.data.success) {
						console.log(res.data.resultObj + "*****************************")
						var _Arr = res.data.resultObj;
						this.RECEIVEACTNO = _Arr[0].ReceiveActNo;
					} else {
						this.$showMessage(res.data.message)
					}
				}).catch(err => {
					this.$showMessage(err)
				});
			},
		async AnalyBarCode() {
				if (this.BoxListInfo.some(box => box.MATERIALPACKING === this.palletName)) {
					this.$showMessage("改标签已扫描，请不要重复扫描！");
					return;
				}
				if (parseFloat(this.REQUESTQUANTITY) * 1.05 < parseFloat(this.RECEIVEDQUANTITY)) {
					this.$showMessage("标签数量大于最大接收数量！");
					return;
				}
				var barCodeArr = this.MATERIALPACKINGNAME.split("|");
				// 470110856|1|C|75mm*1000m|SHBP01JT1202305240009|20230524|11|75
				if (barCodeArr.length < 7) {
					this.$showMessage("条码规则不符合需求!")
					return;
				}
				if (barCodeArr.length != 8) return;
				var materialSpecName = barCodeArr[0];
				var phase = barCodeArr[2];
				var description = barCodeArr[3];
				var materialPackingName = barCodeArr[4];
				var makeDate = barCodeArr[5];
				var materialUnit = barCodeArr[6];
				var materialQuantity = barCodeArr[7];
				if (Number(this.RECEIVEDQUANTITY) + Number(materialQuantity) > Number(this.REQUESTQUANTITY)) {
					this.$showMessage("可接收数量不足")
					return;
				}

				if (this.MATERIALSPECNAME != materialSpecName) {
					this.$showMessage("物料号和单据行明细物料不一致！")
					return;
				}
				console.log(barCodeArr)
				if (this.PHASE != phase) {
					this.$showMessage("阶段和单据行明细阶段不一致!")
					return;
				}
				if (!(this.MATERIALSPECNAME == materialSpecName)) {
					this.$showMessage("该标签物料与到货单明细物料不相同！")
					return;
				}

				if (this.LOCATIONNAME == '') {
					this.$showMessage("货位信息不能为空!")
					return;
				}
				//判断标签是否扫过了
				if (this.RECEIVEACTNO == '') {
					this.createActNo()
				} 

				//发信息到后端绑定条码和条码关系
				// this.RECEIVEDQUANTITY = Number(this.RECEIVEDQUANTITY) + Number(materialQuantity);
				// this.focusBoxQty = true;
				this.focusBoxQty = false;
			await	this.$MyRequest('/OtherIn/createBoxInfo', {
					MATERIALPACKINGNAME: this.MATERIALPACKINGNAME,
					charge: materialPackingName,
					locationName: this.LOCATIONNAME,
					materialSpecName: materialSpecName,
					ReceiveRequestName: this.ReceiveRequestNAME,
					ReceiveRequestDetailName: this.ReceiveRequestDETAILNAME,
					ERPLocation: this.ERPLOCATION,
					RECEIVEACTNO: this.RECEIVEACTNO,
					ERPFactory: this.ERPFACTORY,
					user: this.userId == "" ? "101867" : this.userId
				}).then(res => {
					console.log("************" + res.data.status)
					if (res.data.success) {
						this.RECEIVEDQUANTITY = Number(this.RECEIVEDQUANTITY) + Number(materialQuantity);
						this.focusBoxQty = true;
						this.MATERIALPACKINGNAME = '';
						this.getBarCode();
					} else {
						this.$showMessage(res.data.message)
					}
				}).catch(err => {
					this.$showMessage(err)
				})

			},
			//获取绑定条码信息
			getBarCode() {
				this.resetValue();
				this.$MyRequest('/invoice/getOtherInBindBarCode', {
					RECEIVEACTNO: this.RECEIVEACTNO
				}).then(res => {
					if (res.data.success) {
						this.BoxListInfo = res.data.resultObj;
						this.RECEIVEDQUANTITY = this.BoxListInfo.reduce((accumulator, currentValue) => {  
						    return accumulator + currentValue.MATERIALQUANTITY;  
						}, 0); // 第二个参数是累加器的初始值
					} else {
						this.$showMessage(res.data)
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},

			CommitOtherInInvoice() {
				if (this.BoxListInfo.length ==0) {
					this.$showMessage("没有要提交的信息，请确认")
					return;
				}
				this.loading = true;
				this.$MyRequest('/OtherIn/CommitOtherInInvoice', {
					ReceiveRequestName: this.ReceiveRequestName
				}).then(res => {
					if (res.data.success) {
						this.loading = false;
						this.resetValue();
						this.$showMessage(res.data.message)
					} else {
						this.loading = false;
						this.$showMessage(res.data.message)
					}
				}).catch(err => {
					this.loading = false;
					this.$showMessage(err)
				})
			},


			/**
			 * 清空货位信息
			 */
			clearLocationName() {
				this.LOCATIONNAME = '';
			},

			resetValue() {
				this.BoxListInfo = [];
				this.palletName = "";
				this.BOX_QTY = "";
				this.PLAN_QTY = 0;
				this.PRTBARCODEQTY = 0;
				this.description = "";
				this.array = [];
			}
		}
	}
</script>

<style>

</style>
