<template>
	<view>
		<uni-collapse>
			<view class="cu-card article ">
				<view class="cu-item shadow borderBottom">
					<view class="content">
						<view class="desc">
							<view class="text-content" style="font-size: 15px;">
								<view><strong>物料编码</strong> : {{singleInfo.materialCode}}</view>
								<view><strong>物料名称</strong> : {{singleInfo.materialName}}</view>
								<view><strong>物料批号</strong> : {{singleInfo.materialBatchNo}}</view>
								<view><strong>上架数量</strong> : {{singleInfo.secondNumber}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<uni-row>
				<uni-col :span="12">
					<span style="display: block;text-align: center;">每份数量<view style="margin-left: 15%;"><u-number-box
								v-model="eachNumber" integer min="0" @change="eachNumberClear" /></view></span>
				</uni-col>
				<uni-col :span="12">
					<span style="display: block;text-align: center;">拆分数量<view style="margin-left: 15%;"><u-number-box
								v-model="splitNumber" integer min="0" @change="splitNumberClear" /></view></span>
				</uni-col>
			</uni-row>
			<uni-row style="margin-top: 5px;" :gutter="10">
				<uni-col :span="12">
					<u-button type="primary" icon="cut" :disabled="isSecondOpen" @click="multiSplit" :plain="true"
						text="拆分"></u-button>
				</uni-col>
				<uni-col :span="12">
					<u-button type="success" icon="close-circle" @click="singleSplit" :disabled="isSecondOpen"
						:plain="true" text="不拆分"></u-button>
				</uni-col>
			</uni-row>
			<uni-collapse-item :open="true">
				<uni-forms ref="form" :modelValue="formData">
					<view :key="index" v-for="(item, index) in wmsLotNoList">
						<view>
							<uni-badge :text="index+1" class="uni-badge-left-margin" type="primary"></uni-badge>
						</view>
						<uni-forms-item label="物料批号" :labelWidth='90' :name="item.materialBatchNo">
							<uni-easyinput type="text" v-model="item.materialBatchNo" />
						</uni-forms-item>
						<uni-forms-item label="物料编码" :labelWidth='90' :name="item.materialCode">
							<uni-easyinput type="text" v-model="item.materialCode" />
						</uni-forms-item>
						<uni-forms-item label="拼接库位" :labelWidth='90' :name="item.whCode">
							<uni-easyinput type="text" v-model="item.whCode" />
						</uni-forms-item>
						<uni-forms-item label="出库明细单编码" :labelWidth='90' :name="item.outBillDetailCode">
							<uni-easyinput type="text" v-model="item.outBillDetailCode" />
						</uni-forms-item>
						<uni-forms-item label="数量" :labelWidth='90' :name="item.number">
							<uni-easyinput type="text" v-model="item.number" />
						</uni-forms-item>

					</view>
				</uni-forms>
			</uni-collapse-item>
		</uni-collapse>
		<u-button type="primary" @click="submit" :disabled="disBol">提交</u-button>
	</view>
</template>

<script>
	import {
		addInDetail,
		addLotInfo,
		uploadU9
	} from "@/api/wms/purchase.js";
	// import SecondNumberChangeToConvert from "@/components/SecondNumberChangeToConvert/index.vue";
	import {
		addSplitOutInfo
	} from "@/api/wms/invQuick"
	import {
		listOutSplitHistory
	} from "@/api/wms/stock.js"
	import {
		listOut,
	} from "@/api/wms/invQuick.js"
	import {
		updateInDetail
	} from "@/api/wms/pdcIn.js";
	import {
		getTask
	} from '@/api/wms/pdcMaterial.js'
	export default {
		onLoad: function(option) {
			console.log((JSON.parse(option.item)), 'iophd');
			if (JSON.parse(option.item)) {
				this.singleInfo = JSON.parse(option.item)
			}
			if (JSON.parse(option.drawTaskCode)) {
				this.drawOutCode = JSON.parse(option.drawTaskCode)
			}


			// 获取当前时间
			var currentDate = new Date();
			// 格式化为字符串（YYYY-MM-DD HH:mm:ss）
			var year = currentDate.getFullYear();
			var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
			var day = ("0" + currentDate.getDate()).slice(-2);
			var hours = ("0" + currentDate.getHours()).slice(-2);
			var minutes = ("0" + currentDate.getMinutes()).slice(-2);
			var seconds = ("0" + currentDate.getSeconds()).slice(-2);
			var formattedDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			// 将当前时间赋值给 formData.endTime
			this.rkTime = formattedDate;
			listOutSplitHistory({
				outBillDetailCode: this.singleInfo.drawOutDetailCode
			}).then(res => { //查询有没有拆分记录
				if (res.total) {
					this.wmsLotNoList = res.rows
					this.disBol = true
				} else {
					this.disBol = false
				}
			})

		},
		data() {
			return {
				disBol: false,
				//入库时间
				rkTime: null,
				//是否已分批
				isSecondOpen: false,
				selectedRow: {},
				singleInfo: {},
				wmsLotNoList: [],
				eachNumber: null,
				splitNumber: null,
				drawOutCode: '',
				index: null,
				checkStorageLocationBarcode: true,
				// storageLocationBarcode: ''
				formData: {},
				rules: {
					storageLocationBarcode: [{

						required: true,
						errorMessage: '请输入库位条码!'

					}]
				},
			}
		},
		components: {
			// SecondNumberChangeToConvert
		},
		methods: {
			eachNumberClear() {
				this.eachSecondNumber = null;
				this.splitNumber = null;
			},
			eachSecondNumberClear() {
				this.eachNumber = null;
				this.splitNumber = null;
			},
			//不拆分批次
			singleSplit() {
				this.wmsLotNoList = [];
				this.splitNumber = 1;
				this.eachNumber = 0;
				let obj = {};
				obj.materialCode = this.singleInfo.materialCode;
				obj.materialBatchNo = this.singleInfo.materialBatchNo;
				obj.number = this.singleInfo.number;
				obj.unit = this.singleInfo.unit;
				obj.index = 1;
				obj.outBillCode = this.singleInfo.drawOutCode;
				obj.outBillDetailCode = this.singleInfo.drawOutDetailCode;
				obj.unitName = this.singleInfo.unitName;
				obj.materialName = this.singleInfo.materialName;
				obj.specification = this.singleInfo.specification;
				obj.u9StorageCode = this.singleInfo.u9StorageCode;
				obj.connectedLocation = this.singleInfo.connectedLocation;
				obj.whCode = this.singleInfo.whCode;
				obj.u9IssueApplyDocLineId = this.singleInfo.u9IssueApplyDocLineId;
				this.wmsLotNoList.push(obj);


			},
			//选择条件拆分批次
			multiSplit() {
				this.wmsLotNoList = [];
				let materialCode = this.singleInfo.materialCode;
				let materialBatchNo = this.singleInfo.materialBatchNo;
				let unit = this.singleInfo.unit;
				let outBillCode = this.singleInfo.drawOutCode;
				let outBillDetailCode = this.singleInfo.drawOutDetailCode;
				let materialName = this.singleInfo.materialName;
				let specification = this.singleInfo.specification;
				let unitName = this.singleInfo.unitName;
				let u9StorageCode = this.singleInfo.u9StorageCode;
				let connectedLocation = this.singleInfo.connectedLocation;
				let whCode = this.singleInfo.whCode;
				let u9IssueApplyDocLineId = this.singleInfo.u9IssueApplyDocLineId;
				if (this.eachNumber != null) {
					let t;
					let k;
					t = parseInt(this.singleInfo.number / this.eachNumber);
					k = this.singleInfo.number % this.eachNumber;
					if (k != 0) {
						t++;
					}
					for (let i = 0; i < t; i++) {
						let obj = {};
						obj.materialCode = materialCode;
						obj.materialBatchNo = materialBatchNo;
						obj.number = this.eachNumber;
						obj.unit = unit;
						obj.outBillCode = outBillCode;
						obj.outBillDetailCode = outBillDetailCode;
						obj.materialName = materialName;
						obj.specification = specification;
						obj.unitName = unitName;
						obj.index = i
						obj.connectedLocation = connectedLocation;
						obj.u9StorageCode = u9StorageCode;
						obj.whCode = whCode;
						obj.u9IssueApplyDocLineId = u9IssueApplyDocLineId;
						if (i == t - 1) {
							if (k != 0) {
								obj.number = Number(k);
							}
						}
						// obj.materialLotNo = i + 1;
						console.log(obj);
						this.wmsLotNoList.push(obj);
						console.log(obj.number, k, "num");
					}
					console.log(this.wmsLotNoList, "this.wmsLotNoListthis.wmsLotNoList");

				} else if (this.splitNumber != null) {
					let k;
					let j;
					k = parseInt(this.singleInfo.number / this.splitNumber);
					j = this.singleInfo.number - (this.splitNumber - 1) * k;
					for (let i = 0; i < this.splitNumber; i++) {
						let obj = {};
						obj.materialCode = materialCode;
						obj.materialBatchNo = materialBatchNo;
						obj.number = k;
						obj.unit = unit;
						obj.index = i;
						obj.outBillCode = outBillCode;
						obj.outBillDetailCode = outBillDetailCode;
						obj.materialName = materialName;
						obj.unitName = unitName;
						obj.specification = specification;
						obj.connectedLocation = connectedLocation;
						obj.u9StorageCode = u9StorageCode;
						obj.whCode = whCode;
						obj.storageLocationCode = storageLocationCode;
						obj.u9IssueApplyDocLineId = u9IssueApplyDocLineId;
						// obj.materialLotNo = i + 1;
						if (i == this.splitNumber - 1) {
							obj.number = Number(j);
						}
						console.log(obj.number, Number(j), "num");
						this.wmsLotNoList.push(obj);
					}
					console.log(this.wmsLotNoList, "this.wmsLotNoListthis.wmsLotNoList");
				} else if (this.eachSecondNumber != null) {
					let t;
					let k;
					t = parseInt(this.singleInfo.secondNumber / this.eachSecondNumber);
					k = this.singleInfo.secondNumber % this.eachSecondNumber;
					if (k != 0) {
						t++;
					}
					for (let i = 0; i < t; i++) {
						let obj = {};
						obj.index = i;
						obj.materialCode = materialCode;
						obj.materialBatchNo = materialBatchNo;
						obj.secondNumber = this.eachSecondNumber;
						obj.secondUnit = secondUnit;
						obj.outBillCode = outBillCode;
						obj.outBillCode = outBillDetailCode;
						obj.materialName = materialName;
						obj.unitName = unitName;
						obj.specification = specification;
						obj.connectedLocation = connectedLocation;
						obj.u9StorageCode = u9StorageCode;
						obj.whCode = whCode;
						obj.storageLocationCode = storageLocationCode;
						obj.u9IssueApplyDocLineId = u9IssueApplyDocLineId;
						if (i == t - 1) {
							if (k != 0) {
								obj.secondNumber = k;
							}
						}
						// obj.materialLotNo = i + 1;
						console.log(obj);
						this.wmsLotNoList.push(obj);
						console.log(this.wmsLotNoList, " this.wmsLotNoList");
					}
				}
			},

			async submit() {
				addSplitOutInfo(this.wmsLotNoList).then(res => {
					console.log(res, "拆分记录");
					this.$modal.msgSuccess(res.msg);
				})

				// })
			}

		},


	}
</script>

<style>
	.cu-card.article>.cu-item .content .text-content {
		height: 100% !important;
	}

	.cu-card.article>.cu-item {
		padding-bottom: 0;
	}

	.cu-card>.cu-item {
		margin: 0;
	}

	.uni-swipe {
		overflow: inherit;
	}
</style>