<template>
	<div>
		<error-page v-if="showError" @closeError="closeError"></error-page>
		<!-- 主体内容 -->
		<div v-if="!showError" class="main-box">
			<!-- 左侧 -->
			<div class="main-left">
				<!-- 班级详情 -->
				<div class="left-one flex-s0">
					<div class="left-content">
						<div class="left-title">
							<div class="pultitle">班级详情</div>
						</div>
						<div class="second-title" v-if="className!=''">{{className}}</div>
						<div class="line" v-if="className!=''"></div>
						<div class="three-content" v-if="className!=''">
							<div>老师：{{teacherName}}</div>
							<div>班级人数：{{classNum}}</div>
						</div>
						<div class="line" v-if="className!=''"></div>
						<div class="left-last">
							<div v-for="(item,index) in daoArr" :key="index">
								<div>{{item.num}}</div>
								<div>{{item.title}}</div>
							</div>
						</div>
					</div>
					<loading :isShow="showLoading_one"></loading>
				</div>
				<!-- 宝宝食谱 -->
				<div class="left-one flex-s0">
					<div class="left-content">
						<div class="left-title">
							<div class="pultitle">宝宝食谱</div>
						</div>
						<div v-if="threeMeals.length!=0">
							<div class="eat-three">
								<div :class="threeMealIndex == index ? 'threeMealActive' : ''"
									v-for="(item, index) in threeMeals" :key="index" @click="chooseMeal(index)">
									{{ item.Type }}
								</div>
							</div>
							<div class="line"></div>
							<div v-for="(item, index) in threeMeals" :key="index">
								<div v-if="index == threeMealIndex" class="meal-box">
									<img :src="item.Images" />
									<div class="clips2">{{ item.FoodName }}</div>
								</div>
							</div>
						</div>
					</div>
					<loading :isShow="showLoading_two"></loading>
				</div>
				<!-- 通知公告 -->
				<div class="left-one">
					<div class="left-content">
						<div class="left-title">
							<div class="pultitle">通知公告</div>
						</div>
						<div class="second-title">{{className}}</div>
						<div class="line" v-if="noticeObj!=''"></div>
						<div class="three-content left-notice" v-if="noticeObj!=''">
							<div class="clips2">{{noticeObj.Content}}</div>
						</div>
						<div class="notice-time" v-if="noticeObj!=''">
							<div>{{noticeObj.PublishTime}}</div>
							<div><img src="../../static/img/home/slices.png" />{{noticeObj.BroweTotal}}</div>
						</div>
					</div>
					<loading :isShow="showLoading_three"></loading>
				</div>
			</div>
			<!-- 中部 -->
			<div class="main-center">
				<!-- 班级动态图文 -->
				<div class="center-img-box">
					<div class="center-title">班级动态</div>
					<div class="img-box">
						<swiper :options="swiperOption" style="z-index: -1;position: relative;">
							<swiper-slide v-for="(item,index) in classImgArr" :key="index" class="home-slide" style="">
								<img :src="item" style="object-fit: cover;width: 100%;height: 100%;">
							</swiper-slide>
						</swiper>
						<div class="swiper-pagination"></div>
						<loading :isShow="showLoading_four"></loading>
					</div>
				</div>
				<!-- 提醒 -->
				<div class="remind-box">
					<div class="msg-box">
						<img src="../../static/img/home/icon-notice.png" />
						<div v-if="noticeData!=0">{{noticeData}}</div>
					</div>
					<div class="msg-box">
						<div>宝宝提醒</div>
						<div v-if="babyNotice!=0">{{babyNotice}}</div>
					</div>
					<div class="msg-box">
						<div>生日提醒</div>
						<div v-if="remindData!=0">{{remindData}}</div>
					</div>
					<div class="msg-box">
						<div >生日提醒</div>
						<div v-if="babyBirthdayData!=0">{{babyBirthdayData}}</div>
					</div>
				</div>
			</div>
			<!-- 右侧 -->
			<div class="main-right">
				<div class="left-title">
					<div class="pultitle">今日活动</div>
				</div>
				<div class="right-content">
					<div v-if="noonArr.length!=0">
						<div v-for="(item, index) in noonArr" :key="index">
							<div class="right-title">{{ item.Ampm }}</div>
							<div class="line" style="margin: 0 6px"></div>
							<div class="right-item-box" v-for="(itemc, indexc) in item.Content"
								:class="indexc % 2 != 0 ? 'oddActive' : ''" :key="indexc">
								<div>{{ itemc.TimePart }}</div>
								<div class="clips3">{{ itemc.Name }}</div>
							</div>
						</div>
					</div>
				</div>
				<loading :isShow="showLoading_five"></loading>
			</div>
		</div>
	</div>
</template>
<script>
	import storage from 'good-storage'
	export default {
		name: "homepage",
		data() {
			return {
				threeMeals: [],
				threeMealIndex: 0,
				noticeData: 0,
				babyBirthdayData: 0,
				babyNotice:0,//宝宝提醒数量
				dynamicPictureData: "",
				noonArr: [],
				moId:"",
				model:"",
				remindData:"",
				className:'',//班级名称
				teacherName:'',//老师姓名
				classNum:'',//班级人数
				daoArr:[],//实到、未到、请假
				classImgArr:[],
				showLoading_one:true,
				showLoading_two:true,
				showLoading_three:true,
				showLoading_four:true,
				showLoading_five:true,
				noticeObj:'',
				classGuid:'',
				timer:null,
				
				swiperOption: {
					pagination: '.swiper-pagination',
					paginationClickable: true,
					autoplay: 3000,
					autoplayDisableOnInteraction: false,
					loop: true,
					coverflow: {
						rotate: 30,
						stretch: 10,
						depth: 60,
						modifier: 2,
						slideShadows : true
				　　}
				},
				showError:false
			};
		},
		methods: {
			closeError(){
				this.showError = false;
			},
			// 早中晚餐切换事件
			chooseMeal(e) {
				this.threeMealIndex = e;
			},
			// 宝宝食谱
			async babyRecipeEv() {
				if(this.$storage.get('network')){
					let data = await this.$axios({
						// 调用 serviceAPI
						url: this.$https.babyRecipe,
						methods: "get",
						params: {
							EquipmentID: "yaohaotest001"
						},
					});
					if(data.data.data!=undefined){
						this.threeMeals = data.data.data;
						this.$storage.set('threeMeals',this.threeMeals);
					}
				} else {
					this.threeMeals = this.$storage.get('threeMeals');
				}
				
				this.showLoading_two = false;
			},
			// 通知消息数量
			async notice() {
				if(this.$storage.get('network')){
					let data = await this.$axios({
						// 调用 serviceAPI
						url: this.$https.notice,
						methods: "get",
						params: {
							EquipmentID: "yaohaotest001"
						},
					});
					if(data.data.data!=null){
						this.noticeData = data.data.count;
						this.noticeObj = data.data.data;
						this.noticeObj.PublishTime = this.noticeObj.PublishTime.slice(0,11);
						this.$storage.set('noticeData',this.noticeData)
						this.$storage.set('noticeObj',this.noticeObj)
						this.$storage.set('PublishTime',this.noticeObj.PublishTime)
						storage.set('orgGuid',this.noticeObj.OrgGuid);
					}
				} else {
					this.noticeData = this.$storage.get('noticeData');
					this.noticeObj =this.$storage.get('noticeObj');
					this.noticeObj.PublishTime = this.$storage.get('PublishTime');
				}
				this.showLoading_three = false;
			},
			// 今日活动
			async schedulingWeb() {
				if(this.$storage.get('network')){
					let data = await this.$axios({
						// 调用 serviceAPI
						url: this.$https.schedulingWeb,
						methods: "get",
						params: {
							EquipmentID: "yaohaotest001"
						},
					});
					if(data.data.data!=undefined){
						this.noonArr = data.data.data[0].WeekData;
						this.$storage.set('noonArr',this.noonArr)
					}
				} else {
					if(this.$storage.get('noonArr')==undefined){
						this.noonArr = [];
					} else {
						this.noonArr = this.$storage.get('noonArr');
					}
				}
				
				this.showLoading_five = false;
			},
			// 获取当日宝宝生日
			async babyBirthday() {
				if(this.$storage.get('network')){
					let data = await this.$axios({
						// 调用 serviceAPI
						url: this.$https.babyBirthday,
						methods: "get",
						params: {
							EquipmentGuid: "yaohaotest001"
						},
					});
					this.babyBirthdayData = data.data.count;
					this.$storage.set('babyBirthdayData',this.babyBirthdayData)
				} else {
					this.babyBirthdayData = this.$storage.get('babyBirthdayData');
				}
				
			},
			// 班级动态
			async dynamicPicture() {
				if(this.$storage.get('network')){
					let data = await this.$axios({
						// 调用 serviceAPI
						url: this.$https.dynamicPicture,
						methods: "post",
						params: {
							EquipmentID: "yaohaotest001"
						},
					});
					if(data.data.data!=null){
						this.classImgArr = data.data.data.PictureUrls;
						this.$storage.set('classImgArr',this.classImgArr);
					}
				} else {
					this.classImgArr = this.$storage.get('classImgArr');
				}
				this.showLoading_four = false;
			},
			// 班级情况
			async classStatusEv() {
				if(this.$storage.get('network')){
					this.showError = false;
					let data = await this.$axios({
						url: this.$https.classStatus,
						methods: "get",
						params: {
							EquipmentID: "yaohaotest001"
						},
					});
					if(data.data.code==200){
						let classInfo = data.data.data;
						this.className = classInfo.ClassName;
						this.$storage.set('className',this.className);
						let newTeacher = [];
						classInfo.Teachers.forEach(item=>{
							newTeacher.push(item.TeacherName);
						})
						// 老师
						this.teacherName = newTeacher.join(' ');
						this.$storage.set('teacherName',this.teacherName);
						// 班级人数
						this.classNum = classInfo.StudentCount;
						this.$storage.set('classNum',this.classNum);
						// 实到、未到、请假
						this.daoArr = [
							{num:classInfo.Signed,title:'实到'},
							{num:classInfo.NotSigned,title:'未到'},
							{num:classInfo.Holiday,title:'请假'},
						];
						this.$storage.set('daoArr',this.daoArr);
						// 班级guid
						this.classGuid = classInfo.ClassGuid;
						this.$storage.set('classGuid',this.classGuid);
						// 调取宝宝提醒
						this.remindEv();
					}
				} else {
					this.className = this.$storage.get('className');
					this.teacherName = this.$storage.get('teacherName');
					this.classNum = this.$storage.get('classNum');
					this.daoArr = this.$storage.get('daoArr');
					this.classGuid = this.$storage.get('classGuid');
					if(this.className==undefined){
						this.showError = true;
					}
				}
				this.showLoading_one = false;
			},
			// 宝宝提醒
			async remindEv() {
				if(this.$storage.get('network')){
					let data = await this.$axios({
						// 调用 serviceAPI
						url: this.$https.remind,
						methods: "post",
						params: {
							EquipmentID: "yaohaotest001",
							page:1,
							limit:100,
							ClassGuid:this.classGuid
						},
					});
					this.babyNotice = data.data.count;
					this.$storage.set('babyNotice',this.babyNotice)
				} else {
					this.babyNotice = this.$storage.get('babyNotice');
				}
			},
		},
		mounted() {
			this.babyRecipeEv();
			this.schedulingWeb();
			this.babyBirthday();
			this.dynamicPicture();
			// 班级详情
			this.classStatusEv();
			setTimeout(()=>{
				this.timer = setInterval(()=>{
					// 宝宝提醒
					this.remindEv();
					// 通知公告
					this.notice();
					// 生日提醒
					this.babyBirthday();
				},1000*60)
			},5000)
		},
		destroyed(){
			clearInterval(this.timer);
			this.babyBirthday()
			this.dynamicPicture()

			// 班级详情
			this.classStatusEv();
		},
		created() {
			this.notice();
		},
	};
</script>
