<!-- 数据组件页面 -->
<template>
	<view class="column flex-1 hb">
		<view class="topdata">
			<!-- #ifdef MP-WEIXIN -->
			<view class="h10"></view>
			<!-- #endif -->
			<view class="topdata-box" @click="operation">
				<view class="justify-between">
					<view>数据汇总</view>
					<view class="justify-between center">
						<text class="fs24 " style="color: #666;">查看详细数据</text>
						<u-icon name="arrow-right-double" color="#666666" size="14"></u-icon>
					</view>
				</view>
				<view class="x-bc">
					<view class="topdata-item">
						<view class="topdata-item-num"><text>{{companyUserUserCount}}</text>人</view>
						<view>会员总数</view>
					</view>
					<view class="topdata-item">
						<view class="topdata-item-num"><text>{{companyUserCount}}</text>人</view>
						<view>今日新增会员</view>
					</view>
				</view>
			</view>
			<view>
				<u-collapse ref="pydameinvRef" :value="['show']" :border='false' @change='changelable'>
					<!-- #ifdef MP-WEIXIN -->
					<u-collapse-item name="show" title="标签统计">
					<!-- #endif -->
						<!-- #ifndef MP-WEIXIN -->
						<u-collapse-item name="show">
							<view slot="title">
								<text class="bold fs32">标签统计</text>
								<text class="fs24 base-color-9 ml12">左滑查看更多</text>
							</view>
							<!-- #endif -->
							<text slot="value" class="statistics-slot-title">{{collapseLable?'展开':'收回'}}</text>
							<view slot="right-icon">
								<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
							</view>
							<view class="">
								<!-- #ifdef MP-WEIXIN -->
								<view class="fs24 mb12">左滑查看更多</view>
								<!-- #endif -->
								<view class=" mb20 morelable">
									<view class="base-bg-f5 p20  radius20 boxlable mr40"
										v-for="(item,index) in lablelistnum" :key="index"
										style="display: inline-block;">
										<view class="base-color-3 ">{{item.tagName}}</view>
										<view class="base-color mt8">
											<text class="fs40 bold">{{item.number}}</text>
											人
										</view>
									</view>
								</view>
							</view>
						</u-collapse-item>
				</u-collapse>
			</view>
			<view class="searchbox">
				<view class="searchbox-bar x-ac">
					<view :class="queryParam.type == index ? 'searchbox-item searchbox-active':'searchbox-item'"
						v-for="(item,index) in typeOption" :key="index" @click="handleType(index)">{{item.label}}</view>
				</view>
				<view class="x-ac ss" v-show="queryParam.type == 3">
					<view class="calendar-day x-c" @click="showCalendar = true"><u-icon name="calendar" color="#999"
							size="20"></u-icon>{{startTime}}</view>
					<view class="calendar-day x-c" @click="showCalendar = true"><u-icon name="calendar" color="#999"
							size="20"></u-icon>{{endTime}}</view>
				</view>
			</view>
		</view>
		<view class="">
			<u-calendar :show="showCalendar" :mode="mode" @confirm="confirmCalendar" style="flex: 0; overflow: hidden;"
				@close="closeCalendar" :minDate="new Date().getTime()-(3600*1000*24*60)" :maxDate="new Date().getTime()"
				></u-calendar>
		</view>
		<view class="statistics pb120" :style="{height: contentH}">
			<u-collapse :border='false' :value='collapseValue' @change="changeCollapse">
				<!-- #ifdef MP-WEIXIN -->
				<u-collapse-item name="course" title="课程统计">
				<!-- #endif -->
				<!-- #ifndef MP-WEIXIN -->
				<u-collapse-item name="course">
						<text slot="title" class="statistics-title">课程统计</text>
						<!-- #endif -->
						<text slot="value"
							class="statistics-slot-title">{{collapseValue.includes('course')?'收回':'展开'}}</text>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view class="collapse-content x-ac">
							<view class="collapse-content-item">
								<view class="collapse-content-title">观看人数</view>
								<view class="collapse-content-num"><text>{{courseCount.courseWatchNum?courseCount.courseWatchNum:'0' }}</text>人
								</view>
								<view style="color: #1677ff;" v-show="queryParam.type == 0">
									较昨天持平{{compare(courseCount.courseWatchNum,yesterdayVO.courseWatchNum?yesterdayVO.courseWatchNum:'0')}}</view>
								<view v-show="queryParam.type == 0">昨天{{yesterdayVO.courseWatchNum?yesterdayVO.courseWatchNum:'0'}}</view>
							</view>
							<view class="collapse-content-item">
								<view class="collapse-content-title">完播人数</view>
								<view class="collapse-content-num"><text>{{courseCount.courseCompleteNum || 0}}</text>人
								</view>
								<view style="color: #1677ff;" v-show="queryParam.type == 0">
									较昨天持平{{compare(courseCount.courseCompleteNum,yesterdayVO.courseCompleteNum)}}
								</view>
								<view v-show="queryParam.type == 0">昨天{{yesterdayVO.courseCompleteNum || 0}}
								</view>
							</view>
							<view class="collapse-content-item">
								<view class="collapse-content-title">完播率</view>
								<view class="collapse-content-num"><text>{{courseCount.courseCompleteRate || 0}}</text>%
								</view>
								<view style="color: #1677ff;" v-show="queryParam.type == 0">
									较昨天持平{{compare(courseCount.courseCompleteRate,yesterdayVO.courseCompleteRate)}}
								</view>
								<view v-show="queryParam.type == 0">昨天{{yesterdayVO.courseCompleteRate || 0}}</view>
							</view>
						</view>
					</u-collapse-item>
					<!-- #ifdef MP-WEIXIN -->
					<u-collapse-item name="questions" title="答题统计">
					<!-- #endif -->
						<!-- #ifndef MP-WEIXIN -->
						<u-collapse-item name="questions">
							<text slot="title" class="statistics-title">答题统计</text>
							<!-- #endif -->
							<text slot="value"
								class="statistics-slot-title">{{collapseValue.includes('questions')?'收回':'展开'}}</text>
							<view slot="right-icon">
								<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
							</view>
							<view class="collapse-content x-ac">
								<view class="collapse-content-item">
									<view class="collapse-content-title">答题人数</view>
									<view class="collapse-content-num"><text>{{courseCount.answerNum || 0}}</text>人
									</view>
									<view style="color: #1677ff;" v-show="queryParam.type == 0">
										较昨天持平{{compare(courseCount.answerNum,yesterdayVO.answerNum)}}</view>
									<view v-show="queryParam.type == 0">昨天{{yesterdayVO.answerNum || 0}}</view>
								</view>
								<view class="collapse-content-item">
									<view class="collapse-content-title">正确人数</view>
									<view class="collapse-content-num"><text>{{courseCount.answerRightNum || 0}}</text>人
									</view>
									<view style="color: #1677ff;" v-show="queryParam.type == 0">
										较昨天持平{{compare(courseCount.answerRightNum,yesterdayVO.answerRightNum)}}</view>
									<view v-show="queryParam.type == 0">昨天{{yesterdayVO.answerRightNum || 0}}</view>
								</view>
								<view class="collapse-content-item">
									<view class="collapse-content-title">正确率</view>
									<view class="collapse-content-num">
										<text>{{courseCount.answerRightRate || 0}}</text>%
									</view>
									<view style="color: #1677ff;" v-show="queryParam.type == 0">
										较昨天持平{{compare(courseCount.answerRightRate,yesterdayVO.answerRightRate)}}</view>
									<view v-show="queryParam.type == 0">昨天{{yesterdayVO.answerRightRate || 0}}</view>
								</view>
							</view>
						</u-collapse-item>
						<!-- #ifdef MP-WEIXIN -->
						<u-collapse-item name="redenvelope" title="红包统计">
						<!-- #endif -->
							<!-- #ifndef MP-WEIXIN -->
							<u-collapse-item name="redenvelope">
								<text slot="title" class="statistics-title">红包统计</text>
								<!-- #endif -->
								<text slot="value"
									class="statistics-slot-title">{{collapseValue.includes('redenvelope')?'收回':'展开'}}</text>
								<view slot="right-icon">
									<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
								</view>
								<view>
									<view class="collapse-content x-ac">
										<view class="collapse-content-item">
											<view class="collapse-content-title">答题红包数</view>
											<view class="collapse-content-num">
												<text>{{courseCount.redPacketNum || 0}}</text>个
											</view>
											<view style="color: #1677ff;" v-show="queryParam.type == 0">
												较昨天持平{{compare(courseCount.redPacketNum,yesterdayVO.redPacketNum)}}
											</view>
											<view v-show="queryParam.type == 0">昨天{{yesterdayVO.redPacketNum || 0}}
											</view>
										</view>
										<view class="collapse-content-item">
											<view class="collapse-content-title">答题红包金额</view>
											<view class="collapse-content-num">
												<text>{{courseCount.redPacketAmount || '0.00'}}</text>元
											</view>
											<view style="color: #1677ff;" v-show="queryParam.type == 0">
												较昨天持平{{compare(courseCount.redPacketAmount,yesterdayVO.redPacketAmount,1)}}
											</view>
											<view v-show="queryParam.type == 0">
												昨天{{yesterdayVO.redPacketAmount || '0.00'}}
											</view>
										</view>
									</view>
								</view>
							</u-collapse-item>
							<!-- #ifdef MP-WEIXIN -->
							<u-collapse-item name="funnel" title="转化漏斗图" v-if="!ratelistState">
							<!-- #endif -->
								<!-- #ifndef MP-WEIXIN -->
								<u-collapse-item name="funnel" v-if="!ratelistState">
									<text slot="title" class="statistics-title">转化漏斗图</text>
									<!-- #endif -->
									<text slot="value"
										class="statistics-slot-title">{{collapseValue.includes('funnel')?'收回':'展开'}}</text>
									<view slot="right-icon">
										<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
									</view>
									<view v-show="!ratelistState">
										<funnelChart :getratelist="getrateimg" />
									</view>
									<view v-show="ratelistState" class="center">暂无数据</view>
								</u-collapse-item>
								<!-- #ifdef MP-WEIXIN -->
								<u-collapse-item name="rank" title="排行榜">
								<!-- #endif -->
									<!-- #ifndef MP-WEIXIN -->
									<u-collapse-item name="rank">
										<text slot="title" class="statistics-title">排行榜</text>
										<!-- #endif -->
										<text slot="value"
											class="statistics-slot-title">{{collapseValue.includes('rank')?'收回':'展开'}}</text>
										<view slot="right-icon">
											<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
										</view>
										<view class="flex-1">
											<!-- <view class="mt40">
												<view class="justify-between align-center">
													<view class="base-color-3 bold fs32">群管排行榜</view>
													<view class="justify-start" @click="ordergroup(1)"
														v-if="orderGroup=='asc'">
														<u-icon name="list-dot" size="20"></u-icon>
														<view>按正序(前20名)</view>
													</view>
													<view class="justify-start" @click="ordergroup(0)" v-else>
														<u-icon name="list-dot" size="20"></u-icon>
														<view>按倒序(后20名)</view>
													</view>
												</view>
												<view class="centerV">
													<u-tabs :list="list1" @click="clickTab" lineColor='#1773ff'
														lineWidth='40' activeStyle="font-weight:bold"></u-tabs>
												</view>
												<view class="justify-start align-center mtb28"
													v-for="(item,index) in rankListA" :key="index">
													<u-avatar :src="item.avatar" size="40"></u-avatar>
													<view class="flex-1 ml20">
														<view class="justify-between mb16">
															<view class="base-color-3 single-line-ellipsis">
																{{item.userName}}
															</view>
															<view v-if="index==0">{{item.completeRate}}%</view>
															<view v-else>{{item.answerRightRate}}%</view>
														</view>
														<u-line-progress :percentage="item.completeRate"
															activeColor='#1773ff' v-if="index==0" />
														<u-line-progress :percentage="item.answerRightRate"
															activeColor='#1773ff' v-else />
													</view>
												</view>
												<view v-if="rankListA.length==0" class="center mtb32">暂无数据</view>
											</view> -->
											<view class="mt60 column flex-1">
												<view class="justify-between align-center">
													<view class="base-color-3 bold fs32">课程排行榜</view>
													<view class="justify-start" @click="ordergroupB(1)"
														v-if="orderGroupB=='asc'">
														<u-icon name="list-dot" size="20"></u-icon>
														<view>按正序(前20名)</view>
													</view>
													<view class="justify-start" @click="ordergroupB(0)" v-else>
														<u-icon name="list-dot" size="20"></u-icon>
														<view>按倒序(后20名)</view>
													</view>
												</view>
												<view class="centerV">
													<u-tabs :list="list1" @click="clickTabB" lineColor='#1773ff'
														lineWidth='40' activeStyle="font-weight:bold"></u-tabs>
												</view>
												<view class="justify-start align-center"
													v-for="(item,index) in rankListB" :key="index">
													<view class="flex-1 ml20">
														<view class="justify-between mb16">
															<view class="base-color-3 single-line-ellipsis">
																{{item.videoName}}
															</view>
															<view v-if="index==0">{{item.completeRate}}%</view>
															<view v-else>{{item.answerRightRate}}%</view>
														</view>
														<u-line-progress :percentage="item.completeRate"
															activeColor='#1773ff' v-if="index==0" />
														<u-line-progress :percentage="item.answerRightRate"
															activeColor='#1773ff' v-else />
													</view>
												</view>
												<view v-if="rankListB.length==0" class="center mtb32">暂无数据</view>
											</view>
										</view>
									</u-collapse-item>
			</u-collapse>
			<view class="h70"></view>
		</view>
		<u-loading-page :loading="viewload" iconSize="32" loadingColor="#3c9cff" fontSize="20"
		:loading-text="loadingtext"></u-loading-page>
	</view>
</template>

<script>
	// import code from "@/uni_modules/uview-ui/libs/config/props/code"
	import funnelChart from "../components/chart.vue"
	import {
		getCompanyUserAndUserCount,
		getCourseCount,
		getcourseRate,
		getGroupRanklist,
		getCourseRanklist
	} from "@/api/manageCompany.js"
	export default {
		components: {
			funnelChart,
		},
		data() {
			return {
				viewload:true,
				loadingtext:'数据加载中...',
				list1: [{
					name: '按完播率',
				}, {
					name: '按正确率'
				}],
				orderGroup: 'asc',
				orderGroupB: 'asc',
				showCalendar: false,
				mode: 'range',
				typeOption: [{
					label: '今日',
					value: 0
				}, {
					label: '昨日',
					value: 1
				}, {
					label: '本月',
					value: 2
				}, {
					label: '自定义',
					value: 3
				}],
				queryParam: {
					type: 0
				},
				contentH: 0,
				contentHs: 0,
				collapseValue: ['course', 'questions', 'redenvelope', 'funnel', 'rank'],
				user: {},
				todayday: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
				startTime: '',
				endTime: '',
				companyUserCount: 0,
				companyUserUserCount: 0,
				courseCount: {
					courseWatchNum: 0
				},
				collapseLable: false,
				lablelistnum: [],
				getrateimg: {},
				rankListA: [],
				rankListB: [],
				activeA: 1,
				activeB: 1,
				yesterdayVO: [],
				ratelistState: false
			}
		},
		computed: {
			compare() {
				return (today, yesterday, type) => {
					const num = Number(yesterday || 0) - Number(today || 0)
					return type == 1 ? num.toFixed(2) : 0
				}
			}
		},
		onShow() {
			
		},
		mounted() {
			this.getHeight()
			this.user = uni.getStorageSync("companyUserInfo") ? JSON.parse(uni.getStorageSync("companyUserInfo")) : {}
			// this.resetDate()
			this.getrateList()
			this.getrankCourse()
			this.getrankgroup()
			this.getUserCount()
		},
		methods: {
			operation() {
				console.log(123)
				uni.navigateTo({
					url: '/pages_manage/dataDetails'
				})
			},
			clickTab(item) {
				this.activeA = item.index + 1
				this.getrankgroup()
				console.log(this.activeA)
			},
			clickTabB(item) {
				this.activeB = item.index + 1
				this.getrankCourse()
				console.log(this.activeA)
			},
			ordergroup(item) {
				if (item == 0) {
					this.orderGroup = 'asc'
					this.getrankgroup()
				} else {
					this.orderGroup = 'desc'
					this.getrankgroup()
				}
			},
			ordergroupB(item) {
				if (item == 0) {
					this.orderGroupB = 'asc'
					this.getrankCourse()
				} else {
					this.orderGroupB = 'desc'
					this.getrankCourse()
				}
			},
			getrankCourse() {
				//获取课程排行
				// this.startTime = this.todayday
				// this.endTime = this.todayday
				// this.resetDate()
				
				const params = {
					endTime: this.endTime + ' 23:59:59',
					startTime: this.startTime + ' 00:00:00',
					order: this.orderGroupB,
					type: this.activeB, // 1-按完播率，2-按正确率
				}
				getCourseRanklist(params).then(res => {
					if (res.code == 200) {
						this.rankListB = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getrankgroup() {
				//获取群管排行
				this.startTime = this.todayday
				this.endTime = this.todayday
				// this.resetDate()
				const params = {
					endTime: this.endTime + ' 23:59:59',
					startTime: this.startTime + ' 00:00:00',
					order: this.orderGroup,
					type: this.activeA, // 1-按完播率，2-按正确率
				}
				getGroupRanklist(params).then(res => {
					if (res.code == 200) {
						console.log(res)
						this.rankListA = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getrateList() {
				//获取漏斗图
				this.startTime = this.todayday
				this.endTime = this.todayday
				this.resetDate()
				const params = {
					endTime: this.endTime + ' 23:59:59',
					startTime: this.startTime + ' 00:00:00',
				}
				getcourseRate(params).then(res => {
					if (res.code == 200) {
						this.ratelistState = res.data.every(item => item.value === 0);
						this.getrateimg = res
						console.log('漏斗',this.getrateimg)
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			changelable(e) {
				if (e[0].status == 'open') {
					console.log(e)
					this.collapseLable = false
				} else {
					this.collapseLable = true
				}
			},
			getHeight() {
				const windowHeight = uni.getSystemInfoSync().windowHeight
				const query = uni.createSelectorQuery().in(this);
				query
					.select(".topdata")
					.boundingClientRect((data) => {
						this.contentH = `calc(${windowHeight-120}px - ${data.height}px - 0px)`
					})
					.exec()
			},
			resetDate() {
				if (this.queryParam.type == 0) {
					this.startTime = this.todayday
					this.endTime = this.todayday
				} else if (this.queryParam.type == 1) {
					let yesterday = new Date();
					yesterday.setDate(yesterday.getDate() - 1);

					this.startTime = uni.$u.timeFormat(yesterday, 'yyyy-mm-dd')
					this.endTime = uni.$u.timeFormat(yesterday, 'yyyy-mm-dd')
				} else if (this.queryParam.type == 2) {
					let today = new Date();
					let lastDayOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);

					this.startTime = uni.$u.timeFormat(this.todayday, 'yyyy-mm') + '-01'
					this.endTime = uni.$u.timeFormat(lastDayOfMonth, 'yyyy-mm-dd')
				} else if (this.queryParam.type == 3) {
					this.startTime = this.todayday
					this.endTime = this.todayday
				}
				this.getCount()
			},
			handleType(type) {
				if (this.queryParam.type == type) {
					return
				}
				this.queryParam.type = type
				if (this.queryParam.type == 3) {
					this.getHeight()
				}
				this.resetDate()
				this.getrateList()
				this.getrankCourse()
				this.getrankgroup()
			},
			changeCollapse(e) {
				this.collapseValue = e.filter(item => item.status == 'open').map(it => it.name)
			},
			closeCalendar() {
				this.showCalendar = false
			},
			confirmCalendar(e) {
				this.startTime = e[0]
				this.endTime = e[e.length - 1]
				this.showCalendar = false
				this.getCount()
			},
			getUserCount() {
				getCompanyUserAndUserCount().then(res => {
					if (res.code == 200) {
						this.companyUserCount = res.data.todayNewUser || 0
						this.companyUserUserCount = res.data.userTotal || 0
						this.lablelistnum = res.data.tagList
						this.$nextTick(() => {
							this.viewload=false
							this.$refs.pydameinvRef.init()
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
			getCount() {
				const param = {
					endTime: this.endTime + ' 23:59:59',
					startTime: this.startTime + ' 00:00:00',
				}
				this.getCourseCounts(param)
			},
			getCourseCounts(param) {
				getCourseCount(param).then(res => {
					if (res.code == 200) {
						console.log('课程统计',res)
						this.courseCount = res.data
						this.yesterdayVO = res.data.yesterdayVO
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.boxlable {
		width: 47%;
	}

	.morelable {
		width: 100%;
		overflow-x: auto;
		/* 添加横向滚动条 */
		white-space: nowrap;
	}

	.containers {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 14px;
		color: #222222;
	}

	.calendar-day {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 12px;
		color: #999;
		flex: 1;
		margin: 10px 10px 0 10px;
		background-color: #f5f5f5;
		border-radius: 4px;
	}

	.topdata {
		// padding-top: 25px;
		background: linear-gradient(to right, rgba(225, 238, 255, 1), rgba(223, 224, 254, 1));
		&-box {
			padding: 12px;
			margin: 0 12px 0 12px;
			background-color: rgba(255, 255, 255, 0.5);
			border-radius: 8px 8px;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 14px;
			color: #222222;
		}

		&-item {
			width: 50%;
			text-align: center;
			padding: 8px;
			box-sizing: border-box;

			&-num {
				color: #1677ff;

				text {
					font-family: DIN, DIN;
					font-weight: bold;
					font-size: 25px;
				}
			}
		}

		.searchbox {
			background-color: #fff;
			border-radius: 8rpx 8rpx 0 0;
			padding: 15px;

			&-item {
				height: 23px;
				line-height: 23px;
				width: 65px;
				background: #f5f5f5;
				text-align: center;
				border-radius: 5px;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 12px;
				color: #888;
			}

			&-active {
				color: #fff !important;
				background-color: #1677ff !important;
			}
		}
	}

	.statistics {
		background-color: #fff;
		overflow-y: auto;
		box-sizing: border-box;

		&-title {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 16px;
			color: #222222;
		}

		&-slot-title {
			font-size: 12px;
			color: #1677ff;
		}

		.collapse-content {
			margin: 0 -8px -8rpx 0;

			&-item {
				flex: 1;
				padding: 12px;
				box-sizing: border-box;
				border-radius: 10px;
				background: #f5f5f5;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 10px;
				color: #222222;
				margin: 0 8px 8rpx 0;
			}

			&-title {
				font-size: 14px;
				margin-bottom: 10px;
			}

			&-num {
				color: #1677ff;
				font-size: 10px;

				text {
					font-family: DIN, DIN;
					font-weight: bold;
					font-size: 25px;
				}
			}
		}
	}
</style>