$(function(){
	var maxShowYear = '2024';
	// 获取日历可显示的最大年份
	$.ajax({
		url:'/workingDayManage/do_getMaxYear',
		type:'get',
		data:{},
		success:function(data){
			if( data.success ){
				maxShowYear = data.data;
			}else{
				new $.zui.Messager('获取日历最大可显示年份失败:'+data.msg, {
					type : 'danger',
				}).show();
			}
		},
		error:function(status){
			new $.zui.Messager('获取日历最大可显示年份时出错:'+status, {
				type : 'danger',
			}).show();
		}
	});
	
	// 初始化日历
	$('#workingDayCalendar').calendar({
		'dragThenDrop':false,
		'withHeaderworkingDay':true
	});
	
	// 获取日历对象实例
	var workingDayCalendar = $('#workingDayCalendar').data('zui.calendar');
	
	// 获取当前显示月的信息(哪些是工作日/非工作日,备注是什么),添加到日历中,并给非工作日添加红底标志
	function getDetailMonthNews(yearMonth){
		yearMonth = yearMonth+'-01';
		// 先清除掉原先的事件(备注/工作日标志/日期id)
		var deleteEventsIds=[];
		for(var i=0;i<workingDayCalendar.events.length;i++){
			deleteEventsIds.push(workingDayCalendar.events[i].id)
		};
		workingDayCalendar.removeEvents(deleteEventsIds);
		$(".holiday").removeClass("holiday");
		$(".cell-day.current-month").removeAttr('dayid')
		var monthData =[],
			newEvents = [];
		$.ajax({
			url:'/workingDayManage/do_list',
			type:'get',
			data:{
				'yearMonth':yearMonth
			},
			success:function(data){
				if( data.success ){
					monthData = data.data;
					for( var i=0;i<monthData.length;i++ ){
						$(".cell-day.current-month").eq(i).attr('dayid',monthData[i].id);
						if( !monthData[i].workingDay ){// 不是工作日
							$(".cell-day.current-month").eq(i).addClass('holiday');
						};
						if( monthData[i].remark ){// 有备注
							newEvents.push({
								title:monthData[i].remark,
								start:monthData[i].dateTime,
								end:monthData[i].dateTime
							});
						};
					};
					i = null;
					workingDayCalendar.addEvents(newEvents);
				}else{
					new $.zui.Messager('获取'+yearMonth+'信息失败:'+data.msg, {
						type : 'danger',
					}).show();
					return;
				}
			},
			error:function(status){
				new $.zui.Messager('获取'+yearMonth+'信息时出错:'+status, {
					type : 'danger',
				}).show();
				return;
			}
		});
	};
	getDetailMonthNews(getCurrentDayTime().substr(0,7));
	
	// 切换月份
	function changeMonth(data){
		var currentYear = $(".workingDay-year").text(),
			currentMonth = $(".workingDay-month").text(),
			newCurrentYear = '',
			newCurrentMonth = '',
			newShowMonth = '';
		var judgeCurrentMonth = '01';
		if( data.data.source=='next' ){judgeCurrentMonth='12'}else{judgeCurrentMonth='01'};
		if( currentMonth!=judgeCurrentMonth ){
			newCurrentYear = currentYear;
			if( data.data.source=='next' ){
				newCurrentMonth = (parseInt(currentMonth)+1)>=10?(parseInt(currentMonth)+1):'0'+(parseInt(currentMonth)+1);
			}else{
				newCurrentMonth = (parseInt(currentMonth)-1)>=10?(parseInt(currentMonth)-1):'0'+(parseInt(currentMonth)-1);
			};
			newShowMonth = newCurrentYear+'-'+newCurrentMonth;
		}else{
			if( data.data.source=='next' ){
				if( (parseInt(currentYear)+1)>(parseInt(maxShowYear)) ){
					new $.zui.Messager('该日历最大显示时间为'+maxShowYear+'年12月31号', {
						type : 'danger',
					}).show();
					return;
				};
				newCurrentYear = (parseInt(currentYear)+1);
				newCurrentMonth = '01';
			}else{
				if( (parseInt(currentYear)-1)<2018 ){
					new $.zui.Messager('该日历最小显示时间为2018年01月01号', {
						type : 'danger',
					}).show();
					return;
				};
				newCurrentYear = (parseInt(currentYear)-1);
				newCurrentMonth = '12';
			};
			newShowMonth = newCurrentYear+'-'+newCurrentMonth;
		};
		$(".workingDay-year").text(newCurrentYear);
		$(".workingDay-month").text(newCurrentMonth);
		workingDayCalendar.display('month',newShowMonth);
		getDetailMonthNews(newShowMonth);
	};
	
	// 切换到上个月
	$("#workingDayCalendar").on("click",'.workingDay-prev',{'source':'prev'},changeMonth);
	
	// 切换到下个月
	$("#workingDayCalendar").on("click",'.workingDay-next',{'source':'next'},changeMonth);
	
	// 把"Tue May 01 2018"转换为yyyy-mm-dd格式
	function transferFormat(date){
		var year = date.substr(11,4);
		var day = date.substr(8,2);
		var monthEn = date.substr(4,3);
		var month = '01';
		switch(monthEn){
			case 'Jan': month = '01';break;
			case 'Feb': month = '02';break;
			case 'Mar': month = '03';break;
			case 'Apr': month = '04';break;
			case 'May': month = '05';break;
			case 'Jun': month = '06';break;
			case 'Jul': month = '07';break;
			case 'Aug': month = '08';break;
			case 'Sep': month = '09';break;
			case 'Oct': month = '10';break;
			case 'Nov': month = '11';break;
			case 'Dec': month = '12';break;
		}
		return year+'-'+month+'-'+day;
	};
	
	// 显示编辑框
	function showEditDialog(data){
		var $this;
		if( data.data.source=='td' ){
			$this = $(this);
		}else{
			$this = $(this).parents('.current-month');
		};
		var clickDate = transferFormat($this.find('.day').attr('data-date'));
		var todayDate = getCurrentDayTime();
		if( new Date(Date.parse(clickDate.replace(/-/g,"/"))) < new Date(Date.parse(todayDate.replace(/-/g,"/"))) ){
			new $.zui.Messager('不能编辑今天('+todayDate+')之前的日期', {
			    type: 'danger'
			}).show();	
			return;
		};
		$(".editShowDate").text(clickDate);
		$("#dateTime").val(clickDate);
		$("[name=id]").val($this.attr('dayid'));
		$("#remark").val($this.find(".content .events span.title").text())
		if( $this.hasClass("holiday") ){
			$(".notweekday").prop("checked",true)
		}else{
			$(".weekday").prop("checked",true)
		};
		$('.saveSure').removeClass("loadingCircle");
		$("#myModal-updateWorkingDayNature").modal("show");
	};
	
	// 点击当前显示月的单元格弹出编辑框
	$("#workingDayCalendar").on('click','.cell-day.current-month',{'source':'td'},showEditDialog);
	$("#workingDayCalendar").on('click','.cell-day.current-month .content .event',{'source':'event'},showEditDialog);
	
	// 编辑保存
	$(".saveSure").click(function(){
		$("#editDateForm").validate({
			 promptPosition : "bottomLeft",
			 submitHandler : function(form) {
				$('.saveSure').addClass("loadingCircle");
				$.ajax({
					url:'/workingDayManage/do_update',
					type:'post',
					data:$("#editDateForm").serialize(),
					success:function(data){
						if( data.success ){
							new $.zui.Messager('编辑日期成功', {
							    type: 'success'
							}).show();	
							$("#myModal-updateWorkingDayNature").modal("hide");
							// 重新获取当前显示月的信息
							getDetailMonthNews($(".workingDay-year").text()+'-'+$(".workingDay-month").text());
						}else{
							new $.zui.Messager('编辑日期失败:'+data.msg, {
							    type: 'danger'
							 }).show();	
						};
						$('.saveSure').removeClass("loadingCircle");
					},
					error:function(){
						new $.zui.Messager('编辑日期时发生错误:'+status, {
						    type: 'danger'
						 }).show();
						$('.saveSure').removeClass("loadingCircle");
					}
				})
				
			} 
		}); 
	});
})