<template>
  <view>
	  <view class="work" :style="{height: `${windowHeight}px`}">
			<uni-nav-bar title="课表" :background-color="backgroundColor" :border="navborder" :leftWidth="leftwith+'rpx'" statusBar='true' fixed="true" >
	  			  <block slot="left">
	  			  		<view class="work_top" @click="getApply" v-if="checkPermi(['course:change:add'])">
	  			  			<image :src="wtopimg"></image>
	  						<view>申请调课</view>
	  			  		</view>
	  			  </block>
	  		  </uni-nav-bar>
			<!-- 主体 -->
			<block v-if="dataflag">
				<view class="flex0 w_top" >
					<view class="tab">
						<view class="tabl">
							<view class="tit"  :class="tabidx==idx?'act':''" @click="getTab(idx)" v-for="(ite,idx) in tablist" :key="idx">
								<view class="f15 tits" >{{ite.className}}</view>
								<image :src="tapline" class="line"></image>
							</view>
						</view>
						<view class="curbox" @click="getCourFn" v-if="checkPermi(['course:table:add','course:time:add'])">
							<image :src="curimg"></image>
							<view class="f12 co16">课程管理</view>
						</view>
					</view>
					<view class="box flexc">
						<block v-for="(ite,idx) in titlist" :key="idx">
							<!-- v-if="checkPermi([ite.limt])" -->
							<view class="tit flex1 pr"  :class="titidx==idx?'act':''" @click="getTit(idx)" >
								<view class="f15" >{{ite.tit}}</view>
								<view class="cirt" v-if="ite.val==1"></view>
							</view>
						</block>
					</view>
				</view>
				<view class="flex1 workbox">
					<!-- &&checkPermi(['course:table:list']) -->
					<view v-if="titidx==0">
						<time-table :timetables="timetables" :roles="roles" :timelist='timelist' @getChose="getChose"></time-table>
					</view>
					<view v-if="titidx==1" class="workboxr">
						<scroll-view scroll-y class="scroll-view" upper-threshold="40" lower-threshold="40" @scrolltolower="bot_btn">
							<view style="padding: 30rpx 36rpx;">
								<sh-list :datalist='tklist' :wtdt="wtdt" zhtype="5" :week="week"></sh-list>
							</view>
						</scroll-view>
					</view>
				</view>
			</block>
			<!-- 暂无信息 -->
			<block v-else><no-data ></no-data></block>
			
			<!-- 日历 -->
			<uni-calendar :startDate="startDate"	ref="calendar" :tit="tit" :weekdata="weekdata" :insert="false" 	@confirm="confirm" />
			<pop-up :type='type' :formdata="formdata"  @getClose="getClose"  @getConfirm="getConfirm"></pop-up>
	  </view>
	  <footers v-if="isfootflag" :footlist="footlist" :footerindex="footerindex" :color_checked="color_checked" :color_nochecked="color_nochecked" :isHomeIndex="true"></footers>
  </view>
</template>

<script>
	import popUp from "@/components/popup/popup.vue"
	import noData from "@/components/nodata/nodata.vue"
	import tabList from "@/components/toptab/tab.vue"
	import shList from "@/components/shlist/list.vue"
	import timeTable from "@/components/timetable/timetable.vue"
	import footers from '@/components/footer/footer.vue'
	import {getCourselistFn,getTimelistFn,getChangetklistFn,getChangeAddFn} from "@/api/work/index.js"
	import {getDeptList,getselfList} from "@/api/mine/mine.js"
	import {getDictionaryFn} from '@/api/mine/register.js'
	import {getInfo } from '@/api/login'
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
  export default {
	  components:{tabList,shList,timeTable,noData,popUp,footers},
	data(){
		return{
			footlist:[
				{module:'home',title:'首页',icon_checked:require('@/static/images/tabbar/home_.png'),icon_nochecked:require('@/static/images/tabbar/home.png'),},
				{module:'work',title:'课表',icon_checked:require('@/static/images/tabbar/work_.png'),icon_nochecked:require('@/static/images/tabbar/work.png'),limit:'course:table:list'},
				{module:'trends',title:'动态',icon_checked:require('@/static/images/tabbar/trends_.png'),icon_nochecked:require('@/static/images/tabbar/trends.png'),limit:'xiaoYuan:notice:list'},
				{module:'mine',title:'我的',icon_checked:require('@/static/images/tabbar/mine_.png'),icon_nochecked:require('@/static/images/tabbar/mine.png')},
				],
			footerindex:'work',
			isfootflag:true,
			color_checked :'161616',
			color_nochecked :'666666',
			windowHeight: 0,
			wtopimg:require("@/static/images/wtop.png"),
			tapline:require("@/static/images/hline.png"),
			curimg:require("@/static/images/wcurimg.png"),
			backgroundColor:'#ffffff',
			navborder:false,
			leftwith:'200',
			tabidx:0,
			titidx:0,
			list:[],
			tablist:[],
			// limt:'course:table:list'
			titlist:[{tit:'课程表',val:0,},{tit:'调课通知',val:1,limt:''}],
			timetables: [],
			timelist:[],//时间
			classId:'',
			schoolId:'',
			teacherClass:[],
			dataflag:true,
			roles:'',
			tklist:[],
			pageSize: 10,
			pageNum: 1,
			reachflag:true,
			wtdt:'',
			week:[],
			weekdata:'',//选择的星期
			type:0,
			tiaotype:1,
			tit:'调课日期',
			startDate:'',
			formdata:{
				"subjectClassId": "",
				"subjectClass": "",
				"subject": "",
				"isNum":"",
				"subjectTime":"",
				"subjectWeek":"",
				"beSubject": "",
				"beIsNum": "",
				"beSubjectTime":"",
				"week": "",
				"content":''
			}
		}
	},
	onShow:function(){
		return
		// 判断角色是否一样
		var choseroles=this.$store.state.user.choseroles;
		this.tiaotype=1;
		if(this.roles&&choseroles&&this.roles!=choseroles){
			this.getClassFn()
		}else{
			var newArr=this.$store.state.user.refreshflag
			if(newArr&&newArr.length){
				if(newArr[1]){
					this.getClassFn()
					newArr[1]=false
					this.$store.dispatch('checkRefresh', newArr).then(() => {
						
					}).catch(() => {
					 
					})
				}
			}
		}
	},
	onUnload(){
		uni.$off('refreshdatalis')
	},
	
	onPullDownRefresh:function(){
	    this.reachflag=true;
	    this.pageNum=1;
	    this.list=[];
		if(this.titidx==1){
			this.getDataFn();
			this.init();
		}else{
			this.getClassFn();
		} 
		setTimeout(function () {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	onLoad: function() {
		this.getClassFn();
		this.init();
		this.startDate = this.getNowFormatDate();
		// uni.$emit('refreshdatalistzd','ldps');
		uni.$on('refreshdatalis',(e) => {
			this.reachflag=true;
			this.pageNum=1;
			this.list=[];
			this.getClassFn();
			
			// if(e&&e=='ldps'){
				
			// }else{
			// 	this.getCommonCensus();
			// }
		})
	},
	methods:{
		checkPermi, checkRole,
		getClose(){
			this.type=0;
			this.tiaotype=1;
			var newobj={
				subjectClassId:this.formdata.subjectClassId,
				subjectClass:this.formdata.subjectClass,
			}
			this.formdata=JSON.parse(JSON.stringify(newobj))
		},
		bot_btn() {
			if (this.reachflag&&this.titidx==1) {
				this.pageNum++
				this.getDataFn()
			}
		},
		getNowFormatDate(){
			let date = new Date();
			let y = date.getFullYear();
			let m = date.getMonth() + 1;
			m = m < 10 ? "0" + m : m;
			let d = date.getDate();
			d = d < 10 ? "0" + d : d;
			const timeday = y + "-" + m + "-" + d;
			this.year=y;
			this.month=m;
			return timeday;
		},
		getTfn(e){
			this.tiaotype=e
		},
		getConfirm(e){
			var that=this;
			var params=JSON.parse(JSON.stringify(this.formdata))
			params.content=e;
			getChangeAddFn(params).then(res=>{
				if(res.code==200){
					that.$toast("新增成功");
					that.type=0;
					that.tiaotype=1;
					var newobj={
						subjectClassId:that.formdata.subjectClassId,
						subjectClass:that.formdata.subjectClass,
					}
					that.formdata=JSON.parse(JSON.stringify(newobj))
					setTimeout(function(){
						that.reachflag=true;
						that.pageNum=1;
						that.list=[];
						that.getClassFn();
					},1500)
				}else{
					that.$toast(res.msg)
				}
			})
		},
		getApply(){
			this.tiaotype=1;
			this.$toast('请在课表中选择调课科目')
			// this.$refs.calendar.open()
			 // this.$tab.navigateTo('/work/pages/examine/applyclass')		 
		},
		confirm(e){
			var fulldate=e.fulldate;
			if(this.tiaotype==1){
				this.formdata.subjectTime=fulldate;
				this.$toast('请选择被调课日期')
				this.tiaotype=2;
			}else{
				// 二次确认弹窗
				this.formdata.beSubjectTime=fulldate;
				this.type=7
				
			}	
		},
		getChose(data){
			// 选择的课表
			if(this.tiaotype==1){
				this.tit='调课日期';
				var newdata=JSON.parse(JSON.stringify(data))
				this.formdata.subject=newdata.subject;
				this.formdata.isNum=newdata.isNum;
				this.formdata.subjectWeek=newdata.subjectWeek;
			}else{
				this.tit='被调课日期';
				var newdata=JSON.parse(JSON.stringify(data))
				this.formdata.beSubject=newdata.subject;
				this.formdata.beIsNum=newdata.isNum;
				this.formdata.week=newdata.subjectWeek;
			}
			this.weekdata=data.subjectWeek
			this.$refs.calendar.open()
		},
		getCourFn(){
			this.$tab.navigateTo('/work/pages/examine/course')		
		},
		init(){
			// 星期
			getDictionaryFn('sys_week').then(res=>{
				if(res.code==200){
					this.week = res.data.map(v => {
						return {
							dictLabel: v.dictLabel,
							dictValue: v.dictValue
						}
					})
				}
			})
		},
		getTab(idx){
			this.tabidx=idx;
			this.classId=this.tablist[idx].classId;
			this.schoolId=this.tablist[idx].schoolId;
			this.formdata.subjectClassId=this.tablist[idx].classId;
			this.formdata.subjectClass=this.tablist[idx].className;
			// 获取课表
			this.getCourselistFn()
		},
		getTit(idx){
			this.titidx=idx;
			if(idx==1){
				this.getDataFn()
			}else{
				this.getCourselistFn()
			}
		},
		getDataFn(){
			var params={
				pageSize:this.pageSize,
				pageNum: this.pageNum,
				isPass:2,
			}
			getChangetklistFn(params).then(res=>{
				if(res.code==200){
					if (res.rows.length < this.pageSize) {
						this.reachflag = false
						this.wtdt = '到底了~';
					} else {
						var num = parseInt(res.rows.length) + parseInt(this.pageSize) * parseInt(this.pageNum -	1)
						if (num < res.total) {
							this.reachflag = true
							this.wtdt = '上拉加载更多'
						} else {
							this.reachflag = false
							this.wtdt = '到底了~';
						}
					}
					if (this.pageNum == 1) {
						this.tklist = res.rows;
					} else {
						this.tklist = this.tklist.concat(res.rows)
					}
				}else{
					this.$toast(res.msg)
				}
			})
		},
		getCourselistFn(){
			var that=this;
			var params={
				classId:this.classId,
				schoolId:this.schoolId
			}
			 params['params[role]']=this.$store.state.user.choseroles
			getCourselistFn(params).then(res=>{
				if(res.code==200){
					that.timetables=res.rows;
				}else{
					that.$toast(res.msg)
				}
			})
			var newObj={
				schoolId:this.schoolId
			}
			getTimelistFn(newObj).then(res=>{
				if(res.code==200){
					var newArr=[]
					if(res.rows.length>0){
						var data=res.rows[0];
						newArr=[
							{val:'1',time:(data.oneStartTime||'')+'-'+(data.oneEndTime||'')},
							{val:'2',time:(data.twoStartTime||'')+'-'+(data.twoEndTime||'')},
							{val:'3',time:(data.threeStartTime||'')+'-'+(data.threeEndTime||'')},
							{val:'4',time:(data.fourStartTime||'')+'-'+(data.fourEndTime||'')},
							{val:'5',time:(data.fiveStartTime||'')+'-'+(data.fiveEndTime||'')},
							{val:'6',time:(data.sixStartTime||'')+'-'+(data.sixEndTime||'')},
							{val:'7',time:(data.sevenStartTime||'')+'-'+(data.sevenEndTime||'')},
							{val:'8',time:(data.eightStartTime||'')+'-'+(data.eightEndTime||'')},
						]
						
					}else{
						newArr=[{ val: '1'},{ val: '2'}, { val: '3'},{ val: '4'},{ val: '5'},{ val: '6'},{ val: '7'},{ val: '8'}]
					}
					that.timelist=JSON.parse(JSON.stringify(newArr));
				}else{
					that.$toast(res.msg)
				}

			})
		},
		getclassListFn(id){
			var that=this;
			getDeptList(id).then(res=>{
				if(res.code==200){
					if(res.data&&res.data.length){
						that.tablist = res.data.map(v => {
							return {
								className: v.deptName,
								classId: v.deptId,
								schoolId:v.parentId
							}
						})
						that.classId=res.data[0].deptId;
						that.formdata.subjectClassId=that.classId;
						that.formdata.subjectClass=res.data[0].deptName;
						that.schoolId=res.data[0].parentId;
						that.dataflag=true;
						that.getCourselistFn()
					}
				}else{
					that.$toast(res.msg)
				}
			})
		},
		getselfList(id){
			var that=this;
			var params={
				parentId:id,
			}
			params['params[role]']=this.$store.state.user.choseroles
			getselfList(params).then(res=>{
				if(res.code==200){
					var newArr=res.data;
					if(newArr&&newArr.length){
						this.tablist = newArr.map(v => {
							return {
								className: v.deptName,
								classId: v.deptId,
								schoolId:v.parentId
							}
						})
						that.classId=res.data[0].deptId;
						that.formdata.subjectClassId=that.classId;
						that.formdata.subjectClass=res.data[0].deptName;
						that.schoolId=res.data[0].parentId;
						that.dataflag=true;
						that.getCourselistFn()
					}
				}else{
					this.$toast(res.msg)
				}
			})
		},
		getClassFn(){
			var that=this;
			var roles=that.$store.state.user.choseroles;
			that.roles=roles;
			if(roles=='teacher'){
				var id=that.$store.state.user.schoolId;
				this.getselfList(id)
			}else if(roles=='parents'){
				this.$store.dispatch('GetInforoles').then(resd => {
					if(resd.code==200){
						var res=resd.data;
						if(res.user&&res.user.parentsStudent&&res.user.parentsStudent.length){
							var newArr = [];
							const map = new Map();
							newArr = res.user.parentsStudent.filter(v => !map.has(v.classId) && map.set(v.classId, 1));
							that.tablist = newArr.map(v => {
								return {
									className: v.className,
									classId: v.classId,
									schoolId:v.schoolId
								}
							})
							this.classId=newArr[0].classId;
							that.schoolId=newArr[0].schoolId;
							that.formdata.subjectClassId=that.classId;
							that.formdata.subjectClass=newArr[0].className;
							that.dataflag=true;
							that.getCourselistFn()
						}else{
							that.dataflag=false
						}
					}
				})
			}else{
				// 学校
				that.classId='';
				that.formdata.subjectClassId='';
				that.formdata.subjectClass='';
				var id=that.$store.state.user.deptId||'';
				that.schoolId=id;
				that.getselfList(0)
			}
		},
	},
	mounted() {
		var systemInfo=uni.getSystemInfoSync();
		this.windowHeight=Number(systemInfo.windowHeight);
		
	},
  }
</script>

<style lang="scss" scoped>
.work_top{display: flex;align-items: center;font-size: 30rpx;font-weight: 500;color: #4775EA;padding: 0 10rpx;
	image{width: 30rpx;height: 30rpx;margin-right: 14rpx;flex: 0 0 auto;}
}
.work{display: flex;flex-direction: column;overflow: hidden;
	.w_top{background-color: #ffffff;
		.tab{
			border-bottom: 2rpx solid #E6E6E6;display: flex;overflow-x: auto;align-items: center;position: relative;
			.tabl{flex: 1;overflow: auto;display: flex;align-items: center;}
			.tit{position: relative;padding:32rpx 36rpx;color: #666666 ;flex: 0 0 auto;
			&.act{
				.tits{font-size: 32rpx;font-weight: bold;}
				.line{display: block;}
				}
				
			}
			.line{width: 40rpx;height: 12rpx;position: absolute;left: 50%;margin-left: -20rpx;bottom: 16rpx;display: none;}
			.curbox{min-width: 96rpx;height: 100rpx;background: #FFFFFF;box-shadow: -10rpx 0px 10rpx 0px rgba(196,191,191,0.22);flex: 0;flex: 0 0 auto;padding:0 8rpx;box-sizing: border-box;display: flex;align-items: center;justify-content: center;flex-wrap: wrap;flex-direction: column;
			image{width: 30rpx;height: 32rpx;}
			}
		}
		.box{
			.tit{text-align: center;line-height: 100rpx;color: #666666 ;display: flex;justify-content: center;align-items: center;
				.cirt{width: 12rpx;height: 12rpx;background: #EB5663;border-radius: 50%;margin: -24rpx 0 0 -6rpx;}
				&.act{font-weight: bold;color: #4775EA;}
			}
		}
	}
	.workbox{overflow: auto;
		.workboxr{height: 100%;display: flex;}
		.scroll-view{width: 100%;height: 100%;box-sizing: border-box;}
	}
}
</style>
