<template>
	<view class="app">
		<u-tabs :list="tabList" :current="tabindex" @change="tabchange" active-color='#26A888'
			:show-bar='false'></u-tabs>
		<view class="content">
			<view v-if="tabindex == 0">
				<blood-sugar :current="tabindex" :echartData="list" @taptimeindex='taptimeindex' :tablelist='tablelist'
					v-on:websocket-message="handleWebSocketMessage"></blood-sugar>
			</view>
			<view v-if="tabindex == 1">
				<blood-pressure :current="tabindex" :echartData="list" @taptimeindex='taptimeindex'
					:tablelist='tablelist' v-on:websocket-message="handleWebSocketMessage"></blood-pressure>
			</view>
			<view v-if="tabindex == 2">
				<BloodFat :current="tabindex" :echartData="list" @taptimeindex='taptimeindex' :tablelist='tablelist'
					v-on:websocket-message="handleWebSocketMessage"></BloodFat>
			</view>
			<view v-if="tabindex == 3">
				<BMI :current="tabindex" :echartData="list" @taptimeindex='taptimeindex' :tablelist='tablelist'
					v-on:websocket-message="handleWebSocketMessage"></BMI>
			</view>
			<view v-if="tabindex == 4">
				<Blood-oxygen :current="tabindex" :echartData="list" @taptimeindex='taptimeindex' :tablelist='tablelist'
					v-on:websocket-message="handleWebSocketMessage"></Blood-oxygen>
			</view>
			<view v-if="tabindex == 5">
				<Heart-rate :current="tabindex" :echartData="list" @taptimeindex='taptimeindex' :tablelist='tablelist'
					v-on:websocket-message="handleWebSocketMessage"></Heart-rate>
			</view>
			<view v-if="tabindex == 6">
				<Temperature :current="tabindex" :echartData="list" @taptimeindex='taptimeindex' :tablelist='tablelist'
					v-on:websocket-message="handleWebSocketMessage"></Temperature>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		record,
		getList
	} from '@/api/pagesC/healthdata/index.js'
	import BloodPressure from '../BloodPressure/BloodPressure.vue'
	import BloodSugar from '../BloodSugar/BloodSugar.vue'
	import Temperature from '../Temperature/Temperature.vue'
	import BloodFat from '../BloodFat/BloodFat.vue'
	import BMI from '../BMI/BMI.vue'
	import BloodOxygen from '../BloodOxygen/BloodOxygen.vue'
	import HeartRate from '../HeartRate/HeartRate.vue'
	export default {
		components: {
			BloodPressure,
			BloodSugar,
			Temperature,
			BloodFat,
			BMI,
			BloodOxygen,
			HeartRate
		},
		data() {
			return {
				tabindex: 0,
				tabList: [{
					name: '血糖',
				}, {
					name: '血压',
				}, {
					name: '血脂',
				}, {
					name: 'BMI',
				}, {
					name: '血氧',
				}, {
					name: '心率',
				}, {
					name: '体温',
				}, ],
				list: null,
				type: 0,
				tabletotal: 0,
				tablelist: [],
				pageNum: 1,
			};
		},
		onLoad(options) {
			this.tabindex = options.index
		},
		onShow() {
			this.info();
			this.getinfo();
		},
		methods: {
			taptimeindex(e) {
				this.type = e
				this.info()
				this.getinfo();
			},
			info() {
				let userinfo = uni.getStorageSync('userinfo');
				var obj = {
					label: Number(this.tabindex) + 1,
					identity: userinfo.cardNo,
					type: this.type,
				}
				record(obj).then(res => {
					this.list = res.data
					this.list.list.forEach(e => {
						e.bucket == '1' ? e.timename = '凌晨' : ''
						e.bucket == '2' ? e.timename = '早餐前' : ''
						e.bucket == '3' ? e.timename = '早晨后' : ''
						e.bucket == '4' ? e.timename = '午餐前' : ''
						e.bucket == '5' ? e.timename = '午餐后' : ''
						e.bucket == '6' ? e.timename = '晚餐前' : ''
						e.bucket == '7' ? e.timename = '晚餐后' : ''
						e.bucket == '8' ? e.timename = '睡前' : ''
					})
				})
			},
			getinfo() {
				this.tablelist = []
				this.tabletotal = 0
				this.pageNum = 1
				let userinfo = uni.getStorageSync('userinfo');
				var obj = {
					label: Number(this.tabindex) + 1,
					identity: userinfo.cardNo,
					type: this.type,
					pageNum: this.pageNum,
					pageSize: 15,
				}
				getList(obj).then(res => {
					this.tablelist = res.data.list.list
					this.tabletotal = res.data.list.total
					this.tablelist.forEach(e => {
						e.bucket == '1' ? e.timename = '凌晨' : ''
						e.bucket == '2' ? e.timename = '早餐前' : ''
						e.bucket == '3' ? e.timename = '早晨后' : ''
						e.bucket == '4' ? e.timename = '午餐前' : ''
						e.bucket == '5' ? e.timename = '午餐后' : ''
						e.bucket == '6' ? e.timename = '晚餐前' : ''
						e.bucket == '7' ? e.timename = '晚餐后' : ''
						e.bucket == '8' ? e.timename = '睡前' : ''
					})
				})
			},
			tabchange(e) {
				this.type = 0
				this.list = null
				this.tabindex = e
				setTimeout(() => {
					this.info();
					this.getinfo();
				}, 1000)
			},
		},
		onReachBottom() { //下滑加载
			if (this.tablelist.length >= this.tabletotal) {} else {
				this.pageNum++;
				let userinfo = uni.getStorageSync('userinfo');
				var obj = {
					label: Number(this.tabindex) + 1,
					identity: userinfo.cardNo,
					type: this.type,
					pageNum: this.pageNum,
					pageSize: 15,
				}
				getList(obj).then(res => {
					res.data.list.list.forEach(e => {
						e.bucket == '1' ? e.timename = '凌晨' : ''
						e.bucket == '2' ? e.timename = '早餐前' : ''
						e.bucket == '3' ? e.timename = '早晨后' : ''
						e.bucket == '4' ? e.timename = '午餐前' : ''
						e.bucket == '5' ? e.timename = '午餐后' : ''
						e.bucket == '6' ? e.timename = '晚餐前' : ''
						e.bucket == '7' ? e.timename = '晚餐后' : ''
						e.bucket == '8' ? e.timename = '睡前' : ''
						this.tablelist.push(e)
					})
					this.tabletotal = res.data.list.total
				})
			}
		},
		onPullDownRefresh() { //下拉刷新
			this.getinfo();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #F7F5F5;
	}

	::v-deep .u-tabs {
		background-color: #F7F5F5 !important;
	}
</style>