<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-forms-item label="刀柄编码" :labelWidth='90' name="shankCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.shankCode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="刀刃编码" :labelWidth='90' name="bladeCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarbladeCode" v-model="formData.bladeCode"
						type="text" />
				</uni-forms-item>
				<uni-forms-item label="拆卸员" :labelWidth='90' name="removeCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarremoveBy" @change="selectremoveBy"
						v-model="removeCode" type="text" />
				</uni-forms-item>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>

	</view>
</template>

<script>
	import {
		listUseRecord,
		getUseRecord,
		delUseRecord,
		addStartUse,
		updateEndUse,
		listEntity,
		listMaterial,
		listEquipment,
		getEntity,
		unbindRecord
	} from "@/api/basic/cutter.js";
	import {
		listEmployee
	} from "@/api/mes/jobIn.js";

	export default {
		data() {
			return {
				formData: {
					shankCode: null,
					bladeCode: null,
					removeBy: null,
				},
				removeCode: null,
				rules: {
					bladeCode: {
						rules: [{
							required: true,
							errorMessage: '请输入刀刃编码！'
						}]
					},
					shankCode: {
						rules: [{
							required: true,
							errorMessage: '请输入刀柄编码！'
						}]
					},
				}
			}
		},
		mounted() {},
		methods: {
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.shankCode = res.result;
					}
				});
			},
			scanBarbladeCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.bladeCode = res.result;
					}
				});
			},
			selectremoveBy(code) {
				listEmployee({
					empCode: code
				}).then(async res => {
					if (res.rows.length > 0) {
						this.formData.removeBy = res.rows[0].name;
					} else {
						this.$modal.msg("未查询到该人员，请重新输入！")
					}
				})
			},
			scanBarremoveBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.removeCode = res.result;
						_this.selectremoveBy(res.result);
					}
				});
			},
			submit() {
				this.$refs.form.validate().then(async res => {
					if (this.formData.bladeCode && this.formData.shankCode) {
						console.log(this.formData);
						setTimeout(() => {
							if (this.formData.removeBy) {
								this.$modal.loading('提交中')
								unbindRecord(this.formData).then(res => {
									this.$modal.closeLoading();
									this.$modal.msgSuccess("刀具拆卸成功！")
									setTimeout(() => {
										this.$tab.switchTab("/pages/work/index");
									}, 500);
								})
							} else {
								this.$modal.msg("请输入拆卸员！")
							}
						}, 500);
					}
				})
			}
		}
	}
</script>

<style>
</style>