<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.stockQuantity" type="number" trim="all"
								:placeholder="$t('请输入新增数')" />
						</uni-forms-item>
						<uni-forms-item :label="`${$t('选择仓库')}：`">
							<uni-data-select v-model="formData.warehouse" :localdata="warehouseRange"
								@change="handleChange"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item :label="`${$t('选择储位')}：`">
							<uni-data-select v-model="formData.location" :localdata="storageRange"></uni-data-select>
						</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 {
				info: [],
				warehouseRange: [],
				storageRange: [],
				formData: {
					isAppend: 1,
					physicalQuantity: "",
					stockQuantity: "",
					warehouse: "",
					location: "",
				}
			}
		},
		onLoad(option) {
			this.getWarehouseList();
			if (option.info) {
				this.info = JSON.parse(decodeURIComponent(option.info));
				console.log(this.info);
			};
		},
		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.ni003,
							}
						})
					})
			},
			handleChange() {
				this.getStorageList();
			},
			handleSumbit() {
				if (!this.formData.stockQuantity) {
					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('调入储位不能为空'));
				}
				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, {
					physicalQuantity: this.formData.stockQuantity
				}));
				// 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;

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

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

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

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