<template>
	<view class="container">
		<view class="status-bar"></view>
		<t-emp />
		<view class="jobInfo">
			<uni-card class="jobs" v-for="(item, index) in searchJobWithEqp" :key="index">
				<uni-row>
					<uni-col :span="8">作业编码：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.code }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">目标产品编码：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.ptNoTar }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">目标产品名称：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.ptTitleTar }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">设备编码：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.eqpCode }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">计划数量：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.targetNum }}</uni-col>
				</uni-row>
				<uni-row>
					<uni-col :span="8">未完成数量：</uni-col>
					<uni-col :span="12" :offset="3">{{ item.unFinishQty }}</uni-col>
				</uni-row>
			</uni-card>
		</view>
		<button type="primary" class="searchBtn" @click="fetchJobList">查询</button>
	</view>
</template>

<script>
import { listPwoJobLike } from '@/api/mes/pwoIn';

export default {
	data() {
		return {
			loading: false,
			empName: this.$store.state.employee.empName,
			empCode: this.$store.state.employee.empCode,
			eqpCode: null,
			eqpId: null,
			eqpName: null,
			startedEqp: [],
			searchJobWithEqp: []
		}
	},
	methods: {
		async fetchJobList() {
			this.searchJobWithEqp.length = 0;
			this.$modal.loading('获取作业中');
			try {
				const { rows } = await listPwoJobLike({
					employeeCode: this.empCode
				});
				this.searchJobWithEqp = rows;
			} catch(err) {
				this.$modal.msg('获取作业失败，请稍后重试');
			} finally {
				this.$modal.closeLoading();
			}
		},
		// 根据员工信息查询上机设备
		searchStartedEqp() {
			return new Promise((resolve, reject) => {
				this.startedEqp = [];
				listEmpEqpHistory({
					empName: this.empName,
					empCode: this.empCode
				}).then(res => {
					if (res.rows.length > 0) {
						for (let i in res.rows) {
							if (!res.rows[i].endTime) {
								this.startedEqp.push({
									eqpCode: res.rows[i].equipmentCode,
									eqpId: ''
								});
							}
						}
						if (this.startedEqp.length === 0) {
							this.$modal.closeLoading()
							uni.showToast({
								title: '未查询到已上机设备',
								icon: 'none'
							});
							reject(new Error('无设备'));
						} else {
							resolve(); // 成功时调用 resolve
						}
					}
				}).catch(error => {
					this.$modal.closeLoading()
					reject(error);
				});
			});
		},
		// 根据设备编码查询设备id
		searchEqp() {
			return new Promise((resolve, reject) => {
				if (this.startedEqp.length === 0) {
					this.$modal.closeLoading()
					resolve();
					return;
				}

				const promises = this.startedEqp.map((eqp, index) => {
					return listEquipment({
						equipmentCode: eqp.eqpCode
					}).then(res => {
						this.startedEqp[index].eqpId = res.rows[0].id;
					}).catch(error => {
						this.$modal.closeLoading()
						this.$modal.showToast("查询设备id失败");
						throw error;
					});
				});

				Promise.all(promises)
					.then(() => resolve())
					.catch(error => reject(error));
			});
		},
		// 根据设备查找关联的作业信息
		searchJob() {
			return new Promise((resolve, reject) => {
				this.searchJobWithEqp = [];
				if (this.startedEqp.length === 0) {
					this.$modal.closeLoading()
					resolve();
					return;
				}

				const promises = this.startedEqp.map((eqp, index) => {
					return listPwoJob({
						eqpId: eqp.eqpId
					}).then(res => {
						res.rows.forEach(row => {
							this.searchJobWithEqp.push({
								jobCode: row.code,
								tarProCode: row.ptNoTar,
								tarProName: row.ptTitleTar,
								eqpCode: eqp.eqpCode,
								planNum: row.targetNum,
								unfinishedNum: row.unFinishQty
							});
						});
						this.$modal.closeLoading()
					}).catch(error => {
						this.$modal.closeLoading()
						this.$modal.showToast("查询作业信息失败");
						throw error;
					});
				});

				Promise.all(promises)
					.then(() => resolve())
					.catch(error => reject(error));
			});
		},
		async searchJobWithEmp() {
			try {
				this.$modal.loading("查询中")
				await this.searchStartedEqp()
				await this.searchEqp()
				await this.searchJob()
			} catch (error) {
				uni.showToast({
					title: '查询失败',
					icon: 'none'
				});
				this.$modal.closeLoading()
			}
		}
	},
	// 根据账号获取员工姓名和账号ID
	// onLoad() {
	// 	getInfo().then(res => {
	// 		this.empName = res.user.nickName
	// 	}).catch(() => {
	// 		uni.showToast({
	// 			title: '当前账号无对应人员资料',
	// 			icon: 'none'
	// 		})
	// 	})
	// 	//根据昵称获取对应员工编号
	// 	listEmployee({
	// 		name: this.empName
	// 	}).then(res => {
	// 		this.empCode = res.rows[0].empCode
	// 	}).catch(() => {
	// 		uni.showToast({
	// 			title: '当前账号无对应人员资料',
	// 			icon: 'none'
	// 		})
	// 	})
	// }
}
</script>

<style scoped lang="scss">
.status-bar {
	height: var(--status-bar-height);
	width: 100%;
}

.container {
	width: 100%;
	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 */
}

.jobInfo {
	flex: 1;
	width: 100%;
	overflow: scroll;
	padding: 10px 0;

	.jobs {
		margin: 6px 0 0 !important;
		border-radius: 10px;
	}
}

.searchBtn {
	width: 100%;
	margin-top: 1vh;
	line-height: 6vh;
	font-size: 2.5rem;
}
</style>