<template>
	<view class="container">
		<view class="datebox">
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
				<view class="datebox-item border-line flex-bt">
					<view>日期</view>
					<view class="datebox-item-right">
						<text :class="date ? '':'default'">{{date ? date : "请选择日期"}}</text>
						<image src="@/static/images/health/right_arrow_right_icon24.png"></image>
					</view>
				</view>
			</picker>
			<picker mode="time" :value="time" @change="bindTimeChange">
				<view class="datebox-item flex-bt">
					<view>测量时间</view>
					<view class="datebox-item-right">
						<text :class="time ? '':'default'">{{time ? time : "请选择测量时间"}}</text>
						<image src="@/static/images/health/right_arrow_right_icon24.png"></image>
					</view>
				</view>
			</picker>
		</view>
		<view class="datebox-detail">
			<view class="datebox-detail-header flex-bt">
				<view>腰围</view>
				<view class="datebox-detail-header-right">
					<text class="num">{{active}}</text>
					<text>CM</text>
				</view>
			</view>
			<view class="scale">
				<simpleScale ref="simpleScale" :minVal="0" :maxVal="33" :int="false" :single="10" :h="h" :active="active" :scroll="false" :style="style"
					@value="getScroll" />
			</view>
		</view>
	</view>
</template>

<script>
	import simpleScale from "@/pages_echarts/components/simpleScale/simpleScale.vue"
	// import { bgInfo } from "@/api/pages_watch/healthMonitoring.js"
	export default {
		components: {
			simpleScale
		},
		data() {
			return {
				// 表示有效日期范围的开始，字符串格式为"YYYY-MM-DD"
				startDate: "2020-01-01",
				// 表示有效日期范围的结束，字符串格式为"YYYY-MM-DD"
				endDate: this.utils.timeFormat(new Date()),
				date: "2024-01-01",
				time: "17:12",
				active: "6",
				loading: false,
				style: {
					line: '#ccc',
					bginner: '#fbfbfb',
					bgoutside: '#ffffff',
					fontColor: '#333',
					fontSize: 16
				},
				h: uni.upx2px(88)
			}
		},
		onLoad(option) {
			// const param = JSON.parse(option.param)
			// this.date = param.createTime && param.createTime.split(' ')[0]
			// this.time = param.createTime && param.createTime.substring(11, 16)
			// this.active = String(param.num || 0)
			this.$nextTick(()=>{
				this.$refs.simpleScale.init()
			})
		},
		methods: {
			bindDateChange(e) {
				this.date = e.detail.value
				this.getList()
			},
			bindTimeChange(e) {
				this.time = e.detail.value
				this.getList()
			},
			getScroll(e) {
				this.active = e
			},
			// 查询数据
			getList() {
				this.loading = true
				const param = {
					startTime: this.date.replace(/-/g, '/') +" " +this.time + ":00",
					endTime: this.date.replace(/-/g, '/') +" " + this.time + ":59",
					deviceId: uni.getStorageSync("deviceId"),
				}
				bgInfo(param).then(res => {
					this.loading = false
					if (res.code == 200) {
						if(res.data && res.data.length > 0) {
							this.date = res.data[0].createTime && res.data[0].createTime.split(' ')[0]
							this.time = res.data[0].createTime && res.data[0].createTime.substring(11, 16)
							this.active = String(res.data[0].bloodGlucose || 0)
						} else {
							this.active = "0"
						}
						
					} else {
						this.active = "0"
					}
					this.$nextTick(()=>{
						this.$refs.simpleScale.init()
					})
				}).catch(() => {
					this.loading = false
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@mixin u-flex($flexD, $alignI, $justifyC) {
		display: flex;
		flex-direction: $flexD;
		align-items: $alignI;
		justify-content: $justifyC;
	}
	.flex-bt {
		@include u-flex(row, center, space-between);
	}
	.default {
		font-weight: 400;
		font-size: 28rpx;
		color: #999999;
	}
	.container {
		padding: 20rpx 24rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #333333;
	}
	.datebox {
		padding: 0 32rpx;
		margin-bottom: 20rpx;
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		&-item {
			height: 120rpx;
			&-right {
				font-weight: 400;
				font-size: 32rpx;
				@include u-flex(row, center, flex-start);
				image {
					width: 48rpx;
					height: 48rpx;
				}
			}
		}
	}
	.datebox-detail {
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 40rpx 32rpx;
		&-header-right {
			flex-shrink: 0;
			font-weight: 400;
			font-size: 28rpx;
			color: #999999;
			.num {
				font-size: 48rpx;
				color: #333333;
				margin-right: 14rpx;
			}
		}
	}
	.scale {
		height: 176rpx;
		margin-top: 36rpx;
		overflow: hidden;
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(170,170,170,0.1);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		border: 2rpx solid #ECECEC;
		// width: 200rpx;
		// height: 600rpx;
	}
</style>