<template>
	<view class="bobox" :style="boxstyle">
		<view class="box-title">
			<view class="box-title-left">睡眠趋势</view>
		</view>
		<view class="loading" v-show="loading"><sleepLoading /></view>
		<view class="echartbox-header" v-if="this.sleepValue && this.sleepValue.length > 0">
			<view>{{touchValue.startTime.substring(5,16)}} 至 {{touchValue.endTime.substring(5,16)}}</view>
			<view class="echartbox-header-time">
				<text>{{touchValue.sleepStateText}}</text>
				<text class="echartbox-header-num">{{touchValue.hours}}</text>
				<text>小时</text>
				<text class="echartbox-header-num">{{touchValue.minutes}}</text>
				<text>分钟</text>
				<image src="@/static/images/pages_watch/icons/prompt_icon.png"></image>
			</view>
		</view>
		<view class="echartbox">
			<template v-if="this.sleepValue && this.sleepValue.length > 0">
				<view style="min-height: 446rpx;">
					<sleepCharts ref="sleepCharts" :type="2" :sleepValue="sleepValue" @handleItem="handleItem" />
				</view>
				<view class="legend border-line" v-show="!loading">
					<view class="legend-item">
						<view class="legend-dot" style="background-color: #8C37E6;"></view><text>深睡</text>
					</view>
					<view class="legend-item">
						<view class="legend-dot" style="background-color: #D138CF;"></view><text>浅睡</text>
					</view>
					<view class="legend-item">
						<view class="legend-dot" style="background-color: #F88082;"></view><text>快速眼动</text>
					</view>
					<view class="legend-item">
						<view class="legend-dot" style="background-color: #FDBD27;"></view><text>清醒</text>
					</view>
				</view>
			</template>
			<template v-else>
				<myEmpty />
			</template>
		</view>
	</view>
</template>

<script>
	import sleepLoading from "@/pages_watch/components/sleepCharts/sleepLoading.vue"
	import sleepCharts from "@/pages_watch/components/sleepCharts/sleepCharts.vue";
	import myEmpty from "@/pages_watch/components/myEmpty/myEmpty.vue";
	export default {
		name: "sleep",
		props: {
			loading: {
				type: Boolean,
				default: false
			},
			boxstyle: {
				type: Object,
				default: () => {}
			}
		},
		components: {
			sleepCharts,
			myEmpty,
			sleepLoading
		},
		data() {
			return {
				sleepData: {},
				touchValue: {
					startTime: "",
					endTime: "",
					type: "",
					sleepStateText: "",
					hours: "",
					minutes: ""
				},
				sleepValue:[]
			}
		},
		methods: {
			setChartData(data) {
				this.sleepValue = data.sleepSection || []
				if(this.sleepValue && this.sleepValue.length > 0) {
					this.$nextTick(()=>{
						this.$refs.sleepCharts.initData()
					})
				}
			},
			handleItem(item) {
				let diff = Math.abs(new Date(item.startTime).getTime() - new Date(item.endTime).getTime());
				this.touchValue = {
					...item,
					hours: Math.floor(diff / (1000 * 60 * 60)),
					minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@mixin u-flex($flexD, $alignI, $justifyC) {
		display: flex;
		flex-direction: $flexD;
		align-items: $alignI;
		justify-content: $justifyC;
	}
	.loading {
		height: 90%;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		position: absolute;
		top: 42rpx;
		left: 0;
		background-color: #fff;
		z-index: 99;
	}
	.bobox {
		background: #fff;
		position: relative;
	}
	.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;
			}
		}
	}
	.echartbox {
		// height: 694rpx;
		width: 100%;
		overflow: hidden;
		&-header {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #757575;
			text-align: center;
	
			&-time {
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
			}
	
			&-num {
				margin: 0 8rpx;
				font-family: DIN, DIN;
				font-weight: 500;
				font-size: 56rpx;
				color: #333333;
			}
	
			image {
				width: 32rpx;
				height: 32rpx;
				margin-left: 4rpx;
			}
		}
	}
	
	.legend {
		padding: 46rpx 0;
		box-sizing: border-box;
		@include u-flex(row, center, space-between);
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #757575;
	
		&-item {
			flex: 1;
			text-align: center;
			@include u-flex(row, center, center);
		}
	
		&-dot {
			width: 12rpx;
			height: 12rpx;
			border-radius: 50%;
			margin: 12rpx;
		}
	}
</style>