<!-- 数据组件页面 -->
<template>
	<view class="container">
		<view class="topdata">
			<view class="topdata-box">
				<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 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>
		<u-calendar :show="showCalendar" :mode="mode" @confirm="confirmCalendar" @close="closeCalendar"></u-calendar>
		<view class="statistics" :style="{height: contentH}">
			<u-collapse :border='false' :value='collapseValue' @change="changeCollapse">
				<u-collapse-item name="course">
					<text slot="title" class="statistics-title">课程统计</text>
					<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.count || 0}}</text>人</view>
							<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(courseCount.count,courseCount.yesterdayCount)}}</view>
							<view v-show="queryParam.type == 0">昨天{{courseCount.yesterdayCount || 0}}</view>
						</view>
						<view class="collapse-content-item">
							<view class="collapse-content-title">完播人数</view>
							<view class="collapse-content-num"><text>{{courseCount.completeCount || 0}}</text>人</view>
							<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(courseCount.completeCount,courseCount.yesterdayCountCompleteCount)}}</view>
							<view v-show="queryParam.type == 0">昨天{{courseCount.yesterdayCountCompleteCount || 0}}</view>
						</view>
						<view class="collapse-content-item">
							<view class="collapse-content-title">完播率</view>
							<view class="collapse-content-num"><text>{{courseCount.rate || 0}}</text>%</view>
							<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(courseCount.rate,courseCount.yesterdayRate)}}</view>
							<view v-show="queryParam.type == 0">昨天{{courseCount.yesterdayRate || 0}}</view>
						</view>
					</view>
				</u-collapse-item>
				<u-collapse-item name="questions">
					<text slot="title" class="statistics-title">答题统计</text>
					<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>{{quesCount.count || 0}}</text>人</view>
							<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(quesCount.count,quesCount.yesterdayCount)}}</view>
							<view v-show="queryParam.type == 0">昨天{{quesCount.yesterdayCount || 0}}</view>
						</view>
						<view class="collapse-content-item">
							<view class="collapse-content-title">正确人数</view>
							<view class="collapse-content-num"><text>{{quesCount.completeCount || 0}}</text>人</view>
							<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(quesCount.completeCount,quesCount.yesterdayCountCompleteCount)}}</view>
							<view v-show="queryParam.type == 0">昨天{{quesCount.yesterdayCountCompleteCount || 0}}</view>
						</view>
						<view class="collapse-content-item">
							<view class="collapse-content-title">正确率</view>
							<view class="collapse-content-num"><text>{{quesCount.rate || 0}}</text>%</view>
							<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(quesCount.rate,quesCount.yesterdayRate)}}</view>
							<view v-show="queryParam.type == 0">昨天{{quesCount.yesterdayRate || 0}}</view>
						</view>
					</view>
				</u-collapse-item>
				<u-collapse-item name="redenvelope">
					<text slot="title" class="statistics-title">红包统计</text>
					<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>{{redPacketCount.count || 0}}</text>个</view>
								<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(redPacketCount.count,redPacketCount.yesterdayCount)}}</view>
								<view v-show="queryParam.type == 0">昨天{{redPacketCount.yesterdayCount || 0}}</view>
							</view>
							<view class="collapse-content-item">
								<view class="collapse-content-title">答题红包金额</view>
								<view class="collapse-content-num"><text>{{redPacketCount.amount || '0.00'}}</text>元</view>
								<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(redPacketCount.amount,redPacketCount.yesterdayAmount,1)}}</view>
								<view v-show="queryParam.type == 0">昨天{{redPacketCount.yesterdayAmount || '0.00'}}</view>
							</view>
						</view>
						<view class="collapse-content x-ac" style="padding: 8px 0;">
							<view class="collapse-content-item">
								<view class="collapse-content-title">新会员奖励数</view>
								<view class="collapse-content-num"><text>{{redPacketCount.newCount || 0}}</text>个</view>
								<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(redPacketCount.newCount,redPacketCount.yesterdayNewCount)}}</view>
								<view v-show="queryParam.type == 0">昨天{{redPacketCount.yesterdayNewCount || 0}}</view>
							</view>
							<view class="collapse-content-item">
								<view class="collapse-content-title">新会员奖励金额</view>
								<view class="collapse-content-num"><text>{{redPacketCount.totalAmount || '0.00'}}</text>元</view>
								<view style="color: #1677ff;" v-show="queryParam.type == 0">较昨天持平{{compare(redPacketCount.totalAmount,redPacketCount.yesterdayTotalAmount,1)}}</view>
								<view v-show="queryParam.type == 0">昨天{{redPacketCount.yesterdayTotalAmount || '0.00'}}</view>
							</view>
						</view>
					</view>
				</u-collapse-item>
			</u-collapse>
		</view>
	</view>
</template>

<script>
	import {getCompanyUserAndUserCount,getCourseCount,getQuesCount,getRecPacketCount} from "@/api/courseManage.js"
	export default {
		data() {
			return {
				showCalendar: false,
				mode: 'range',
				typeOption: [{
					label: '今日',
					value: 0
				}, {
					label: '昨日',
					value: 1
				}, {
					label: '本月',
					value: 2
				}, {
					label: '自定义',
					value: 3
				}],
				queryParam: {
					type: 0
				},
				contentH: 0,
				collapseValue:['course','questions','redenvelope'],
				user: {},
				todayday: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
				startTime: '',
				endTime: '',
				companyUserCount: 0,
				companyUserUserCount: 0,
				courseCount: {},
				quesCount: {},
				redPacketCount: {}
			}
		},
		computed: {
			compare() {
				return (today,yesterday,type)=>{
					const num = Number(yesterday || 0) - Number(today || 0)
					return type == 1 ? num.toFixed(2) : 0
				}
			}
		},
		mounted() {
			this.getHeight()
			this.user = uni.getStorageSync("companyUserInfo") ? JSON.parse(uni.getStorageSync("companyUserInfo")) : {}
			// this.getUserCount()
			// this.resetDate()
		},
		methods: {
			getHeight() {
				const windowHeight = uni.getSystemInfoSync().windowHeight
				const query = uni.createSelectorQuery().in(this);
				query
				  .select(".topdata")
				  .boundingClientRect((data) => {
					this.contentH = `calc(${windowHeight}px - ${data.height}px - 56px)`
				  })
				  .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()
			},
			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() {
				const day = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd')
				const param = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					courseId: '',
					endTime: day + ' 23:59:59',
					startTime: day + ' 00:00:00',
					type: this.user.userType=='00' ? 0 : 1, // 0:经销商/1:群管
				}
				getCompanyUserAndUserCount(param).then(res=>{
					if(res.code==200){
						this.companyUserCount = res.data.companyUserCount || 0
						this.companyUserUserCount = res.data.companyUserUserCount || 0
					}else{
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			},
			getCount() {
				const param = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					courseId: '',
					endTime: this.endTime + ' 23:59:59',
					startTime: this.startTime + ' 00:00:00',
					type: this.user.userType=='00' ? 0 : 1, // 0:经销商/1:群管
				}
				this.getCourseCount(param)
				this.getQuesCount(param)
				this.getRecPacketCount(param)
			},
			getCourseCount(param){
				getCourseCount(param).then(res=>{
					if(res.code==200){
						this.courseCount = res.data
					}else{
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			},
			getQuesCount(param){
				getQuesCount(param).then(res=>{
					if(res.code==200){
						this.quesCount = res.data
					}else{
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			},
			getRecPacketCount(param){
				getRecPacketCount(param).then(res=>{
					if(res.code==200){
						this.redPacketCount = res.data
					}else{
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		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-color: rgb(216, 232, 255);

		&-box {
			padding: 12px;
			margin: 0 12px 0 12px;
			background-color: rgba(255, 255, 255, 0.5);
			border-radius: 8px 8px 0 0;
			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>