<template>
	<view class="page-wrap">
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item label="发货点位：" :labelWidth='100' name="srcLocationCode">
				<uni-easyinput type="text" v-model="formData.srcLocationCode" />
			</uni-forms-item>
			<uni-forms-item label="目的点位：" :labelWidth='100' name="destLocationCode">
				<uni-easyinput type="text" v-model="destLocationCode" />
			</uni-forms-item>
			<uni-forms-item label="机器人类型：" :labelWidth='100' name="robotType">
				<uni-data-select placeholder="请选择机器类型" v-model="formData.robotType" :localdata="RobotData" />
			</uni-forms-item>
			<uni-forms-item label="容器类型：" :labelWidth='100' name="ctnrTyp" v-if="formData.robotType == 2">
				<uni-easyinput type="text" v-model="formData.ctnrTyp" />
			</uni-forms-item>
		</uni-forms>
		<view class="btns">
			<u-button type="primary" @click="handleDelivery" class="sub-btn">配送</u-button>
			<u-button type="warning" @click="handleCancle" class="pri-btn">取消</u-button>
		</view>
	</view>
</template>

<script>
import RobotType from "@/utils/enums/RobotType";
import { delivery, cancel } from "@/api/les/lesMaterialDelivery.js"
export default {
	onLoad(option) {
		const form = JSON.parse(option.formData);
		this.formData.taskTyp = form.taskType;
		this.formData.id = form.id;
		this.formData.srcLocationCode = form.srcLocationCode;
		this.destLocationCode = form.destLocationCode;
	},
	data() {
		return {
			formData: {
				id: null,
				srcLocationCode: null,
				taskTyp: null,
				robotType: null,
				ctnrTyp: null,
			},
			destLocationCode: null,
			RobotData: [
				{ value: RobotType.LATENT_MOBILE_ROBOT, text: "潜伏车" },
				{ value: RobotType.LATENT_FORKLIFT_AGV, text: "潜伏叉车" },
			],
			/** 校验规则 */
			rules: {
				srcLocationCode: {
					rules: [{
						required: true,
						errorMessage: '请输入发货点位',
					}, ]
				},
				RobotType: {
					rules: [{
						required: true,
						errorMessage: '请选择机器人类型',
					}, ]
				},
			},
		}
	},
	methods: {
		handleDelivery() {
			const _this = this;
			this.$refs.form.validate().then(res => {
				if (_this.formData.srcLocationCode && _this.formData.robotType) {
					_this.$modal.loading('提交中')
					delivery(_this.formData).then(res => {
						_this.$modal.closeLoading();
						_this.$modal.msgSuccess("提交成功！");
						setTimeout(() => {
							this.$tab.switchTab("/pages/work/index");
						}, 500);
					})
				} else {
					_this.$modal.msg("请将信息补充完整")
				}
			})
		},
		handleCancle() {
			const _this = this;
			const arr = [];
			arr.push(_this.formData.id);
			this.$refs.form.validate().then(res => {
				uni.showModal({
					title: '提示',
					content: '您确定取消该任务吗？',
					success: function(res) {
						if (res.confirm) {
							_this.$modal.loading('取消中')
							cancel(arr).then(res => {
								_this.$modal.closeLoading();
								_this.$modal.msgSuccess("取消成功！");
								setTimeout(() => {
									this.$tab.switchTab("/pages/work/index");
								}, 500);
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			})
		},
	}
}
</script>

<style scoped lang="scss">
.btns {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	display: flex;
	justify-content: space-evenly;
	background: #fff;
	padding: 8px 0;
}

::v-deep .uni-easyinput__content-input {
	height: 80rpx;
	line-height: 60rpx;
	display: flex;
	align-items: center;
}

::v-deep .uni-select {
	height: 60rpx;
}

.btns .sub-btn,
.btns .pri-btn {
	width: 47%;
	height: 60rpx;
}
</style>