<template>
	<view class="container clearfix">
		<view class="charts-navbar" id="topBar">
			<view
				class="nav-item tui-skeleton-fillet"
				v-for="(nav, index) in navBarList"
				:key="index"
				:class="{ current: tabCurrentIndex === nav.index }"
				@click="tabChange(nav.index)"
			>
				<text>{{ nav.name }}</text> <text class="line"></text>
			</view>
		</view>
		<view class="charts-content">
			<!-- 重点资料 -->
			<view class="charts-box charts-box0" id="charts-box0"> <echartInfo ref="echart-info" /> </view>
			<!-- 订单 -->
			<view class="charts-box charts-box1" id="charts-box1"> <echartOrder ref="echart-order" /> </view>
			<!-- 关键词 -->
			<view class="charts-box charts-box2" id="charts-box2"> <echartKeyword ref="echart-keyword" /> </view>
			<!-- 咨询记录 -->
			<view class="charts-box charts-box3" id="charts-box3"> <echartContact ref="echart-contact" /> </view>
			<!-- 访问记录 -->
			<view class="charts-box charts-box4" id="charts-box4"> <echartService ref="echart-service" /> </view>
		</view>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { debounce } from '@/common/config/common.js'
import echartInfo from './components/echart-info'
import echartOrder from './components/echart-order'
import echartKeyword from './components/echart-keyword'
import echartContact from './components/echart-contact'
import echartService from './components/echart-service'
const observers = {}
export default {
	components: {
		echartInfo,
		echartOrder,
		echartKeyword,
		echartContact,
		echartService
	},
	data() {
		return {
			navBarList: [
				{ name: '重点资料', index: 0 },
				{ name: '订单数据', index: 1 },
				{ name: '搜索关键词', index: 2 },
				{ name: '咨询记录', index: 3 },
				{ name: '访问记录', index: 4 }
			],
			tabCurrentIndex: 0,
			anchorList: [], // 锚点元素节点信息
			anchorStatus: [0, 0, 0, 0, 0] // 分别代表5个锚点区域的开关 0:关  1:开
		}
	},
	onLoad(option) {},
	onReady() {
		console.log('onReady')
		// if (!this.isRequest) {
		//     this.getAnchorSection()
		// }
		setTimeout(() => {
			this.getAnchorSection()
		}, 2000)
	},
	filters: {},
	computed: {},
	methods: {
		tabChange(index) {
			this.tabCurrentIndex = index
			this.scrollToAnchor()
		},
		// 获取锚点元素信息
		getAnchorSection() {
			const query = uni.createSelectorQuery().in(this)
			query.selectAll('.charts-box').boundingClientRect(data => {
				if (data.length > 0) {
					this.anchorList = data
					this.anchorList.forEach((dom, index) => {
						uni.createSelectorQuery().select('.charts-content').boundingClientRect(data => {
							//目标节点、也可以是最外层的父级节点
							uni.createSelectorQuery().select(`#${dom.id}`).boundingClientRect(res => {
								//dom
								dom.top = res.top - data.top + 5
							}).exec()
						}).exec()
					})
					console.log(this.anchorList)
					console.log('tabs is ready')
					// clearInterval(this.selectorTimer)
					this.observerAnchor(data)
				}
			}).exec()
		},
		// 滚动到锚点
		scrollToAnchor() {
			// const selector = '#anchor' + (this.currentTab + 1)
			let scrollTop = 0
			const query = uni.createSelectorQuery().in(this)
			if (this.tabCurrentIndex > 0) {
				scrollTop = this.anchorList[this.tabCurrentIndex].top
			}
			uni.pageScrollTo({
				scrollTop: scrollTop
			})
		},
		// 为需要观测的区域创建观测者
		observerAnchor(selectorList = []) {
			const height = uni.getSystemInfoSync().windowHeight - 70
			selectorList.forEach((selector, index) => {
				observers[selector.id] = uni.createIntersectionObserver(this)
				observers[selector.id].relativeToViewport({ bottom: -height }).observe(`#${selector.id}`, res => {
					if (res.intersectionRatio > 0) {
						this.anchorStatus[index] = 1
						// console.log(index, `当前区域为${selector.id}标签选择器的区域...`)
					} else {
						this.anchorStatus[index] = 0
						// console.log(index, `离开区域为${selector.id}标签选择器的区域...`)
					}
				})
			})
		},
		// 设置currentTab
		setCurrentTab: debounce(function() {
			const index = this.anchorStatus.lastIndexOf(1)
			if (this.tabCurrentIndex !== index) this.tabCurrentIndex = index
		}, 200)
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop
	},
	onShow() {}
}
</script>

<style lang="scss">
page {
	background-color: #f5f5f5 !important;
}
.container {
	width: 100%;
	height: auto;
	box-sizing: border-box;
	padding: 0 24rpx;
	padding-top: 122rpx;
}
.charts-navbar {
	width: 100%;
	height: 90rpx;
	background-color: #ffffff;
	box-sizing: border-box;
	padding: 20rpx 0;
	display: flex;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 99;
	.nav-item {
		display: flex;
		flex: 1;
		justify-content: center;
		align-items: center;
		height: 50rpx;
		font-size: $font-size-28;
		color: $text-color;
		position: relative;
		float: left;
		position: relative;
		.line {
			width: 60rpx;
			height: 2px;
			border-radius: 1px;
			background: #ffffff;
			position: absolute;
			bottom: 0;
			left: 50%;
			margin-left: -30rpx;
		}
		&.current {
			color: $color-system;
			.line {
				background: $color-system;
			}
		}
	}
}
.charts-content{
	width: 100%;
	height: auto;
}
.charts-box {
	width: 100%;
	min-height: 500rpx;
	box-sizing: border-box;
	padding: 34rpx 24rpx;
	background-color: #fff;
	margin-bottom: 32rpx;
	border-radius: 16rpx;
	float: left;
	.echart-content {
		width: 100%;
		.echart-title {
			width: 100%;
			height: 96rpx;
			float: left;
			box-sizing: border-box;
			padding: 16rpx 0 24rpx 0;
			.e-icon {
				width: 56rpx;
				height: 56rpx;
				float: left;
				text-align: center;
				line-height: 56rpx;
				border-radius: 50%;
				&.e1 {
					background-color: #1890f9;
				}
				&.e2 {
					background-color: #e15616;
				}
				&.e3 {
					background-color: #f2637b;
				}
				&.e4 {
					background-color: #f99e0a;
				}
				&.e5 {
					background-color: #36cbcb;
				}
				.iconfont {
					font-size: 30rpx;
					color: #fff;
				}
			}
			.e-name {
				float: left;
				line-height: 56rpx;
				font-size: 30rpx;
				margin-left: 24rpx;
				color: #333;
			}
			.e-start {
				float: right;
				line-height: 56rpx;
				font-size: 28rpx;
				color: #34cc8c;
			}
		}
		.echart-main {
			width: 100%;
			height: 404rpx;
			box-sizing: border-box;
			padding: 32rpx 40rpx;
			position: relative;
			float: left;
			background-color: #f7f7f7;
			.echart-text {
				width: 100%;
				line-height: 40rpx;
				margin-bottom: 20rpx;
				font-size: 28rpx;
				color: #333;
				float: left;
				.label {
					color: #999;
					text-align-last: justify;
				}
			}
			.echart-next {
				width: 100rpx;
				height: 100%;
				position: absolute;
				right: 10rpx;
				top: 0;
				line-height: 404rpx;
				font-size: 28rpx;
				color: #1890f9;
			}
		}
		.echart-search {
			width: 100%;
			height: auto;
			float: left;
			margin-bottom: 58rpx;
			.echart-search-text {
				width: 100%;
				height: 34rpx;
				margin-bottom: 20rpx;
				.search-text {
					float: left;
					line-height: 34rpx;
					font-size: 24rpx;
					color: #333;
					margin-right: 20rpx;
					text {
						color: #e15616;
					}
				}
			}
			.echart-search-date {
				width: 100%;
				height: 56rpx;
				box-sizing: border-box;
				.line {
					color: #999999;
					float: left;
					line-height: 56rpx;
					margin: 0 20rpx;
				}
				.echart-search-date-input {
					width: 228rpx;
					height: 56rpx;
					border: 1px solid #e2e2e2;
					border-radius: 8rpx;
					box-sizing: border-box;
					padding: 0 20rpx;
					line-height: 56rpx;
					float: left;
					position: relative;
					.input-text {
						display: block;
						height: 56rpx;
						font-size: 26rpx;
						color: #666666;
					}
					.icon-riqi {
						color: #999;
						display: block;
						width: 40rpx;
						height: 56rpx;
						position: absolute;
						right: 10rpx;
						top: 0;
						line-height: 56rpx;
					}
				}
			}
			.echart-search-time {
				width: 100%;
				height: 40rpx;
				margin-top: 24rpx;
				.time-tab {
					width: 80rpx;
					height: 40rpx;
					float: left;
					border-radius: 20rpx;
					margin-right: 24rpx;
					box-sizing: border-box;
					border: 1px solid #b2b2b2;
					line-height: 38rpx;
					font-size: 24rpx;
					text-align: center;
					color: #999;
					&.current {
						background-color: #fef6f3;
						border-color: #e15616;
						color: #e15616;
					}
				}
			}
		}
		.echart-mains {
			width: 100%;
			min-height: 400rpx;
			float: left;
		}
	}
}
</style>
