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

		<view class="all-con-item-box" style="margin-top: 0!important;">
			<Section title="堆总压/SOC">
			</Section>
			<view class="chart-box">
				<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="运行数据" />
			<zero-loading v-if="runLoading" position="absolute"></zero-loading>
			<view v-else class="group-box">
				<view class="group-item" @click="showHistory('总压', 'bmsTotalVol', '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/rcdl.png"></image>
					</view>
					<view class="item-con">
						<view class="item-num">{{panelData.bmsTotalVol | isNull}}

						</view>
						<view class="item-title">总压(V)</view>
					</view>
				</view>

				<view class="group-item" @click="showHistory('总电流', 'bmsTotalCurrent', 'bms','A','','')">
					<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">{{panelData.bmsTotalCurrent | isNull}}

						</view>
						<view class="item-title">总电流(A)</view>
					</view>
				</view>

				<view class="group-item " @click="showHistory('SOC', 'soc', 'bms','%','','')">
					<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">{{panelData.soc | isNull}}

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

				<view class="group-item" @click="showHistory('SOH', 'soh', 'bms','%','','')">
					<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">{{panelData.soh | isNull}}

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

				<view class="group-item" @click="showHistory('累计充电量', 'totalDischarge', '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">{{(panelData.totalDischarge / 1000).toFixed(2) | isNull}}

						</view>
						<view class="item-title">累计充电量(MWh)</view>
					</view>
				</view>

				<view class="group-item" @click="showHistory('累计放电量', 'totalDischarge', '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">{{(panelData.totalDischarge / 1000).toFixed(2) | isNull}}

						</view>
						<view class="item-title">累计放电量(MWh)</view>
					</view>
				</view>
				<view class="group-item" @click="showHistory('可充电量', 'rechargeCapacity', '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">{{panelData.rechargeCapacity | kwhFormat}}

						</view>
						<view class="item-title">可充电量({{panelData.rechargeCapacity | kwhUnitFormat}})</view>
					</view>
				</view>

				<view class="group-item" @click="showHistory('可放电量', 'dischargeCapacity', '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">{{panelData.dischargeCapacity | kwhFormat}}

						</view>
						<view class="item-title">可放电量({{panelData.dischargeCapacity | kwhUnitFormat}})</view>
					</view>
				</view>

				<view class="group-item " @click="showHistory('电流差异值', 'bmsCurrentDiff', 'bms','A','')">
					<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">{{panelData.bmsCurrentDiff | isNull}}

						</view>
						<view class="item-title">电流差异值(A)</view>
					</view>
				</view>

				<view class="group-item" @click="showHistory('总压差异值', 'bmsVolDiff', '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/rcdl.png"></image>
					</view>
					<view class="item-con">
						<view class="item-num">{{panelData.bmsVolDiff | isNull}}

						</view>
						<view class="item-title">总压差异值(V)</view>
					</view>
				</view>
			</view>
		</view>

		<view class="all-con-item-box">
			<Section title="运行数据" />
			<zero-loading v-if="runLoading" position="absolute"></zero-loading>
			<template v-else>
				<view class="group-box border-bottom padding-top-30 padding-bottom-30">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								最大电压差
							</view>
							<view class="value">
								{{(panelData.bmsMaxVol - panelData.bmsMinVol).toFixed(3) | isNull  }}V
							</view>
						</view>

					</view>

				</view>

				<view class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory('单体最高电压', 'bmsMaxVol', 'bms','V','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								单体最高电压
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{panelData.bmsMaxVol | isNull}}V
							</view>
						</view>
						<view class="bottom-value">
							<view>
								电池簇号：{{panelData.bmsMaxVolNum | isNull}}
							</view>

							<view>
								电池组号：{{panelData.bmsMaxVolBMUNum | isNull}}
							</view>

							<view>
								电芯位置：{{panelData.bmsMaxVolBoxNum | isNull}}
							</view>

						</view>
					</view>

				</view>

				<view class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory('单体最低电压', 'bmsMinVol', 'bms','V','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								单体最低电压
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{panelData.bmsMinVol | isNull}}V
							</view>
						</view>
						<view class="bottom-value">
							<view>
								电池簇号：{{panelData.bmsMinVolNum | isNull}}
							</view>

							<view>
								电池组号：{{panelData.bmsMinVolBMUNum | isNull}}
							</view>

							<view>
								电芯位置：{{panelData.bmsMinVolBoxNum | isNull}}
							</view>

						</view>
					</view>

				</view>

				<view class="group-box border-bottom padding-top-30 padding-bottom-30">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								最大温度差
							</view>
							<view class="value">
								{{ (panelData.bmsMaxTem - panelData.bmsMinTem).toFixed(3) | isNull}}℃
							</view>
						</view>

					</view>

				</view>

				<view class="group-box border-bottom padding-top-30 padding-bottom-30"
					@click="showHistory('单体最高温度', 'bmsMaxTem', 'bms','℃','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								单体最高温度
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{ panelData.bmsMaxTem | isNull}}℃
							</view>
						</view>
						<view class="bottom-value">
							<view>
								电池簇号：{{ panelData.bmsMaxTemNum | isNull}}
							</view>

							<view>
								电池组号：{{ panelData.bmsMaxTemBMUNum | isNull}}
							</view>

							<view>
								电芯位置：{{ panelData.bmsMaxTemBoxNum | isNull}}
							</view>

						</view>
					</view>

				</view>

				<view class="group-box  padding-top-30 " @click="showHistory('单体最低温度', 'bmsMinTem', 'bms','℃','')">
					<view class="other-value">
						<view class="top-value">
							<view class="title">
								单体最低温度
							</view>
							<view class="zhexian-icon">
								<image src="/static/aidex/images/zhexian.png"></image>
							</view>
							<view class="value">
								{{ panelData.bmsMinTem  | isNull}}℃
							</view>
						</view>
						<view class="bottom-value">
							<view>
								电池簇号：{{ panelData.bmsMinTemNum | isNull}}
							</view>

							<view>
								电池组号：{{ panelData.bmsMinTemBMUNum | isNull}}
							</view>

							<view>
								电芯位置：{{ panelData.bmsMinTemBoxNum | isNull }}
							</view>

						</view>
					</view>

				</view>

			</template>
		</view>

		<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 {
				loading: false,
				curve_option: {},
				stationId: null,
				srcId: null,
				chartLoading: false,
				panelData: {},
				chartTitle: null,
				histroyShow: false,
				hisParams: {},
				curLoading:false,
				runLoading: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,
					srcId:this.srcId
				}
				this.chartTitle = name
				this.histroyShow = true
			},

			getTotalData() {
				this.runLoading =  true
				const self = this
				return new Promise((resolve, reject) => {
					self.$u.api.deviceList
						.GetNewValue({
							colList: ["bmsTotalVol", "bmsTotalCurrent", "soc", "soh",
								"combinerCabinetQFClosingFeedback", "distributionBoxQF1ClosingFeedback",
								"totalDischarge", "rechargeCapacity", "dischargeCapacity",
								"bmsCurrentDiff", "bmsVolDiff", "totalCharge", "bmsMaxVol", "bmsMaxVolNum",
								"bmsMaxVolBMUNum", "bmsMaxVolBoxNum",
								"bmsMinVol", "bmsMinVolNum", "bmsMinVolBMUNum", "bmsMinVolBoxNum",
								"bmsMaxTem", "bmsMaxTemNum", "bmsMaxTemBMUNum", "bmsMaxTemBoxNum",
								"bmsMinTem", "bmsMinTemNum", "bmsMinTemBMUNum", "bmsMinTemBoxNum"
							],
							stationId: this.stationId,
							srcId: this.srcId
						})
						.then((res) => {
							this.panelData = {
								bmsTotalVol: res.data.bmsTotalVol?.value,
								bmsTotalCurrent: res.data.bmsTotalCurrent?.value,
								soc: res.data.soc?.value,
								soh: res.data.soh?.value,
								combinerCabinetQFClosingFeedback: res.data.combinerCabinetQFClosingFeedback
									?.value,
								distributionBoxQF1ClosingFeedback: res.data
									.distributionBoxQF1ClosingFeedback?.value,
								totalDischarge: res.data.totalDischarge?.value,
								rechargeCapacity: res.data.rechargeCapacity?.value,
								dischargeCapacity: res.data.dischargeCapacity?.value,
								bmsCurrentDiff: res.data.bmsCurrentDiff?.value,
								bmsVolDiff: res.data.bmsVolDiff?.value,
								totalCharge: res.data.totalCharge?.value,
								bmsMaxVol: res.data.bmsMaxVol?.value,
								bmsMaxVolNum: res.data.bmsMaxVolNum?.value,
								bmsMaxVolBMUNum: res.data.bmsMaxVolBMUNum?.value,
								bmsMaxVolBoxNum: res.data.bmsMaxVolBoxNum?.value,
								bmsMinVol: res.data.bmsMinVol?.value,
								bmsMinVolNum: res.data.bmsMinVolNum?.value,
								bmsMinVolBMUNum: res.data.bmsMinVolBMUNum?.value,
								bmsMinVolBoxNum: res.data.bmsMinVolBoxNum?.value,
								bmsMaxTem: res.data.bmsMaxTem?.value,
								bmsMaxTemNum: res.data.bmsMaxTemNum?.value,
								bmsMaxTemBMUNum: res.data.bmsMaxTemBMUNum?.value,
								bmsMaxTemBoxNum: res.data.bmsMaxTemBoxNum?.value,
								bmsMinTem: res.data.bmsMinTem?.value,
								bmsMinTemBMUNum: res.data.bmsMinTemBMUNum?.value,
								bmsMinTemBoxNum: res.data.bmsMinTemBoxNum?.value,

							}
							resolve(res);
						})
						.finally((err) => {
							this.runLoading = false
						})
				});
			},
			getData(srcId) {
				this.srcId = srcId
				this.loading = true
				const api = [
					this.getSocChart(),
					this.getTotalData()
				]
				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: ["soc", "stackTotalvol"],
							stationId: this.stationId,
							type: 'day',
							srcId: this.srcId
						})
						.then((res) => {

							self.initChargeChart(res.data);
							resolve(res);
						})
						.finally((err) => {
							this.chartLoading = false
						})
				});
			},
			initChargeChart(val) {

				const gonglv = []
				const soc = []
				const xAxis = []
				val[1].staticCurveList.forEach(v => {
					xAxis.push(v.date)
					gonglv.push(v.digital)
				})
				val[0].staticCurveList.forEach(v => {
					soc.push(v.digital)
				})
				this.curve_option = {
					animationDuration: 500,
					animationEasing: "cubicInOut",
					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];
						},
					},
					color: ['#009458', '#BFE49F'],
					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: '堆总压(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: '堆总压',
							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>
	.stack-warp {
		height: 100% !important;
		background-color: #f5f5f5;

		.all-con-item-box {
			background: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			width: 710rpx;
			border: 16rpx;
			position: relative;
			margin-top: 30rpx;
			box-shadow: 0px 4rpx 16rpx rgba(0, 0, 0, 0.08);

			.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;
					}
				}
			}



		}

		.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;
			}
		}

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

		.slot-content {
			padding: 20rpx;
		}

		.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;
		}
	}
</style>