<template>
	<view>
		<uni-forms ref="form" :model="formData" :rules="rules">
			<uni-row>
				<uni-forms-item label="模具编码" :labelWidth='90' name="mouldUuid">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarMouldUuid" v-model="formData.mouldUuid"
						type="text" @confirm="confirmMouldUuid" />
				</uni-forms-item>
				<uni-forms-item label="模具名称" :labelWidth='90' name="mouldName">
					{{formData.mouldName}}
				</uni-forms-item>
				<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="storageLocationBarcode">
					<!-- 					<uni-easyinput suffixIcon="scan" @iconClick="scanBarStorageLocationBarcode"
						@confirm="confirmStorageLocationBarcode" v-model="formData.storageLocationBarcode"
						type="text" /> -->
					<uni-easyinput v-model="formData.storageLocationBarcode" type="text" disabled />
				</uni-forms-item>
				<uni-forms-item label="安装员" :labelWidth='90' name="installBy">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarInstallCode" @confirm="confirmInstallCode"
						@input="inputInstallBy" v-model="formData.inputInstallBy" type="text" style="margin-bottom: 10px;" />
					<uni-data-select v-model="formData.installBy" :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,
	} from "@/api/tpmMould/useRecord";
	import {
		listMould
	} 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,
					installBy: null,
					operator: null
				},
				empList: [],
				rules: {
					equipmentCode: {
						rules: [{
								required: true,
								errorMessage: '请输入设备编码！',
								trigger: 'blur'
							},
							{
								pattern: '^[A-Z0-9]+$',
								errorMessage: '请输入正确格式的设备编码！',
								trigger: 'blur',
							},
						]
					},
					mouldUuid: {
						rules: [{
							required: true,
							errorMessage: '请输入模具编码！'
						}]
					},
					installBy: {
						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) {
					listMould({
						mouldUuid: this.formData.mouldUuid
					}).then(res => {
						if (res.rows.length > 0) {
							this.formData.mouldName = res.rows[0].mouldName
							this.formData.storageLocationBarcode = res.rows[0].storageLocationBarcode;
						} else {
							this.$modal.msg("未查询到该模具，请重新输入！")
						}
					})
				}
			},
			//扫描设备编码带出设备信息
			confirmEquipmentCode() {
				if (this.formData.equipmentCode) {
					listEquipment({
						equipmentCode: this.formData.equipmentCode
					}).then(res => {
						if (res.rows.length > 0) {
							this.formData.equipmentTitle = res.rows[0].equipmentTitle
						} 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()
					}
				});
			},
			scanBarInstallCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.installBy = res.result;
						_this.confirmInstallCode()
					}
				});
			},
			scanBarOperator() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.operator = res.result;
						_this.confirmOperator()
					}
				});
			},
			confirmOperator() {
				listEmployee({
					empCode: this.formData.operator
				}).then(async res => {
					if (res.rows.length == 0) {
						this.$modal.msg("未查询到该人员，请重新输入！")
						this.formData.operator = null;
					}
				})
			},
			confirmInstallCode() {
				listEmployee({
					empCode: this.formData.inputInstallBy
				}).then(async res => {
					if (res.rows.length == 0) {
						this.$modal.msg("未查询到该人员，请重新输入！")
						this.formData.installBy = null;
					}
				})
			},
			async submit() {
				this.$refs.form.validate().then(async res => {
					const {mouldName,equipmentTitle,installBy} = this.formData
					// if (this.formData.installBy == "") this.formData.installBy = null;
					// if (this.formData.operator == "") this.formData.operator = null;
					// console.log(11111111,!await this.getValue(installBy))
					// if(!await this.getValue(mouldName) || !await this.getValue(equipmentTitle) || !await this.getValue(installBy)){
					// 	this.$modal.msg("有值不符合规范")
					// 	return
					// } 
					// console.log(this.formData)
					this.$modal.loading('提交中')
					bindMould(this.formData).then(res => {
						this.$modal.closeLoading();
						this.$modal.msgSuccess("模具上机成功！")
						setTimeout(() => {
							this.$tab.switchTab("/pages/work/index");
						}, 500);
					})
				})
			},
			/**
			 * 判断是否有值
			 * @param {Object} value
			 */
			getValue(value){
				if(value === undefined || value === null || value === '') return false
				this.$modal.msg("未查询到该人员，请重新输入！")
				return true
			},
			inputInstallBy(value){
				const list = this.empList
				let data = false 
				for(let i in list){
					if(list[i].value === value){
						data = true;
						break
					}
				}
				if(data){
					this.formData.installBy = value
				}else{
					this.formData.installBy = null
				}
				
			}
		}
	}
</script>

<style>
</style>