<template>
	<view class="home-container">
		<!-- 头部区域 -->
		<top-box v-bind:timeStamp="timeStamp"></top-box>
		<!-- 滚动条 -->
		<view class="notice">
			<u-notice-bar :text="noticeMsg" mode="closable" bgColor="rgba(73, 125, 220, 0.25)" color="#FFF"
				url="/subpkg/mymsg/mymsg"></u-notice-bar>
		</view>
		<!-- 卡片区域 -->
		<view class="card-box">

			<!-- 今日课程 -->
			<view class="card-item" style="width: 100%;">
				<view class="card-info" style="padding-bottom: 5rpx;">
					<view :class="icon.class"></view>
					<text class="info-text">今日课程</text>
				</view>
				<todayclass-box :classCurrent="classIndex"></todayclass-box>
			</view>

			<!-- 今日打卡 -->
			<view class="card-item" @click="navCalendar">
				<view class="card-info">
					<view :class="icon.record"></view>
					<text class="info-text">今日打卡</text>
				</view>

				<view class="space">
					<view class="home-title-text">
						<view class="msg">今日出勤:</view>
						<view class="card-content">{{homeData.attendance}}</view>
					</view>
					<view class="home-title-text">
						<view class="msg">打卡时间:</view>
						<view style="margin-left: 10rpx;">
							<!-- <u-tag text="8点15分" plain shape="circle"></u-tag> -->
							<view style="display: flex; align-items: center; margin-left: 10rpx;">
								<view class="number-box">{{homeData.attendanceTimeAfter[0]}}</view>
								<view style="color: #3C9CFF;">:</view>
								<view class="number-box">{{homeData.attendanceTimeAfter[1]}}</view>
							</view>
						</view>
					</view>
					<view class="home-title-text">
						<view class="msg">本周出勤质量:</view>
						<view class="card-content">{{homeData.attendanceQuality}}</view>
					</view>
				</view>
			</view>


			<!-- 评测 -->
			<view class="card-item" @click="navHomework(0)">
				<view class="card-info">
					<view :class="icon.evaluation"></view>
					<text class="info-text">今日评测</text>
				</view>
				<view class="charts-box">
					<qiun-data-charts type="arcbar" :chartData="todayData.evaluationArcbar" :loadingType="4"
						:canvas2d='true' canvasId='canvans9313111' :opts="evaluationOpts" />
				</view>
			</view>
			<!-- 作业 -->
			<view class="card-item" @click="navHomework(1)">
				<view class="card-info">
					<view :class="icon.homework"></view>
					<text class="info-text">今日作业</text>
				</view>
				<view class="charts-box">
					<qiun-data-charts type="arcbar" :chartData="todayData.homeworkArcbar" :loadingType="4"
						:canvas2d='true' canvasId='canvans9327676' :opts="homeworkOpts" />
				</view>
			</view>
			<!-- 问卷 -->
			<view class="card-item" @click="navHomework(2)">
				<view class="card-info">
					<view :class="icon.activity"></view>
					<text class="info-text">今日活动</text>
				</view>
				<view class="charts-box">
					<qiun-data-charts type="arcbar" :chartData="todayData.activityArcbar" :loadingType="4"
						:canvas2d='true' canvasId='canvans9313176' :opts="activityOpts" />
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			...mapState('m_chart', ['todayData']),
			...mapState('m_children', ['childreninfo', 'classList']),
			...mapState('m_parent', ['myData'])
		},
		data() {
			return {
				//滚动通知
				noticeMsg: '',
				//当前课程索引
				classIndex: '',
				//首页数据
				homeData: {
					//出勤状况
					attendance: '已打卡',
					//打卡时间
					attendanceTime: '08:15',
					//处理后时间
					attendanceTimeAfter: [],
					//出勤质量
					attendanceQuality: '优秀'
				},
				//属性(不修改)
				//图标
				icon: {
					record: 't-icon t-icon-dakaqiandao',
					class: 't-icon t-icon-banji',
					evaluation: 't-icon t-icon-ceping',
					homework: 't-icon t-icon-bianji1',
					activity: 't-icon t-icon-huodong'
				},
				//评测进度条属性
				evaluationOpts: {},
				//作业进度条属性
				homeworkOpts: {},
				//活动进度条属性
				activityOpts: {},
				//时间戳
				timeStamp: '',

			}
		},
		onLoad() {
			this.setOpts()
			this.$getTimeStamp()
			this.getNoticeMsg()
			this.getClassCurrent()
			this.getAttendanceTimeAfter()
		},
		onPullDownRefresh() {
			this.$getTimeStamp()
			this.getClassCurrent()
			console.log('refresh')
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 1000);
		},
		methods: {
			//获取滚动通知
			getNoticeMsg() {
				this.noticeMsg = `您有 ${this.myData.msgList.length} 个通知,请点击查看`
			},
			//计算当前课程
			getClassCurrent() {
				//当前时间戳是否在课程时间段内
				function CompareDate(start, finish) {
					//获得当前时间时间戳
					let timeNow = (new Date()).format("yyyy-MM-dd hh:mm").replace(new RegExp("-", "gm"), "/")
					let timeHaoMiao = (new Date(timeNow)).getTime()
					//获得课程开始结束时间戳
					let today = (new Date()).format("yyyy-MM-dd ")
					let classStart = (today + start).replace(new RegExp("-", "gm"), "/")
					let classFinish = (today + finish).replace(new RegExp("-", "gm"), "/")
					let classStartHaoMiao = (new Date(classStart)).getTime()
					let classFinishHaoMiao = (new Date(classFinish)).getTime()
					//比较
					if (timeHaoMiao < classFinishHaoMiao) {
						if (classStartHaoMiao < timeHaoMiao)
							return true
					}
					return false
				}
				//获取当前课程索引值
				let i = 1
				for (let item of this.classList) {
					let timeArr = item.time.split("-")
					let res = CompareDate(timeArr[0], timeArr[1])
					if (res) {
						break
					}
					i++
				}
				this.classIndex = i
				i = 0
			},
			//处理打卡时间
			getAttendanceTimeAfter(){
				this.homeData.attendanceTimeAfter = this.homeData.attendanceTime.split(':')
			},
			navHomework(index) {
				uni.reLaunch({
					url: `/pages/homework/homework?index=${index}`
				})
			},
			navCalendar() {
				uni.navigateTo({
					url: '/subpkg/calendarmsg/calendarmsg'
				})
			},
			//设置圆环图描述更新
			setOpts() {
				//圆环图标题和副标题动态变化
				let evaluation = {
					title: {
						name: this.todayData.evaluationArcbar.series[0].data * 100 + "%",
						color: this.todayData.evaluationArcbar.series[0].color
					},
					subtitle: {
						name: this.todayData.evaluationArcbar.series[0].name
					},
					extra: {
						arcbar: {
							type: 'circle'
						}
					}
				}
				let homework = {
					title: {
						name: this.todayData.homeworkArcbar.series[0].data * 100 + "%",
						color: this.todayData.homeworkArcbar.series[0].color
					},
					subtitle: {
						name: this.todayData.homeworkArcbar.series[0].name
					},
					extra: {
						arcbar: {
							type: 'circle'
						}
					}
				}
				let activity = {
					title: {
						name: this.todayData.activityArcbar.series[0].data * 100 + "%",
						color: this.todayData.activityArcbar.series[0].color
					},
					subtitle: {
						name: this.todayData.activityArcbar.series[0].name
					},
					extra: {
						arcbar: {
							type: 'circle'
						}
					}
				}
				this.evaluationOpts = evaluation;
				this.homeworkOpts = homework;
				this.activityOpts = activity;
			},

		},
	}
</script>

<style lang="scss">
	@import '@/pages/common/mainpage.scss';

	.number-box {
		width: 40rpx;
		height: 40rpx;
		border-radius: 8rpx;
		background-color: #3C9CFF;
		color: #FFF;
		padding: 5rpx;
		margin: 5rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.space {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}
</style>
