<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item label="发货点位：" :labelWidth='90' name="srcLocationCode">
				<uni-easyinput type="text" v-model="formData.srcLocationCode" />
			</uni-forms-item>
			<uni-forms-item label="目的货位：" :labelWidth='90' name="destLocationCode">
				<uni-easyinput suffixIcon="scan" @iconClick="scanDestLocationUuid" type="text" @blur="searchSrcLocation"
					v-model="formData.destLocationCode" />
			</uni-forms-item>
		</uni-forms>
		<u-button type="success" @click="handleCall">呼叫</u-button>
	</view>
</template>

<script>
import { addDeliveryTask, findPreparation } from "@/api/les/lesMaterialCall.js";
import LesDeliveryTaskType from "@/utils/enums/LesDeliveryTaskType";
export default {
	data() {
		return {
			formData: {
				srcLocationCode: null,
				destLocationCode: null,
				taskType: LesDeliveryTaskType.PRODUCTION
			},
			/** 校验规则 */
			rules: {
				srcLocationCode: {
					rules: [{
						required: true,
						errorMessage: '请输入发货点位',
					}, ]
				},
				destLocationCode: {
					rules: [{
						required: true,
						errorMessage: '请选择目的货位',
					}, ]
				},
			},
		}
	},
	methods: {
		searchSrcLocation() {
			const _this = this;
			if (!_this.formData.destLocationCode) {
				_this.formData.srcLocationCode = null;
				return;
			}
			findPreparation(_this.formData.destLocationCode).then(res => {
				if (res.code == 200) {
					this.$set(this.formData, "srcLocationCode", res.data);
				} else {
					this.$modal.showToast("未找到对应发货点位")
				}
			})
		},

		scanDestLocationUuid() {
			const _this = this;
			uni.scanCode({
				scanType: ['qrCode', 'barCode'],
				success: function(res) {
					_this.formData.destLocationCode = res.result;
					_this.searchSrcLocation();
				}
			});
		},
		handleCall() {
			const _this = this;
			this.$refs.form.validate().then(res => {
				if (_this.formData.srcLocationCode && _this.formData.destLocationCode) {
					_this.$modal.loading('提交中')
					addDeliveryTask(_this.formData).then(res => {
						_this.$modal.closeLoading();
						_this.$modal.msgSuccess("提交成功！");
						_this.reset();
						setTimeout(() => {
							_this.$tab.switchTab("/pages/work/index");
						}, 500);
					})
				} else {
					_this.$modal.msg("请将信息补充完整")
				}
			})
		},

		reset() {
			this.formData.srcLocationCode = null;
			this.formData.destLocationCode = null;
		}
	}
}
</script>

<style>
::v-deep .uni-easyinput__content-input {
	height: 80rpx !important;
	line-height: 60rpx !important;
	display: flex !important;
	align-items: center !important;
	min-height: 80rpx !important;
}

.u-button {
	height: 10% !important;
	position: absolute;
	bottom: 0;
}
</style>