<template>
	<view class="run-page">
		<navigation title="设备运行情况"></navigation>
		<view class="list-box" v-if="dataList.length>0">
			<view class="list-item" v-for="(item, index) in dataList" :key="index" @click="toDetail(item)">
				<view :style="{ color:item.productStatus == 1? '#2fc25b': 'red'}">
					{{item.equipmentName}}
				</view>
				<view class="item-text">当日产量：{{ item.number || '-' }}</view>
				<view class="item-text">工作时间：{{ item['1'] || 0 }}分</view>
				<view class="item-text">停机时间：{{ item['0'] || 0 }}分</view>
				<view class="item-text">当班稼动率：{{ ((item['1'] / (item['1'] + item['0'])) * 100).toFixed(2) || '-' }}%
				</view>
			</view>
		</view>
		<empty v-else text="暂无数据"></empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: [],
			}
		},
		onLoad(option) {},
		onShow() {
			this.getList()
		},
		methods: {
			getList() {
				uni.showLoading({
					title: '正在加载...'
				})
				let params = {
					pageSize: '9999',
				}
				this.$api.equipmentList(params).then(res => {
					uni.hideLoading()
					this.dataList = res.rows
					for (let i = 0; i < this.dataList.length; i++) {
						let attrList = this.dataList[i].attrList
						for (let j = 0; j < attrList.length; j++) {
							if (attrList[j].attrCode == 'Product_Status') {
								this.dataList[i]['productStatus'] = attrList[j].lastVal
							}
						}
					}
					this.listTagDifferenceDevice()
					this.listDayCropRate()
				}).catch(err => {})
			},
			listTagDifferenceDevice() {
				let params = {
					tag: 'Number',
					rq: this.dayjs().format('YYYY-MM-DD')
				}
				this.$api.listTagDifferenceDevice(params).then(res => {
					for (const item in res.data) {
						for (let i = 0; i < this.dataList.length; i++) {
							if (this.dataList[i].equipmentName == res.data[item]['name']) {
								this.dataList[i]['number'] = res.data[item]['val']
							}
						}
					}
					this.$forceUpdate();
				}).catch(err => {})
			},
			listDayCropRate() {
				let params = {
					tag: 'Product_Status',
					start: this.dayjs().format('YYYY-MM-DD'),
					end: this.dayjs().format('YYYY-MM-DD')
				}
				this.$api.listDayCropRate(params).then(res => {
					for (const temp in res.data) {
						for (const item in res.data[temp]) {
							if (res.data[temp][item].day['in']) {
								for (let i = 0; i < this.dataList.length; i++) {
									if (this.dataList[i].equipmentName == item) {
										this.dataList[i]['1'] = res.data[temp][item].day['1']
										this.dataList[i]['0'] = res.data[temp][item].day['0']
									}
								}
							} else {
								for (let i = 0; i < this.dataList.length; i++) {
									if (this.dataList[i].equipmentName == item) {
										this.dataList[i]['1'] = res.data[temp][item].day['1']
										this.dataList[i]['0'] = res.data[temp][item].day['0']
									}
								}
							}
						}
					}
					this.$forceUpdate();
				}).catch(err => {})
			},
			toDetail(item) {
				uni.navigateTo({
					url: '/subPackages/equipment/single?productStatus=' + item.productStatus + '&equipmentName=' + item.equipmentName +
						'&equipmentCode=' + item.equipmentCode
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.run-page {
		.list-box {
			width: 98%;
			margin: auto;
			color: $uni-text-color-list;
			display: flex;
			flex-wrap: wrap;

			.list-item {
				width: 42%;
				background-color: $uni-bg-color;
				padding: 2%;
				border-radius: 20rpx;
				margin: 2%;

				.item-text {
					margin-top: 8rpx;
				}
			}

		}
	}
</style>
