<template>
	<view>
		<view class="navbar-c">
			<view class="back" @click="toStatistics">
				<u-icon name="arrow-left" color="#101010" size="36"></u-icon>
			</view>
			<view class="title">
				设备数据监测<image class="img" src="@/assets/img/refresh-line.svg"></image>
			</view>
			<view class="right" @click="toDataMonitoringMap" v-if="false">
				<image class="img" src="@/assets/img/riLine-road-map-line.svg" mode=""></image>地图
			</view>
		</view>
		<view class="dropdown">
			<view class="dropdown-item" @click="show1=true">
				{{device}} <u-icon name="arrow-down" color="#999999"></u-icon>
			</view>
			<view class="dropdown-item" @click="show2=true">
				{{state}} <u-icon name="arrow-down" color="#999999"></u-icon>
			</view>
		</view>
		
		<u-select v-model="show1" mode="single-column" :list="selectDeviceList" @confirm="deviceChange"></u-select>
		
		<u-select v-model="show2" mode="single-column" :list="stateList" @confirm="stateChange"></u-select>

		<view class="main">
			<view class="item" v-for="(item,index) in deviceList" :key="index" @click="toElectronicMonitoring(item)">
				<view class="title">
					<view class="icon-box">
						<image class="img" src="@/assets/img/transformer1.svg" mode=""></image>
					</view>
					<view class="equipment">
						<view class="name1">
						
							{{replaceLastTwoWords(item.name)}}
						</view>
						<view class="name2">
							{{item.installationAddressSimple}}
						</view>
					</view>
					<!-- 状态 -->
					<view class="state">
						<!-- <view class="state1" v-if="item.deviceStatus == '1'">
							<view class="icon"></view>
							<view class="text">
								正常运行
							</view>
						</view>
						<view class="state1" v-else>
							<view class="icon icon2"></view>
							<view class="text">
								设备异常
							</view>
						</view> -->
						<view class="state1" v-if="item.temperatureStatus == '1'">
							<view class="icon"></view>
							<view class="text">
								温度正常
							</view>
						</view>
						<view class="state1" v-else>
							<view class="icon icon2"></view>
							<view class="text">
								温度异常
							</view>
						</view>
						<view class="state1" v-if="item.smokeStatus == '1'">
							<view class="icon"></view>
							<view class="text">
								烟感正常
							</view>
						</view>
						<view class="state1" v-else>
							<view class="icon icon2"></view>
							<view class="text">
								烟感异常
							</view>
						</view>
						<view class="state1" v-if="item.online">
							<view class="icon icon4"></view>
							<view class="text">
								设备在线
							</view>
						</view>
						<view class="state1" v-else>
							<view class="icon icon3"></view>
							<view class="text">
								设备离线
							</view>
						</view>
					</view>
					<!-- 箭头 -->
						<view class="more">
							<u-icon name="arrow-right" color="#acacac"  size="24"></u-icon>
						</view>
				</view>
				<!-- 设备信息 -->
				<view class="infos" v-if="item.remoteMonitorRecord != null">
					<view class="infos-item">
						<view class="item-title">
							当前电流：
						</view>
						<view class="item-value">
							{{item.remoteMonitorRecord.dcaTotal.toFixed(2)}}A
						</view>
					</view>
					<view class="infos-item">
						<view class="item-title">
							当前电压：
						</view>
						<view class="item-value">
							{{item.remoteMonitorRecord.dcvTop.toFixed(2)}}V
						</view>
					</view>
					<view class="infos-item">
						<view class="item-title">
							当前温度：
						</view>
						<view class="item-value">
							{{item.remoteMonitorRecord.temperatureTop}}°C
						</view>
					</view>
					<view class="infos-item">
						<view class="item-title">
							当前功率：
						</view>
						<view class="item-value">
							{{item.remoteMonitorRecord.totalPower.toFixed(2)}}kW
						</view>
					</view>
					<view class="infos-item">
						<view class="item-title">
							当前功率因数：
						</view>
						<view class="item-value">
							{{item.remoteMonitorRecord.dcfTop.toFixed(2)}}
						</view>
					</view>
					<view class="infos-item">
						<view class="item-title">
							平均功率因数：
						</view>
						<view class="item-value">
							{{item.remoteMonitorRecord.averPowerFactor.toFixed(2)}}
						</view>
					</view>
				</view>
			</view>
			
			<u-divider :isnone="deviceList.length==0" nonetext="暂无数据" border-color="#CFD2D5">已经到底了</u-divider>
		</view>
	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/index.js'

	export default {
		data() {
			return {
				show2: false, // 状态选择
				state: '全部状态',
				stateList: [
					{label: '全部状态', value: ''},
					{label: '设备正常', value: '0'},
					{label: '设备离线', value: '1'},
					{label: '温度异常', value: '2'},
					{label: '烟感异常', value: '3'},
					{label: '设备异常', value: '4'}
				],
				show1: false, // 设备选择
				device: '全部设备',
				selectDeviceList: [],
				deviceList: [],
				companyId: '',
				status: '',
				pageIndex: 1,
				recordsTotal: 0,
				value1: 0,
				value2: 0,
				options1: [{
						label: '荆鹏集团',
						value: 1,
					},
					{
						label: '青少年宫',
						value: 2,
					},
					{
						label: '荆州院子',
						value: 3,
					},

				],
				options2: [{
						label: '设备离线',
						value: 1,
					},
					{
						label: '温度异常',
						value: 2,
					},
					{
						label: '电压异常',
						value: 3,
					},
					{
						label: '功率因数异常',
						value: 4,
					},
				],
			}
		},
		onReady() {
			this.getList();
			this.getAlarmConfiguration();
		},
		onReachBottom() {
			if (this.deviceList.length < this.recordsTotal) {
				this.myLoadmore();
			}
		},
		methods: {
			// 异常查询条件
			getAlarmConfiguration() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.alarmConfiguration().then((response) => {
					uni.hideLoading();
					var list2 = [];
					
					if(response.data.companyInfoList && response.data.companyInfoList.length != 0) {
						list2 = response.data.companyInfoList.map(item => {
							return {
								label: item.name,
								value: item.id
							}
						});
					}
					list2.unshift({
						value: '',
						label: '全部设备'
					});
					
					this.selectDeviceList = list2;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			// 状态查询
			stateChange(e) {
				console.log(e);
				this.status = e[0].value;
				this.state = e[0].label;
				
				this.getList(true);
			},
			// 设备查询
			deviceChange(e) {
				console.log(e);
				this.companyId = e[0].value;
				this.device = e[0].label;
				
				this.getList(true);
			},
			myLoadmore() {
				this.pageIndex += 1;
				this.getList();
			},
			getList(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.deviceList = [];
					this.pageIndex = 1;
				}
				var data = {
					pageIndex: this.pageIndex,
					pageSize: 5,
					companyId: this.companyId,
					status: this.status
				};
				API.homePageDeviceData(data).then((res) => {
					uni.hideLoading()
					this.deviceList = [
						...this.deviceList,
						...res.data.data
					];
					this.recordsTotal = res.data.recordsTotal;

				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			toStatistics() {
				uni.navigateBack()
			},
			toDataMonitoringMap() {
				uni.navigateTo({
					url: '/pages/equipmentDataMonitoring/dataMonitoring-map'
				})
			},
			toElectronicMonitoring(item) {
				uni.navigateTo({
					url: '/pages/equipmentDataMonitoring/electronicMonitoring?id=' + item.id + '&name=' + item.name
						+ '&companyId=' + item.companyId
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	
	.dropdown {
		background-color: #fff;
		position: sticky;
		top: 88rpx;
		z-index: 999;
		padding: 18rpx 46rpx;
		display: flex;
		border-bottom: 1px solid rgba(245, 245, 245, 1);
		.dropdown-item{
			width: 50%;
			text-align: center;
			height: 60rpx;
			line-height: 60rpx;
		}
	}

	.back {
		z-index: 999;
		width: 200rpx;
	}

	.slot {
		display: flex;
		align-items: center;

		.img {
			width: 32rpx;
			height: 32rpx;
			margin-right: 4rpx;
		}
	}

	.dropdown {
		background-color: #fff;
		position: sticky;
		top: 87rpx;
		z-index: 999;
	}

	.main {
		background-color: #fff;
		

		.item {
			border-bottom: 1px solid rgba(245, 245, 245, 1);
			padding: 32rpx 32rpx 16rpx 32rpx;

			.title {
				display: flex;
				align-items: flex-start;

				.icon-box {
					width: 72rpx;
					height: 72rpx;
					border-radius: 4px;
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: rgba(219, 234, 255, 1);

					.img {
						width: 48rpx;
						height: 48rpx;
					}
				}

				.equipment {
					margin-left: 16rpx;
					width: 35%;

					.name1 {
						color: rgba(51, 51, 51, 1);
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.name2 {
						color: rgba(119, 119, 119, 1);
						font-size: 24rpx;
						margin-top: 4rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}

				// 状态
				.state {
					display: flex;
					align-items: center;
					flex-wrap: wrap;
					justify-content: flex-end;
					margin-left: auto;
					flex: 1;

					.state1 {
						display: flex;
						align-items: center;
						margin-left: 24rpx;

						.icon {
							width: 16rpx;
							height: 16rpx;
							border-radius: 99px;
							background-color: rgba(22, 119, 255, 1);
							margin-right: 8rpx;
						}

						.icon2 {
							background-color: #FF4F3F;
						}
						
						.icon3 {
							background-color: #C2C2C2;
						}
						.icon4{
							background-color: rgba(0,185,98,1);
						}
					}
				}
				.more{
					margin-left: 8rpx;
				}
			}

			// 设备信息
			.infos {
				margin-top: 32rpx;
				padding-left: 88rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex-wrap: wrap;
				font-size: 24rpx;

				.infos-item {
					width: 48%;
					display: flex;
					margin-bottom: 16rpx;
					color: rgba(51,51,51,1);
					font-size: 24rpx;
                .item-value{
					font-weight: bold;
				}
					.warning {
						color: rgba(255, 61, 0, 1);
					}
				}
			}
		}
	}
</style>