<template>
	<view>
		<uni-forms ref="form" :model="formData" :rules="rules">
			<uni-row>
				<uni-forms-item label="设备编码" :labelWidth='90' name="equipmentCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarEquipmentCode" @confirm="confirmEquipmentCode"
						v-model="formData.equipmentCode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="设备名称" :labelWidth='90' name="equipmentTitle">
					{{formData.equipmentTitle}}
				</uni-forms-item>
				<uni-forms-item label="模具编码" :labelWidth='90' name="mouldUuid">
					{{formData.mouldUuid}}
				</uni-forms-item>
				<uni-forms-item label="模具名称" :labelWidth='90' name="mouldName">
					{{formData.mouldName}}
				</uni-forms-item>
				<uni-forms-item label="库位" :labelWidth='90' name="storageLocationBarcode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarStorageLocationBarcode"
						v-model="formData.storageLocationBarcode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="拆卸员" :labelWidth='90' name="removeBy">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarRemoveBy" @confirm="confirmRemoveBy"
						v-model="formData.removeBy" type="text" style="margin-bottom: 10px;" />
					<uni-data-select v-model="formData.removeBy" :localdata="empList" />
				</uni-forms-item>
				<!-- <uni-forms-item label="操作员" :labelWidth='90' name="operator">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarOperator" @confirm="confirmOperator"
						v-model="formData.operator" type="text" style="margin-bottom: 10px;" />
					<uni-data-select v-model="formData.operator" :localdata="empList" />
				</uni-forms-item> -->
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listUseRecord,
		getUseRecord,
		delUseRecord,
		addUseRecord,
		updateUseRecord,
		bindMould,
		unbindMould,
		startRemove,
		listUseRecordAvailable
	} from "@/api/tpmMould/useRecord";
	import {
		listMould,
		updateMould
	} from "@/api/tpmMould/mould";
	import {
		listEquipment
	} from "@/api/mes/jobReport";
	import {
		listEmployee
	} from "@/api/mes/jobIn";

	export default {
		data() {
			return {
				formData: {
					mouldUuid: null,
					mouldName: null,
					equipmentCode: null,
					equipmentTitle: null,
					storageLocationBarcode: null,
					removeBy: null,
					operator: null
				},
				empList: [],
				rules: {
					equipmentCode: {
						rules: [{
								required: true,
								errorMessage: '请输入设备编码！',
								trigger: 'blur'
							},
							{
								pattern: '^[A-Z0-9]+$',
								errorMessage: '请输入正确格式的设备编码！',
								trigger: 'blur',
							},
						]
					},
					mouldUuid: {
						rules: [{
							required: true,
							errorMessage: '请输入模具编码！'
						}]
					},
					removeBy: {
						rules: [{
							required: true,
							errorMessage: '请输入拆卸员！'
						}]
					},
				}
			}
		},
		mounted() {
			listEmployee().then((res) => {
				this.empList = res.rows.map(item => {
					return {
						text: item.empCode + ':' + item.name,
						value: item.empCode,
						diasble: false
					}
				});
			})
		},
		methods: {
			//扫描模具编码带出模具信息
			confirmMouldUuid() {
				if (this.formData.mouldUuid) {}
			},
			//扫描设备编码带出设备信息
			confirmEquipmentCode() {
				if (this.formData.equipmentCode) {
					listUseRecordAvailable({
						equipmentCode: this.formData.equipmentCode,
						searchType: '3'
					}).then(res => {
						if (res.rows.length > 0) {
							listMould({
								mouldUuid: res.rows[0].mouldUuid
							}).then(resp => {
								if (resp.rows.length > 0) {
									res.rows[0].storageLocationBarcode = resp.rows[0]
										.storageLocationBarcode;
									res.rows[0].mouldId = resp.rows[0].id
									this.formData = res.rows[0]
								} else {
									this.$modal.msg("未查询到该模具，请重新输入！")
								}
							})

						} else {
							this.$modal.msg("未查询到可下模开始的记录，请重新输入！")
						}
					})
				}
			},
			scanBarMouldUuid() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.mouldUuid = res.result;
						_this.confirmMouldUuid()
					}
				});
			},
			scanBarEquipmentCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.equipmentCode = res.result;
						_this.confirmEquipmentCode()
					}
				});
			},
			scanBarRemoveBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.removeBy = res.result;
						_this.confirmRemoveBy()
					}
				});
			},
			scanBarOperator() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.operator = res.result;
						_this.confirmOperator()
					}
				});
			},
			scanBarStorageLocationBarcode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.storageLocationBarcode = res.result;
					}
				});
			},
			confirmOperator() {
				listEmployee({
					empCode: this.formData.operator
				}).then(async res => {
					if (res.rows.length == 0) {
						this.$modal.msg("未查询到该人员，请重新输入！")
						this.formData.operator = null;
					}
				})
			},
			confirmRemoveBy() {
				listEmployee({
					empCode: this.formData.removeBy
				}).then(async res => {
					if (res.rows.length == 0) {
						this.$modal.msg("未查询到该人员，请重新输入！")
						this.formData.removeBy = null;
					}
				})
			},
			submit() {
				this.$refs.form.validate().then(async res => {
					console.log(this.formData)
					if (this.formData.id) {
						if (this.formData.removeBy == "") this.formData.removeBy = null;
						if (this.formData.operator == "") this.formData.operator = null;
						this.$modal.loading('提交中')
						startRemove(this.formData).then(res => {
							this.$modal.closeLoading();
							this.$modal.msgSuccess("模具下机成功！")
							updateMould({
								id: this.formData.mouldId,
								storageLocationBarcode: this.formData.storageLocationBarcode
							}).then(res => {
								setTimeout(() => {
									this.$tab.switchTab("/pages/work/index");
								}, 500);
							})
						})
					} else {
						this.$modal.msg("未查询到可下模开始的记录，请重新输入！")
					}
				})
			}
		}
	}
</script>

<style>
</style>