<template>
	<!-- 历史数据 -->
	<view>
		<view class="card_time_row" :style="{padding:'10rpx 10rpx'}">
			<view class="time_picker">
				<view class="time_title">开始时间</view>
				<uni-datetime-picker style="width: 210rpx;" type="datetime" :clear-icon="false" v-model="startTime"
					@change="maskClick" />
			</view>
			<view class="time_picker">
				<view class="time_title">结束时间</view>
				<uni-datetime-picker style="width: 210rpx;" type="datetime" :clear-icon="false" v-model="endTime"
					@change="maskClick" />
			</view>
		</view>
		<view class="card_layout">
			<view class="facility_card">
				<view class="card_title_name">名称</view>
				<scroll-view class="scroll-view" :style="{height:leftHeight + 'px'}" :scroll-y="true"
					@scrolltolower="loadMore">
					<view class="item_title_name" v-for="(item, index) in tableData" :key="index">
						{{item.name}}
					</view>
				</scroll-view>
			</view>
			<view class="table_card_data">
				<uni-table ref="table" :loading="loading" border emptyText="暂无更多数据">
					<uni-tr>
						<uni-th width="10" align="center"></uni-th>
						<uni-th align="center">名称</uni-th>
						<uni-th width="40" align="center">温度(℃)</uni-th>
						<uni-th width="40" align="center">湿度(℃)</uni-th>
						<uni-th align="center">保存时间</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in tableData" :key="index">
						<uni-td align="center">{{ index + 1 }}</uni-td>
						<uni-td align="center">{{ item.name }}</uni-td>
						<uni-td align="center">
							<view class="name">{{ item.name }}</view>
						</uni-td>
						<uni-td align="center">{{ item.name }}</uni-td>
						<uni-td align="center">{{ item.date }}</uni-td>
					</uni-tr>
				</uni-table>
			</view>
		</view>
	</view>
</template>

<script>
	import tablelist from './tableData.js'
	export default {
		data() {
			return {
				loading: false,
				tableData: tablelist,
				startTime: this.getDateTime(new Date()),
				endTime: this.getDateTime(new Date()),
				leftHeight: 0,
			}
		},
		watch: {
			// demo 是要深度监听的值
			// headHeight: {
			// 	handler(newVal, oldVal) {
			// 		if (newVal) {
						// this.$nextTick(() => {
						// 	let topHeight = 0
						// 	const navbar = uni.createSelectorQuery().select('.card_layout')
						// 	navbar.boundingClientRect(data => {
						// 		topHeight = data.height
						// 	}).exec()
						// 	let titleHeight = 0
						// 	const titleBar = uni.createSelectorQuery().select('.card_title_name')
						// 	titleBar.boundingClientRect(data => {
						// 		titleHeight = data.height
						// 	}).exec()
						// 	this.leftHeight = topHeight - titleHeight
						// })
			// 		}
			// 	},
			// 	immediate: true,
			// 	deep: true
			// }
		},
		mounted() {},
		methods: {
			maskClick(event) {
				console.log(event, 264)
			},
			// 滚动加载更多
			loadMore() {
				console.log(3214)
			},
			getDateTime(date, addZero = true) {
				return `${this.getDate(date, addZero)} ${this.getTime(date, addZero)}`
			},
			getDate(date, addZero = true) {
				date = new Date(date)
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				const day = date.getDate()
				return `${year}-${addZero ? this.addZero(month) : month}-${addZero ? this.addZero(day) : day}`
			},
			getTime(date, addZero = true) {
				date = new Date(date)
				const hour = date.getHours()
				const minute = date.getMinutes()
				const second = date.getSeconds()
				return this.hideSecond ?
					`${addZero ? this.addZero(hour) : hour}:${addZero ? this.addZero(minute) : minute}` :
					`${addZero ? this.addZero(hour) : hour}:${addZero ? this.addZero(minute) : minute}:${addZero ? this.addZero(second) : second}`
			},
			addZero(num) {
				if (num < 10) {
					num = `0${num}`
				}
				return num
			}
		}
	}
</script>

<style lang="scss">
	.card_time_row {
		position: sticky;
		top: 0px;
		background-color: #fff;
		z-index: 2;
		display: flex;
		align-items: center;
		padding-bottom: 10rpx;
	}

	.time_picker {
		display: flex;
		align-items: center;
		margin-right: 20rpx;
	}

	.time_title {
		font-size: 8px;
		margin-right: 10rpx;
	}

	.card_layout {
		display: flex;
		margin-left: 10rpx;
		height: calc(100% - 20rpx);
	}

	.facility_card {
		width: 140rpx;
		height: 100%;
		flex: none;
		border: 1px solid #EBEEF5;
	}

	.scroll-view {
		overflow-y: auto;
	}

	.card_title_name {
		position: sticky;
		top: 23px;
		z-index: 1;
		padding: 8rpx;
		font-size: 8px;
		background-color: #EBEEF5;
	}

	.item_title_name {
		padding: 6rpx;
		font-size: 8px;
		border-bottom: 1px solid #EBEEF5;
	}

	.table_card_data {
		overflow-y: auto;
		width: 100%;
		height: 100%;
		border-top: 1px solid #EBEEF5;
		border-bottom: 1px solid #EBEEF5;
	}
</style>