<template>
	<view class="container clearfix">
		<tui-skeleton
			v-if="skeletonShow"
			backgroundColor="#fafafa"
			borderRadius="10rpx"
			:isLoading="true"
			:loadingType="5"
		></tui-skeleton>
		<template v-else>
			<!-- <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" :clubInfo="clubInfo" :clubId="clubInfo.clubId" v-if="isRequest" />
				</view>
				<!-- 订单 -->
				<view class="charts-box charts-box1" id="charts-box1">
					<echartOrder
						ref="echartOrder"
						:clubId="clubInfo.clubId"
						:totalAmount="totalData.orderTotalAmount"
						:totalNum="totalData.orderTotal"
						v-if="isRequest"
					/>
				</view>
				<!-- 关键词 -->
				<view class="charts-box charts-box2" id="charts-box2">
					<echartKeyword
						ref="echartKeyword"
						:clubId="clubInfo.clubId"
						:totalNum="totalData.totalKeywords"
						v-if="isRequest"
					/>
				</view>
				<!-- 咨询记录 -->
				<view class="charts-box charts-box3" id="charts-box3">
					<echartContact
						ref="echartContact"
						:clubId="clubInfo.clubId"
						:totalNum="totalData.remarksTotal"
						v-if="isRequest"
					/>
				</view>
				<!-- 访问记录 -->
				<view class="charts-box charts-box4" id="charts-box4">
					<echartService ref="echartService" :clubId="clubInfo.clubId" v-if="isRequest" />
				</view>
			</view>
		</template>
	</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'
import chartMixin from './components/mixins/chart.mixin.js'
const observers = {}
export default {
	mixins: [chartMixin],
	components: {
		echartInfo,
		echartOrder,
		echartKeyword,
		echartContact,
		echartService
	},
	data() {
		return {
			skeletonShow: true,
			isRequest: false,
			clubInfo: {},
			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:开
			totalData: {
				orderTotalAmount: 0, // 订单总金额
				orderTotal: 0, // 订单总量
				remarksTotal: 0, // 所有咨询记录条数
				totalKeywords: 0 // 所有关键词条数
			}
		}
	},
	onLoad(option) {
		this.getClubInfo(option.userId)
	},
	onReady() {
		console.log('onReady')
		setTimeout(() => {
			this.getAnchorSection()
		}, 2000)
	},
	methods: {
		async getClubInfo(userId) {
			//获取机构信息
			try {
				const clubRes = await this.UserService.OrganizationUpdateModifyInfo({ userId: userId })
				this.clubInfo =  clubRes.data.club
				const tialRes = await this.UserService.userClubInitial({ clubId: this.clubInfo.clubId })
				this.totalData.orderTotal = tialRes.data.orderTotal
				this.totalData.orderTotalAmount = tialRes.data.orderTotalAmount
				this.totalData.remarksTotal = tialRes.data.remarksTotal
				this.totalData.totalKeywords = tialRes.data.totalKeywords
				setTimeout(() => {
					this.skeletonShow = false
					this.isRequest = true
				}, 1000)
			} catch (error) {
				console.log(error)
			}
		},
		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 + 8
										})
										.exec()
								})
								.exec()
						})
						console.log(this.anchorList)
						this.observerAnchor(data)
					}
				})
				.exec()
		},
		// 滚动到锚点
		scrollToAnchor() {
			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}标签选择器的区域...`)
					}
				})
			})
		}
	},
	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: 24rpx;
}
.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: 9999999;
	.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: 350rpx;
	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: #FF5B00;
				}
				&.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%;
			min-height: 200rpx;
			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: 50%;
				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: #FF5B00;
					}
				}
			}
			.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: 24rpx;
						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: #FF5B00;
						color: #FF5B00;
					}
				}
			}
		}
		.echart-mains {
			width: 100%;
			min-height: 300rpx;
			float: left;
			.echart-mains-none {
				width: 100%;
				height: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;
				.none-image {
					width: 220rpx;
					height: 220rpx;
					margin-bottom: 20rpx;
				}
				.none-text {
					font-size: $font-size-28;
					color: #999999;
					line-height: 44rpx;
				}
			}
		}
	}
}
</style>
