<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="ShipRequestName" placeholder="请选择单据" :focus="focusShipRequest"
					@confirm="checkShipRequest" style="background: #ffffff; margin-right: 10rpx" />
				<u-button @click="selectShipRequest" 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 v-show="false">
				<u-col span="2"><label>料号：</label> </u-col>
				<u-col span="10"><label>{{MATERIALSPECNAME}}</label> </u-col>
				<u-col span="2"><label>品名：</label> </u-col>
				<u-col span="10"><label>{{DESC_CN}}</label> </u-col>
				<u-col span="2"><label>规格：</label> </u-col>
				<u-col span="10"><label>{{SPECNAME}}</label> </u-col>
				<u-col span="2"><label>阶段：</label> </u-col>
				<u-col span="10"><label>{{PHASE}}</label> </u-col>
				<u-col span="2"><label>单号：</label> </u-col>
				<u-col span="4"><label>{{SHIPREQUESTNAME}}</label> </u-col>
				<u-col span="2"><label>行号：</label> </u-col>
				<u-col span="4"><label>{{SHIPREQUESTDETAILNAME}}</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>接收数量：{{ ASSIGNEDQUANTITY || '0' }}</view>
						</u-col>
					</u-row>
				</view>
			</u-card>
			<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>发出：{{ ERPFACTORY}} {{ ERPLOCATION }}</view>
						</u-col>
						<u-col span="6">
							<view>接收：{{ ERPRECEIVEFACTORY }} {{ ERPRECEIVELOCATION }}</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" style="height: 400upx;"	>
			<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>
		<view class="tabbar_bottom">
			<uni-datetime-picker style="align-items: center;" type="date" :clear-icon="false" v-model="commitDate" @maskClick="maskClick" />
		<u-button class="btn-bottom" @click="CommitMoveInInvoice" style="background: #0081ff; color: #ffffff" :loading="loading">确认提交
		</u-button>
		</view>
	</view>
</template>

<script>
	import {
		print
	} from '../../../common/BluetoothPrint.js';
	import { cals } from '@/utils/cal'
	export default {
		data() {
			return {
				array: ['请选择行号'],
				background: {
					backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				},
				index: 0,
				REQUESTQUANTITY: 0,
				ASSIGNEDQUANTITY: 0,
				BOX_QTY: "",
				focusPalletName: false,
				ShipRequestName: "",
				SHIPREQUESTNAME: "",
				SHIPREQUESTDETAILNAME: "",
				focusShipRequest: "",
				dlist: [], //选择内容
				dlistShow: false, //是否显示选择框
				description: "描述",
				loading: false,
				BoxListInfo: [],
				jg: "",
				zpl: "",
				commitDate:'',
				focusBoxQty: false,
				rangedata: [],
				DESC_CN: "",
				SPECNAME: "",
				MATERIALSPECNAME: "",
				PHASE: "",
				LOCATIONNAME: "",
				MATERIALPACKINGNAME: "",
				ShipRequestType: "",
				userId: "",
				ERPRECEIVEFACTORY: "",
				ERPRECEIVELOCATION: "",
				ERPLOCATION: "",
				ERPFACTORY: ""
			}
		},
		created() {
			let that = this
			uni.getStorage({
				key: "sitename",
				success(res) {
					that.siteName = res.data
				}
			})
			uni.getStorage({
				key: "userid",
				success(res) {
					that.userId = res.data
				}
			})
			this.getTimeData();
		},
		computed: {},
		methods: {
			getTimeData(){
				var year=new Date().getFullYear();//年
				if (year< 1900) year = year + 1900;
				var month = new Date().getMonth() + 1;//月
				if (month < 10) month = '0' + month;
				var day = new Date().getDate();//日
				if (day < 10) day = '0' + day;
				const calendar1 = year+'-'+month+'-'+day
				this.commitDate = calendar1
			},
			maskClick(e) {
				console.log('maskClick事件:', e);
			},
			selConfirm(e) {
				this.ShipRequestName = e[0].value;
				this.checkShipRequest();
			},

			bindPickerChange: function(e) { //改变的事件名
				// console.log('picker发送选择改变，携带值为', e.target.value) // 用于输出改变索引值
				// this.index = e.target.value //将数组改变索引赋给定义的index变量
				console.log(e)
				this.jg = this.array[0] //将array【改变索引】的值赋给定义的jg变量
				this.$MyRequest('/invoice/getMoveInLineInfo', {
					ShipRequestName: 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.ASSIGNEDQUANTITY = _Arr[i]['ASSIGNEDQUANTITY'];
							this.SPCNAME = _Arr[i]['SPCNAME'];
							this.MATERIALSPECNAME = _Arr[i]['MATERIALSPECNAME'];
							this.SHIPREQUESTNAME = _Arr[i]['SHIPREQUESTNAME'];
							this.SHIPREQUESTDETAILNAME = _Arr[i]['SHIPREQUESTDETAILNAME'];
							this.PHASE = _Arr[i]['PHASE'];
							this.ERPRECEIVEFACTORY = _Arr[i]['ERPRECEIVEFACTORY'];
							this.ERPRECEIVELOCATION = _Arr[i]['ERPRECEIVELOCATION'];
							this.ERPLOCATION = _Arr[i]['ERPLOCATION'];
							this.ERPFACTORY = _Arr[i]['ERPFACTORY'];
						}
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			//查找待操作的调拨入库单集合
			selectShipRequest() {
				this.dlist = [];
				this.BoxListInfo = [];
				this.$MyRequest('/invoice/getMoveInList', {
					ShipRequestName: this.ShipRequestName,
					ShipRequestType: "4I-Cxx-08"
				}).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].SHIPREQUESTNAME)
							this.dlist.push({
								id: res.data.resultObj[i].SHIPREQUESTNAME,
								label: res.data.resultObj[i].SHIPREQUESTNAME
							});
							console.log(this.dlist);
							this.dlistShow = true;
							this.checkShipRequest();
						}
					} else {
						this.$showMessage(res.data.message)
					}

				}).catch(err => {
					this.$showMessage(err)
				})
			},
			checkShipRequest() {
				this.resetValue();
				this.$MyRequest('/invoice/getMoveInLine', {
					ShipRequestName: this.ShipRequestName
				}).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.bindPickerChange(this.array)
					}
					this.focusBarCode = true
					this.focusShipRequest = false
				}).catch(err => {
					this.$showMessage(err)
				});
				this.getBarCode();
			},
			//创建条码信息
			AnalyBarCode() {
				var barCodeArr = this.MATERIALPACKINGNAME.split("|");
				// 470110856|1|C|75mm*1000m|SHBP01JT1202305240009|20230524|11|75
				if (barCodeArr.length < 7) {
					this.$showMessage("条码规则不符合需求!")
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					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.ASSIGNEDQUANTITY) + Number(materialQuantity) > Number(this.REQUESTQUANTITY)) {
					this.$showMessage("可接收数量不足")
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					return;
				}
				//发信息到后端绑定条码和条码关系
				if (this.LOCATIONNAME == '') {
					this.$showMessage("货位信息不能为空!")
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					return;
				}
				// if (this.MATERIALSPECNAME != materialSpecName) {
				// 	this.$showMessage("物料号和单据行明细物料不一致！")
				// 	return;
				// }
				// if (this.PHASE != phase) {
				// 	this.$showMessage("阶段和单据行明细阶段不一致!")
				// 	return;
				// }
				// this.ASSIGNEDQUANTITY = Number(this.ASSIGNEDQUANTITY) + Number(materialQuantity);
				// this.focusBoxQty = true;
				this.$MyRequest('/MoveIn/BindBarCodeToMoveIn', {
					MATERIALPACKINGNAME: this.MATERIALPACKINGNAME,
					charge: materialPackingName,
					locationName: this.LOCATIONNAME,
					materialSpecName: materialSpecName,
					shipRequestName: this.SHIPREQUESTNAME,
					shipRequestDetailName: this.SHIPREQUESTDETAILNAME,
					user: this.userId == "" ? "101867" : this.userId,
					materialQuantity: materialQuantity
				}).then(res => {
					console.log("************" + res.data.status)
					if (res.data.success) {
						// this.$showMessage(11111)
						this.ASSIGNEDQUANTITY = (cals.add(this.ASSIGNEDQUANTITY, materialQuantity)).toNumber()
						this.focusBoxQty = true;
						this.MATERIALPACKINGNAME = '';
						this.getBarCode();
					} else {
						this.$showMessage(res.data.message)
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
					}
				}).catch(err => {
					this.$showMessage(err)
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
				})

			},
			//获取绑定条码信息
			getBarCode() {
				this.resetValue();
				this.$MyRequest('/invoice/getMoveInBindBarCode', {
					ShipRequestName: this.ShipRequestName
				}).then(res => {
					if (res.data.success) {
						this.BoxListInfo = res.data.resultObj;
						try {
							Vue.prototype.$playSuccess()
						} catch (e) {}
					} else {
						this.$showMessage(res.data)
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
					}
				}).catch(err => {
					this.$showMessage(err)
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
				})
			},

			CommitMoveInInvoice() {
				console.log(this.commitDate)
				if (this.BoxListInfo == '') {
					this.$showMessage("没有要提交的信息，请确认")
					return;
				}
				this.loading = true;
				this.$MyRequest('/MoveIn/CommitMoveInInvoice', {
					ShipRequestName: this.ShipRequestName,
					commitDate:this.commitDate,
				}).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>
	.btn-bottom {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		padding-bottom: constant(safe-area-inset-bottom);padding-bottom: env(safe-area-inset-bottom);
	}
	
</style>
