<template>
	<view>
		<uni-forms>
			<uni-collapse>
				<uni-row>
					<uni-col :span="12">物料编码:{{checkedMaterialDetailList[0].materialCode}}</uni-col>
					<uni-col :span="12">待分配:{{options.number}},已分配:{{planNumer}}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="24">物料名称:{{checkedMaterialDetailList[0].materialName}}</uni-col>

				</uni-row>
				<uni-collapse-item title="选择物料">
					<uni-table ref="table" border type="selection" emptyText="暂无更多数据"
						@selection-change="selectionChange">
						<uni-tr>
							<uni-th width="40" align="center">库位</uni-th>
							<!-- <uni-th width="40" align="center">数量</uni-th> -->
							<uni-th width="60" align="center">可用数量</uni-th>
							<uni-th width="60" align="center">库存总量</uni-th>
							<uni-th width="120" align="center">计划拣货数量</uni-th>
						</uni-tr>
						<uni-tr v-for="(item, index) in checkedMaterialDetailList" :key="index">
							<uni-td>{{ item.whCode }}</uni-td>
							<uni-td align="center">{{ item.number }}</uni-td>
							<uni-td>
								{{ item.number }}
							</uni-td>
							<uni-td align="center">
								<input type="number" v-model="item.planNum" placeholder="请输入计划拣货数量" />
							</uni-td>
						</uni-tr>
					</uni-table>
				</uni-collapse-item>
			</uni-collapse>
			<button @click="submit" type="primary" style="position: fixed;bottom: 0;width: 100vw;">确定</button>
		</uni-forms>
	</view>
</template>

<script>
	import {
		selectExactStockList,
		getConfigKey,
		addU9StockRecord,
		updateTask
	} from "@/api/wms/materRequisitiontask";
	import {
		convertBySecondUnitOrNum
	} from "@/api/basic/unit"

	export default {
		data() {
			return {
				options: {},
				checkedMaterialDetailList: [],
				wmsDrawTaskDetailList: [],
				planNumer: 0,
				countNum: 0,
				checkedMaterialDetailListArr: [],
				getListArr: {},
			}
		},
		onLoad(options) {
			this.options = JSON.parse(options.data)
			this.getListArr = JSON.parse(options.getListArr)
			this.wmsDrawTaskDetailList = JSON.parse(options.wmsDrawTaskDetailList)
			console.log(this.wmsDrawTaskDetailList, options, "this.wmsDrawTaskDetailList");
			selectExactStockList({
				materialCode: JSON.parse(options.data).materialCode,
				businessType: 'PRODUCE_DRAW'
			}).then(res => {
				if (res.total) {
					this.checkedMaterialDetailList = res.rows
				}

			})

			convertBySecondUnitOrNum({
				materialCode: this.wmsDrawTaskDetailList[JSON.parse(options.index)].materialCode,
				unitId: this.wmsDrawTaskDetailList[JSON.parse(options.index)].unitId,
				secondUnitId: this.wmsDrawTaskDetailList[JSON.parse(options.index)].secondUnitId,
				secondNumber: this.wmsDrawTaskDetailList[JSON.parse(options.index)].secondNumber,
			}).then(res => {
				console.log("convertBySecondUnitOrNum", res.data);
			})
		},
		methods: {
			selectionChange(item, indexs) {
				this.checkedMaterialDetailListArr = [];
				let count = 0
				count = item.length;
				this.countNum = count
				let planNumers = 0

				item.detail.index.map(item => {

					planNumers += Number(this.checkedMaterialDetailList[item].planNum) ?? 0;
					// console.log(this.checkedMaterialDetailList[item],"this.checkedMaterialDetailList[item]",item);
					this.checkedMaterialDetailListArr.push(this.checkedMaterialDetailList[item])
				})
				console.log(this.checkedMaterialDetailListArr, "this.checkedMaterialDetailListArr");
				this.planNumer = planNumers
			},
			/**第二数量（数量改变）
			 * 要求基本单位和数量不为空
			 * 
			 * */
			async secondNumberChange(materialCode, number, unitId, seconNumber, secondUnitId) {

				console.log(materialCode, number, unitId, seconNumber, secondUnitId);

				// 设置父组件的第一数量
				if (seconNumber == null || seconNumber === "") {
					number = 0;
				} else if (unitId == null || unitId === "") {
					number = seconNumber;
				} else if (materialCode && materialCode !== "" && unitId && secondUnitId) {
					let params = {
						materialCode: materialCode,
						number: null,
						unitId: unitId,
						secondUnitId: secondUnitId,
						secondNumber: seconNumber,
					};
					const response = await convertBySecondUnitOrNum(params);
					console.log("res", params, "params", response);
					number = response.data.number;
				} else {
					// 其它所有异常情况，都将第二数量同步传给第一数量
					number = seconNumber;
				}
				return number; // 返回修改后的 number 值
			},
			async submit() {
				let row = this.options; //明细主表信息
				if (this.planNumer > this.options.number) {
					return this.$modal.msgError("分配数量不能大于待分配数量");
				}
				if (this.countNum == 0) {
					return this.$modal.msgError("请先选择要调拨的物料详细数据")
				}
				/**删除用来选择的物料，即无批号箱号信息的记录 */
				for (let i = 0; i < this.wmsDrawTaskDetailList.length; i++) {
					if (row.id == this.wmsDrawTaskDetailList[i].id) {
						this.wmsDrawTaskDetailList.splice(i, 1);
						break;
					}
				};

				/**插入选中的物料 */
				let count = 0;
				for (let i = 0; i < this.checkedMaterialDetailListArr.length; i++) { //库存物料
					// let obj = { ...this.row };
					let obj = {};
					Object.assign(obj, row)
					obj.id = null;
					obj.originWmsMaterialStockId = row.wmsMaterialStockId == null ? this.checkedMaterialDetailListArr[
							i]
						.id :
						row.wmsMaterialStockId;
					obj.wmsMaterialStockId = this.checkedMaterialDetailListArr[i].id;
					obj.materialCode = this.checkedMaterialDetailListArr[i].materialCode;
					obj.materialName = this.checkedMaterialDetailListArr[i].materialName;
					obj.materialBatchNo = this.checkedMaterialDetailListArr[i].batchNo;
					obj.materialLotNo = this.checkedMaterialDetailListArr[i].lotNo;
					obj.originAreaCode = this.checkedMaterialDetailListArr[i].areaCode;
					obj.originShelvesCode = this.checkedMaterialDetailListArr[i].shelvesCode;
					obj.originLocationCode = this.checkedMaterialDetailListArr[i].storageLocationCode;
					obj.number = isNaN(this.checkedMaterialDetailListArr[i].planNum) ? 0 : Number(this
						.checkedMaterialDetailListArr[i].planNum);
					//将一单一数传入二单二数中，根据库存的第一单位，第二单位，第一数量获取第二数量
					if (obj.unitId != obj.secondUnitId) {
						obj.secondNumber = await this.secondNumberChange(obj.materialCode, obj.secondNumber, obj
							.secondUnitId, obj.number, obj.unitId);

					} else {
						obj.secondNumber = obj.number;
					}

					obj.productionVersion = this.checkedMaterialDetailListArr[i].productionVersion;
					// obj.schemeNumber = this.row[i].schemeNumber;
					obj.targetAreaCode = "";
					obj.targetShelves = "";
					obj.targetLocation = "";
					obj.availableNumber = this.checkedMaterialDetailListArr[i].number - this
						.checkedMaterialDetailListArr[i]
						.lockNumber;
					obj.schemeNumber = "";
					obj.actualNumber = "";
					obj.numUnitId = "";
					obj.delStatus = "";

					count += Number(obj.number) ?? 0;
					console.log("count", count, obj.number)
					//u9库存信息插入
					getConfigKey("wms.u9.whMode").then(response => {
						if (response.msg == 'true') {
							let u9Obj = {};
							u9Obj.materialCode = this.checkedMaterialDetailListArr[i].materialCode;
							u9Obj.materialName = this.checkedMaterialDetailListArr[i].materialName;
							u9Obj.batchNo = this.checkedMaterialDetailListArr[i].batchNo;
							u9Obj.lotNo = this.checkedMaterialDetailListArr[i].lotNo;
							u9Obj.whCode = this.checkedMaterialDetailListArr[i].whCode;
							u9Obj.areaCode = this.checkedMaterialDetailListArr[i].areaCode;
							u9Obj.shelvesCode = this.checkedMaterialDetailListArr[i].shelvesCode;
							u9Obj.storageLocationCode = this.checkedMaterialDetailListArr[i]
								.storageLocationCode;
							u9Obj.number = this.checkedMaterialDetailListArr[i].number;
							u9Obj.seibanNo = this.checkedMaterialDetailListArr[i].seibanNo;
							u9Obj.resvStQty = this.checkedMaterialDetailListArr[i].resvStQty;

							u9Obj.lockNumber = this.checkedMaterialDetailListArr[i].planNum;
							addU9StockRecord(u9Obj).then(res => {
								this.$modal.msgSuccess("库存选择成功");
								console.log(res, "666");
								obj.u9WmsMaterialStockId = res.obj.id;
							});
						}
					})
					this.wmsDrawTaskDetailList.push(obj);
				}
				if (count < row.number) {
					// let obj = this.row;
					let obj = {};
					Object.assign(obj, row)
					obj.number = row.number - count;
					console.log(obj.materialCode, obj.secondNumber, obj
						.secondUnitId, obj.number, obj.unitId, "obj.materialCode,");
					//将一单一数传入二单二数中，根据库存的第一单位，第二单位，第一数量获取第二数量
					if (obj.unitId != obj.secondUnitId) {
						obj.secondNumber = await this.secondNumberChange(obj.materialCode, obj.secondNumber, obj
							.secondUnitId, obj.number, obj.unitId);
						console.log("sec2", obj.secondNumber);
					} else {
						obj.secondNumber = obj.number;
					}
					// obj.secondNumber = obj.number;
					console.log(obj)
					this.wmsDrawTaskDetailList.push(obj);
				}
				console.log(this.$tab, "this.$tab", this.wmsDrawTaskDetailList);
				// uni.setStorageSync('inVentData', JSON.stringify(this.wmsDrawTaskDetailList));
				// this.$tab.navigateBack({
				// 	delta: 1 // 返回上一级页面
				// });
				this.getListArr.wmsDrawTaskDetailList = this.wmsDrawTaskDetailList
				updateTask(this.getListArr).then(res => {
					this.$modal.msgSuccess("修改成功")
				})

		

			},
		},

	}
</script>

<style>
</style>