<template>
	<view class="container">
		<t-emp />
		<uni-card class="eqpInfo">
			<uni-row class="eqpCode">
				<uni-col :span="10">
					<span>设备编码</span>
				</uni-col>
				<uni-col :span="14">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="eqpCode" type="text" />
				</uni-col>
			</uni-row>
			<uni-row class="eqpName">
				<uni-col :span="10">
					<span>设备名称</span>
				</uni-col>
				<uni-col :span="14">
					<span>{{ eqpName }}</span>
				</uni-col>
			</uni-row>
			<button type="primary" class="searchBtn" @click="searchEqp">查询</button>
		</uni-card>
		<view class="jobInfo">
			<uni-card class="jobs" v-for="(item, index) in searchJobWithEqp" :key="index">
				<uni-section class="mb-10" :title="jobStatuses[item.jobStatus - 1]"
					:title-color="jobStatusColor[item.jobStatus - 1]" title-font-size="40rpx" type="line" />
				<uni-row>
					<uni-col :span="8">作业编码：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.jobCode }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">工单编码：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.pwoCode }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">目标产品编码：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.tarProCode }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">产品名称：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.tarProName }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">规格一：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.specification1 }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">制程名称：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.opTitle }}</uni-col>
				</uni-row><uni-row>
					<uni-col :span="8">目标产量：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.planNum }}</uni-col>
				</uni-row><uni-row>
					<uni-col :span="8">未完成：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.unfinishedNum }}</uni-col>
				</uni-row>
			</uni-card>
		</view>
		<button v-if="isLegal" type="primary" class="confirmStart" @click="submit">确认上机</button>
	</view>
</template>

<script>
	import {
		addStart,
		listEmpEqpHistory
	} from '@/api/basic/empEqpHistory.js'
	import {
		listPwoJob
	} from '@/api/mes/jobReport.js'
	import {
		listEmployee
	} from '@/api/mes/jobIn.js'
	import {
		listEquipment
	} from '@/api/basic/cutter.js'
	import {
		getInfo
	} from '@/api/login'
	import {
		getDicts
	} from '@/api/system/dict/dictData.js'


	export default {
		data() {
			return {
				userId: null,
				empName: this.$store.state.employee.empName,
				empCode: this.$store.state.employee.empCode,
				eqpCode: null,
				eqpId: null,
				eqpName: null,
				jobStatuses: [],
				jobStatusColor: [
					'#FFBA00',
					'#13CE66',
					'#1890FF'
				],
				searchJobWithEqp: [],
				isLegal: false
			}
		},
		methods: {
			// 根据设备编码查询设备名称
			async searchEqp() {
				this.eqpId = null
				this.eqpName = null
				this.isLegal = false
				if (this.eqpCode) {
					await listEquipment({
						equipmentCode: this.eqpCode
					}).then((res) => {
						if (res.rows.length > 0) {
							this.eqpId = res.rows[0].id
							this.eqpName = res.rows[0].equipmentTitle
							this.isLegal = true
						} else {
							this.eqpName = null
							this.searchJobWithEqp = null
							this.$modal.showToast("未查询到设备信息，请检查设备编号")
						}
					})
				} else {
					this.$modal.showToast("请先输入设备编码")
				}
				this.searchJob()
			},
			// 根据设备查找关联的作业信息
			searchJob() {
				this.searchJobWithEqp = []
				if (!this.isLegal) {
					this.eqpName = null
				} else {
					this.$modal.loading('查询中')
					listPwoJob({
						eqpId: this.eqpId
					}).then(async res => {
						this.$modal.closeLoading()
						for (var i in res.rows) {
							// 只显示已派工、材料已转入、作业开工3个状态的作业信息
							let filterNum = res.rows[i].status % 9
							if (filterNum > 0 && filterNum < 4 && res.rows[i].status != 1) {
								this.searchJobWithEqp.push({
									jobCode: res.rows[i].code,
									pwoCode: res.rows[i].pwoCode,
									tarProCode: res.rows[i].ptNoTar,
									tarProName: res.rows[i].ptTitleTar,
									specification1: res.rows[i].specification1,
									opTitle: res.rows[i].opTitle,
									planNum: res.rows[i].targetNum,
									unfinishedNum: res.rows[i].unFinishQty,
									jobStatus: filterNum
								})
							}
						}
					}).catch(() => {
						this.$modal.showToast("查询失败")
					})
				}
			},
			scanBar() {
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: (res) => {
						this.eqpCode = res.result;
					},
					fail: () => {
						this.$modal.showToast("获取设备编码失败")
					}
				});
			},
			submit() {
				if (this.eqpCode) {
					this.$modal.loading('提交中')
					addStart({
						empCode: this.empCode,
						eqpCode: this.eqpCode
					}).then(res => {
						this.$modal.closeLoading();
						this.$modal.msgSuccess("上机成功！")
						setTimeout(() => {
							uni.navigateBack()
						}, 500);
					})
				} else {
					this.$modal.showToast("请先输入设备编码")
				}
			}
		},
		// 根据账号获取员工姓名和账号ID
		onLoad() {
			// 获取字典中的作业状态
			getDicts("mes_job_status").then(res => {
				for (var i in res.data) {
					let filterNum = res.data[i].dictValue % 9
					if (filterNum > 0 && filterNum < 4 && res.data[i].dictValue != 1) {
						this.jobStatuses.push(res.data[i].dictLabel)
					}
				}
			})
			console.log(this.jobStatuses)
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding: 6px 12px;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		overflow: hidden;
		/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */
		/* #ifndef H5 */
		height: 100vh;
		/* #endif */
	}

	.eqpInfo {
		display: flex;
		flex-direction: column;
		max-height: 20vh;
		margin: 10px 0 0 0 !important;
		padding: 0;
		border-radius: 10px;

		.eqpCode,
		.eqpName,
		.searchBtn {
			flex: 1;
			font-size: 1.5rem;
			line-height: 2rem;
		}

		.searchBtn {
			margin-top: 30rpx;
			height: 5vh;
			width: 80%;
			line-height: 5vh;
			font-size: 2rem;
		}
	}

	.jobInfo {
		flex: 1;
		width: auto;
		overflow: scroll;
		padding: 4px 0;

		.jobs {
			margin: 0 0 2px 0 !important;
		}
	}


	.confirmStart {
		height: 6vh;
		width: 90vw;
		margin-top: 1vh;
		// height: 6vh;
		// position: absolute;
		// bottom: 4px;
		// left: 5vw;
		line-height: 6vh;
		font-size: 2rem;
	}
</style>