<template>
	<view class="stock-check-add-page">
		<NavBar :title="$t('新增库存')"></NavBar>
		<view class="container">
			<view class="title">
				{{info.ml001}}|{{info.materialName}}
			</view>
			<view class="detail">
				<view class="card">
					<uni-forms :modelValue="formData" :label-width="90" label-align="right">
						<uni-forms-item :label="`${$t('新增数')}：`">
							<uni-easyinput v-model="formData.physicalQuantity" type="number" trim="all"
								:placeholder="$t('请输入新增数')" />
						</uni-forms-item>
						<uni-forms-item :label="`${$t('选择仓库')}：`">
							<zxz-uni-data-select filterable v-model="formData.warehouse" dataKey="text" dataValue="value"
								:localdata="warehouseRange" @change="handleChange" />
						</uni-forms-item>
						<uni-forms-item :label="`${$t('选择储位')}：`">
							<zxz-uni-data-select filterable v-model="formData.location" dataKey="text" dataValue="value"
								:localdata="storageRange" />
						</uni-forms-item>
						<uni-forms-item :label="`${$t('批次号')}：`">
							<uni-easyinput v-model="formData.batchNo" trim="all"
								:placeholder="$t('请输入批次号')" />
						</uni-forms-item>
					</uni-forms>
				</view>
			</view>
		</view>
		<view class="footer">
			<button class="sumbit-btn" @click="handleSumbit">{{$t('确认')}}</button>
		</view>
	</view>
	</view>
</template>

<script>
	import i18n from '@/lang/index';
	export default {
		data() {
			return {
				list: [],
				info: [],
				warehouseRange: [],
				storageRange: [],
				formData: {
					isAppend: 1,
					physicalQuantity: "",
					stockQuantity: "",
					warehouse: "",
					location: "",
					batchNo: "",
				}
			}
		},
		onLoad(option) {
			this.getWarehouseList();
			if (option.list) {
				this.list = JSON.parse(decodeURIComponent(option.list));
				this.info = this.list[0];
			};
		},
		methods: {
			getWarehouseList() {
				this.$u.api.getWarehouseListApi({})
					.then(res => {
						this.warehouseRange = res.data.map((item) => {
							return {
								...item,
								value: item.mc001,
								text: item.mc002,
							}
						})
					})
			},
			getStorageList() {
				const params = {
					ni001: this.formData.warehouse
				}
				this.$u.api.getStorageListApi(params)
					.then(res => {
						this.storageRange = res.data.map((item) => {
							return {
								...item,
								value: item.ni002,
								text: item.ni002,
							}
						})
					})
			},
			handleChange() {
				this.getStorageList();
			},
			handleSumbit() {
				if (!this.formData.physicalQuantity) {
					return this.$u.toast(i18n.t('调拨数不能为空'));
				}
				if (!this.formData.warehouse) {
					return this.$u.toast(i18n.t('调入库存不能为空'));
				}
				if (!this.formData.location) {
					return this.$u.toast(i18n.t('调入储位不能为空'));
				}
				if (!this.formData.batchNo) {
					return this.$u.toast(i18n.t('批次号不能为空'));
				}
				if (this.list.some((item) => {
						return item.warehouse == this.formData.warehouse && item.location == this.formData.location;
					})) {
					return this.$u.toast(i18n.t('该数据已存在'));
				}
				let pages = getCurrentPages();
				// 2. 上一页面实例
				// 注意是length长度，所以要想得到上一页面的实例需要 -2
				// 若要返回上上页面的实例就 -3，以此类推
				let prevPage = pages[pages.length - 2];
				// 3. 给上一页面实例绑定getValue()方法和参数（注意是$vm）
				prevPage.$vm.getAddValue(Object.assign(this.info, this.formData));
				// 4. 返回上一页面
				uni.navigateBack({
					delta: 1 // 返回的页面数
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.stock-check-add-page {
		.container {
			.title {
				font-size: 32rpx;
				font-weight: bolder;
				padding-top: 24rpx;
				padding-left: 24rpx;
			}

			.detail {
				width: calc(100% - 40rpx);
				margin: 20rpx;
				padding: 20rpx;
				background-color: #f1f1f3;
				border-radius: 12rpx;

				.card {
					padding: 12rpx;
					margin-bottom: 24rpx;
					box-sizing: border-box;
					background-color: #ffffff;
					border-radius: 12rpx;

					::v-deep.uni-forms {
						.uni-forms-item {
							margin: 24rpx;

							.uni-forms-item__label {
								font-size: 28rpx;
							}
						}
					}
				}
			}
		}

		.footer {
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 150rpx;
			padding: 24rpx;
			background-color: #ffffff;

			.sumbit-btn {
				width: 300rpx;
			}
		}
	}
</style>