<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" @change="scanbarshankCode"
						v-model="formData.shankCode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="已绑定刀刃编码" :labelWidth='90' name="oldbladeCode">
					<uni-easyinput disabled v-model="oldbladeCode" 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="updateBy">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarupdateBy" @change="selectEmpCode"
						v-model="updateBy" type="text" />
				</uni-forms-item>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listInstallRecord,
		unbindRecord,
		bindRecord
	} from "@/api/basic/cutter.js";
	import {
		listEmployee
	} from "@/api/mes/jobIn.js";
	export default {
		data() {
			return {
				formData: {
					shankCode: null,
					bladeCode: null,
				},
				//临时切换人编码
				updateBy: null,
				//切换人姓名
				updateName: null,
				//拆卸表单
				unbindForm: {},
				//安装表单
				bindForm: {},
				//旧刀刃编码
				oldbladeCode: null,
				rules: {
					bladeCode: {
						rules: [{
							required: true,
							errorMessage: '请输入刀刃编码！'
						}]
					},
					shankCode: {
						rules: [{
							required: true,
							errorMessage: '请输入刀柄编码！'
						}]
					},
				}
			}
		},
		mounted() {},
		methods: {
			selectEmpCode(code) {
				this.updateName = null;
				listEmployee({
					empCode: code
				}).then(async res => {
					if (res.rows.length > 0) {
						this.updateName = res.rows[0].name;
					} else {
						this.$modal.msg("未查询到该人员，请重新输入！")
					}
				})
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.shankCode = res.result;
						_this.formData.scanbarshankCode();
					}
				});
			},
			scanbarshankCode() {
				this.oldbladeCode = null;
				if (this.formData.shankCode) {
					listInstallRecord({
						shankCode: this.formData.shankCode,
						removeTime: null
					}).then(async res => {
						//判断是否存在已绑定且未拆卸的刀具
						console.log(res);
						for (var i in res.rows) {
							if (!res.rows[i].removeTime) {
								console.log(res.rows[i].bladeCode)
								this.oldbladeCode = res.rows[i].bladeCode;
							}
						}
					});
				}
			},
			scanBarbladeCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.bladeCode = res.result;
					}
				});
			},
			scanBarupdateBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.updateBy = res.result;
						_this.selectEmpCode(res.result);
					}
				});
			},
			submit() {
				this.$refs.form.validate().then(async res => {
					if (this.formData.bladeCode && this.formData.shankCode) {
						console.log(this.updateName)
						if (this.updateName) {
							this.unbindForm = {
								shankCode: this.formData.shankCode,
								bladeCode: this.oldbladeCode,
								removeBy: this.updateName
							}
							//拆卸
							unbindRecord(this.unbindForm);
							this.bindForm = this.formData
							this.bindForm.installBy = this.updateName;
							//由于bindRecord接口过快，导致切换人没有时间赋值，故设置定时器
							setTimeout(() => {
								this.$modal.loading('提交中')
								bindRecord(this.bindForm).then(res => {
									this.$modal.closeLoading();
									this.$modal.msgSuccess("刀具切换成功！")
									setTimeout(() => {
										this.$tab.switchTab("/pages/work/index");
									}, 500);
								})
							}, 500);
						} else {
							this.$modal.msg("请输入切换人！")
						}

					}
				})
			}
		}
	}
</script>

<style>
</style>