<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="callLocCode">
					<view class="fromItem">
						<uni-data-select v-model="formData.callLocCode" :localdata="callLocCodeList" placeholder="请选择上料工位" />
						<image style="width: 28rpx;" src="/static/images/right.png" mode="widthFix"></image>
					</view>
				</uni-forms-item>
				<view style="margin-top: 10px;">
					<checkbox-group class="block" @change="CheckboxChange">
						<checkbox :class="formData.isAutoCall ? 'checked':''" value="Y"
							:checked="formData.isAutoCall ? true: false">
						</checkbox>
						<label style="margin: 10px;">自动呼叫空料架</label>
					</checkbox-group>

				</view>
			</uni-forms>
		</view>
		<bottomBtn :isSubmit="true" :isWhite="false" position="fixed" :disable="isButtonDisabled" :texts="['确认']"
			@onSubmit="confirmed"></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 {
		changeModel
	} from "@/api/wms/project.js"
	import {
		page,
		edit
	} from "@/api/wms/loc.js"
	import {
		formatDate
	} from 'tough-cookie';
	import {
		bottomBtn
	} from '@/components/bottomBtn/bottomBtn.vue'
	export default {
		data() {
			return {
				productLineList: [],
				callLocCodeList: [],
				// callLocCode: "",
				productLineLocList: [],
				cacheLoc: null,

				//表单中的数据信息	
				formData: {
					productLine: "",
					callLocCode: "",
					isAutoCall: false
				},
				rules: {
					productLine: {
						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) {

				var items = e.detail.value
				this.formData.isAutoCall = items.includes('Y')

				console.log('formData', this.formData)
			},
			productLineChange: function(productLine) {
				if(!productLine){
					this.formData.callLocCode=''
					this.callLocCodeList=[]
					this.formData.isAutoCall=false
					return
				}
				page({
					parentCode: productLine
				}).then(response => {
					this.productLineLocList = response.data.record
					if (!this.productLineLocList || this.productLineLocList.length == 0) {
						this.$modal.msgError('未查询到产线工位数据')
					}
					this.cacheLoc = this.productLineLocList.filter(f => f.ext5 == 'emptyCacheLoc')[0]
					if (!this.cacheLoc) {
						this.$modal.msgError('未找到产线空料架缓存工位！')
					}
					// this.formData.callLocCode=this.cacheLoc
					this.callLocCodeList =
						this.productLineLocList
						.filter(f => f.ext5 == 'PalletizationLoc')
						.map(item => ({
							value: item.locCode,
							text: item.locName
						}))
					if (!this.callLocCodeList || this.callLocCodeList.length == 0) {
						this.$modal.msgError('未找到产线上料工位！')
					}

					this.formData.callLocCode = this.cacheLoc.ext3
					this.formData.isAutoCall = this.cacheLoc.ext4 == 'Y'

					// console.log("this.callLocCode", this.callLocCode, this.callLocCodeList)
				});

			},

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

					// this.formData = this.callLocCode
					// this.formData.productLine=this.productLine
					changeModel(this.formData).then(response => {
						console.log("response", response)
						this.$modal.msgSuccess("修改成功," + response.msg)
					}).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>