<template>
	<view class="container">
		<cu-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">空容器上架</block>
		</cu-custom>
		<view class="example">
			<uni-forms ref="form" :model="formData" labelWidth="80px">
				<uni-forms-item label="容器编号" name="containerCode">
					<uni-easyinput v-model="formData.containerCode" placeholder="请扫描容器号" @blur="trimPalletInput" />
				</uni-forms-item>
				<uni-forms-item label="货位编号" name="locCode">
					<uni-easyinput v-model="formData.locCode" placeholder="请扫描货位编号" @blur="trimLocInput" />
				</uni-forms-item>
				<uni-forms-item label="目标产线" name="productLine">
					<view class="fromItem">
						<uni-data-select v-model="formData.productLine" :localdata="productLineList"
							placeholder="请选择目标产线" @change="productLineChange"/>
						<image style="width: 28rpx;" src="/static/images/right.png" mode="widthFix"></image>
					</view>
				</uni-forms-item>
<!-- 				<uni-forms-item label="目标工位" name="targetLoc">
					<view class="fromItem">
						<uni-data-select v-model="formData.targetLoc" :localdata="targetLocList"
							placeholder="请选择目标工位" />
						<image style="width: 28rpx;" src="/static/images/right.png" mode="widthFix"></image>
					</view>
				</uni-forms-item> -->
			</uni-forms>

			<!-- 			<view class="button-group">
				<button style="width: 100px;" @click="confirmedPutAway">确认上架</button>
			</view> -->
		</view>
		<bottomBtn :isSubmit="true" :isWhite="false" position="fixed" :disable="isButtonDisabled" :texts="['确认上架']"
			@onSubmit="confirmedPutAway"></bottomBtn>
		<view class="cu-modal" :class="msgModalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">{{msgTitleName}}</view>
					<view class="action" @tap="hideMsgModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					{{msg}}
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		getDictionary
	} from "@/api/wms/dictData.js"
	import {
		emptyCagePutAway
	} from "@/api/wms/project.js"
	import {
		formatDate
	} from 'tough-cookie';
	export default {
		data() {
			return {
				productLineList: [],
				targetLocList: [],
				//表单中的数据信息	
				formData: {
					containerCode: "",
					locCode: "",
					productLine: "",
					qty:1
				},
				rules: {
					containerCode: {
						rules: [{
							required: true,
							errorMessage: '请扫码容器编号'
						}]
					},
					locCode: {
						rules: [{
							required: true,
							errorMessage: '请扫描货位编号'
						}]
					},
					productLine: {
						rules: [{
							required: true,
							errorMessage: '请选择目标产线'
						}]
					}
				},

				isButtonDisabled: false,
				palletCode: null,
				locCode: null,
				productLine: null,

				msgModalName: null,
				msgTitleName: null,
				msg: null
			};
		},
		onLoad() {
			//加载目标产线下拉值列表
			this.getLineCodes();
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},

		methods: {

			//去除空格
			trimPalletInput(event) {
				this.formData.containerCode = event.target.value.trim();
			},

			productLineChange(e){
				// console.log("productLineChange",this.formData.productLine,e)
				var productLine=this.productLineList.filter(x=>x.value==e)[0];
				
			},	
			//去除空格
			async trimLocInput(event) {
				this.formData.locCode = event.target.value.trim();

				if (!this.formData.locCode) {
					//提示
					this.$modal.msgError("请扫描货位编号")
					return;
				}
			},
			//获取线体编号
			async getLineCodes() {
				this.productLineList = await getDictionary('productLine');
				console.log("this.productLineList", this.productLineList)
			},

			//确认上架
			confirmedPutAway: function() {
				this.isButtonDisabled = true;
				console.log('开始上架')
				this.$modal.loading("请等待...")
				this.$refs.form.validate().then(res => {

					console.log('开始执行上架')
					emptyCagePutAway(this.formData).then(response => {
						console.log("response", response)
						this.$modal.msgSuccess("上架成功")
						this.formData.locCode=''
						this.formData.containerCode=''
					}).finally(() => {
						this.isButtonDisabled = false; // 恢复按钮可用
						this.$modal.closeLoading()
					});
				}).catch(e => {
					console.error('上架异常',e)
					this.isButtonDisabled = false; // 恢复按钮可用
					this.$modal.closeLoading()
				})
			},
			hideMsgModal(e) {
				//关闭信息模态框
				this.msgModalName = null;
			},
		}
	}
</script>

<style lang="scss">
	.container {
		background: rgb(242, 243, 245);
		height: 100vh;
	}
	@media screen and (max-width: 520px) {
	::v-deep .uni-scroll-view, .uni-scroll-view-content {
			 height: calc(100vh - 350px);		
		}
	}
	.uni-forms-item {
		border-bottom: 1px solid #E5E6EB;
		margin-bottom: 0;
		padding: 24rpx 0;
	}

	::v-deep .uni-forms-item__label {
		color: #1D2129;
		font-size: 28rpx;
		font-weight: 400;
	}

	::v-deep .uni-select {
		border: 0px;
	}

	::v-deep.uni-select__input-placeholder {
		font-size: 28rpx;
		font-weight: 400;
		color: #86909C;
	}

	.textarea {
		background-color: #F2F3F5;
		width: 100%;
	}

	::v-deep.full-width-textarea {
		width: 100% !important;
		border: unset !important;
	}

	::v-deep .is-input-border {
		border: unset;
	}

	::v-deep .uni-easyinput__placeholder-class {
		font-size: 28rpx;
		color: #86909C;
	}

	::v-deep .uni-input-input {
		font-size: 28rpx;
	}

	::v-deep .uni-select__input-placeholder {
		font-size: 28rpx;
		color: #86909C;
	}

	::v-deep .uniui-bottom {
		display: none;
	}

	::v-deep .uniui-top {
		display: none;
	}

	.fromItem {
		display: flex;
		align-items: center;
	}

	.example {
		padding: 0 24rpx;
		background-color: #fff;
		width: 702rpx;
		border-radius: 24rpx;
		margin: 24rpx auto;

	}

	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	page {
		background-color: #ffffff;
	}



	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}

	.switch-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10px;
		border-bottom: 1px solid #eee;
	}
</style>