<template>
	<scroll-view scroll-y="true" :style="'height:' + clientHeight + 'px;'" @scrolltolower="scrolltolower">
		<template v-if="list &&list.length > 0">
			<abnormalList :abnormalList="abnormalList" :unit="'mmHg'" :type="'bp'"
				@monthChange="monthChange">
				<template v-slot:default="slotProps">
					<view :class="'slotview status'+slotProps.data.status">
						{{_typeText(slotProps.data.status)}}</view>
				</template>
			</abnormalList>
		</template>
		<view v-else>
			<myEmpty />
		</view>
	</scroll-view>
</template>

<script>
	import abnormalList from "@/pages_watch/components/abnormalList/abnormalList.vue"
	import myEmpty from "@/pages_watch/components/myEmpty/myEmpty.vue"
	import {bpInfoPage} from "@/api/pages_watch/healthMonitoring.js";
	export default {
		props: {
			clientHeight: {
				type: Number,
				default: 0
			}
		},
		components: {
			abnormalList,
			myEmpty
		},
		data() {
			return {
				activeTab: 0,
				loading: false,
				isMore: true,
				queryParam: {
					startTime: undefined,
					endTime: undefined,
					deviceId: undefined,
					status: undefined,
					pageSize: 10,
					pageNum: 1,
				},
				total: 0,
				abnormalList: {},
				list: [],
			}
		},
		methods: {
			// 获取数据
			getList() {
				this.loading = true
				this.queryParam.status = this.activeTab > 0 ? this.activeTab : undefined
				this.queryParam.deviceId = uni.getStorageSync("deviceId")
				bpInfoPage(this.queryParam).then(res => {
					this.loading = false
					if (res.code == 0) {
						let list = res.rows && res.rows.length > 0 ? res.rows : []
						this.list = this.list.concat(list)
						this.total = res.total
						this.queryParam.pageNum++
						if (this.list.length >= this.total) {
							this.isMore = false
						}
						this.resetData(this.list)
					}
				}).catch(() => {
					this.loading = false
				})
			},
			resetData(array) {
				this.abnormalList = array.reduce((acc, obj) => {
					const [year, month] = obj.createTime.split(' ')[0].split('-');
					const date = obj.createTime.split(' ')[0];
			
					if (!acc[`${year}-${month}`]) {
						acc[`${year}-${month}`] = {};
					}
			
					if (!acc[`${year}-${month}`][date]) {
						acc[`${year}-${month}`][date] = [];
					}
					obj.num = obj.sbp + '/' + obj.dbp
					acc[`${year}-${month}`][date].push(obj);
			
					return acc;
				}, {});
			},
			refresh(activeTab) {
				this.activeTab = activeTab || this.activeTab
				this.loading = false
				this.isMore = true
				this.queryParam.pageNum = 1
				this.list = []
				this.getList()
			},
			// 滚动到底部
			scrolltolower() {
				if (this.isMore) {
					this.getList()
				}
			},
			// 改变月份(时间范围：查询选中的月份-现在的月份) 2024-07
			monthChange(month) {
				const date = month + "-01 00:00:00"
				this.queryParam.startTime = this.$timeFormat(date, "yyyy/mm/dd hh:MM:ss")
				this.queryParam.endTime = this.$timeFormat(new Date(), "yyyy/mm/dd hh:MM:ss")
				this.refresh()
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>