<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" @change="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">
					<uni-data-select v-model="formData.mouldUuid" :localdata="mouldList" @change="clickDetail(mouldList[formData.mouldUuid])" />
				</uni-forms-item>
				<!-- <uni-forms-item label="模具名称" :labelWidth='90' name="mouldName">
					{{formData.mouldName}}
				</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,
		listUseRecordAvailable,
		endRemove
	} 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: 0,
					mouldName: null,
					equipmentCode: null,
					equipmentTitle: null,
					index:1,
				},
				mouldList: [],
				rules: {
					equipmentCode: {
						rules: [{
								required: true,
								errorMessage: '请输入设备编码！',
								trigger: 'blur'
							},
							{
								pattern: '^[A-Z0-9]+$',
								errorMessage: '请输入正确格式的设备编码！',
								trigger: 'blur',
							},
						]
					},
					mouldUuid: {
						rules: [{
							required: true,
							errorMessage: '请输入模具编码！'
						}]
					},
				}
			}
		},
		mounted() {},
		methods: {
			clickDetail(e) {
				
				this.id = e.id
			},
			//扫描设备编码带出设备信息
			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("未查询到该设备，请重新输入！")
						}
					})
					listUseRecordAvailable({
						equipmentCode: this.formData.equipmentCode,
						searchType: '4'
					}).then(res => {
						if (res.rows.length > 0) {
		
							this.mouldList = res.rows.map((item,index) => {
								item.text = item.mouldUuid + ':' + item.mouldName;
								item.value = index;
								item.diasble = false;
								item.index = index;
								return item
							});
						} else {
							this.$modal.msg("未查询到可下模完成的记录，请重新输入！")
						}
					})
				}
			},
			scanBarEquipmentCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.equipmentCode = res.result;
						_this.confirmEquipmentCode()
					}
				});
			},
			submit() {
				this.$refs.form.validate().then(async res => {
					this.$modal.loading('提交中')
					endRemove([this.id]).then(res => {
						this.$modal.closeLoading();
						this.$modal.msgSuccess("下模完成成功！")
						setTimeout(() => {
							this.$tab.switchTab("/pages/work/index");
						}, 500);
					})
				})
			}
		}
	}
</script>

<style>
</style>