<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="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 style="margin-top: 10px;">
				<button style="margin: 5px;" @click="confirmedCall('cacheLocCall')">缓存工位呼叫</button>
				<button style="margin: 5px; background-color: royalblue;"
					@click="confirmedCall('emptyPltCall')">空容器呼叫</button>
				<button style="margin: 5px;" type="primary" @click="confirmedCall('fullCageIn')">满容器回库</button>
			</view> -->

			<view style="margin-top: 10px;">
				<button :style="{'margin': '5px','backgroundColor': item.color}" :type="item.type" :key="item.label"
					v-for="item in currentProductLine" @click="confirmedCall(item)">{{item.label}}</button>
			</view>
			<view>
				<checkbox-group class="block" @change="CheckboxChange">
					<checkbox :class="isfullDirectIn ? 'checked':''" value="in" :checked="isfullDirectIn ? true: false">
					</checkbox>
					<label style="margin: 10px;">满料直接回库</label>
					<!-- <checkbox :class="isCallEmpty ? 'checked':''" value="call"
						:checked="isCallEmpty ? true: false"></checkbox>
					<label style="margin: 10px;">回库并呼叫空容器</label> -->
				</checkbox-group>

			</view>
		</view>

		<!-- 		<view>
			<bottomBtn :isThird="true" :isSubmit="true" :isWhite="false" position="fixed" :disable="isButtonDisabled"
				:texts="['满容器返库','缓存工位呼叫','空容器呼叫']" @onCancel="confirmedCall" @onSubmit="confirmedCall" @onCheck="confirmedCall"></bottomBtn>
		</view> -->
		<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 {
		containerMove
	} from "@/api/wms/project.js"
	import {
		list
	} from "@/api/wms/loc.js"
	import {
		formatDate
	} from 'tough-cookie';
	import {
		bottomBtn
	} from '@/components/bottomBtn/bottomBtn.vue'
	export default {
		data() {
			return {
				productLineList: [],
				targetLocList: [],
				currentProductLine: [],

				//表单中的数据信息	
				formData: {
					productLine: "",
					locCode: "",
					operationCode: "",
					isFullDirectIn:false
				},
				rules: {
					productLine: {
						rules: [{
							required: true,
							errorMessage: '请选择目标产线'
						}]
					},
					// targetLoc: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择目标工位'
					// 	}]
					// }
				},

				isButtonDisabled: false,
				targetLoc: null,
				productLine: null,
				isfullDirectIn: false, //满料直接入库
				isCallEmpty: true,
				msgModalName: null,
				msgTitleName: null,
				msg: null
			};
		},
		onLoad() {
			//加载目标产线下拉值列表
			this.getLineCodes();
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		// watch: {
		// 	"formData.productLine": {
		// 		immediate: false,
		// 		handler: async function(newVal) {
		// 			var result = await list({
		// 				parentCode: newVal
		// 			});
		// 			console.log('result', result);
		// 			this.targetLocList = result.data.map(a => ({
		// 				label: a.locName,
		// 				value: a.locCode
		// 			}));
		// 			console.log('this.targetLocList', this.targetLocList);
		// 		}
		// 	}
		// },
		methods: {
			//获取线体编号
			async getLineCodes() {
				this.productLineList = await getDictionary('productLine');
				console.log("this.productLineList", this.productLineList)
			},
			CheckboxChange(e) {
				// console.log('CheckboxChange',e)
				var items = e.detail.value
				console.log('this.isButtonDisabled', this.isButtonDisabled, e)
				if (items.includes('in')) {
					uni.showModal({
						title: '系统提示',
						content: `确定启用满料直接回库吗？启用后无多余AGV可用，会导致上料位空料架不能及时供应！`,
						confirmText: '确定',
						confirmColor: '#DD524D',
						cancelText: '取消',
						success: res => {
							if (res.confirm) {
								this.isfullDirectIn = true
							} else if (res.cancel) {
								this.isfullDirectIn = false
							}
						},

					})

				} else {
					this.isfullDirectIn = false
				}

				this.isCallEmpty = items.includes('call')
				console.log('this.isButtonDisabled', this.isButtonDisabled)
			},
			productLineChange: function(productLine) {
				//this.currentProductLine = productLineOperates
				const currentLinelist = this.productLineList.filter(x => x.value == productLine)
				if (currentLinelist && currentLinelist.length > 0) {
					const currentLine = currentLinelist[0]
					console.log("currentLine.remark", currentLine.remark)
					this.currentProductLine = JSON.parse(currentLine.remark)
					console.log("this.currentProductLine ", this.currentProductLine)
				}
				// console.log("productLineChange",productLine,currentLine)
			},

			//确认上架
			confirmedCall: function(operation) {
				this.isButtonDisabled = true;
				this.$modal.loading("请等待...")
				this.$refs.form.validate().then(res => {

					this.formData.operationCode = operation.operationCode
					this.formData.locCode = operation.locCode
					this.formData.isFullDirectIn=this.isfullDirectIn
					containerMove(this.formData).then(response => {
						console.log("response", response)
						this.$modal.msgSuccess(operation == 'fullCageIn' ? "回库成功" : "呼叫成功")
					}).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;
	}

	.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: 24rpx 24rpx;
		background-color: #fff;
		width: 720rpx;
		border-radius: 24rpx;
		margin: 10rpx 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;
		margin-top: 5px;
	}

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