<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item label="货位号：" name="locationCode">
				<uni-easyinput suffixIcon="scan" @iconClick="scanLocationCodeUuid" type="text"
					v-model="formData.locationCode" />
			</uni-forms-item>
			<uni-forms-item label="批次号：" name="batchNo">
				<uni-easyinput suffixIcon="scan" @iconClick="scanBatchNoUuid" type="text" v-model="formData.batchNo" />
			</uni-forms-item>
		</uni-forms>
		<uni-row :gutter="10">
			<uni-col :span="12">
				<u-button type="success" @click="handleBind">绑定</u-button>
			</uni-col>
			<uni-col :span="12">
				<u-button type="primary" @click="handleUnBind">解绑</u-button>
			</uni-col>
		</uni-row>
	</view>
</template>

<script>
import { bind, unBind } from "@/api/les/lesMaterialBind.js";
export default {
	data() {
		return {
			formData: {
				locationCode: null,
				batchNo: null,
			},
			/** 校验规则 */
			rules: {
				locationCode: {
					rules: [{
						required: true,
						errorMessage: '请输入货位号',
					}, ]
				},
				batchNo: {
					rules: [{
						required: true,
						errorMessage: '请选择批次号',
					}, ]
				},
			},
		}
	},
	methods: {
		scanLocationCodeUuid() {
			const _this = this;
			uni.scanCode({
				scanType: ['qrCode', 'barCode'],
				success: function(res) {
					_this.formData.locationCode = res.result;
				}
			});
		},
		scanBatchNoUuid() {
			const _this = this;
			uni.scanCode({
				scanType: ['qrCode', 'barCode'],
				success: function(res) {
					_this.formData.batchNo = res.result;
				}
			});
		},
		handleBind() {
			const _this = this;
			this.$refs.form.validate().then(res => {
				uni.showModal({
					title: '提示',
					content: '您确定绑定该数据吗？',
					success: function(res) {
						if (res.confirm) {
							if (_this.formData.locationCode && _this.formData.batchNo) {
								_this.$modal.loading('绑定中')
								bind(_this.formData).then(res => {
									_this.$modal.closeLoading();
									_this.$modal.msgSuccess("绑定成功！");
									_this.reset();
									setTimeout(() => {
										this.$tab.switchTab("/pages/work/index");
									}, 500);
								})
							} else {
								_this.$modal.msg("请将信息补充完整")
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			})
		},

		handleUnBind() {
			const _this = this;
			this.$refs.form.validate().then(res => {
				uni.showModal({
					title: '提示',
					content: '您确定解绑该数据吗？',
					success: function(res) {
						if (res.confirm) {
							if (_this.formData.locationCode) {
								_this.$modal.loading('解绑中')
								unBind(_this.formData.locationCode).then(res => {
									_this.$modal.closeLoading();
									_this.$modal.msgSuccess("解绑成功！");
									_this.reset();
									setTimeout(() => {
										this.$tab.switchTab("/pages/work/index");
									}, 500);
								})
							} else {
								_this.$modal.msg("请将信息补充完整")
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			})
		},

		reset() {
			this.formData = {
				locationCode: null,
				batchNo: null,
			};
		}
	}
}
</script>