(function() {
	var weekData = [];
	var weekTimeSpan;
	var weekTimeI;

	function compare(property) {
		return function(a, b) {
			var value1 = a[property];
			var value2 = b[property];
			return value1 - value2;
		};
	}
	//处理数据
	function ProcessingData(data) {
		var dataM = [{
				weekTime: 1,
				lessonData: []
			},
			{
				weekTime: 2,
				lessonData: []
			},
			{
				weekTime: 3,
				lessonData: []
			},
			{
				weekTime: 4,
				lessonData: []
			},
			{
				weekTime: 5,
				lessonData: []
			},
			{
				weekTime: 6,
				lessonData: []
			},
			{
				weekTime: 7,
				lessonData: []
			},
		]
		$.each(data, function(i, n) {
			n.sectionTime = n.LessonTime.slice(1);
			$.each(dataM, function(j, k) {
				if (k.weekTime == n.LessonTime.slice(0, 1)) {
					k.lessonData.push(n);
				}
			});
		});
		return dataM;
	}

	//判断学期
	function judgeDate(obj) {
		// console.log(obj)
		// obj = 1561861094577;
		var date = new Date(obj);
		var y = 1900 + date.getYear();
		var m = date.getMonth() + 1;
		if (m >= 2) {
			if (m < 8) {
				$('#semester span').text((y - 1) + '-' + y + '学年第二学期');
			} else {
				$('#semester span').text(y + '-' + (y + 1) + '学年第一学期');
			}
		}
	}

	//根据今天得到周一与周日的时间
	var oneDayTime = 24 * 60 * 60 * 1000;

	function weekday() {
		var now = new Date();
		var nowTime = now.getTime();
		var day = now.getDay();

		//显示周一
		var MondayTime = nowTime - (day - 1) * oneDayTime;
		//显示周日
		var SundayTime = nowTime + (7 - day) * oneDayTime;
		return {
			MondayTime: MondayTime,
			Monday: fmtDate(MondayTime),
			SundayTime: SundayTime,
			Sunday: fmtDate(SundayTime)
		};
	}

	function fmtDate(obj) {
		var date = new Date(obj);
		var y = 1900 + date.getYear();
		var m = "0" + (date.getMonth() + 1);
		var d = "0" + date.getDate();
		return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
	}

	function loadData(start, end) {
		judgeDate(todayWeek.SundayTime);
		
		 var param = {
		 	StartDate: start,
		 	EndDate: end
		 };
		//var param = {
		//	StartDate: '2019-06-10',
		//	EndDate: '2019-06-16'
		//};
		learun.httpget(config.webapi + "learun/adms/timetable/list", param, (data) => {
			weekData = data;
			var weekData2 = ProcessingData(weekData);
			var tSec2List = $('.tSec2List');

			weekTimeSpan.eq(0).text(todayWeek.Monday).end().eq(1).text(todayWeek.Sunday);
			$.each(weekData2,
				function(i, n) {
					var html = '';
					n.lessonData.sort(compare('sectionTime'));
					if (n.lessonData.length == 0) {
						html += '<li><div class="noHtml">该时间段没有课程</div></li>';
					} else {
						$.each(n.lessonData,
							function(j, k) {
								
								html += '<li AcademicYear="' + k.AcademicYear + '"  ClassRoomNo="' + k.ClassRoomNo + '" EmpNo= "' + k.EmpNo +
									'" LessonDate= "' + k.LessonDate + '"  LessonNo= "' + k.LessonNo + '" LessonSortNo= "' + k.LessonSortNo +
									'" LessonTime= "' + k.LessonTime + '" Semester= "' + k.Semester + '" IsCheck="' + k.IsCheck +
									'" LeaveType="' + k.LeaveType + '" TeachClassNo="' + k.TeachClassNo + '" ClassroomName="' + k.ClassroomName +
									'" LessonName="' + k.LessonName + '" EmpName="'+ k.EmpName +'">' +
									'<div class="tSec2ListL"><b>' +
									k.sectionTime + '</b>' +
									'节</div>' +
									'<div class="tSec2ListR">' +
									'<div class="tSec2ListBox">' +
									'<div class="tSec2ListT">' +
									k.LessonName +
									'</div>' +
									'<div class="tSec2ListTxt">' +
									'<i class="iconfont icon-addressbook1"></i>' +
									k.EmpName +
									'</div>' +
									'</div>' +
									'<div class="tSec2Location">' +
									'<i class="iconfont icon-locationfill"></i>' +
									k.ClassroomName +
									'</div>' +
									'</div>' +
									'</li>';
							});
					}
					tSec2List.eq(n.weekTime - 1).html(html);
				});
		});
	}

	var todayWeek = weekday();

	var page = {
		isScroll: false,
		init: function($page) {
			weekTimeSpan = $page.find('#weekTime span');
			weekTimeI = $page.find('#weekTime i');
			loadData(todayWeek.Monday, todayWeek.Sunday);
			$page.find('.tSec2Top li').on('tap',
				function() {
					$(this).addClass('active').siblings().removeClass('active');
					var ind = $(this).index();
					$('.tSec2Con').css('display', 'none').eq(ind).stop().fadeIn();
				});
			$page.find('.tSec2List').on('tap', 'li', function() {

				var AcademicYear = $(this).attr("AcademicYear");
				var Semester = $(this).attr("Semester");
				var EmpNo = $(this).attr("EmpNo");
				var LessonNo = $(this).attr("LessonNo");
				var TeachClassNo = $(this).attr("TeachClassNo");
				var LessonTime = $(this).attr("LessonTime");
				var LessonDate = $(this).attr("LessonDate");
				var LessonSortNo = $(this).attr("LessonSortNo");
				var ClassRoomNo = $(this).attr("ClassRoomNo");
				var ClassroomName = $(this).attr("ClassroomName");
				var LessonName = $(this).attr("LessonName");
				var EmpName = $(this).attr("EmpName");


				learun.nav.go({
					path: 'studentAttendance/form',
					title: '学生签到',
					isBack: true,
					isHead: true,
					param: {
						AcademicYear: AcademicYear,
						Semester: Semester,
						EmpNo: EmpNo,
						LessonNo: LessonNo,
						TeachClassNo: TeachClassNo,
						LessonTime: LessonTime,
						LessonDate: LessonDate,
						LessonSortNo: LessonSortNo,
						ClassRoomNo: ClassRoomNo,
						ClassroomName: ClassroomName,
						LessonName: LessonName,
						EmpName: EmpName
					},
					type: 'right'
				});


			});
			//上下周切换
			var weekDayTime = 7 * oneDayTime;
			weekTimeI.on('tap',
				function() {
					var index = weekTimeI.index($(this));
					if (index == 0) {
						todayWeek.MondayTime = todayWeek.MondayTime - weekDayTime;
						todayWeek.SundayTime = todayWeek.SundayTime - weekDayTime;
					} else {
						todayWeek.MondayTime = todayWeek.MondayTime + weekDayTime;
						todayWeek.SundayTime = todayWeek.SundayTime + weekDayTime;
					}
					todayWeek.Monday = fmtDate(todayWeek.MondayTime);
					todayWeek.Sunday = fmtDate(todayWeek.SundayTime);
					weekTimeSpan.eq(0).text(todayWeek.Monday).end().eq(1).text(todayWeek.Sunday);
					judgeDate(todayWeek.SundayTime);
					loadData(todayWeek.Monday, todayWeek.Sunday);
				});

		}
	};
	return page;
})();
