<template>
	<view class="container clearfix">
		<tui-skeleton
			v-if="skeletonShow"
			backgroundColor="#fafafa"
			borderRadius="10rpx"
			:isLoading="true"
			:loadingType="5"
		/>
		<template v-else>
			<view class="charts-content">
				<!-- 机构活跃统计 -->
				<view class="charts-box charts-box0" id="charts-box0">
					<echart-active
						ref="echart-active"
						:clubId="clubInfo.clubId"
						:totalAmount="totalData.orderTotalAmount"
						:totalNum="totalData.orderTotal"
						v-if="isRequest"
					/>
				</view>
				<!-- 机构填写咨询记录统计 -->
				<view class="charts-box charts-box1" id="charts-box1">
					<echart-consult
						ref="echart-consult"
						:clubId="clubInfo.clubId"
						:totalAmount="totalData.orderTotalAmount"
						:totalNum="totalData.orderTotal"
						v-if="isRequest"
					/>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { debounce } from '@/common/config/common.js'
import echartActive from './components/echart-active'
import echartConsult from './components/echart-consult'
import chartMixin from './components/mixins/chart.mixin.js'
const observers = {}
export default {
	mixins: [chartMixin],
	components: {
		echartActive,
		echartConsult,
	},
	data() {
		return {
			skeletonShow: true,
			isRequest: false,
			clubInfo: {},
			clubUserInfo: {},
			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: 666747 })
				this.clubInfo =  clubRes.data.club
				this.clubUserInfo =  clubRes.data.user
				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;
			.e-name {
				float: left;
				line-height: 56rpx;
				font-size: 30rpx;
				color: #333;
			}
			.e-more {
				float: right;
				line-height: 56rpx;
				font-size: 28rpx;
				color: #999999;
			}
		}
		.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-mains {
			width: 100%;
			min-height: 350rpx;
			float: left;
			&.demand{
				min-height: 200rpx;
			}
			.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>
