<template>
	<view class="container">
		<view class="formBox">
			<uv-form labelPosition="left" :model="taskInfo" :rules="rules" ref="form" labelWidth="120">
				<!-- <uv-form-item label="任务详情"  prop="taskDtlName" borderBottom @click="showtaskDtlSelect">
					<uv-input v-model="taskInfo.taskDtlName" border="none">
					</uv-input>
					<template v-slot:right>
						<uv-icon name="arrow-right"></uv-icon>
					</template>
				</uv-form-item> -->
				<uv-form-item label="任务类型"  prop="taskTypeName" borderBottom @click="showTypeSelect">
					<uv-input v-model="taskInfo.taskTypeName" border="none">
					</uv-input>
					<template v-slot:right>
						<uv-icon name="arrow-right"></uv-icon>
					</template>
				</uv-form-item>
				<uv-form-item label="物料编码"  prop="mid" borderBottom >
					<uv-input v-model="taskInfo.mid" border="none">
					</uv-input>
					<template v-slot:right>
						<uv-icon name="arrow-right" @click="showWlbmList()"></uv-icon>
					</template>
				</uv-form-item>
				<!-- <uv-form-item label="物料数量"  prop="num" type="number" borderBottom >
					<uv-input v-model="taskInfo.num" border="none">
					</uv-input>
				</uv-form-item> -->
				<uv-form-item label="起始位置"  prop="sourceName" borderBottom >
					<uv-input v-model="taskInfo.sourceName" border="none">
					</uv-input>
					<template v-slot:right>
						<uv-icon name="arrow-right" @click="showqswzTypeSelectList()"></uv-icon>
					</template>
				</uv-form-item>
				<uv-form-item label="目标位置"  prop="targetName" borderBottom >
					<uv-input v-model="taskInfo.targetName" border="none">
					</uv-input>
					<template v-slot:right>
						<uv-icon name="arrow-right" @click="showmbwzTypeSelectList()"></uv-icon>
					</template>
				</uv-form-item>
			</uv-form>
		</view>
		
		<view class="btn">
			<uv-button type="primary" shape="circle" text="提 交" class="mainBtn"   @click="submit"></uv-button>
		</view>
		<!-- 任务详情弹框 -->
		<uv-action-sheet ref="taskDtlSelect" :actions="taskDtlActions"   @select="taskDtlSelectFun">
		</uv-action-sheet>
		<!-- 任务类型弹框 -->
		<uv-action-sheet ref="taskTypeSelect" :actions="typeActions"   @select="taskTypeSelectFun">
		</uv-action-sheet>
		<!-- 物料编码弹框 -->
		<uv-action-sheet ref="wlbmTypeSelect" :actions="wlbmActions"   @select="wlbmSelectFun">
		</uv-action-sheet>
		<!-- 起始位置弹框 -->
		<uv-action-sheet ref="qswzTypeSelect" :actions="qswzActions"   @select="qswzSelectFun">
		</uv-action-sheet>
		<!-- 目标位置弹框 -->
		<uv-action-sheet ref="mbwzTypeSelect" :actions="qswzActions"   @select="mbwzSelectFun">
		</uv-action-sheet>
		<uv-toast ref="toast"></uv-toast>
	</view>
</template>
<script > 
import {  AddTask } from "@/api/dispatch"
export default{
	data(){
		return {
			taskInfo: {
				taskDtl:"",	//任务详情id
				taskDtlName:'',	//任务详情中文
				taskType:null,	//任务类型id
				taskTypeName:"",	//任务类型中文
				mid:"",			//物料编码
				num:1,		//物料数量（固定传1）
				sourceName:"",	//起始位置
				targetName:""	//目标位置
			},
			//任务详情
			taskDtlActions: [{
				id:"0",
				name: '入库',
			}, {
				id:"1",
				name: '送检',
			}, {
				id:"2",
				name: '留样入库',
			}, {
				id:"3",
				name: '出库',
			}],
			// 任务类型
			typeActions: [{
				id:"0",
				name: '上架',
			}, {
				id:"1",
				name: '下架',
			}],
			//物料编码
			wlbmActions:[{
				id:"500136003",
				name: '500136003',
			}],
			//起始位置、目标位置下拉
			qswzActions:[{
				id:"V01-230101",
				name: 'V01-230101',
			},{
				id:"A01-010102",
				name: 'A01-010102',
			}],
			rules: {
				// 'taskDtlName': {
				// 	type: 'string',
				// 	required: true,
				// 	message: '请选择任务详情',
				// 	trigger: ['blur', 'change']
				// },
				'taskTypeName': {
					type: 'string',
					required: true,
					message: '请选择任务类型',
					trigger: ['blur', 'change']
				},
				'mid':{
					type: 'string',
					required: true,
					message: '请输入物料编码',
					trigger: ['blur', 'change']
				},
				// 'num':{
				// 	type: 'number',
				// 	required: true,
				// 	message: '请输入物料数量',
				// 	trigger: ['blur']
				// },
				'sourceName':{
					type: 'string',
					required: true,
					message: '请输入起始位置',
					trigger: ['blur', 'change']
				},
				'targetName':{
					type: 'string',
					required: true,
					message: '请输入目标位置',
					trigger: ['blur', 'change']
				}
			},
			radio: '',
			switchVal: false
		}
	},
	methods: {
		//物料编码下拉
		showWlbmList(){
			this.$refs.wlbmTypeSelect.open();
			this.hideKeyboard();
		},
		//起始位置下拉
		showqswzTypeSelectList(){
			this.$refs.qswzTypeSelect.open();
			this.hideKeyboard();
		},
		//目标位置下拉
		showmbwzTypeSelectList(){
			this.$refs.mbwzTypeSelect.open();
			this.hideKeyboard();
		},
		submit(){
			this.$refs.form.validate().then(res => {
				var firststr = "";
				var altstr = "";
				
				if(this.taskInfo.taskType == 0)
				{
					firststr = this.taskInfo.targetName.substr(0,1);
					altstr = '目标位置输入错误';
				}
				else if(this.taskInfo.taskType == 1)
				{
					firststr = this.taskInfo.sourceName.substr(0,1);
					altstr = '起始位置输入错误';
				}

				if(firststr != 'A' && firststr != 'B')
				{
					// uni.showToast({
					// 	icon: 'error',
					// 	title: altstr
					// })
					this.$refs.toast.show({
						type: 'error',
						icon: false,
						title: '',
						message: altstr
					})
					return;
				}
				// console.log(333333)
				// return
				const obj = this.taskInfo
				AddTask(obj).then(res => {
					uni.showToast({
						title: '操作成功',
						icon: 'success',
						mask: true
					})
					uni.navigateBack({})
				})
				// uni.showToast({
				// 	icon: 'success',
				// 	title: '校验通过'
				// })
			})
		},
		// 点击任务详情
		showtaskDtlSelect(){
			this.$refs.taskDtlSelect.open();
			this.hideKeyboard();
		},
		//选择任务详情
		taskDtlSelectFun(e){
			this.taskInfo.taskDtlName = e.name
			this.taskInfo.taskDtl = e.id
			// console.log(11111)
			// console.log(e.id)
			// console.log(e.name)
		},
		// 点击任务类型
		showTypeSelect(){
			this.$refs.taskTypeSelect.open();
			this.hideKeyboard();
		},
		//选择任务类型
		taskTypeSelectFun(e){
			this.taskInfo.taskTypeName = e.name
			this.taskInfo.taskType = e.id
		},
		//选择物料编码
		wlbmSelectFun(e){
			this.taskInfo.mid = e.name
		},
		//选择起始位置
		qswzSelectFun(e){
			this.taskInfo.sourceName = e.name
		},
		//选择目标位置
		mbwzSelectFun(e){
			this.taskInfo.targetName = e.name
		},
		hideKeyboard() {
			uni.hideKeyboard()
		}
	}
}
</script>
<style scoped lang="scss">
.container{
	height: 100vh;
	padding:40rpx;
	.formBox{
		height: 85%;
	}
}
</style>