<template>
	<view class="fullHeight">
		<headers :showBack="true">
			<view class="headerName">
				卸料平台监测
			</view>
		</headers>
		<view class="menuBox">
			<view class="menuItem" @click="changeMenu(0)" :class="tabIndex==0?'active':''">
				数据总览
			</view>
			<view class="menuItem" @click="changeMenu(1)" :class="tabIndex==1?'active':''">
				数据趋势
			</view>
			<view class="menuItem" @click="changeMenu(2)" :class="tabIndex==2?'active':''">
				报警列表
			</view>
		</view>
		<!-- 数据总览 -->
		<view class="" v-if="tabIndex==0">
				<view class="flex">
					<view class="statistics">
						<view style="font-size: 34rpx;">{{onLineNumber}}</view>
						<view>在线设备数量</view>
					</view>
					<view class="statistics">
						<view style="font-size: 34rpx;">{{devList.length}}</view>
						<view>设备总数量</view>
					</view>
				</view>
			<view class="selectContent">
				<picker mode="selector" :range="devList" @change="changeDev" range-key="devName" :value="devIndex">
					<view class="selectVideoBox">
						<text class="videoName" v-if="devList.length>0">{{devList[devIndex].devName}}</text>
						<uni-icons class="arrow" type="arrowdown" size="15"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="blockBox">		
				<view class="realTimeBox">
					<view class="item">
						<view class="itemInner">
							<view class="num">
								{{realTimeData.loading}} t
							</view>
							<view class="label">
								载重
							</view>
						</view>
					</view>
					<view class="item">
						<view class="itemInner">
							<view class="num">
								{{realTimeData.displacement}} m
							</view>
							<view class="label">
								位移
							</view>
						</view>
					</view>
					<view class="item">
						<view class="itemInner">
							<view class="num">
								{{realTimeData.xDipAngle}} °
							</view>
							<view class="label">
								X倾角
							</view>
						</view>
					</view>
					<view class="item">
						<view class="itemInner">
							<view class="num">
								{{realTimeData.yDipAngle}} °
							</view>
							<view class="label">
								Y倾角
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<view class="historyBox">
				<view class="historyBox_title">历史载重数据</view>
				<view class="historyBox_item flex2" v-if="historyLoadList.length>0" v-for="(item,index) in historyLoadList">
					<view class="historyBox_load">载重 {{item.loading}}t</view>
					<view class="historyBox_time">{{item.time}}</view>
				</view>
				<view class="placeholderBox" v-if="historyLoadList==0">
					<image src="/static/noData.png" mode="" class="noDataImg"></image>
					<view class="text">
						暂无数据
					</view>
				</view>
			</view>
			
		</view>

		<view class="" v-if="tabIndex==1">
			<view class="selectContent">
				<picker mode="selector" :range="devList" @change="changeDev" range-key="devName" :value="devIndex">
					<view class="selectVideoBox">
						<text class="videoName" v-if="devList.length>0">{{devList[devIndex].devName}}</text>
						<uni-icons class="arrow" type="arrowdown" size="15"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="blockBox">
				<view class="chartTitle">
					载重 (t)
				</view>
				<u-charts canvas-id="lineChart1" chartType="line" :opts="lineChartData1" ref="lineChart1" :cWidth="315" :cHeight="200"
				 :legends="false" />
			</view>
			<view class="blockBox">
				<view class="chartTitle">
					位移 (m)
				</view>
				<u-charts canvas-id="lineChart2" chartType="line" :opts="lineChartData2" ref="lineChart2" :cWidth="315" :cHeight="200"
				 :legends="false" />
			</view>
		
		</view>
		<view class="" v-if="tabIndex==2">
			<view class="selectContent selectContent2">
				<picker mode="selector" :range="devList" @change="changeDev" range-key="devName" :value="devIndex">
					<view class="selectVideoBox">
						<text class="videoName" v-if="devList.length>0">{{devList[devIndex].devName}}</text>
						<uni-icons class="arrow" type="arrowdown" size="15"></uni-icons>
					</view>
				</picker>
				<picker mode="date" @change="changeDate" :value="searchDate" style="margin-left:15px">
					<view class="selectVideoBox">
						<text class="videoName">{{searchDate}}</text>
						<uni-icons class="arrow" type="arrowdown" size="15"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="blockBox" v-for="(item,index) in alarmList" :key="index">
				<view class="alarmItem">
					<text class="label">报警类型：</text><text class="value">{{item.alarmType}}</text>
				</view>
				<view class="alarmItem">
					<text class="label">载重：</text><text class="value">{{item.loading}}t</text>
				</view>
				<view class="alarmItem">
					<text class="label">位移：</text><text class="value">{{item.displacement}}m</text>
				</view>
				<view class="alarmItem">
					<text class="label">X倾角：</text><text class="value">{{item.xDipAngle}}°</text>
				</view>
				<view class="alarmItem">
					<text class="label">Y倾角：</text><text class="value">{{item.yDipAngle}}°</text>
				</view>
			</view>
			<view class="placeholderBox" v-if="alarmList.length==0">
				<image src="/static/noData.png" mode="" class="noDataImg"></image>
				<view class="text">
					暂无报警
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uCharts from '@/components/u-charts/component.vue';
	import headers from "../../../components/headers/headers.vue"
	import {
		GetDateStr
	} from "../../../static/js/util.js"
	export default {
		components: {
			uCharts
		},
		data() {
			return {
				projectDetail: '',
				devList: [],
				devIndex: 0,
				realTimeData: {
					loading: '--',
					yDipAngle: '--',
					xDipAngle: '--',
					displacement: '--',
				},
				 onLineNumber:0, 
				alarmList: [],
				currentDevDetail: {},
				searchDate: '',
				lineChartData1: {
					categories: [],
					series: [{
						name: "载重",
						data: []
					}]
				},
				lineChartData2: {
					categories: [],
					series: [{
						name: "位移",
						data: []
					}]
				},
			
				tabIndex: 0,
				onlineDevNum: 0,
				historyLoadList:[], //历史载重数据
			};
		},
		mounted() {
			this.projectDetail = JSON.parse(uni.getStorageSync('projectDetail'))
			this.searchDate = GetDateStr(0, '-')
			this.getEnvironmentDev()
		},
		methods: {

			changeDate(e) {
				this.searchDate = e.target.value
				this.getAlarmData()
			},
			changeMenu(index) {
				this.tabIndex = index;
				this.loadData()
			},
			loadData() {
				if (this.devList.length > 0) {
					switch (this.tabIndex) {
						case 0:
							this.getRealTimeData();
							this.getDustData();
							break;
						case 1:
							this.getDustData()
							break;
						case 2:
							this.getAlarmData()
							break;
					}
				}
			},
			changeDev(e) {
				this.devIndex = e.target.value
				this.currentDevDetail = this.devList[this.devIndex]
				this.loadData()
			},
			//获取数据趋势
			getDustData() {
				var that = this
				this.sendRequest({
					url: "xmgl/dischargingPlatformCurrentData/getTodayDischargingPlatformCurrentDataList",
					data: {
						// projectSn: this.projectDetail.projectSn,
						// searchDate: this.searchDate,
						devSn: this.currentDevDetail.devSn
					},
					method: "POST",
					success(res) {
						var DATA = res.result;
						var xdata = [],
							ydata1 = [],
							ydata2 = [],
							ydata3 = [],
							ydata4 = [];
							that.historyLoadList = [];
						DATA.forEach((element,index) => {
							xdata.push(element.reciveTime.substring(12))
							ydata1.push(element.loading)
							ydata2.push(element.displacement)
							//获取历史记录最新10 条
							if(that.tabIndex==0){
								if(index>DATA.length-11){
									let obj = {loading:element.loading,time:element.reciveTime};
									that.historyLoadList.push(obj)
								}
							}
						})
						
						
						
						
						that.lineChartData1.categories = xdata
						that.lineChartData2.categories = xdata
						// that.lineChartData3.categories=xdata
						// that.lineChartData4.categories=xdata

						that.lineChartData1.series = [{
							name: "载重",
							data: ydata1
						}]
						that.lineChartData1.series = [{
							name: "位移",
							data: ydata2
						}]
					
						if(that.tabIndex==1){
							that.$refs.lineChart1.changeData('lineChart1', that.lineChartData1)
							that.$refs.lineChart2.changeData('lineChart2', that.lineChartData2)
						}
						
						
					}
				})
			},
			//获取报警列表
			getAlarmData() {
				var that = this
				this.sendRequest({
					url: "xmgl/dischargingPlatformAlarm/list",
					data: {
						projectSn: this.projectDetail.projectSn,
						pageNo: 1,
						pageSize: 100,
						startTime: this.searchDate,
						endTime: this.searchDate,
						devSn: this.currentDevDetail.devSn
					},
					method: "POST",
					success(res) {
						that.alarmList = res.result.records
					}
				})
			},
			//获取设备
			getEnvironmentDev() {
				var that = this;
				this.sendRequest({
					url: "xmgl/dischargingPlatformDev/list",
					data: {
						projectSn: that.projectDetail.projectSn,
					},
					method: "POST",
					success(res) {
						that.devList = res.result;
						if (res.result.length > 0) {
							that.currentDevDetail = res.result[0];
							res.result.forEach((item,index)=>{
								if(item.devonline==1){
									that.onLineNumber = that.onLineNumber+1
								}
							})
							that.getRealTimeData()
						}
					}
				})
			},
		
			//获取实时数据
			getRealTimeData() {
				var that = this
				this.sendRequest({
					url: "xmgl/dischargingPlatformCurrentData/getNewestDischargingPlatformCurrentData",
					data: {
						devSn: this.currentDevDetail.devSn
					},
					method: "POST",
					success(res) {
						if (res.result) {
							that.realTimeData = res.result;
						} else {
							that.realTimeData = {
								loading: '--',
								yDipAngle: '--',
								xDipAngle: '--',
								displacement: '--',
							}
						}

					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.flex{
		display: flex;
		align-items: center;
		justify-content: space-around;
	}
	.flex2{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.menuBox {
		display: flex;
		align-items: center;
		height: 36px;
		font-size: 15px;
		background-color: white;
		margin-top: -1px;
		position: relative;
		z-index: 999;
		border-bottom: 1px solid rgba(194, 194, 194, 0.2);

		.menuItem {
			flex: 1;
			text-align: center;

			&.active {
				color: rgba(43, 141, 243, 1);
			}
		}
	}

	.blockBox {
		box-shadow: 0 4px 24px 0px rgba(212, 220, 236, 0.69);
		border-radius: 8px;
		margin: 15px;
		padding: 15px;
	}

	.numContent {
		text-align: center;
	}

	.numBox {
		width: 130px;
		height: 50px;
		border-radius: 8px;
		background-image: linear-gradient(rgba(134, 145, 252, 1), rgba(16, 97, 254, 1));
		font-size: 12px;
		color: white;
		text-align: center;
		display: inline-block;

		.num {
			font-size: 15px;
			margin: 5px 0 0px;

		}
	}

	.numBox1 {
		margin-right: 10px;
	}

	.selectContent {
		text-align: center;
		margin-top: 15px;
	}

	.selectVideoBox {
		border: 1px solid rgba(42, 43, 91, 0.2);
		border-radius: 18px;
		height: 35px;
		font-size: 15px;

		display: inline-block;

		.videoName {
			padding: 0 12px 0 15px;
			// border-right: 1px solid rgba(42, 43, 91, 0.2);
			line-height: 35px;
			height: 35px;
		}

		.arrow {
			padding: 0 12px 0 2px;
		}
	}

	.arrow {
		margin-left: 10px;
	}

	.realTimeBox {
		overflow: hidden;

		.item {
			float: left;
			width: 33.33%;

			.itemInner {
				// box-shadow: 0 4px 24px 0px rgba(212, 220, 236, 0.69);
				border: 1px solid rgba(220, 220, 220, 0.3);
				margin: 3.5px;
				font-size: 12px;
				text-align: center;
				padding: 7px 0;
			}

			.num {
				font-size: 15px;
				color: rgba(43, 141, 243, 1);
			}
		}
	}

	.alarmIcon {
		width: 20px;
		height: 20px;
	}

	.switchIcon {
		width: 50px;
		height: 29px;
	}

	.blockTitle {
		text-align: center;
	}

	.tableBox {
		font-size: 14px;
		width: 100%;

		.tableHead {
			font-weight: bold;
			display: flex;
			border-bottom: 1px solid rgba(42, 43, 91, 0.1);

			.item {
				flex: 1;
				padding: 0 0 10px;
				text-align: center;
			}
		}

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

			.item {
				flex: 1;
				text-align: center;
				justify-content: center;
				padding: 10px 0 0px;
			}
		}
	}

	.chartTitle {
		font-size: 14px;
		font-weight: 500;
		margin-bottom: 15px;
	}

	.selectContent2 {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.alarmItem {
		font-size: 14px;
		line-height: 22px;
		letter-spacing: 2px;
		.label {
			opacity: 0.6;
		}
	}

	.profile_photo {
		width: 30px;
		height: 30px;
	}

	.moudleTitle2 {
		font-size: 10px;
		opacity: 0.69;
		font-weight: 400;
		margin: 15px 0;
		text-align: center;
	}

	.videoBox {
		width: calc(100% - 30px);
		height: 225px;
		margin: 0px 15px;
		border-radius: 10px;
	}
	
	.statistics{
		width: 240rpx;
		text-align: center;
		padding: 10px 0;
		line-height: 22px;
		box-sizing: border-box;
		font-size: 28rpx;
		background-color: #4181FE;
		margin-top: 12px;
		color: #fff;
		border-radius: 5px;
		box-shadow: 0 0 15px rgba(200, 204, 208, 0.5);
	}
	
	.historyBox{
		margin:10px 0;
		padding: 10px 30rpx;
		box-sizing: border-box;
	}
	.historyBox_title{
		box-sizing: border-box;
		color: #999;
		font-size: 28rpx;
		letter-spacing: 2px;
	}	
	.historyBox_item{
		padding: 15px 30rpx;
		box-sizing: border-box;
		box-shadow: 0 0 15px rgba(200, 204, 208, 0.5);
		margin-top: 15px;
		font-size: 30rpx;
		border-radius: 4px;
	}
	.historyBox_time{
		font-size: 26rpx;
		color: #999;
	}
</style>
