<template>
	<view class="container">
		<uni-nav-bar fixed :border="false" :backgroundColor="`rgba(255,255,255,${opacity})`" title="心率监测" :statusBar="true"
			left-icon="left" @clickLeft="back"></uni-nav-bar>
		<image class="page-bg" src="@/static/images/pages_watch/images/heart_rate_top_bg.png" mode="widthFix"></image>
		<view class="container-body">
			<dateTimePicker @onChange="onChangeTime" />
			<!-- 心率趋势 -->
			<view class="box">
				<view class="box-title">
					<view class="box-title-left">心率趋势</view>
				</view>
				<view class="ytitle" v-show="!loading && !isEmpty">单位次/分钟</view>
				<view class="echartbox">
					<qiun-data-charts v-show="!isEmpty" type="area" :opts="opts" :chartData="chartData" tooltipFormat="heartTooltip" />
					<myEmpty v-show="isEmpty" style="padding: 40rpx 0 0 0;"></myEmpty>
				</view>
				<view class="heart-rate-statistics">
					<view class="heart-rate-statistics-item">
						<view>平均值</view>
						<view class="heart-rate-statistics-num">{{avg||'--'}}</view>
					</view>
					<view class="heart-rate-statistics-item">
						<view>最低值</view>
						<view class="heart-rate-statistics-num">{{min||'--'}}</view>
					</view>
					<view class="heart-rate-statistics-item">
						<view>最高值</view>
						<view class="heart-rate-statistics-num">{{max||'--'}}</view>
					</view>
				</view>
				<view class="heart-rate-range">
					<text>心率范围</text>
					<view class="heart-rate-rangeright">
						<text>{{min||''}}-{{max||''}}</text>
						<text class="heart-rate-unit">次/分钟</text>
					</view>
				</view>
				<view class="abnormal">
					<view class="abnormal-item colorbg1">
						<view class="abnormal-item-title">心率过高提醒</view>
						<view class="abnormal-item-num">
							<text>{{status2}}次</text>
						</view>
					</view>
					<view class="abnormal-item colorbg2">
						<view class="abnormal-item-title">心率过低提醒</view>
						<view class="abnormal-item-num">
							<text>{{status1}}次</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 心率标准 -->
			<view class="standard standardbox">
				<view class="standard-title">
					<image src="@/static/images/pages_watch/icons/note_icon.png"></image>
					<text>心率</text>
					<text>标准</text>
				</view>
				<view class="standard-con">
					<view>心率标准范围为60～100次/分，但可因年龄、性别或其他生理因素产生个体差异。</view>
					</view>
				</view>
			</view>
		</view>
</template>

<script>
	import myEmpty from "@/pages_watch/components/myEmpty/myEmpty.vue"
	import dateTimePicker from "@/pages_watch/components/dateTimePicker/dateTimePicker.vue"
	import {heartRateInfo,heartRateCount} from "@/api/pages_watch/healthMonitoring.js";
	
	export default {
		components: {
			myEmpty,
			dateTimePicker
		},
		data() {
			return {
				opacity: 0,
				loading: false,
				isEmpty: false,
				avg: undefined,
				max: undefined,
				min: undefined,
				status1: 0,
				status2: 0,
				chartData: {},
				opts: {
					color: ["#FF7700"],
					padding: [25, 0, 10, 0],
					enableScroll: false,
					dataLabel: false,
					dataPointShapeType: "hollow",
					legend: {
						show: false,
					},
					xAxis: {
						disableGrid: true,
						fontSize: 12,
						axisLine: false,
						fontColor: '#ccc',
						labelCount: 7,
						format: "formatterTime",
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2,
						data: [{
							fontColor: '#ccc',
							min: 0,
							axisLine: false,
							fontSize: 12,
						}]
					},
					extra: {
						tooltip: {
							gridType: "dash",
							showArrow: false,
							legendShow: true,
							legendShape: "circle"
						},
						area: {
							type: "straight",
							opacity: 0.2,
							addLine: true,
							width: 2,
							gradient: true,
							activeType: "solid"
						}
					}
				}
			}
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			onChangeTime(time) {
				const param = {
					startTime: this.$timeFormat(time[0],'yyyy/mm/dd hh:MM:ss'),
					endTime: this.$timeFormat(time[1],'yyyy/mm/dd hh:MM:ss'),
					deviceId: uni.getStorageSync("deviceId") || ""
				}
				this.getServerData(param)
				this.getAbnormalInfo(param)
			},
			getServerData(param) {
				this.loading = true
				heartRateInfo(param).then((res) => {
					this.loading = false
					this.isEmpty = !(res.data && res.data.list && res.data.list.length>0)
				  if(res.code == 200 && res.data) {
					  this.avg = res.data.avg
					  this.max = res.data.max
					  this.min = res.data.min
					  let chartData = {
						  categories: res.data.list.map(item=>item.createTime),
						  series: [{
							  name: "心率",
							  data: res.data.list.map(item=>item.avgBpm)
						  }]
					  }
					  this.chartData = JSON.parse(JSON.stringify(chartData));
				  } else {
					  this.avg = undefined
					  this.max = undefined
					  this.min = undefined
				  }
				}).catch((err) => { 
					this.loading = false
				});
			},
			// 获取异常数据
			getAbnormalInfo(param) {
				this.status1 = 0
				this.status2 = 0
				heartRateCount(param).then((res) => {
					if (res.code == 200 && res.data && res.data.length > 0) {
						// 0:正常，1：偏低2:偏高
						res.data.forEach(item => {
							this.status1 += item.status == 1 ? item.count : 0
							this.status2 += item.status == 2 ? item.count : 0
						})
					}
				}).catch((err) => {});
			},
		},
		onPageScroll(e) {
			if (e.scrollTop <= 44) {
				this.opacity = e.scrollTop / 44 * 1
			} else if (e.scrollTop > 44) {
				this.opacity = 1
			}
		
		}
	}
</script>

<style lang="scss" scoped>
	@mixin u-flex($flexD, $alignI, $justifyC) {
		display: flex;
		flex-direction: $flexD;
		align-items: $alignI;
		justify-content: $justifyC;
	}

	.container {
		padding: 0 24rpx;
		position: relative;
	}

	.page-bg {
		width: 100%;
		height: auto;
		position: absolute;
		top: 0;
		left: 0;
	}

	.container-body {
		position: relative;
		z-index: 1;
		padding-bottom: 24rpx;
	}

	.box-title {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #757575;
		@include u-flex(row, center, space-between);

		&-left {
			font-weight: 500;
			font-size: 36rpx;
			color: #222222;
			line-height: 42rpx;
		}

		&-right {
			@include u-flex(row, center, flex-start);

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

	.box {
		padding: 24rpx;
		margin-bottom: 20rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
	}
	.ytitle {
		margin-top: 10rpx;
		line-height: 34rpx;
		margin-bottom: 24rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #999999;
	}
	.echartbox {
		height: 506rpx;
		width: 100%;
	}

	.heart-rate {
		&-statistics {
			@include u-flex(row, center, space-between);
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 26rpx;
			color: #999999;
		}

		&-statistics-item {
			flex: 1;
			height: 200rpx;
			@include u-flex(column, center, center);
		}

		&-statistics-num {
			font-weight: 500;
			font-size: 48rpx;
			color: #333333;
			height: 68rpx;
			margin-top: 6rpx;
		}

		&-range {
			height: 96rpx;
			padding: 0 24rpx;
			background: linear-gradient(135deg, #FF7E9E 0%, #FE3565 100%);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			@include u-flex(row, center, space-between);
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
		}

		&-rangeright {
			font-weight: 500;
			font-size: 40rpx;
			@include u-flex(row, center, flex-start);
		}

		&-unit {
			margin-left: 18rpx;
			font-weight: 400;
			font-size: 24rpx;
		}
	}

	.abnormal {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 28rpx;
		color: #757575;
		@include u-flex(row, center, flex-start);
		flex-wrap: wrap;
		// gap: 18rpx;
		margin-top: 24rpx;
		margin-bottom: -2rpx;
		margin-right: -18rpx;

		&-item {
			width: 318rpx;
			height: 144rpx;
			margin: 0 18rpx 18rpx 0;
			padding: 0 24rpx;
			box-sizing: border-box;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			border: 2rpx solid #FCF4F4;
			@include u-flex(column, flex-start, center);

			&-title {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
				margin-bottom: 16rpx;
			}

			&-num {
				width: 100%;
				@include u-flex(row, center, space-between);
			}
		}

		.colorbg1 {
			background: #FFFCFC;
			border-color: #FCF4F4;
		}

		.colorbg2 {
			background: #FAFBFF;
			border-color: #F2F7FE;
		}
	}

	.standardbox {
		background: linear-gradient(360deg, #FFFFFF 0%, #FFF5EC 100%);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		border: 2rpx solid #FFFFFF;
		margin-bottom: 20rpx;
	}

	.standard {
		padding: 24rpx;
		margin-top: 32rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;

		&-title {
			@include u-flex(row, center, flex-start);
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;

			image {
				width: 48rpx;
				height: 48rpx;
				margin-right: 14rpx;
			}

			text:last-child {
				color: #FF7700;
			}
		}

		&-con {
			margin-top: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #757575;

			view {
				margin-bottom: 12rpx;
				padding-left: 24rpx;
				position: relative;

				&::before {
					content: "";
					width: 8rpx;
					height: 8rpx;
					background: rgba(255, 119, 0, 0.5);
					border-radius: 50%;
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
	}
</style>