<template>
	<view class="main">
		<view class="tabar">
			<view class="title">
				<view>预警与定位</view>
				<view class="positioning">Warning and positioning</view>
			</view>
			<view>
				<image class="earlyWarning" src="../../static/hoem/ic_positioning.png"></image>
			</view>
		</view>
		<view class="bgMian">

			<view class="notify" @click="s_TO_Click">
				<image class="notifyBarImg" src="../../static/hoem/ic_NotifyBar.png"></image>
				<view class="prompt">
					<swiper class="swiper" autoplay>
						<swiper-item v-for="(item, index) in Knowledge" :key="index">
							<view class="" style="display: flex;align-items: center;">
								{{item.toiletId}}公厕{{item.equipmentId}}
								<text v-if="item.notificationStatus == 1">主动下线</text>
								<text v-if="item.notificationStatus == 2">用纸预警</text>
								<text v-if="item.notificationStatus == 3">异常警告</text>
							</view>
						</swiper-item>
					</swiper>
				</view>
			</view>

			<view class="devicesStatus">
				<view class="devicesTitle">
					<view class="icon"> </view>
					<text class="title">设备状态统计</text>
				</view>
				<view>

				</view>
				<view class="Grid">
					<view class="Grid-Item">
						<view class="devicesInfo">
							<view class="number">{{PapergetEquipentstaus.normalCount}}</view>
							<view class="numberTitle">设备总数/台</view>
						</view>
						<view class="view" @click="s_TO_Click">查看</view>
					</view>
					<view class="Grid-Item">
						<view class="devicesInfo">
							<view class="number">{{PapergetEquipentstaus.noexpCount}}</view>
							<view class="numberTitle">设备在线/台</view>
						</view>
						<view class="view" @click="s_TO_Click">查看</view>
					</view>
					<view class="Grid-Item">
						<view class="devicesInfo">
							<view class="number">{{PapergetEquipentstaus.downlineCount}}</view>
							<view class="numberTitle">设备离线/台</view>
						</view>
						<view class="view" @click="s_TO_Click">查看</view>
					</view>
					<view class="Grid-Item">
						<view class="devicesInfo">
							<view class="number">{{PapergetEquipentstaus.errorCount}}</view>
							<view class="numberTitle">设备故障/台</view>
						</view>
						<view class="view" @click="s_TO_Click">查看</view>
					</view>
					<view class="Grid-Item">
						<view class="devicesInfo">
							<view class="number">{{PapergetEquipentstaus.exceptionCount}}</view>
							<view class="numberTitle">设备预警/台</view>
						</view>
						<view class="view" @click="s_TO_Click">查看</view>
					</view>
					<view class="Grid-Item">
						<view class="devicesInfo">
							<view class="number">{{PapergetEquipentstaus.paperoutCount}}</view>
							<view class="numberTitle">设备断纸/台</view>
						</view>
						<view class="view" @click="s_TO_Click">查看</view>
					</view>
				</view>
			</view>
			<view v-show="!show" class="positioningMap" @click="map">
				<view class="devicesTitle">
					<view class="icon"> </view>
					<text class="title">定位</text>
				</view>
				<map @tap="map" style="width: 662rpx;height: 337rpx;margin: 0 20rpx;" :show-location='true' ref="map"
					id="map" :latitude="latitude" :longitude="longitude" :scale="scale">
				</map>
			</view>
			<view class="devicesMaintenance">
				<view class="devicesTitle">
					<view class="icon"> </view>
					<text class="title">设备/公厕状态维护</text>
				</view>

				<view class="maintenanceItem" v-for="item in devicesList" :key="item.id">
					<view class="title">
						<view>{{item.equipmentId}}设备</view>
						<view class="subscript" v-if="item.notificationStatus == 0">设备正常</view>
						<view class="subscript" v-else-if="item.notificationStatus == 1">主动下线</view>
						<view class="subscript" v-else-if="item.notificationStatus == 2">用纸预警</view>
						<view class="subscript" v-else-if="item.notificationStatus == 3">设备异常</view>
						<view class="subscript" v-else-if="item.notificationStatus == 5">设备维护</view>
					</view>
					<view class="inspectionRecordsTime">
						<image class="icTimer" src="../../static/hoem/ic_time.png"></image>
						<text class="inspectime">
							<view class="" v-if="item.notificationStatus == 0">设备正常：{{item.updatedAt}}</view>
							<view class="" v-else-if="item.notificationStatus == 1">主动下线：{{item.updatedAt}}</view>
							<view class="" v-else-if="item.notificationStatus == 2">用纸预警：{{item.updatedAt}}</view>
							<view class="" v-else-if="item.notificationStatus == 3">设备异常：{{item.updatedAt}}</view>
							<view class="" v-else-if="item.notificationStatus == 5">设备维护：{{item.updatedAt}}</view>
						</text>
					</view>
					<u-line class="line" length="90%"></u-line>
					<view class="viewEdit">
						<view></view>
						<view class="viewEditStyle">
							<view class="view" @click="select(item)">查看</view>
							<view @click="editStatus(item)">编辑</view>
						</view>
					</view>
				</view>

			</view>
		</view>

		<u-popup :show="show" :round="10" mode="center" @close="close">
			<view class="upopup">
				<view class="popup">
					<view class="close">
						<view></view>
						<view>
							<image @click="dismiss" class="icClose" src="../../static/hoem/ic_close.png"></image>
						</view>
					</view>
					<view class="title">上传巡查信息</view>
				</view>
				<view style="text-align: center;">
					当前状态：设备异常
				</view>
				<view class="region">
					<view style="display: flex;">
						<view>编辑状态</view>
					</view>
					<view class="editRegion" @click="showOne = true">
						<text>{{acttext?acttext:"选择设备状态"}}</text>
						<image class="ic_botom" src="../../static/hoem/ic_filter.png"></image>
					</view>
				</view>
				<view class="butSubmit">
					<view style="margin-top: 30rpx;" @click="seteditequipmentstatus">
						提交
					</view>
				</view>
			</view>
		</u-popup>
		<u-picker keyName="text" :show="showOne" :columns="oneList" :closeOnClickOverlay="true" @close="showOne = false"
			@cancel="showOne = false" @confirm="onOne"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showOne: false,
				oneList: [
					[{
							text: "设备正常",
							id: 0
						},
						{
							text: "主动下线",
							id: 1
						}, {
							text: "用纸预警",
							id: 2
						}, {
							text: "设备异常",
							id: 3
						}, {
							text: "设备维护",
							id: 5
						},
					]
				],
				latitude: "", //纬度
				longitude: "", //经度
				scale: 16, //缩放级别
				show: false,
				devicesList: [],
				acttext: "",
				actstatus: "",
				actdata: {},
				Knowledge: [],
				PapergetEquipentstaus: [],
			}
		},
		onLoad() {
			
			this.getCheckList();
			this.allequipmentstatus();
			this.getEquipentpaper();
		},
		onShow() {
			let that = this;
			uni.getLocation({
			  type: 'gcj02',
			  // type: 'wgs84',
			  success: function(res) {
			    const latitude = res.latitude // 纬度
			    const longitude = res.longitude // 经度
				that.latitude = latitude;
				that.longitude = longitude;
			  },
			  fail: function(err) {
			    console.log('获取经纬度失败：', err)
			  }
			})
		},
		methods: {
			s_TO_Click() {
				uni.navigateTo({
					url: '/pages/index/infolist'
				})
			},
			// 耗纸统计
			getEquipentpaper() {
				const params = {
					url: '/sun/mqtt/getEquipentstaus',
					data: {},
				}
				this.$request(params).then(res => {
					this.PapergetEquipentstaus = res.data;
				})
			},
			allequipmentstatus() {
				const params = {
					url: '/sun/mqtt/allequipmentstatus',
					data: {},
				}
				this.$request(params).then(res => {
					let arr = [];
					res.rows.forEach((item, index) => {
						if (item.notificationStatus == 1 || item.notificationStatus == 2 || item
							.notificationStatus == 3) {
							arr.push(item)
						}
					});
					this.Knowledge = arr;
				})
			},
			onOne(e) {
				this.acttext = e.value[0].text;
				this.actstatus = e.value[0].id;
				this.showOne = false;
			},
			//修改设备状态
			seteditequipmentstatus() {
				console.log(this.actdata.id)
				console.log(this.acttext)
				console.log(this.actstatus)
				const params = {
					url: '/sun/mqtt/editequipmentstatus',
					data: {
						"id": this.actdata.id, //唯一标识
						"notificationStatus": this.actstatus
					},
				}
				this.$request(params).then(res => {
					this.$u.toast('设置成功')
					this.getCheckList();
					this.show = false;
				})
			},
			//设备/公厕状态维护
			getCheckList() {
				const params = {
					url: '/sun/mqtt/allequipmentstatus',
					data: {},
				}
				this.$request(params).then(res => {
					this.devicesList = res.rows;
				})
			},
			select(item) {
				uni.navigateTo({
					url: '/pages/equipmentStatus/equipmentStatus?id=' + item.id
				})
			},
			map() {
				uni.navigateTo({
					url: '/pages/map/map'
				})


			},
			editStatus(item) {
				this.show = true;
				this.actdata = item;
			},
			close() {
				this.show = false

			},
			dismiss() {
				this.show = false
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .uni-swiper {
		width: 100%;
		height: 88rpx;
	}

	.swiper {
		width: 100%;
		height: 88rpx;
	}

	.main {
		.upopup {
			height: 609rpx;

			.close {
				display: flex;
				justify-content: space-between;

				.icClose {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.butSubmit {
				height: 80rpx;
				background: linear-gradient(90deg, #FCD4D2 0%, #FB262A 100%);
				border-radius: 40rpx 40rpx 40rpx 40rpx;
				text-align: center;
				color: white;
				width: 302rpx;
				margin: auto;
				line-height: 80rpx;
			}


			.popup {
				padding: 24rpx;
				width: 634rpx;
				background: linear-gradient(180deg, #FDEDEE 0%, #FFFFFF 100%);
				border-radius: 20rpx 20rpx 20rpx 20rpx;

				.title {
					text-align: center;
					font-weight: 800;
					font-size: 30rpx;
					color: #333333;

				}
			}

			.region {
				padding: 24rpx;

				.prompt {
					padding-top: 10rpx;
					padding-left: 10rpx;
					font-size: 24rpx;
					color: #999999;
				}

				.editRegion {
					display: flex;
					margin-top: 10rpx;
					padding: 20rpx;
					align-items: center;
					font-size: 24rpx;
					color: #999999;
					background: #F8F4F4;
					border-radius: 4rpx 4rpx 4rpx 4rpx;
					justify-content: space-between;

					.ic_botom {
						width: 18rpx;
						height: 18rpx;
					}
				}

			}
		}

		.bgMian {
			padding: 24rpx;
			background-color: #F8F4F4;

			.line {
				padding: 20rpx;
				padding-right: 15rpx;
			}

			.devicesMaintenance {
				padding: 20rpx;
				margin-top: 20rpx;
				background: linear-gradient(180deg, #FDEDEE 0%, #FFFFFF 100%);
				border-radius: 14rpx 14rpx 14rpx 14rpx;
				border: 2rpx solid #FFFFFF;

				.maintenanceItem {
					background: linear-gradient(90deg, #FDEEEF 0%, #FFFFFF 100%);
					box-shadow: 0rpx 0rpx 6rpx 1rpx #FFC6C7;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					position: relative;
					margin-top: 20rpx;
					padding: 20rpx;

					.viewEdit {
						display: flex;
						justify-content: space-between;

						.viewEditStyle {
							display: flex;
							color: #FB262A;
							font-size: 24rpx;
							font-weight: 800;

							.view {
								padding-right: 77rpx;
							}
						}
					}

					.title {
						display: flex;
						justify-content: space-between;
						font-size: 28rpx;
						font-weight: 800;

						.subscript {
							width: 120rpx;
							height: 50rpx;
							background: #FDF6ED;
							border-radius: 0rpx 8rpx 0rpx 0rpx;
							font-weight: 400;
							font-size: 24rpx;
							color: #FFA42E;
							text-align: center;
							position: absolute;
							top: 0%;
							right: 0%;
						}
					}

					.inspectionRecordsTime {
						display: flex;
						align-items: center;
						margin-top: 20px;

						.inspectime {
							padding-left: 10rpx;
							color: #666666;
							font-size: 24rpx;
						}

						.icTimer {

							width: 32rpx;
							height: 32rpx;
						}
					}
				}
			}

			.devicesTitle {
				padding: 24rpx;
				margin-top: 20rpx;
				display: flex;

				.title {
					font-weight: 800;
					font-size: 28rpx;
					padding-left: 10rpx;
				}

				.icon {
					width: 10rpx;
					height: 37rpx;
					background: #FB262A;
					border-radius: 3rpx 3rpx 3rpx 3rpx;
				}
			}

			.positioningMap {
				width: 702rpx;
				height: 441rpx;
				background: linear-gradient(180deg, #FDEDEE 0%, #FFFFFF 100%);
				border-radius: 14rpx 14rpx 14rpx 14rpx;
				border: 2rpx solid #FFFFFF;
				padding-bottom: 27rpx;
			}

			.devicesStatus {
				background: linear-gradient(180deg, #FDEDEE 0%, #FFFFFF 100%);
				border-radius: 14rpx 14rpx 14rpx 14rpx;
				border: 2rpx solid #FFFFFF;
				margin-top: 10rpx;
				padding-bottom: 10rpx;

				.Grid {
					display: flex;
					flex-wrap: wrap;
					padding: 10rpx;

					.Grid-Item {
						padding: 22rpx;
						width: 320rpx;
						height: 106rpx;
						display: flex;
						margin-top: 15rpx;
						justify-content: space-between;
						border: 1rpx solid #ccc;
						box-sizing: border-box;

						.devicesInfo {
							display: block;
						}

						.view {
							width: 84rpx;
							height: 41rpx;
							background: #FFFFFF;
							font-size: 24rpx;
							text-align: center;
							justify-self: center;
							align-items: center;
							border-radius: 8rpx 8rpx 8rpx 8rpx;
						}

						.number {
							font-size: 26rpx;
							font-weight: 800;
						}

						.numberTitle {

							font-size: 22rpx;
							color: #999999;
						}

						&:nth-child(odd) {
							background: linear-gradient(180deg, #D2E2FC 0%, #EBEFF7 100%);
							box-shadow: inset 0rpx 0rpx 6rpx 1rpx #8AB7FF;
							border-radius: 6rpx 6rpx 6rpx 6rpx;
						}

						&:nth-child(even) {
							background: linear-gradient(180deg, #FCE4D2 0%, #F7F2EB 100%);
							box-shadow: inset 0rpx 0rpx 6rpx 1rpx #FFAF8C;
							border-radius: 6rpx 6rpx 6rpx 6rpx;
							margin-left: 20rpx;
						}

					}

				}

				.devicesInfo {}


			}

			.notify {
				display: flex;
				margin-top: -100rpx;
				padding: 24rpx;
				background: #FDEDEE;
				box-shadow: inset 0rpx 0rpx 10rpx 1rpx #FF8B85;
				border-radius: 14rpx 14rpx 14rpx 14rpx;
				border: 2rpx solid #FFFFFF;

				.notifyBarImg {
					width: 77rpx;
					height: 77rpx;
				}

				.prompt {
					padding-left: 20rpx;
					// font-size: 24rpx;
					width: 500rpx;
					line-height: 88rpx;
				}
			}

		}

		.tabar {
			display: flex;
			justify-content: space-between;
			text-align: center;
			padding: 55rpx;
			background: linear-gradient(91deg, #FCD4D2 0%, #FDEDEE 100%);
			border-radius: 0rpx 0rpx 0rpx 0rpx;

			.title {

				color: #333333;
				font-size: 36rpx;
				font-weight: 800;
			}

			.positioning {
				color: #BBA4A4;
				font-size: 17rpx;
			}

			.earlyWarning {
				width: 294rpx;
				height: 176rpx;
			}
		}

	}
</style>