<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData">
				<uni-forms-item label="模具编码" :labelWidth='90' name="mouldUuid">
					<uni-easyinput v-model="formData.mouldUuid" suffixIcon="scan" @iconClick="scanBarMouldUuid"
						@confirm="confirmMouldUuid" type="text" />
				</uni-forms-item>
				<uni-collapse-item title="查询结果" :open="true">
					<uni-card :is-shadow="false" v-for="(item,index) in mouldList" :key="index" is-full>
						<view><strong>模具编码</strong> : {{item.mouldUuid}}</view>
						<view><strong>模具名称</strong> : {{item.mouldName}}</view>
						<view><strong>型号编码</strong> : {{item.mouldCode}}</view>
						<view><strong>所属部门名</strong> : {{item.departmentName}}</view>
						<view><strong>材质</strong> : {{item.material}}</view>
						<view><strong>库位编码</strong> : {{item.storageLocationBarcode}}</view>
						<view><strong>模穴数</strong> : {{item.cavityNumber}}</view>
						<view><strong>状态</strong> : <uni-tag :text="tagText(item.status)" :type="tagType(item.status)"
								v-if="item.status"></uni-tag></view>
						<view><strong>上次保养</strong> : {{item.lastMaintainTime}}</view>
						<view><strong>上次维修</strong> : {{item.lastFixTime}}</view>
						<view><strong>使用次数</strong> : {{item.useCount + "/"+item.useCountAfterFix}}</view>
						<view><strong>使用时间(分)</strong> : {{item.useTime+"/"+item.useTimeAfterFix}}</view>
						<view><strong>备注</strong> : {{item.remark}}</view>
						<view><strong>所在设备</strong> :
							{{item.equipmentCode?(item.equipmentCode+':'+item.equipmentTitle):''}}
						</view>
					</uni-card>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
	</view>
</template>

<script>
	import {
		listUseRecord,
		getUseRecord,
		delUseRecord,
		addUseRecord,
		updateUseRecord,
		bindMould,
		unbindMould,
	} from "@/api/tpmMould/useRecord";
	import {
		listMould
	} from "@/api/tpmMould/mould";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	export default {
		mounted() {
			getDicts("tpm_mould_status").then(res => {
				this.mouldStatusOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
		},
		data() {
			return {
				mouldStatusOptions: [],
				formData: {
					mouldUuid: null
				},
				mouldList: [],
			}
		},
		methods: {
			tagText(status) {
				let obj = this.mouldStatusOptions.find(item => item.value == status)
				if (obj !== undefined) {
					return this.mouldStatusOptions.find(item => item.value == status).text
				}
			},
			tagType(status) {
				let obj = this.mouldStatusOptions.find(item => item.value == status)
				if (obj !== undefined) {
					switch (obj.text) {
						case '维修':
							return 'warning';
							break;
						case '空闲':
							return 'primary';
							break;
						case '使用中':
							return 'success';
							break;
					}
				}
			},
			scanBarMouldUuid(code) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.mouldUuid = res.result;
						_this.confirmMouldUuid()
					}
				});
			},
			confirmMouldUuid() {
				if (this.formData.mouldUuid) {
					listMould({
						mouldUuid: this.formData.mouldUuid
					}).then(res => {
						if (res.rows.length > 0) {
							listUseRecord({
								mouldUuid: this.formData.mouldUuid,
								removeTime: null
							}).then(resp => {
								if (resp.rows.length > 0) {
									let array = resp.rows.filter(item => item.removeTime ==
										null)
									if (array.length > 0) {
										res.rows[0].equipmentCode = array[0]
											.equipmentCode;
										res.rows[0].equipmentTitle = array[0]
											.equipmentTitle;
									}
									this.mouldList = res.rows;
								} else {
									this.$modal.msg("未查询到该实例相关信息，请重新输入！")
									this.formData.mouldUuid = null;
								}
							})
						} else {
							this.$modal.msg("未查询到该实例相关信息，请重新输入！")
							this.formData.mouldUuid = null;
						}
					})

				}
			}
		}
	}
</script>

<style>
	.text {
		text-align: center;
	}
</style>