<template>
	<view class="cluster-warp">


		<template>
			<view class="all-con-item-box" style="margin-top: 0!important;">
				<Section :title="$t('homePage.device.clusterTotalVol')">
				</Section>
				<view class="chart-box" style="position: relative;">
					<zero-loading v-if="chartLoading" position="absolute"></zero-loading>
					<charts v-else :id="'pcsChart'" :options="curve_option"></charts>

				</view>
			</view>

			<view class="all-con-item-box">
				<Section :title="$t('homePage.device.runData')" />
				<!-- <u-section title="电站数据" :right="false" line-color="#4C9CE7" color="#2A2A2A"></u-section> -->
				<zero-loading v-if="totalLoading" position="absolute"></zero-loading>
				<view v-else class="group-box">
					<view class="group-item "
						@click="showHistory($t('homePage.device.cumCharge'), 'cTotalChargePower', 'bms','kWh','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/rcdl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{totalPanelData.cTotalChargePower | kwhFormat}}

							</view>
							<view class="item-title">
								{{$t('homePage.device.cumCharge')}}({{ totalPanelData.cTotalChargePower | kwhUnitFormat }})
							</view>
						</view>
					</view>


					<view class="group-item "
						@click="showHistory($t('homePage.device.cumDischarge'), 'cTotalDisChargePower', 'bms','kWh','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/zcdl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{ totalPanelData.cTotalDisChargePower | kwhFormat}}

							</view>
							<view class="item-title">
								{{$t('homePage.device.cumDischarge'),}}({{ totalPanelData.cTotalDisChargePower | kwhUnitFormat }})
							</view>
						</view>
					</view>

					<view class="group-item" @click="showHistory($t('homePage.device.avgTem'), 'cageT', 'bms','℃','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/zjrl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{totalPanelData.cageT | isNull}}

							</view>
							<view class="item-title">{{$t('homePage.device.avgTem')}}(℃)</view>
						</view>
					</view>
					<view class="group-item  "
						@click="showHistory($t('homePage.device.avgVol'), 'csingleAgeV', 'bms','V','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/dqgl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{ totalPanelData.csingleAgeV | isNull }}

							</view>
							<view class="item-title">{{$t('homePage.device.avgVol')}}(V)</view>
						</view>
					</view>


					<view class="group-item " @click="showHistory('SOC', 'clusterSOC', 'bms','%','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/dqgl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{ totalPanelData.clusterSOC | isNull}}

							</view>
							<view class="item-title">SOC(%)</view>
						</view>
					</view>


					<view class="group-item" @click="showHistory('SOH', 'clusterSOH', 'bms','%','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/dqgl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{ totalPanelData.clusterSOH | isNull}}

							</view>
							<view class="item-title">SOH(%)</view>
						</view>
					</view>

					<view class="group-item "
						@click="showHistory($t('homePage.device.maxCellVol'), 'csingleMaxVolData', 'bms','V','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/zjrl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{panelData.csingleMaxVolData | isNull}}

							</view>
							<view class="item-title">{{$t('homePage.device.maxCellVol')}}(V)</view>
						</view>
					</view>

					<view class="group-item"
						@click="showHistory($t('homePage.device.minCellVol'), 'csingleMinVolData', 'bms','V','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/zjrl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{panelData.csingleMinVolData | isNull}}

							</view>
							<view class="item-title">{{$t('homePage.device.minCellVol')}}(V)</view>
						</view>
					</view>
					<view class="group-item "
						@click="showHistory($t('homePage.device.maxCellTem'), 'csingleMaxTemData', 'bms','℃','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/zjrl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{panelData.csingleMaxTemData | isNull}}</view>
							<view class="item-title">{{$t('homePage.device.maxCellTem')}}(℃)</view>
						</view>
					</view>

					<view class="group-item"
						@click="showHistory($t('homePage.device.minCellTem'), 'csingleMinTemData', 'bms','℃','')">
						<view class="history-icon">
							<image src="/static/aidex/images/history-icon.png"></image>
						</view>
						<view class="item-icon">
							<image src="/static/aidex/images/zjrl.png"></image>
						</view>
						<view class="item-con">
							<view class="item-num">{{panelData.csingleMinTemData | isNull}}

							</view>
							<view class="item-title">{{$t('homePage.device.minCellTem')}}(℃)</view>
						</view>
					</view>
				</view>
			</view>

			<view class="all-con-item-box">
				<Section :title="$t('homePage.device.runData')" />
				<zero-loading v-if="runLoading" position="absolute"></zero-loading>
				<view v-else class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory($t('homePage.device.maxvolDiff'), 'csinglePressureDifference', 'bms','V','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								{{$t('homePage.device.maxvolDiff')}}
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{Number(panelData.csingleMaxVolData - panelData.csingleMinVolData).toFixed(3) | isNull}}V
							</view>
						</view>
						<view class="bottom-value">
							{{$t('homePage.device.avgVol')}}：{{totalPanelData.csingleAgeV | isNull}}V
						</view>
					</view>

				</view>

				<view class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory($t('homePage.device.avgTem'), 'csingleTDifference', 'bms','%','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								{{$t('homePage.device.maxTemDiff')}}
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{Number(panelData.csingleMaxTemData -panelData.csingleMinTemData ).toFixed(3) | isNull}}℃
							</view>
						</view>
						<view class="bottom-value">
							{{$t('homePage.device.avgTem')}}：{{totalPanelData.cageT | isNull}}℃
						</view>
					</view>

				</view>

				<view class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory($t('homePage.device.maxCellVol'), 'csingleMaxVolData', 'bms','V','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								{{$t('homePage.device.maxCellVol')}}
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{panelData.csingleMaxVolData | isNull}}V
							</view>
						</view>
						<view class="bottom-value">


							<view>
								{{$t('homePage.device.batteryGroupNum')}}：{{panelData.csingleMaxVolBMUNum | isNull}}
							</view>

							<view>
								{{$t('homePage.device.cellPostion')}}：{{panelData.csingleMaxVolBoxNum | isNull}}
							</view>

						</view>
					</view>

				</view>


				<view class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory($t('homePage.device.minCellVol'), 'csingleMinVolData', 'bms','V','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								{{$t('homePage.device.minCellVol')}}
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{panelData.csingleMinVolData | isNull}}V
							</view>
						</view>
						<view class="bottom-value">


							<view>
								{{$t('homePage.device.batteryGroupNum')}}：{{panelData.csingleMinVolBMUNum | isNull}}
							</view>

							<view>
								{{$t('homePage.device.cellPostion')}}：{{panelData.csingleMinVolBoxNum | isNull}}
							</view>

						</view>
					</view>

				</view>

				<view class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory($t('homePage.device.maxCellTem'), 'csingleMaxTemData', 'bms','℃','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								{{$t('homePage.device.maxCellTem')}}
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{panelData.csingleMaxTemData | isNull}}℃
							</view>
						</view>
						<view class="bottom-value">


							<view>
								{{ $t('homePage.device.batteryGroupNum') }}：{{panelData.csingleMaxTemBMUNum | isNull}}
							</view>

							<view>
								{{ $t('homePage.device.cellPostion') }}：{{panelData.csingleMaxTemBoxNum | isNull}}
							</view>

						</view>
					</view>

				</view>

				<view class="group-box padding-top-30 "
					@click="showHistory($t('homePage.device.minCellTem'), 'csingleMinTemData', 'bms','℃','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								{{$t('homePage.device.minCellTem')}}
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{panelData.csingleMinTemData | isNull}}℃
							</view>
						</view>
						<view class="bottom-value">
							<view>
								{{ $t('homePage.device.batteryGroupNum') }}：{{panelData.csingleMinTemBMUNum | isNull}}
							</view>

							<view>
								{{ $t('homePage.device.cellPostion') }}：{{panelData.csingleMinTemBoxNum | isNull}}
							</view>

						</view>
					</view>

				</view>
			</view>

		</template>
		<historyModal :is-show.sync="histroyShow" :title="chartTitle" :params="hisParams" />


	</view>
</template>

<script>
	import Section from '@/components/section/index.vue'
	import charts from "@/components/charts/index";
	import historyModal from '@/components/history-modal/index.vue'

	export default {
		data() {
			return {

				histroyShow: false,
				runLoading: false,
				totalLoading: false,
				chartTitle: null,

				hisParams: {

				},
				curve_option: {},
				srcId: null,
				stationId: null,
				panelData: {},
				totalPanelData: {},
				chartLoading: false,


			}
		},
		components: {
			Section,
			charts,
			historyModal
		},
		computed: {
			currentStation() {
				return this.vuex_currentStation
			}
		},
		watch: {
			currentStation: {
				handler(val) {
					this.stationId = val.id;
				},
				deep: true,
				immediate: true
			},
		},
		methods: {
			// 历史曲线
			showHistory(name, modelCol, modelType, unit) {
				this.hisParams = {
					modelCol: modelCol,
					modelType: modelType,
					unit: unit
				}
				this.chartTitle = name
				this.histroyShow = true
			},

			getData(srcId) {
				this.srcId = srcId
				this.loading = true
				const api = [
					this.getSocChart(),
					this.getTotalData(),
					this.getTotalPanelData()
				]
				Promise.all(api).then(result => {
					this.err = result
					this.loading = false
				})

			},
			getSocChart() {
				this.chartLoading = true
				const self = this
				return new Promise((resolve, reject) => {
					self.$u.api.homePageData
						.GetSocCurve({
							colName: ["clusterVol", "clusterSOC"],
							stationId: this.stationId,
							type: 'day',
							srcId: this.srcId
						})
						.then((res) => {
							self.initChargeChart(res.data);

							resolve(res);
						})
						.finally(() => {
							this.chartLoading = false
						})
				});
			},
			getTotalPanelData() {
				this.totalLoading = true
				const self = this
				return new Promise((resolve, reject) => {
					self.$u.api.deviceList
						.GetNewValue({
							colList: ["cTotalChargePower", "cTotalDisChargePower",
								'cInsulationPositiveResistance', 'cInsulationNegativeResistance',
								'clusterSOC', 'clusterSOH', 'csinglePressureDifference', 'csingleAgeV',
								'csingleTDifference', 'cageT'
							],
							stationId: this.stationId,
							srcId: this.srcId
						})
						.then((res) => {
							this.totalPanelData = {
								cTotalChargePower: res.data.cTotalChargePower?.value ? res.data
									.cTotalChargePower?.value : '',
								cTotalDisChargePower: res.data.cTotalDisChargePower?.value ? res.data
									.cTotalDisChargePower?.value : '0',
								cInsulationPositiveResistance: res.data.cInsulationPositiveResistance
									?.value ? res.data.cInsulationPositiveResistance
									?.value : '',
								cInsulationNegativeResistance: res.data.cInsulationNegativeResistance
									?.value ? res.data.cInsulationNegativeResistance
									?.value : '',
								clusterSOC: res.data.clusterSOC?.value ? res.data.clusterSOC?.value : '',
								clusterSOH: res.data.clusterSOH?.value ? res.data.clusterSOH?.value : '',
								csinglePressureDifference: res.data.csinglePressureDifference?.value ? res
									.data.csinglePressureDifference?.value : '',
								csingleAgeV: res.data.csingleAgeV?.value ? res.data.csingleAgeV?.value :
									'',
								csingleTDifference: res.data.csingleTDifference?.value ? res.data
									.csingleTDifference?.value : '',
								cageT: res.data.cageT?.value ? res.data.cageT?.value : '',
							}
							resolve(res);
						})
						.finally(() => {
							this.totalLoading = false
						})
				});
			},
			getTotalData() {
				this.runLoading = true
				const self = this
				return new Promise((resolve, reject) => {
					self.$u.api.deviceList
						.GetNewValue({
							colList: ['csingleMaxVolData', 'csingleMinVolData',
								'csingleMaxTemData', 'csingleMinTemData',
								'csingleMaxVolBMUNum', 'csingleMaxVolBoxNum', 'csingleMinVolBMUNum',
								'csingleMinVolBoxNum', 'csingleMaxTemBMUNum', 'csingleMaxTemBoxNum',
								'csingleMinTemBMUNum', 'csingleMinTemBoxNum'
							],
							stationId: this.stationId,
							srcId: this.srcId
						})
						.then((res) => {
							this.panelData = {
								csingleMaxVolData: res.data.csingleMaxVolData?.value,
								csingleMinVolData: res.data.csingleMinVolData?.value,
								csingleMaxTemData: res.data.csingleMaxTemData?.value,
								csingleMinTemData: res.data.csingleMinTemData?.value,
								csingleMaxVolBMUNum: res.data.csingleMaxVolBMUNum?.value,
								csingleMaxVolBoxNum: res.data.csingleMaxVolBoxNum?.value,
								csingleMinVolBMUNum: res.data.csingleMinVolBMUNum?.value,
								csingleMinVolBoxNum: res.data.csingleMinVolBoxNum?.value,
								csingleMaxTemBMUNum: res.data.csingleMaxTemBMUNum?.value,
								csingleMaxTemBoxNum: res.data.csingleMaxTemBoxNum?.value,
								csingleMinTemBMUNum: res.data.csingleMinTemBMUNum?.value,
								csingleMinTemBoxNum: res.data.csingleMinTemBoxNum?.value,
							}
							resolve(res);
						})
						.finally(() => {
							this.runLoading = false
						})
				});
			},
			initChargeChart(val) {

				const gonglv = []
				const soc = []
				const xAxis = []
				val[0].staticCurveList.forEach(v => {
					xAxis.push(v.date)
					gonglv.push(v.digital)
				})
				val[1].staticCurveList.forEach(v => {
					soc.push(v.digital)
				})
				this.curve_option = {
					animationDuration: 500,
					animationEasing: "cubicInOut",
					color: ['#009458', '#BFE49F'],
					tooltip: {
						trigger: "axis",
						textStyle: {
							textShadowBlur: 10, // 重点
							textShadowColor: 'transparent', // 重点
						},
						axisPointer: {},
						confine: true,
						position: function(point, params, dom, rect, size) {
							// 鼠标坐标和提示框位置的参考坐标系是：以外层div的左上角那一点为原点，x轴向右，y轴向下
							// 提示框位置
							var x = 0; // x坐标位置
							var y = 0; // y坐标位置

							// 当前鼠标位置
							var pointX = point[0];
							var pointY = point[1];

							// 外层div大小
							// var viewWidth = size.viewSize[0];
							// var viewHeight = size.viewSize[1];

							// 提示框大小
							var boxWidth = size.contentSize[0];
							var boxHeight = size.contentSize[1];

							// boxWidth > pointX 说明鼠标左边放不下提示框
							if (boxWidth > pointX) {
								x = 5; // 自己定个x坐标值，以防出屏
								y -= 15; // 防止点被覆盖住，可根据情况自行调节
							} else {
								// 左边放的下
								x = pointX - boxWidth - 15;
							}

							// boxHeight > pointY 说明鼠标上边放不下提示框
							if (boxHeight + 20 > pointY) {
								y = pointY + 15;
							} else if (boxHeight > pointY) {
								y = 5;
							} else {
								// 上边放得下
								y += pointY - boxHeight;
							}
							return [x, y];
						},
					},
					grid: {
						top: '15%',
						left: '5%',
						right: '3%',
						bottom: '5%',
						containLabel: true
					},
					xAxis: {
						type: 'category',
						data: xAxis,
						splitLine: {
							show: false
						},
						axisLabel: {
							show: true,
							color: "#A3A3A3",
							formatter: function(params) {
								let newParamsName = '';
								const paramsNameNumber = params.length; // 文字总长度
								const provideNumber = 11; //一行显示几个字
								const rowNumber = Math.ceil(paramsNameNumber / provideNumber);
								if (paramsNameNumber > provideNumber) {
									for (let p = 0; p < rowNumber; p++) {
										const start = p * provideNumber;
										const end = start + provideNumber;
										const tempStr = p === rowNumber - 1 ? params.substring(start,
											paramsNameNumber) : params.substring(start, end) + '\n';
										newParamsName += tempStr;
									}
								} else {
									newParamsName = params;
								}
								return newParamsName;
							},
						},

					},
					yAxis: [

						{
							type: 'value',
							name:  this.$t('homePage.device.vol') + '(V)',
							axisLabel: {
								color: '#A3A3A3'
							},
							nameTextStyle: {
								color: '#A3A3A3'
							}
						},
						{
							type: 'value',
							name: 'SOC(%)',
							axisLabel: {
								color: '#A3A3A3'
							},
							nameTextStyle: {
								color: '#A3A3A3'
							}
						}
					],
					dataZoom: [{
							type: 'inside',
							start: 0,
							end: 100
						},
						{
							start: 0,
							height: 20,
							bottom: 10,
							end: 100
						}
					],
					series: [{
							name: this.$t('homePage.device.vol'),
							type: 'line',
							smooth: true,
							symbol: 'none',
							areaStyle: {
								opacity: 0
							},
							data: gonglv
						},
						{
							name: 'SOC',
							type: 'line',
							smooth: true,
							yAxisIndex: 1,
							symbol: 'none',
							areaStyle: {
								opacity: 0
							},
							data: soc
						}

					]
				}
			}

		}

	}
</script>

<style lang="scss" scoped>
	.cluster-warp {
		background-color: #f5f5f5;

		.all-con-item-box {
			background: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			width: 710rpx;
			border: 16rpx;

			margin-top: 30rpx;
			box-shadow: 0px 4rpx 16rpx rgba(0, 0, 0, 0.08);
			position: relative;

			.top-right-box {
				display: flex;
				flex-direction: row;
				align-items: center;
				flex: 1;
				justify-content: flex-end;

				.top-right-item {
					padding: 5rpx 10rpx;
					border: 1rpx solid #4c9ee6;
					font-size: 24rpx;
					color: #4c9ee6;
					cursor: pointer;
					margin-right: 10rpx;
					border-radius: 8rpx;

					&.active {
						background: #4c9ee6;
						color: #ffffff;
					}
				}
			}

			.ring-data-box {
				padding: 30rpx;

				.ring-data-item {
					.ring-title {
						font-size: 28rpx;
						color: #4c9ee6;
					}
				}
			}

			.group-box {
				width: 100%;
				display: flex;
				flex-wrap: wrap;
				position: relative;


				.other-value {
					display: flex;
					flex-direction: column;
					width: 100%;

					.top-value {
						display: flex;
						justify-content: space-between;
						position: relative;

						.title {
							font-size: 24rpx;
							color: rgba(40, 40, 40, 1);
						}

						.zhexian-icon {
							position: absolute;
							right: 70%;

							image {
								width: 28rpx;
								height: 28rpx;
							}
						}

						.value {
							font-size: 32rpx;
							color: rgba(0, 156, 119, 1);
							font-weight: 400;
						}
					}

					.bottom-value {
						width: 100%;
						display: flex;
						justify-content: space-between;
						font-size: 24rpx;
						color: rgba(153, 153, 153, 1);
					}
				}
			}



			.group-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 199rpx;

				justify-content: center;
				background: #d7e9e548;
				border-radius: 10rpx;
				margin: 10rpx 10rpx;
				position: relative;
				padding: 10rpx 0;

				.history-icon {
					position: absolute;
					right: 0rpx;
					top: 0rpx;
				}

				image {
					width: 40rpx;
					height: 40rpx;
				}

				.item-con {
					display: flex;
					flex-direction: column;
					align-items: center;
					// margin-left: 15rpx;
					width: 100%;

					.item-title {
						width: 100%;
						font-size: 24rpx;
						color: #2a2a2a;
						margin-top: 10rpx;
						text-align: center;
					}

					.item-num {
						font-size: 36rpx;
						color: #282828;
						font-weight: bold;
						max-width: 90%;
						text-align: center;
					}

					.item-unit {
						color: #cccccc;
						font-size: 16rpx;
						padding-left: 10rpx;
					}
				}
			}


			.border-bottom {
				border-bottom: 4rpx dotted #e5e5e5;
			}

			.border-right {
				border-right: 4rpx dotted #e5e5e5;
			}

			.padding-top-30 {
				padding-top: 30rpx;
			}

			.padding-bottom-30 {
				padding-bottom: 30rpx;
			}
		}

		.chart-box {
			width: 650rpx;
			height: 500rpx;
			margin-top: 20rpx;
			position: relative;
		}

		.time-box {
			display: flex;
			justify-content: space-around;
			align-items: center;
			font-size: 28rpx;
			margin-top: 20rpx;
			// border: 1px solid #a3a3a3;
			background-color: #f5f5f5;
			border-radius: 8rpx;
			padding: 10rpx;


			.time {

				font-size: 28rpx;
				white-space: nowrap;
			}
		}

		.slot-content {
			padding: 20rpx;
		}


	}
</style>