<template>
	<view class="page-wraper">
		<view class="" v-if='detailSource.subList.length==0'>
			<com-blank-view @goScan='goScan(true)'></com-blank-view>
		</view>
		<view v-else class="page-wraper">
			<view class="page-main">
				<view class="">
					<view class="" style="margin-left: 20rpx; margin-top: 20rpx;margin-bottom:20rpx;font-size: 30rpx;">
						目标库位：{{detailSource.toLocationCode}}
					</view>
					<view class="flex u-col-center"
						style="width: 100%;background-color:#fff;margin-bottom: 20rpx;margin-top: 20rpx;">
						<view class="" style="margin-left: 20rpx;  font-size: 30rpx;flex-shrink: 0;">入库原因：</view>
						<view style="width: 100%">
							<uni-data-picker style="background-color:#fff;margin-right: 20rpx;" class='uni-data-picker'
								placeholder="请选择入库原因" popup-title="入库原因" :localdata="reasonList" @change="reasonChange">
							</uni-data-picker>
						</view>
					</view>
				</view>

				<u-line />
				<view v-for="(item, index) in detailSource.subList" :key="index">
					<item-qty :dataContent="item" :isShowStdPack="false">
					</item-qty>
					<u-line />
				</view>
				<button class="btn_add" @click="goScan(false)">+去添加</button>
			</view>
			<div class="btn_bottom">
				<view class="" style="display: flex;flex-direction: row;">
					<view class="">
						<button class="btn_commit" hover-class="btn_commit_after" @click="submit()">提交</button>
					</view>
				</view>
			</div>

			<comMessage ref="comMessage"></comMessage>
		</view>
	</view>
	<comReceiptRequestPopup ref="comIssueRequestPopup" @confirm='requestConfirm' :itemCodeTypeList="itemCodeTypeList"></comReceiptRequestPopup>
</template>

<script>
	import {
		unPlannedReceiptRequestCreate
	} from '@/api/request2.js';

	import {
		calc
	} from '@/common/calc.js';
	import {
		getBusinessType,
	} from '@/common/record.js';

	import {
		goHome,
		getRemoveOption,
		getISODateTime,
		navigateBack,
		getBatch,
		deepCopyData,
		getCurrDateTimes,
		getCurrDateOneMonthsTimes
	} from '@/common/basic.js';
	import {
		getUnPlannedReceiptReasonList,
	} from '@/common/directory.js';

	import comBlankView from '@/mycomponents/common/comBlankView.vue'
	import comReceiptRequestPopup from '@/pages/unPlanned/coms/comReceiptRequestPopup.vue'
	import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
	import comIssueRequestCreator from '@/pages/issue/coms/comIssueRequestCreator.vue'
	import itemQty from '@/mycomponents/item/itemQty.vue'

	export default {
		name: '',
		components: {
			comBlankView,
			comReceiptRequestPopup,
			jobDetailPopup,
			itemQty
		},
		data() {
			return {
				subList: [], //接口返回的任务subList
				detailSource: {
					subList: []
				}, //绑定在页面上的数据源
				reasonList: [], //需求信息
				reasonText: "",
				reasonCode: "",
				itemCodeTypeList:[]

			}
		},
		mounted() {
		},
		onLoad(option) {
			this.reasonList = getUnPlannedReceiptReasonList()
			var typeCode = "UnplannedReceipt"
			getBusinessType(typeCode, res => {
				if (res.success) {
					this.itemCodeTypeList = res.itemCodeTypeList;
					this.goScan(true)
				} else {
					this.showErrorMessage(res.message)
				}
			});
		
		},
		methods: {
			goScan(editPosition) {
				this.$refs.comIssueRequestPopup.openRequestPopup(editPosition);
			},

			//确定需求信息
			requestConfirm(action, item) {

				if (this.detailSource.subList.length == 0) {
					this.detailSource = {
						totalQty: 0,
						toLocationCode: item.toLocationCode,
						subList: []
					}

					var subItem = {
						toLocationCode: item.toLocationCode,
						itemCode: item.itemCode,
						itemName: item.itemName,
						qty: item.qty,
						uom: item.uom,
					}
					this.detailSource.subList.push(subItem)
				} else {
					var result = this.detailSource.subList.filter(res => {
						if (res.itemCode == item.itemCode) {
							return res
						}
					})

					//没有添加数据
					if (result.length == 0) {
						var subItem = {
							itemCode: item.itemCode,
							itemName: item.itemName,
							qty: item.qty,
							uom: item.uom
						}

						this.detailSource.subList.push(subItem)

					} else {
						//有了更新数据
						result[0].qty = calc.add(result[0].qty,item.qty)

					}
				}
				this.caclcQty();

			},

			caclcQty() {
				var totalQty = 0;
				this.detailSource.subList.forEach(res => {
					totalQty = calc.add(totalQty,res.qty)
				})
				this.detailSource.totalQty = totalQty;
			},

			submit() {
				if (this.reasonText == '') {
					this.showErrorMessage("请选择入库原因")
					return;
				}
				uni.showLoading({
					title: "提交中....",
					mask: true
				});
				var params = this.setRequestParams()
				console.log("提交参数", JSON.stringify(params));
				unPlannedReceiptRequestCreate(params).then(res => {
					uni.hideLoading()
					if (res.data) {
						this.showCommitSuccessMessage("提交成功<br>生成计划外入库申请<br>" + res.data)
					} else {
						this.showErrorMessage("提交失败[" + res.msg + "]")
					}
				}).catch(error => {
					uni.hideLoading()
					this.showErrorMessage(error)
				})
			},

			setRequestParams() {
				var subList = []
				this.detailSource.subList.forEach(detail => {
					var submitItem = deepCopyData(detail)
					submitItem.toLocationCode = this.detailSource.toLocationCode;
					submitItem.reason = this.reasonCode;
					submitItem.containerNumber = "";
					submitItem.batch = getBatch();
					submitItem.inventoryStatus = "OK";
					console.log("", getBatch())
					subList.push(submitItem)
				})
				this.dataContent.subList = subList
				this.dataContent.status = "1";
				this.dataContent.requestTime = getCurrDateTimes();
				this.dataContent.dueTime = getCurrDateOneMonthsTimes();

				return this.dataContent;
			},
			showCommitSuccessMessage(hint) {
				this.$refs.comMessage.showSuccessMessage(hint, res => {
					navigateBack(1)
				})
			},
			showErrorMessage(message) {
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {}
				});
			},
			reasonChange(e) {
				if (e.detail.value.length == 0) {
					this.reasonCode = ""
					this.reasonText = ""
				} else {
					this.reasonCode = e.detail.value[0].value
					this.reasonText = e.detail.value[0].text
				}
			}
		}
	}
</script>

<style>
</style>
