<template>
	<view class="column flex-1 hb">
		<!-- <view class="headnav x-bc">
			<view :class="activeTab == 0 ? 'headnav-item headnav-active':'headnav-item'" @click="handleNav(0)">
				<view>直播计划</view>
				<view class="headnav-num">3</view>
				<image src="@/static/images/idle.png" mode="aspectFill"></image>
			</view>
			<view :class="activeTab == 1? 'headnav-item headnav-active':'headnav-item'" @click="handleNav(1)">
				<view>今日直播</view>
				<view class="headnav-num">3</view>
				<image src="@/static/images/streaming.png" mode="aspectFill"></image>
			</view>
			<view :class="activeTab == 2 ? 'headnav-item headnav-active':'headnav-item'" @click="handleNav(2)">
				<view>往日直播</view>
				<view class="headnav-num">3</view>
				<image src="@/static/images/finished.png" mode="aspectFill"></image>
			</view>
		</view> -->
		<view>
			<dropdownPanel :filterData='filterData' @onChange="onChange" @confirm="confirm" @reset="reset">
				<view class="column flex-1 hb hidden">
					<view v-if="searchbarNav == 0">
						<view class="p20 fs28 column flex-1 scrolly">
							<view v-for="(item,index) in courseOne" :key="item.index"
								:class="courseid==item.courseId?'actNav':''" class="m10 p10 center"
								style="border-bottom: 2rpx solid #eee;" @click="getCourseOne(item.courseId)">
								{{item.courseName}}
							</view>
						</view>
					</view>
					<view v-else class="p20 fs28 column flex-1 hidden">
						<scroll-view scroll-y="true" class="hb" :refresher-enabled="isEnabled"
							:refresher-triggered="triggered" refresher-background="rgba(0,0,0,0)"
							@refresherrefresh="pullDownRefresh" @refresherrestore="triggered = false"
							:upper-threshold="100" :lower-threshold="100" @refresherabort="triggered = false"
							@scrolltolower="reachBottom">
							<view v-for="(item,index) in courseTwo" :key="item.index"
								:class="courseids==item.videoId?'actNav':''" class="m10 p10 center"
								style="border-bottom: 2rpx solid #eee;" @click="getCourseTwo(item.videoId)">
								{{item.title}}
							</view>
							<u-loadmore :status="status" />
							<view class="ptb40"></view>
						</scroll-view>
					</view>
				</view>
			</dropdownPanel>
		</view>
		<view class="bgf m20 radius8 column flex-1 scrolly" style="max-height: 100%;">
			<view class="column flex-1 scrolly">
				<u-collapse ref="collapseRef" :border="false" :value="collapseValue" @change="changeCollapse">
					<u-collapse-item name="course">
						<text slot="title" class="bold fs32">课程统计</text>
						<text slot="value" class="base-color fs24">{{collapseValue.includes('course')?'收回':'展开'}}</text>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view class="justify-around ">
							<view class="base-bg-f8 radius16 p30 flex-1">
								<view class="mb12 bold">营期</view>
								<view class="base-color"><text class="bold fs40">
										{{courselist.course}}</text>期</view>
							</view>
							<view class="base-bg-f8 radius16 p20 flex-1 mlr20">
								<view class="mb12 bold">课程数</view>
								<view class="base-color"><text class="bold fs40">
										{{courselist.count}}</text>课</view>
							</view>
							<view class="base-bg-f8 radius16 p20 flex-1">
								<view class="mb12 bold">参与会员</view>
								<view class="base-color"><text class="bold fs40">
										{{courselist.watchCount}}</text>人</view>
							</view>
						</view>
					</u-collapse-item>
					<!-- <u-collapse-item name="questions">
						<text slot="title" class="bold fs32">直播统计</text>
						<text slot="value"
							class="base-color fs24">{{collapseValue.includes('questions')?'收回':'展开'}}</text>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view class="justify-around ">
							<view class="base-bg-f8 radius16 p30 flex-1">
								<view class="mb12 bold">观看人次</view>
								<view class="base-color"><text class="bold fs40">3</text>人</view>
							</view>
							<view class="base-bg-f8 radius16 p20 flex-1 mlr20">
								<view class="mb12 bold">完播人次</view>
								<view class="base-color"><text class="bold fs40">3</text>人</view>
							</view>
							<view class="base-bg-f8 radius16 p20 flex-1">
								<view class="mb12 bold">完播率</view>
								<view class="base-color"><text class="bold fs40">3</text>%</view>
							</view>
						</view>
					</u-collapse-item> -->
					<u-collapse-item name="redenvelope">
						<text slot="title" class="bold fs32">答题统计</text>
						<text slot="value"
							class="base-color fs24">{{collapseValue.includes('redenvelope')?'收回':'展开'}}</text>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view class="justify-around ">
							<view class="base-bg-f8 radius16 p30 flex-1">
								<view class="mb12 bold">答题人次</view>
								<view class="base-color"><text class="bold fs40">
										{{answerlist.count}}</text>人</view>
							</view>
							<view class="base-bg-f8 radius16 p20 flex-1 mlr20">
								<view class="mb12 bold">正确人次</view>
								<view class="base-color"><text class="bold fs40">
										{{answerlist.completeCount}}</text>人</view>
							</view>
							<view class="base-bg-f8 radius16 p20 flex-1">
								<view class="mb12 bold">正确率</view>
								<view class="base-color"><text class="bold fs40">
										{{answerlist.rate}}</text>%</view>
							</view>
						</view>
					</u-collapse-item>
					<u-collapse-item name="live">
						<text slot="title" class="bold fs32">红包统计</text>
						<text slot="value" class="base-color fs24">{{collapseValue.includes('live')?'收回':'展开'}}</text>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view class="justify-around ">
							<view class="base-bg-f8 radius16 p30 flex-1">
								<view class="mb12 bold">红包个数</view>
								<view class="base-color"><text class="bold fs40">
										{{redbaglist.count}}</text>个</view>
							</view>
							<view class="base-bg-f8 radius16 p20  mlr20 justify-start align-center">
								<image src="../../../static/images/redenvelope.png" class="w102 h102"></image>
								<view class="ml20">
									<view class="mb12 bold">答题红包金额</view>
									<view class="base-color">
										<text class="bold fs40">{{redbaglist.amount}}</text>元
									</view>
								</view>
							</view>
						</view>
					</u-collapse-item>
					<u-collapse-item name="funnel">
						<text slot="title" class="bold fs32">转化漏斗图</text>
						<text slot="value" class="base-color fs24">{{collapseValue.includes('funnel')?'收回':'展开'}}</text>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view>
							<funnelChart :getratelist="getrateimg" />
						</view>
					</u-collapse-item>
					<u-collapse-item name="rank">
						<text slot="title" class="bold fs32">排行榜</text>
						<text slot="value" class="base-color fs24">{{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">{{item.name}}</view>
											<view>{{item.rate}}%</view>
										</view>
										<u-line-progress :percentage="30" activeColor='#1773ff' />
									</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">{{item.name}}</view>
											<view>{{item.rate}}%</view>
										</view>
										<u-line-progress :percentage="30" activeColor='#ed0922' />
									</view>
								</view>
								<view v-if="rankListB.length==0" class="center mtb32">暂无数据</view>
							</view>
						</view>
					</u-collapse-item>
				</u-collapse>
			</view>
			<view class="h120"></view>
		</view>
	</view>
</template>

<script>
	import {
		getFsCourseList,
		getCourseVdieoList,
		getshopCoursenum,
		getQuesCount,
		getRecPacketCount,
		getcourseRate,
		getGroupRanklist,
		getCourseRanklist
	} from "@/api/courseManage.js"
	import dropdownPanel from "../components/dropdownPanel.vue"
	import parse from "../../../uni_modules/uview-ui/libs/config/props/parse"
	import funnelChart from "../components/chart.vue"
	export default {
		components: {
			dropdownPanel,
			funnelChart,
		},
		data() {
			return {
				activeTab: 0,
				showCalendar: false,
				user: {},
				filterData: [{
						name: '训练营-营期',
						value: 1,
					},
					{
						name: '课程',
						value: 2,
					}
				],
				searchbarNav: 0,
				collapseValue: ['course', 'questions', 'redenvelope', 'live', 'funnel', 'rank'],
				courseOne: [],
				courseTwo: [],
				courseid: '',
				courseids: '',
				triggered: false,
				status: 'loadmore',
				isEnabled: true,
				pageNum: 1,
				pageSize: 10,
				courselist: [],
				answerlist: [],
				redbaglist: [],
				getrateimg: {},
				list1: [{
					name: '按完播率',
				}, {
					name: '按正确率'
				}],
				orderGroup: 'asc',
				orderGroupB: 'asc',
				rankListA: {},
				rankListB: {},
				activeA: 1,
				activeB: 1,
			}
		},
		async mounted() {
			this.user = uni.getStorageSync("companyUserInfo") ? JSON.parse(uni.getStorageSync("companyUserInfo")) : {}
			this.getCoursestatistics()
			this.getquslist()
			this.getredbaglist()
			this.getrateList()
			this.getrankgroup()
			await this.getrankCourse()
			this.$nextTick(() => {
				setTimeout(()=>{
					this.$refs?.collapseRef?.init()
				},1000)
			})
		},
		methods: {
			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() {
				//获取课程排行
				// console.log(this.orderGroupB)
				const params = {
					companyId: this.user.companyId,
					endTime: '',
					startTime: '',
					courseId: this.courseid,
					videoId: this.courseids,
					order: this.orderGroupB,
					type: this.activeB, // 0:经销商/1:群管
				}
				getCourseRanklist(params).then(res => {
					if (res.code == 200) {
						// console.log(res)
						this.rankListB = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getrankgroup() {
				//获取群管排行
				const params = {
					companyId: this.user.companyId,
					endTime: '',
					startTime: '',
					courseId: this.courseid,
					videoId: this.courseids,
					order: this.orderGroup,
					type: this.activeA, // 0:经销商/1:群管
				}
				getGroupRanklist(params).then(res => {
					if (res.code == 200) {
						// console.log(res)
						this.rankListA = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getrateList() {
				//获取漏斗图
				const params = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					endTime: '',
					startTime: '',
					courseId: this.courseid,
					videoId: this.courseids,
					type: this.user.userType == '00' ? 0 : 1, // 0:经销商/1:群管
				}
				getcourseRate(params).then(res => {
					if (res.code == 200) {
						this.getrateimg = res
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getredbaglist() {
				//红包统计
				const params = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					type: this.user.userType == '00' ? 0 : 1, // 0:经销商/1:群管
					courseId: this.courseid,
					videoId: this.courseids,
					endTime: '',
					startTime: '',
				}
				getRecPacketCount(params).then(res => {
					if (res.code == 200) {
						// console.log(res)
						this.redbaglist = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getquslist() {
				//答题统计
				const params = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					type: this.user.userType == '00' ? 0 : 1, // 0:经销商/1:群管
					courseId: this.courseid,
					videoId: this.courseids,
					endTime: '',
					startTime: '',
				}
				getQuesCount(params).then(res => {
					if (res.code == 200) {
						// console.log(res)
						this.answerlist = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getCoursestatistics() {
				//课程统计
				const params = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					type: this.user.userType == '00' ? 0 : 1, // 0:经销商/1:群管
					courseId: this.courseid,
					// videoId:this.courseids,
					endTime: '',
					startTime: '',
				}
				getshopCoursenum(params).then(res => {
					if (res.code == 200) {
						// console.log(res)
						this.courselist = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})

			},
			pullDownRefresh() {
				// 下拉
				this.triggered = true; //下拉了状态为true
				setTimeout(() => {
					this.triggered = false;
					uni.stopPullDownRefresh()
					this.pageNum = 1;
					this.getCourseListsmall('refresh') //触底  不穿执行else
					// 请求接口里面需要判断是不是最后一页   是最后一页 status赋值为‘loadmore’没有更多了
					// 请求接口
				}, 1000)
			},
			reachBottom() {
				// status这个是加载状态
				console.log(111);
				if (this.status === 'loadmore') {
					this.status = 'loading'
					uni.showNavigationBarLoading()
					setTimeout(() => {
						this.pageNum++
						this.getCourseListsmall() //触底  不穿执行else
						uni.hideNavigationBarLoading()
					}, 1000);
				}
			},
			getCourseOne(id) {
				this.courseid = id
			},
			getCourseTwo(id) {
				this.courseids = id
			},
			getCourseList() {
				const param = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					type: this.user.userType == '00' ? 0 : 1, // 0:经销商/1:群管
				}
				getFsCourseList(param).then(res => {
					if (res.code == 200) {
						this.courseOne = res.data
						// console.log(res)
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getCourseListsmall(type) {
				const param = {
					courseId: this.courseid,
					pageNum: this.pageNum,
					pageSize: this.pageSize, //
					status: ''
				}
				getCourseVdieoList(param).then(res => {
					if (res.code == 200) {
						// refresh 下拉
						if (type == 'refresh') {
							this.courseTwo = res.data.list
						} else {
							// 加载更多 当前页和下一页合并
							this.courseTwo = [...this.courseTwo, ...res.data.list]
						}
						if (this.pageNum >= res.data.pages) {
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
					} else {

					}
				})
			},
			changeCollapse(e) {
				this.collapseValue = e.filter(item => item.status == 'open').map(it => it.name)
			},
			handleNav(type) {
				this.activeTab = type
			},
			onChange(index) {
				this.searchbarNav = index
				if (index == 0) {
					this.getCourseList()
				} else {
					this.pageNum = 1
					if (this.courseid == 0) {
						uni.showToast({
							icon: 'none',
							title: "请先选择训练营-营期"
						})
					} else {
						this.getCourseListsmall('refresh')
					}
				}
			},
			confirm() {
				this.getCoursestatistics()
				this.getquslist()
				this.getredbaglist()
				this.getrateList()
				this.getrankgroup()
				this.getrankCourse()
			},
			reset() {
				console.log(this.searchbarNav)
				if (this.searchbarNav == 0) {
					this.courseid = ''
				} else {
					this.courseids = ''
				}
			},
			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)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.headnav {
		padding: 15px 12px;
		margin: 0 -10px -10px 0;
		box-sizing: border-box;
		background-color: #fff;

		image {
			height: 60px;
			width: 50px;
			position: absolute;
			z-index: 0;
			bottom: 0;
			right: 0;
			display: none;
		}

		&-item {
			flex: 1;
			font-size: 16px;
			padding: 10px;
			border-radius: 10px;
			background: #f8f8f8;
			margin: 0 10px 10px 0;
			position: relative;
			z-index: 1;
			overflow: hidden;
			color: #555;
		}

		&-active {
			background-color: rgb(231, 241, 255) !important;

			.headnav-num {
				color: #1677ff !important;
			}

			image {
				display: block !important;
			}
		}

		&-num {
			font-family: DIN, DIN;
			font-weight: bold;
			font-size: 25px;
			margin: 5px 0;
		}
	}

	.actNav {
		color: #1677ff !important;
		background-color: #e7f2fe;
	}
</style>