<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="员工编码" :labelWidth='90' name="empCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="search"
							v-model="formData.empCode" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24" style="margin-bottom: 10px;">
					该员工已绑定设备：<br><span
						v-for="item in searchEmpWithEqp">{{item.equipmentCode+':'+item.equipmentTitle}}<br></span>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="设备编码" :labelWidth='90' name="eqpCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar1" v-model="formData.eqpCode" type="text" />
					</uni-forms-item>
				</uni-col>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>

	</view>
</template>

<script>
	import {
		addStart,
		addEnd,
		listEmpEqpHistory
	} from '@/api/basic/empEqpHistory.js'

	export default {
		data() {
			return {
				formData: {
					empCode: null,
					eqpCode: null
				},
				searchEmpWithEqp: [],
				rules: {
					empCode: {
						rules: [{
							required: true,
							errorMessage: '请输入员工编码！'
						}]
					},
					eqpCode: {
						rules: [{
							required: true,
							errorMessage: '请输入设备编码！'
						}, {
							pattern: '^[A-Z0-9]+$',
							errorMessage: '请输入正确格式的设备编码！',
							trigger: 'blur',
						}]
					}
				}
			}
		},
		methods: {
			//根据员工编码来查询已上机但并未下机的设备
			search() {
				this.searchEmpWithEqp = [];
				listEmpEqpHistory({
					empCode: this.formData.empCode
				}).then(async res => {
					if (res.rows.length > 0) {
						for (var i in res.rows) {
							//判断是否下机
							if (!res.rows[i].endTime) {
								this.searchEmpWithEqp.push({
									equipmentCode: res.rows[i].equipmentCode,
									equipmentTitle: res.rows[i].equipmentTitle
								});
							}
						}
					}
				})
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.empCode = res.result;
						_this.search();
					}
				});
			},
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.eqpCode = res.result;
					}
				});
			},
			submit() {
				this.$refs.form.validate().then(res => {
					if (this.formData.empCode && this.formData.eqpCode) {
						this.$modal.loading('提交中')
						addEnd(this.formData).then(res => {
							this.$modal.closeLoading();
							this.$modal.msgSuccess("下机成功！")
							setTimeout(() => {
								this.$tab.switchTab("/pages/work/index");
							}, 500);
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>