<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="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-form-item :border-bottom="false">
				<label>物料描述：</label>
				<label>{{description}}</label>
			</u-form-item>

			<u-form-item :border-bottom="false">
				<u-row>
					<u-col span="4"><label>制造日期：</label> </u-col>
					<u-col span="8">
						<picker mode="date" :end="endDate" @change="bindDateChange">
							<label class="">{{date}}</label>
						</picker>
					</u-col>
				</u-row>
			</u-form-item>


			<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-form-item :border-bottom="false">
				<u-row>
					<u-col span="4">
						<p>来料条码:</p>
					</u-col>
					<u-col span="8">
						<u-input :border="true" v-model="palletName" @confirm="AnalyBarCode" placeholder="请扫描条码"
							:focus="focusPalletName" style="background: #ffffff;" />
					</u-col>
				</u-row>
			</u-form-item>

			<u-form-item :border-bottom="false">
				<u-row>
					<u-col span="4">
						<p>货位条码:</p>
					</u-col>
					<u-col span="8">
						<u-input :border="true" v-model="locationName" @confirm="AnalyBarCode" placeholder="请扫描货位"
							style="background: #ffffff;" />
					</u-col>
				</u-row>
			</u-form-item>
		</u-form>

		<uni-table ref="BoxList" :loading="loading" 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-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.MATERIALPACKING}}</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.DESCRIPTION }}</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="GenerateBox" style="background: #0081ff; color: #ffffff">确认提交</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))'
				},
				date: this.getDate(),
				index: 0,
				REQUESTQUANTITY: 0,
				RECEIVEDQUANTITY: 0,
				RECEIVEACT:'',
				MATERIALSPECNAME:'',
				BOX_QTY: "",
				palletName: "",
				focusPalletName: false,
				ReceiveRequestName: "",
				focusReceiveRequest: "",
				dlist: [], //选择内容
				dlistShow: false, //是否显示选择框
				description: "描述",
				loading: false,
				BoxListInfo: [],
				jg: "",
				zpl: "",
				focusBoxQty: false,
				rangedata: [],
			}
		},
		created() {
			//挂载页面的时候就查询下拉框的数据,这是无参请求所以没参数
			this.$MyRequest('/split/getbluetoothprinter', {}, 'GET').then(res => {
				if (res.data.success) {
					this.rangedata = res.data.resultObj //接数据，是一个对象数组
					this.show = true //设置为true展示picker组件
				} else {
					console.log(this.data.message)
				}
			}).catch(err => {
				this.$showMessage(err)
			});
			let that = this
			uni.getStorage({
				key: "sitename",
				success(res) {
					that.siteName = res.data
				}
			})
			uni.getStorage({
				key: "userid",
				success(res) {
					that.userId = res.data
				}
			})
		},
		computed: {
			endDate() {
				return this.getDate();
			}
		},
		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('/barCode/getSLInfo', {
					ReceiveRequestName: this.jg
				}).then(res => {
					if (res.data.success) {
						var _Arr = res.data.resultObj;
						for (let i = 0; i < _Arr.length; ++i) {
							this.description = _Arr[i]['DESCRIPTION'];
							this.REQUESTQUANTITY = _Arr[i]['PLAN_QTY'];
							this.RECEIVEDQUANTITY = _Arr[i]['PRTBARCODEQTY'];
							this.MATERIALSPECNAME=_Arr[i]['MATERIALSPECNAME'];
						}
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			getDate() {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			bindDateChange: function(e) {
				this.date = e.detail.value;
			},
			//查找代打条码的领料单集合
			selectReceiveRequest() {
				this.dlist = [];
				this.BoxListInfo = [];
				this.$MyRequest('/barCode/getSLList', {
					ReceiveRequestName: this.ReceiveRequestName
				}).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();
						}
					}

				}).catch(err => {
					this.$showMessage(err)
				})
			},
			checkReceiveRequest() {
				this.resetValue();
				this.$MyRequest('/barCode/getSLCBM', {
					ReceiveRequestName: this.ReceiveRequestName
				}).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)
				})
			},
			//创建条码信息
			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.palletName.split("|");
				// 470110856|1|C|75mm*1000m|SHBP01JT1202305240009|20230524|11|75
				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(!(this.MATERIALSPECNAME==materialSpecName))
				{
					this.$showMessage("该标签物料与到货单明细物料不相同！")
					return;
				}
				if (Number(this.RECEIVEDQUANTITY) + Number(materialQuantity) > Number(this.REQUESTQUANTITY)) {
					this.$showMessage("可收货数量不足")
					return;
				}
				this.RECEIVEDQUANTITY = Number(this.RECEIVEDQUANTITY) + Number(materialQuantity);
				//生成入库单，判断入库单状态

				this.$MyRequest('/invoice/createActNo', {
					ReceiveRequestName: this.jg, //单号
					palletName: this.palletName,
					makeDate: new Date(),
					qty: this.BOX_QTY
				}).then(res => {
					console.log(res.data.success)
					if (res.data.success) {
						var _Arr = res.data.resultObj;
						console.log(_Arr)
						this.BoxListInfo.push(_Arr[0]);
						console.log("***********************")
						console.log(this.BoxListInfo)
					}
					this.focusBarCode = true
					this.focusReceiveRequest = false
				}).catch(err => {
					this.$showMessage(err)
				})
				this.BOX_QTY = "";
				this.focusBoxQty = true;
			},

			GenerateBox() {
				if (this.BoxListInfo.length > 0) {
					let macaddress = this.rangedata[0].ENUMVALUE
					this.$MyRequest('/barCode/getZICOXCPCL', {
						info: this.BoxListInfo
					}).then(res => {
						if (res.data.success) {
							this.zpl = res.data.resultObj[0]['LABELSTRING'].toString();
							console.log(this.zpl)


						}
					}).catch(err => {
						this.$showMessage(err)
					})
					for (let i = 0; i < this.BoxListInfo.length; ++i) {
						let _zpl = '! 0 200 200 400 1\n' +
							'PAGE-WIDTH 560\n' +
							/* 'PW 848\n' + */
							'TONE 0\n' +
							'SPEED 0\n' +
							'GAP-SENSE\n' +
							'NO-PACE\n' +
							'POSTFEED 0\n' +
							'LEFT\n' +
							'T 65 1 20 20 物料编码：\n' +
							'SETBOLD 2\n' +
							'T 65 1 135 20 ' + this.BoxListInfo[i]['MATERIALSPECNAME'] + '\n' +
							'SETBOLD 0\n' +
							'T 65 1 20 50 客供料号：\n' +
							'T 65 1   135 50 ' + this.BoxListInfo[i]['FK_TWH_NUMBER'] + '\n' +

							'T 65 0 20 80 物料名称：\n' +
							'T 65 1 135 80 ' + this.BoxListInfo[i]['DESCRIPTION'] + '\n' +

							'T 65 0 20 110 SN：\n' +
							'T 65 1 135 110 ' + this.BoxListInfo[i]['MATERIALPACKING'] + '\n' +


							'T 65 0 20 140 客户S/N：\n' +
							'T 65 1 135 140 ' + this.BoxListInfo[i]["SOURCESN"] + '\n' +

							'T 65 0 20 170 供应商：\n' +
							'T 65 1 135 170 ' + this.BoxListInfo[i]["SUPPLIERNAME"] + '\n' +



							'T 65 0 20 200 批次：\n' +
							'T 65 1 135 200 ' + this.BoxListInfo[i]["CHARGE"] + '\n' +


							'T 65 0 20 230 收料日期：\n' +
							'T 65 1 135 230 ' + this.BoxListInfo[i]['SLDATE'] + '\n' +

							'T 65 0 20 260 数量：\n' +
							'T 65 1 135 260  ' + this.BoxListInfo[i]['QTY'] + '\n' +

							'SETBOLD 2\n' +
							'SETBOLD 0\n' +
							'B QR 400 230 M 6 U 3\n' +
							'MA,' + this.BoxListInfo[i]['MATERIALPACKING'] + '\n' +
							'ENDQR\n' +
							/* 'B 128 2 1 50 50 560 ' + this.barCode + '\n' +
							'T 65 1 200 620 ' + this.barCode + '\n' + */
							'FORM\n' +
							'PRINT\n';
						console.log(_zpl);
						print(macaddress, _zpl);
					}
					this.BoxListInfo = []
				} else {
					this.$showMessage("入库标签数量为空！")
				}


			},

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

<style>

</style>
