<template>
	<view>
		<u-navbar back-text="返回" title="PO入库" :background="background"></u-navbar>
		<view v-show="loadding"
			style="width: 100%; height: 900rpx; display: flex; justify-content: center; align-items: center;">
			<u-loading mode="circle" :size="70" color="#00aaff"></u-loading>
		</view>
		<view class='content' v-show="!loadding">
			<view style="display: flex; justify-content: space-between; margin-top: 40rpx;">
				<!-- <view style="width: 15%;margin-right:10rpx; height: 10rpx;">扫描PO</view> -->
				<u-input v-model="receiveRequestName" :border="true" style="background: #FFFFFF; margin-right: 10rpx;"
					@confirm="queryPOInfo" :trim="true" placeholder="扫描PO"></u-input>
				<u-button style="width: 15%;margin-left:10rpx;" @click="clearPOInfo">清空</u-button>
			</view>
			<view style="margin-top: 20rpx; height: 120rpx;">
				<u-table class="main-table">
					<u-tr class="u-tr">
						<u-th class="u-th">PO号</u-th>
						<u-th class="u-th">供应商</u-th>
						<u-th class="u-th">PO类型</u-th>
						<u-th class="u-th">申请人</u-th>

					</u-tr>
					<u-tr class="u-tr" v-for="(item,index) in poDisplaySource" :key="index">
						<u-td class="u-td">{{ item.RECEIVEREQUESTNAME }}</u-td>
						<u-td class="u-td">{{ item.SUPPLIERNAME }}</u-td>
						<u-td class="u-td">{{ item.POGROUP }}</u-td>
						<u-td class="u-td">{{ item.APPLICANT }}</u-td>
					</u-tr>
				</u-table>
			</view>
			<view style="margin-top: 20rpx; height: 5rpx;  background: #E0E3DA; width: 100%;"> </view>
			<!-- <u-divider>大漠孤烟直</u-divider> -->
			<view class="card-body" style="display: flex; justify-content: space-between; margin-top: 20rpx;">
				<view style="margin-right:10rpx; ">扫描<br />标签</view>
				<u-input v-model="qrCode" :border="true" style="background: #FFFFFF; margin-right: 10rpx;"
					@confirm="createBoxM(qrCode)" :trim="true" :disabled="qrCOdeDisable" placeholder="扫描标签二维码">
				</u-input>
			</view>

			<view style="margin-top: 20rpx;">
				<u-card style="border: 1rpx solid #0081FF;" :show-head="false" :full="true"
					:body-style="{height: '200rpx'}">
					<view class="card-body" slot="body">
						<u-row gutter="12" justify="between">
							<u-col :span="4">
								<view class="info-label">BoxID</view>
								<view></view>

							</u-col>
							<u-col :span="4">
								<view class="info-label">物料号</view>
								<view>{{createBox.materialSpecName}}</view>
							</u-col>
							<u-col :span="4">
								<view class="info-label">库位</view>
								<view></view>
							</u-col>
						</u-row>
						<u-row justify="between" style="margin-top: 10rpx;">
							<u-col :span="4">
								<view class="info-label">本次<br />数量</view>
								<view>{{createBox.materialQty}}</view>
							</u-col>
							<u-col :span="4">
								<view class="info-label">已收<br />总数</view>
								<view></view>
							</u-col>
							<u-col :span="4">
								<view class="info-label">剩余<br />数量</view>
								<view></view>
							</u-col>
						</u-row>
					</view>
				</u-card>

				<view class="card-body" style="display: flex; justify-content: space-between; margin-top: 20rpx;">
					<view style="margin-right:10rpx; ">抬头<br />文本</view>
					<u-input v-model="ACTNO" :border="true" style="background: #FFFFFF; margin-right: 10rpx;"
						:trim="true" :disabled="true"></u-input>
					<u-button style="width: 15%;margin-left:10rpx;" @click="getStockInInfo">刷新</u-button>
				</view>

				<view style="margin-top: 20rpx;">
					<u-table class="main-table">
						<u-tr class="u-tr">
							<u-th class="u-th">物料号</u-th>
							<u-th class="u-th">ERP库位</u-th>
							<u-th class="u-th" width="140rpx">接收数量</u-th>
							<u-th class="u-th">行项目</u-th>
							<u-th class="u-th">可关单数量</u-th>
						</u-tr>
						<u-tr class="u-tr" v-for="(item,index) in POStockInfo" :key="index">
							<u-td class="u-td">{{ item.MATERIALPSECNAME }}</u-td>
							<u-td class="u-td">{{ item.ERPLOCATION }}</u-td>
							<u-td class="u-td" width="140rpx">{{ item.MATERIALQUANTITY }}</u-td>
							<u-td class="u-td">{{ item.RECEIVEREQUESTDETAILNAME }}</u-td>
							<u-td class="u-td">{{ item.RELEASEDQTY }}</u-td>
						</u-tr>
					</u-table>
				</view>
				<u-button style="width: 100%; margin-top: 50rpx; background-color:#0081FF; color: white;"
					@click="submit">过账提交</u-button>
			</view>

		</view>
		<u-toast ref="uTakeOverToast" />

	</view>
</template>

<script>
	import {
		QueryInfo,
		CreateBoxID_PO
	} from '../../../common/api.js'
	export default {
		data() {
			return {
				loadding: false,

				background: {
					backgroundImage: 'linear-gradient(60deg, rgb(124, 187, 180), rgb(141, 198, 63))'
				},

				qrCOdeDisable: false,
				// data
				createBox: {},
				receiveRequestName: '',
				qrCode: '',
				ACTNO: ' ',
				poDisplaySource: [],
				POStockInfo: [],
				poQueryaram: {
					queryID: "GetPOInvoiceInfo",
					version: "00001",
					BINDV: {
						SITENAME: "BADT",
						RECEIVEREQUESTNAME: "",
					}
				},
			}
		},
		methods: {
			// 查询PO明细
			queryPOInfo() {
				let _param = this.poQueryaram;
				_param.queryID = "GetPOInvoiceInfo";
				_param.version = "00001";
				_param.BINDV.SITENAME = uni.getStorageSync("SITENAME");
				_param.BINDV.RECEIVEREQUESTNAME = this.receiveRequestName.trim();
				if (this.receiveRequestName == null || "" == (this.receiveRequestName.trim())) {
					this.showToast('PO不能为空，请确认', 'error');
					return;
				}
				QueryInfo({
					param: JSON.stringify(_param)
				}).then(res => {

					console.log(uni.getStorageSync("SITENAME"));
					this.poDisplaySource = res.DATA;
					this.qrCOdeDisable = false;
				}).catch(err => {
					this.showToast(err, 'error')
				})
			},
			showToast(text, type) {
				this.$refs.uTakeOverToast.show({
					title: text,
					type: type,
					position: "bottom"
				})
			},
			clearPOInfo() {
				this.poDisplaySource = null;
				this.receiveRequestName = "";
			},
			//查看待入库信息
			getStockInInfo() {

			},
			//点击收货行明细跳转收货
			createBoxM(item) {
				let boxIdStr = item.trim();
				if (boxIdStr.length > 0) {
					let count = boxIdStr.split("|").length;
					//标准标签格式中 | 出现次数为5
					//SN|物料号|数量|供应商|制造日期|过期日
					if (count != 6) {
						this.showToast('标签不符合规范请确认', 'error');
						return;
					}
					let qrCodeArr = boxIdStr.split("|");
					let boxId = qrCodeArr[0];
					let materialSpecName = qrCodeArr[1];
					let materialQty = qrCodeArr[2];
					let supplierNo = qrCodeArr[3];
					let makeDate = new Date(qrCodeArr[4].substr(0, 4), parseInt(qrCodeArr[4].substr(4, 2)) - 1,
						qrCodeArr[4].substr(6, 2));
					let expiringDate = new Date(qrCodeArr[5].substr(0, 4), parseInt(qrCodeArr[5].substr(4, 2)) - 1,
						qrCodeArr[5].substr(6, 2));

					let dtNow = new Date();
					dtNow.setHours(0);
					dtNow.setMinutes(0);
					dtNow.setSeconds(0);
					this.createBox.materialSpecName = materialSpecName;
					this.createBox.materialQty = materialQty;
					expiringDate.setDate(expiringDate.getDate() + 1);
					if (makeDate > dtNow) {
						this.showToast('生产日期大于今天，不能入库！', 'error');
						return;
					}
					if (expiringDate < dtNow) {
						this.showToast('BOX已过期，不能入库！', 'error');
						return;
					}


					// 1.判断box是否已经创建
					let _param = this.poQueryaram;
					_param.queryID = "GetBoxInfo";
					_param.version = "WP005";
					_param.BINDV.SITENAME = uni.getStorageSync("SITENAME");
					_param.BINDV.NPFLAG = "N";
					_param.BINDV.MATERIALPACKINGNAME = boxId;
					
					QueryInfo({
						param: JSON.stringify(_param)
					}).then(res => {
						console.log("查询box状态" + res.DATA[0]['PACKINGSTATE'])
						if (res) {
							this.showToast('失败，BOXId' + boxId + res.DATA[0]['PACKINGSTATE'], 'error');
							return ;
						} else {
							let createBoxParam = {};
							createBoxParam.Labelstr = boxIdStr;
							createBoxParam.SITENAME = uni.getStorageSync("SITENAME");
							createBoxParam.MATERIALSPECNAME = qrCodeArr[1];
							let a = qrCodeArr[4].substr(0, 4);
							let b = qrCodeArr[4].substr(4, 2);
							let c = qrCodeArr[4].substr(6, 2);
							let e = qrCodeArr[3];
							createBoxParam.MAKEDATE = a + "-" + b + "-" + c;
							a = qrCodeArr[5].substr(0, 4);
							b = qrCodeArr[5].substr(4, 2);
							c = qrCodeArr[5].substr(6, 2);
							createBoxParam.EXPIRINGDATE = a + "-" + b + "-" + c;
							createBoxParam.RECEIVEQTY = qrCodeArr[2];
							createBoxParam.SUPPLIERNAME = qrCodeArr[3];
							createBoxParam.RECEIVEREQUESTNAME = this.receiveRequestName;
							createBoxParam.REASONCODETYPE = "101";
							createBoxParam.SUPERMATERIALPACKINGNAME = "";
							createBoxParam.GROUPNO = "";
							createBoxParam.NPFLAG = "N";
							createBoxParam.MATERIALPACKINGNAME = qrCodeArr[0];
							debugger
							CreateBoxID_PO({
								param: JSON.stringify(createBoxParam)
							}).then(res => {
							
							
							}).catch(err => {
								//this.showToast(err, 'error')
								this.qrCOdeDisable = false;
							})
						}
					}).catch(err => {
						this.showToast(err, 'error')
					});


				}
				this.qrCOdeDisable = false;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		margin: 5rpx 18rpx;
	}



	.main-table {
		::v-deep .u-th {
			background: #0081FF !important;
			color: #ffffff;
		}



		::v-deep .u-td {
			overflow: hidden;
			word-break: keep-all;
			text-overflow: ellipsis
		}

	}

	.u-tr {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis
	}

	view {
		color: #068bff;
		text-align: center;
	}
</style>
