function getDate(date){
	var d = new Date(date),m,day;
	parseInt(d.getMonth())+1<10?m="0"+(parseInt(d.getMonth())+1):m=d.getMonth()+1;
	d.getDate()<10?day="0"+d.getDate():day=d.getDate();
	return d.getFullYear() + '-' + m + '-' + day;
}
function getNowDate(obj){
	var nowdate=0;
	$.ajax({
     	url: '/weekWorkPlan/do_getNowDate',
     	dataType: 'json',
     	async:false,
		data: {},
     	success: function(data) { // 获取当前月的数据
        	if(obj=="year"){
        		nowdate=new Date(data).getFullYear()
        	}else if(obj=="month"){
        		nowdate=new Date(data).getMonth()+1;
        		/* if(nowdate<10){
        			nowdate="0"+nowdate
        		} */
        	}else {
        		nowdate=new Date(data)
        	}
     	},
     	error:function(error){
     		new $.zui.Messager('获取当前时间出错：'+error, {
			    type: 'warning',
			    time:1500
			}).show();
     	}
	});
	return nowdate
}
function getYearMonth(year,month){
	if(year==undefined){
		var date=doGetNowDate().substring(0,7);//获取系统时间
		return date;
	}else {
		if(parseInt(month)<10){
			return year+"-"+"0"+month;
		}else {
			return year+"-"+month;
		}
	}
}
$(function() {
	var weekviewEvents=[]; 
	var systemYear=getNowDate("year");
	var systemMonth=getNowDate("month");
	var weekSystemYear=$("#weekYear").val();
	function initCalendar(){
		$('.calendar').each(function() { 
			var newEvents = [];
			var $this = $(this);
			//获取后台数据---------------------------------------------------------------月视图init-------------------------------------------------1
			$.ajax({
             	url: '/weekWorkPlan/do_findCalendarList',
             	dataType: 'json',
 			 	data: {
 			 		'yearMonth':getYearMonth()
 	         	},
             	success: function(json) { // 获取当前月的数据
                	$.each(json.data,function(i,val) {     
                    	newEvents.push({
	                     	id:val.id,
	                       	title:val.title,
	                       	desc:val.desc,
	                       	calendar:val.calendar,
	                       	priority:val.priority,
	                       	content:val.content,
	                       	start:val.start,
	                       	end:val.end
                    	}); 
                	});
                	var data = { 
						calendars: [
							{name: "workState_pause", color: '#a1a1a1'}, //暂停
							{name: "workState_finsh", color: '#83af6e'}, //已完成
							{name: "workState_stop", color: '#d15a44'}, //终止
							{name: "workState_continue_next_week", color: '#fddf87'}, //下周继续
							{name: "workState_working", color: '#3a87ad'}, //工作中
							{name: "workState_overdue", color: '#000'} //已过期
						], 
						events: [] 
					}; 
					for(var i=0;i<newEvents.length;i++){
						data.events.push(newEvents[i]);
					} 
					$this.calendar({
						data: data,
						dragThenDrop:false,
						clickEvent: function(e) { 
							weekWorkView(e.event.id,e.event.start);
						}
					});
             	},
 	        	error:function(error){
 	        		new $.zui.Messager('获取数据发生错误', {
					    type: 'warning',
					    time:1500
					}).show();
 	        	}
        	});
			
		});
	}
	//进入周工作计划的事件
	$("#myModal-viewweekworkplan").on("click",".enterWeekWorkBtn",function(){
		var time=getDate($(this).data("time"));
		$("#myModal-viewweekworkplan").modal('hide');
		$('.modal-backdrop').remove();
		window.location.href = window.location.href+'?fromCalendar=1'
		$(".yk_rightcontent").load("/weekWorkPlan/list?workTime="+time,function(){
			backTop('.yk_rightcontent');
		});
		$(".yk_secondmenu").find("#workingCalendar").removeClass("yk_menuactive").parents().find("#myWeekworkManage").addClass("yk_menuactive");
	})
	initCalendar();
	//下拉框按钮的点击事件:使当前的时间（年份或月份）显示在下拉框的第一位
	$("#calendar").delegate(".btn-group .dropdown-toggle","click",function(){
		if(!$(this).parent().hasClass('open')){
			var that=$(this);
			var h=parseInt($(this).next().find(".actived").data("index"))-parseInt($(this).next().children("li:first-child").data("index"));
			setTimeout(function(){
				that.next().scrollTop(h*26);
			},100)

		}
	})
	// 根据日期添加月视图事件
	function addEventByMySelf(year,month){
		$('#calendar').calendar();//创建calendar
		var calendar = $('#calendar').data('zui.calendar');//实例化日历
	  	//清空之前的事件--开始
		var events=calendar.events;
		var eventid=[];
		for(var i=0;i<events.length;i++){
			eventid.push(events[i].id)
		}
		calendar.removeEvents(eventid);
		//清空之前的事件--结束
		//if( parseInt(month)<10 ){month = '0'+month};
		calendar.display('month', year+'-'+month+'-'+'01');//切换视图
	
		var newEvents = [];
		//用户选择年月后，获取后台数据---------------------------------------------------------------月视图--------------------------------------------2
		$.ajax({
         	url: '/weekWorkPlan/do_findCalendarList',
         	dataType: 'json',
		 	data: {
             	'yearMonth':getYearMonth(year,month)
         	},
         	success: function(json) { // 获取当前月的数据
            	$.each(json.data,function(i,val) {                     
                	newEvents.push({
                     	id:val.id,
                       	title:val.title,
                       	desc:val.desc,
                       	calendar:val.calendar,
                       	priority:val.priority,
                       	content:val.content,
                       	start:val.start,
                       	end:val.end,
                       	allDay:false
                	}); 
            	});	
            	calendar.addEvents(newEvents);  
         	},
        	error:function(error){
        		new $.zui.Messager('获取数据发生错误', {
				    type: 'warning',
				    time:1500
				}).show();
        	}
    	});
	}
	 
	//根据年份获取本年的所有周数据
	function getWeekNumDetail(year){
		var weekdetail=[];
		//用户选择年月后，获取后台数据---------------------------------------------------------------月视图--------------------------------------------3
		$.ajax({
         	url: '/weekWorkPlan/do_getWeeks',
         	dataType: 'json',
         	async:false,
		 	data: {
             	'year':year
         	},
         	success: function(data) { // 获取当前月的数据
            	if(data.success){
            		weekdetail=data.data
            	}
         	},
        	error:function(error){
        		new $.zui.Messager('获取所有周出错：'+error, {
				    type: 'warning',
				    time:1500
				}).show();
        	}
    		});
		return weekdetail;
	}
	//根据年份+周获取数据
	function getWeekData(start,end){
		var weekData=[];
		//用户选择年月后，获取后台数据---------------------------------------------------------------月视图--------------------------------------------4
		$.ajax({
         	url: '/weekWorkPlan/do_findWeekList',
         	dataType: 'json',
		 	data: {
             	'startTime':start,
             	'endTime':end
         	},
         	success: function(data) { // 获取当前月的数据
          		weekviewEvents=data;
            	var obj=data.data;
				var viewweekcontent='';
				var sum=0;
				if(obj.length!=0){//这周有计划
					$(".calendar-views .week .weekview-body").removeClass("nomore-content");
					for(var i=0;i<obj.length;i++){//头部信息
						if(obj[i].data.length){
							viewweekcontent+='<div class="view-week-content"><div class="content-header">'+obj[i].week+'&nbsp;'+obj[i].start+'</div>'
							for(var j=0;j<obj[i].data.length;j++){
								sum++;
								var a="";
								if(obj[i].data[j].priority=="urgent"){
									a="priority";
								}
								viewweekcontent+='<div class="content-detail '+a+'" data-index="'+obj[i].data[j].id+'"><div class="detail-title '+obj[i].data[j].calendar+'">'+obj[i].data[j].title+'</div><div class="detail-dec">'+obj[i].data[j].content+'</div></div>';
							}
							viewweekcontent+='</div>';
						}
					}
					$(".week-view-record .record").html("(共"+sum+"条记录)");
				}else {//这周没有计划
					$(".calendar-views .week .weekview-body").addClass("nomore-content");
					$(".week-view-record .record").html("(共0条记录)");
					viewweekcontent='<div class="nomore-font">本周没有数据<div>'
				}
				$(".calendar-views .week .weekview-body").empty().append(viewweekcontent);
         	},
        	error:function(error){
        		new $.zui.Messager('获取数据失败', {
					type : 'danger',
					time : 1500
				}).show();
        	}
    	});	
	}
	// 月视图----年份月份下拉框的点击事件
	$("#calendar").delegate(".btn-group-center .dropdown-menu li","click",function(){
		//获取要刷新的月历日期--开始
		var ind=$(this).data("index");
		if(parseInt(ind)<10){
			ind='0'+ind
		}
		$(this).parent().prev().empty().text(ind);
		var index1=$(this).data("index");
		var year=systemYear;
		var month=systemMonth;
		index1>12?year=index1:month=index1;//如大于12是年，小于12是月
		index1>12?month=$(".month-dropdown-menu").find(".actived").data("index"):year=$(".year-dropdown-menu").find(".actived").data("index");
	  	//获取要刷新的月历日期--结束
		$(this).parent().find(".actived").removeClass("actived");$(this).addClass("actived");//切换选中的下拉框的项
		addEventByMySelf(year,month);
	});
	//周视图----年份周数下拉框的点击事件
	$("#calendar").delegate(".btn-group-weekcenter li","click",function(){
		if($(this).parent().hasClass("week-dropdown-menu")){//点击的是周
			$(this).parent().prev().html($(this).data("index"));
			var timeslot=$(this).find("span").text();
			var start=timeslot.substring(1,3)+"月"+timeslot.substring(4,6)+"日";var end=timeslot.substring(7,9)+"月"+timeslot.substring(10,12)+"日";
			$(".calendar-dateRange").empty().html(start+'~'+end);
		}else {//点击的是年
			$(this).parent().prev().html($(this).find("a").text());
			var weekdetail=getWeekNumDetail(parseInt($(this).find("a").text()));//根据年份获取本年的所有周数
			var weekL='';
			var weekindex=$(".btn-group-weekcenter .week-dropdown-menu").find(".actived").data("index");
			for(var z=0;z<weekdetail.length;z++){
		    	if(weekindex==weekdetail[z].week){
		    		weekL+='<li class="actived" data-index="'+weekdetail[z].week+'"><a javascript:void(0)>'+weekdetail[z].week+'周<span class="dateSlot">('+weekdetail[z].startDate+'~'+weekdetail[z].endDate+')</span></a></li>';
		    		var sss=weekdetail[z].startDate.substring(0,2)+"月"+weekdetail[z].startDate.substring(3,5)+"日";
		    		var eee=weekdetail[z].endDate.substring(0,2)+"月"+weekdetail[z].endDate.substring(3,5)+"日";
		    		$(".calendar-dateRange").empty().html(sss+'~'+eee);
		    	}else {
		    		weekL+='<li data-index="'+weekdetail[z].week+'"><a javascript:void(0)>'+weekdetail[z].week+'周 <span class="dateSlot">('+weekdetail[z].startDate+'~'+weekdetail[z].endDate+')</span></a></li>';
		    	}
		    }
		    $(".btn-group-weekcenter .week-dropdown-menu").empty().html(weekL);
		   	var flag=false;
	    	$(".week-dropdown-menu li").each(function(){
	    		if($(this).hasClass("actived")) flag=true;
	    	})
	    	if(!flag){//修改btn值／span值／设置actived
	    		var num=weekdetail[weekdetail.length-1].week;
	    		$(".btn-group-weekcenter .btn-group-week .dropdown-toggle").html(num);
	    		var sss=weekdetail[weekdetail.length-1].startDate.substring(0,2)+"月"+weekdetail[weekdetail.length-1].startDate.substring(3,5)+"日";
	    		var eee=weekdetail[weekdetail.length-1].substring(0,2)+"月"+weekdetail[weekdetail.length-1].substring(3,5)+"日";
	    		$(".calendar-dateRange").empty().html(sss+'~'+eee);
	    		$(".week-dropdown-menu li").last().addClass("actived");
	    	}
		}
		var index1=$(this).data("index");
		var year=weekSystemYear;
		index1>60?year=index1:year=$(".year-dropdown-menu").find(".actived").data("index");
	  	//获取要刷新的月历日期--结束
		$(this).parent().find(".actived").removeClass("actived");$(this).addClass("actived");//切换选中的下拉框的项
		var timeslot=$(".week-dropdown-menu").find(".actived").find(".dateSlot").text();
		var start=timeslot.substring(1,6);var end=timeslot.substring(7,12); 
		getWeekData(year+'-'+start,year+'-'+end);
	})
	//点击月视图---切换到月视图
	$("#calendar").delegate(".btn-group-right .group-right-monthview","click",function(){
		if(!$(this).hasClass("group-btn-active")){//确定月视图本来未处于active状态
			$(this).addClass("group-btn-active").next().removeClass("group-btn-active");
			//重新显示月视图
			$("#calendar .btn-group-center").css("display","block");//显示头部
			$("#calendar .calendar-views").find(".month").show();
			$("#calendar .calendar-views").find(".week").remove();
			$("#calendar").find(".week-view-record .time-slot").empty().end().find(".week-view-record .record").empty();
			$("#calendar .btn-group-weekcenter").remove();
			// 重新给下拉框赋值--开始
			$(this).parent().next().find(".actived").removeClass("actived");
			var year=systemYear;var month=systemMonth;
			$(this).parent().next().find(".year-dropdown-menu li").each(function(){
				if($(this).text()==year){
					$(this).addClass("actived");
					$(this).parent().prev().empty().html(year);
				}
			})
			$(this).parent().next().find(".month-dropdown-menu li").each(function(){
				if($(this).text()==month+"月"){
					$(this).addClass("actived");
					if(month<10){
						$(this).parent().prev().empty().html('0'+month);
					}else {
						$(this).parent().prev().empty().html(month);
					}
				}
			})
			// 重新给下拉框赋值--结束
			addEventByMySelf(year,month);//重新获取数据
		}
	})
	//点击周视图---切换到周视图
	$("#calendar").delegate(".btn-group-right .group-right-weekview","click",function(){
		if(!$(this).hasClass("group-btn-active")){//周视图本来未处于active状态
			$(this).addClass("group-btn-active").prev().removeClass("group-btn-active");
			$("#calendar .calendar-views .calendar-view").hide();
			$("#calendar .btn-group-center").css('display','none');//月视图头部内容隐藏
			$("#calendar .calendar-views").find(".calendar-view").after('<div class="calendar-view week"></div>');//清空月视图的所有元素
			//年周的下拉框
		    var yearL='';
		    var weekL='';
		    for(var i=1970;i<=2116;i++){
		        if(weekSystemYear==i){
		            yearL+='<li class="actived" data-index="'+i+'"><a javascript:void(0)>'+i+'</a></li>';
		        }else {
		            yearL+='<li data-index="'+i+'"><a javascript:void(0)>'+i+'</a></li>';
		        }
		    }
		    var weekindex=$("#weekWeekth").val();
		    var weekdetail=getWeekNumDetail(weekSystemYear);
		    var dateRange;
		    for(var z=0;z<weekdetail.length;z++){
		    	if(weekindex==weekdetail[z].week){
		    		weekL+='<li class="actived" data-index="'+weekdetail[z].week+'"><a javascript:void(0)>'+weekdetail[z].week+'周<span class="dateSlot">('+weekdetail[z].startDate+'~'+weekdetail[z].endDate+')</span></a></li>';
		    		var sss=weekdetail[z].startDate.substring(0,2)+"月"+weekdetail[z].startDate.substring(3,5)+"日";var eee=weekdetail[z].endDate.substring(0,2)+"月"+weekdetail[z].endDate.substring(3,5)+"日";
		    		dateRange=sss+'~'+eee;
		    	}else {
		    		weekL+='<li data-index="'+weekdetail[z].week+'"><a javascript:void(0)>'+weekdetail[z].week+'周<span class="dateSlot">('+weekdetail[z].startDate+'~'+weekdetail[z].endDate+')</span></a></li>';
		    	}
		    }
		    var yearHTML='<div class="btn-group"><button class="btn btn-link" type="button" style="float:right">年</button><button type="button" class="btn dropdown-toggle" data-toggle="dropdown">'+weekSystemYear+'</button><ul class="year-dropdown-menu dropdown-menu pull-right" role="menu">'+yearL+'</ul></div><div class="btn-group prev_next_btn"><button type="button" class="btn year_prev_btn"><i class="icon icon-caret-up"></i></button><button type="button" class="btn year_next_btn"><i class="icon icon-caret-down"></i></button></div>';
			var weekHTML='<div class="btn-group btn-group-week"><button class="btn btn-link" type="button" style="float:left;margin-right: 0px;">第</button><button class="btn btn-link" type="button" style="float:right">周</button><button type="button" class="btn dropdown-toggle" data-toggle="dropdown">'+weekindex+'</button><ul class="week-dropdown-menu dropdown-menu pull-right" role="menu">'+weekL+'</ul></div><div class="btn-group prev_next_btn"><button type="button" class="btn week_prev_btn"><i class="icon icon-caret-up"></i></button><button type="button" class="btn week_next_btn"><i class="icon icon-caret-down"></i></button></div>';

			var weekgroupcenter='<div class="btn-group btn-group-weekcenter">'+yearHTML+weekHTML+'<span class="calendar-dateRange">'+dateRange+'</span></div>';
			$("#calendar .btn-toolbar").append(weekgroupcenter);
			//向calendar-views内部添加内容
			var viewsWeekHeader='<div class="weekview-header"><div class="state"><div class="weekview-state workState_working">进行中</span></div><div class="weekview-state workState_finsh">完成</div><div class="weekview-state workState_pause">暂停</div><div class="weekview-state workState_continue_next_week">下周继续</div><div class="weekview-state workState_stop">终止</div><div class="weekview-state workState_overdue">逾期</div></div></div><div class="weekview-body"></div>';
			$(".calendar-views").find(".week").append(viewsWeekHeader);
			//获取数据
			var timeslot=$(".week-dropdown-menu").find(".actived").find(".dateSlot").text();
			var start=timeslot.substring(1,6);var end=timeslot.substring(7,12); 
			getWeekData(weekSystemYear+'-'+start,weekSystemYear+'-'+end);
		}
	})
	
	//周视图的单个事件点击的弹框事件
	$("#calendar").delegate(".calendar-views .view-week-content .content-detail","click",function(){
		weekWorkView($(this).data("index"),$(this).parent().find(".content-header").text().substring(3,13));
	})
	/* 月视图的年份月份prev／next点击 */
	$("#calendar").delegate(".btn-group-center .prev_next_btn .btn","click",function(){
		var myyear=systemYear,mymonth=systemMonth;
		if($(this).hasClass("year_prev_btn")||$(this).hasClass("year_next_btn")){//年份
			if($(this).hasClass("year_prev_btn")){
				$(this).parent().prev().find(".year-dropdown-menu .actived").prev().addClass("actived");
				$(this).parent().prev().find(".year-dropdown-menu .actived").eq(1).removeClass("actived");
			}else{
				$(this).parent().prev().find(".year-dropdown-menu .actived").next().addClass("actived");
				$(this).parent().prev().find(".year-dropdown-menu .actived").eq(0).removeClass("actived");
			}
			myyear=$(this).parent().prev().find(".year-dropdown-menu .actived").data("index");
			mymonth=$(this).parent().next().find(".month-dropdown-menu .actived").data("index");
			$(this).parent().prev().find(".dropdown-toggle").html(myyear);
		}else {//月份
			var activeMonth=$(this).parent().prev().find(".month-dropdown-menu .actived");
			var activeYear=parseInt($(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").data("index"));
			if($(this).hasClass("month_prev_btn")){
				if(activeMonth.data("index")==1){
					activeYear--;
					$(this).parent().prev().prev().prev().find(".dropdown-toggle").text(activeYear);
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").prev().addClass("actived");
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").eq(1).removeClass("actived"); //重新渲染年份显示及年下拉框的选中值
					$(this).parent().prev().find(".month-dropdown-menu li").last().addClass("actived");
					
					$(this).parent().prev().find(".month-dropdown-menu .actived").eq(0).removeClass("actived");
				}else {
					$(this).parent().prev().find(".month-dropdown-menu .actived").prev().addClass("actived");
					$(this).parent().prev().find(".month-dropdown-menu .actived").eq(1).removeClass("actived");
				}
			}else {
				if(activeMonth.data("index")==12){
					activeYear++;
					$(this).parent().prev().prev().prev().find(".dropdown-toggle").text(activeYear);
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").next().addClass("actived");
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").eq(0).removeClass("actived"); //重新渲染年份显示及年下拉框的选中值
					$(this).parent().prev().find(".month-dropdown-menu li").first().addClass("actived");
					$(this).parent().prev().find(".month-dropdown-menu .actived").eq(1).removeClass("actived");
				}else {
					$(this).parent().prev().find(".month-dropdown-menu .actived").next().addClass("actived");
					$(this).parent().prev().find(".month-dropdown-menu .actived").eq(0).removeClass("actived");
				}
			}
			myyear=activeYear;
			mymonth=$(this).parent().prev().find(".month-dropdown-menu .actived").data("index");
			if(parseInt(mymonth)<10){
				$(this).parent().prev().find(".dropdown-toggle").html('0'+mymonth);
			}else {
				$(this).parent().prev().find(".dropdown-toggle").html(mymonth);
			}
		}
		addEventByMySelf(myyear,mymonth);
	})
	/* 周视图的年份周数prev／next点击 */
	$("#calendar").delegate(".btn-group-weekcenter .prev_next_btn .btn","click",function(){
		var myyear,start,end;
		if($(this).hasClass("year_prev_btn")||$(this).hasClass("year_next_btn")){
			if($(this).hasClass("year_prev_btn")){//上一年
				$(this).parent().prev().find(".year-dropdown-menu .actived").removeClass("actived").prev().addClass("actived");
				var year=$(this).parent().prev().find(".year-dropdown-menu .actived").data("index");
				$(this).parent().prev().find(".dropdown-toggle").html(year);
			}else if($(this).hasClass("year_next_btn")){//下一年
				$(this).parent().prev().find(".year-dropdown-menu .actived").removeClass("actived").next().addClass("actived");
				var year=$(this).parent().prev().find(".year-dropdown-menu .actived").data("index");
				$(this).parent().prev().find(".dropdown-toggle").html(year);
			}
			//重新获取年份下的所有周数，并填充进入周的下拉框中
			var weekdetail=getWeekNumDetail(parseInt($(this).parent().prev().find(".actived a").text()));//根据年份获取本年的所有周数
			var weekL='';
			var weekindex=$(".btn-group-weekcenter .week-dropdown-menu").find(".actived").data("index");
			for(var z=0;z<weekdetail.length;z++){
		    	if(weekindex==weekdetail[z].week){
		    		weekL+='<li class="actived" data-index="'+weekdetail[z].week+'"><a javascript:void(0)>'+weekdetail[z].week+'周<span class="dateSlot">('+weekdetail[z].startDate+'~'+weekdetail[z].endDate+')</span></a></li>';
		    		var sss=weekdetail[z].startDate.substring(0,2)+"月"+weekdetail[z].startDate.substring(3,5)+"日";
		    		var eee=weekdetail[z].endDate.substring(0,2)+"月"+weekdetail[z].endDate.substring(3,5)+"日";
		    		$(".calendar-dateRange").html(sss+'~'+eee);
		    	}else {
		    		weekL+='<li data-index="'+weekdetail[z].week+'"><a javascript:void(0)>'+weekdetail[z].week+'周 <span class="dateSlot">('+weekdetail[z].startDate+'~'+weekdetail[z].endDate+')</span></a></li>';
		    	}
		    }
		    $(".btn-group-weekcenter .week-dropdown-menu").empty().html(weekL);
		    //判断已选的周数在此年份是否存在，不存在就默认最后一周为所选的周数
		   	var flag=false;
	    	$(".week-dropdown-menu li").each(function(){
	    		if($(this).hasClass("actived")) flag=true;
	    	})
	    	if(!flag){//修改btn值／span值／设置actived
	    		var num=weekdetail[weekdetail.length-1].week;
	    		$(".btn-group-weekcenter .btn-group-week .dropdown-toggle").html(num);
	    		var sss=weekdetail[weekdetail.length-1].startDate.substring(0,2)+"月"+weekdetail[weekdetail.length-1].startDate.substring(3,5)+"日";
	    		var eee=weekdetail[weekdetail.length-1].endDate.substring(0,2)+"月"+weekdetail[weekdetail.length-1].endDate.substring(0,2)+"日";
	    		$(".calendar-dateRange").html(sss+'~'+eee);
	    		$(".week-dropdown-menu li").last().addClass("actived");
	    	}
	   		myyear=$(this).parent().prev().find(".dropdown-toggle").html();//保存最新的所选年
			
		}else {
			if($(this).hasClass("week_prev_btn")){//上一周
				if($(this).parent().prev().find(".week-dropdown-menu .actived").data("index")==1){//判断已点击到本年第一周
					var prevyear=parseInt($(this).parent().prev().prev().prev().find(".dropdown-toggle").text());
					prevyear--;//将年份减少一年
					renderingWeekOption(prevyear,"last");//重新渲染周下拉框
					$(this).parent().prev().prev().prev().find(".dropdown-toggle").text(prevyear);
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").prev().addClass("actived");
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").eq(1).removeClass("actived");//重新渲染年份显示及年下拉框的选中值
				}else {
					$(this).parent().prev().find(".week-dropdown-menu .actived").prev().addClass("actived");
					$(this).parent().prev().find(".week-dropdown-menu .actived").eq(1).removeClass("actived");//将周下拉框上移一个li
					var week=$(this).parent().prev().find(".week-dropdown-menu .actived").data("index");//获取新的周数
					if(parseInt(week)<10){
						week='0'+week;
					}
					$(this).parent().prev().find(".dropdown-toggle").empty().html(week);//渲染新的周数
				}
				var timeslot=$(this).parent().prev().find(".week-dropdown-menu .actived span").text();
				var sss=timeslot.substring(1,3)+"月"+timeslot.substring(4,6)+"日";
	    		var eee=timeslot.substring(7,9)+"月"+timeslot.substring(10,12)+"日";
				$(".calendar-dateRange").empty().html(sss+'~'+eee);//重新渲染新的周范围
			}else if($(this).hasClass("week_next_btn")){//下一周
				if($(this).parent().prev().find(".week-dropdown-menu li:last-child").hasClass("actived")){//判断已点击到本年第一周
					var nextyear=parseInt($(this).parent().prev().prev().prev().find(".dropdown-toggle").text());
					nextyear++;//将年份增加一年
					renderingWeekOption(nextyear,"first");//重新渲染周下拉框
					$(this).parent().prev().prev().prev().find(".dropdown-toggle").text(nextyear);
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").next().addClass("actived");
					$(this).parent().prev().prev().prev().find(".year-dropdown-menu .actived").eq(0).removeClass("actived");//重新渲染年份显示及年下拉框的选中值
				}else {
					$(this).parent().prev().find(".week-dropdown-menu .actived").next().addClass("actived");
					$(this).parent().prev().find(".week-dropdown-menu .actived").eq(0).removeClass("actived");//将周下拉框下移一个li
					var week=$(this).parent().prev().find(".week-dropdown-menu .actived").data("index");//获取新的周数
					if(parseInt(week)<10){
						week='0'+week;
					}
					$(this).parent().prev().find(".dropdown-toggle").empty().html(week);//渲染新的周数
				}
				var timeslot=$(this).parent().prev().find(".week-dropdown-menu .actived span").text();
				var sss=timeslot.substring(1,3)+"月"+timeslot.substring(4,6)+"日";
	    		var eee=timeslot.substring(7,9)+"月"+timeslot.substring(10,12)+"日";
				$(".calendar-dateRange").empty().html(sss+'~'+eee);//重新渲染新的周范围
				
			}
			myyear=$(this).parent().prev().prev().prev().find(".dropdown-toggle").html();//保存最新的所选年
		}
		//获取周范围
		var timeslot=$(".calendar-dateRange").html();
		var start=timeslot.substring(0,2)+"-"+timeslot.substring(3,5);var end=timeslot.substring(7,9)+"-"+timeslot.substring(10,12);
		//重新获取数据
		getWeekData(myyear+'-'+start,myyear+'-'+end); 
	})
	//将获取到的数据渲染到周下拉框中，同时将最后一周作为选中周
	function renderingWeekOption(year,number){
		var weekdetail=getWeekNumDetail(year);
		var weekL='';
		for(var z=0;z<weekdetail.length;z++){
	    	weekL+='<li data-index="'+weekdetail[z].week+'"><a javascript:void(0)>'+weekdetail[z].week+'周 <span class="dateSlot">('+weekdetail[z].startDate+'~'+weekdetail[z].endDate+')</span></a></li>';
	    }
	    $(".btn-group-weekcenter .week-dropdown-menu").empty().html(weekL);
    	if(number=="first"){
    		$(".btn-group-weekcenter .btn-group-week .dropdown-toggle").empty().html("01");
    		$(".weekwork-dateRange").html(weekdetail[0].startDate+'~'+weekdetail[0].endDate);
    		$(".btn-group-weekcenter .week-dropdown-menu li").eq(0).addClass("actived");
    	}else if(number=="last"){
    		var num=weekdetail[weekdetail.length-1].week;
    		$(".btn-group-weekcenter .btn-group-week .dropdown-toggle").empty().html(num);
    		$(".weekwork-dateRange").html(weekdetail[weekdetail.length-1].startDate+'~'+weekdetail[weekdetail.length-1].endDate);
    		$(".btn-group-weekcenter .week-dropdown-menu li").last().addClass("actived");
    	}
	}
	//月视图--“进入周计划录入”按钮的点击事件
	$("#myModal-viewweekworkplan").on("click",".enterWeekWorkBtn",function(){
		var time=getDate($(this).data("time"));
		$("#myModal-viewweekworkplan").modal('hide');
		$('.modal-backdrop').remove();
		window.location.href = window.location.href+'?fromCalendar=1'
		$(".yk_rightcontent").load("/weekWorkPlan/list?workTime="+time);
		$(".yk_secondmenu").find("#workingCalendar").removeClass("yk_menuactive").parents().find("#myWeekworkManage").addClass("yk_menuactive");
	})
	//周视图---点击某一天跳入周计划录入
	$("#calendar").delegate(".cell-day","click",function(){
		if($(this).hasClass("current-month")){
			var time=getDate($(this).find(".day").data("date"));
    		window.location.href = window.location.href+'?fromCalendar=1'
    		$(".yk_rightcontent").load("/weekWorkPlan/list?workTime="+time,function(){
    			backTop('.yk_rightcontent');
    		});
    		$(".yk_secondmenu").find("#workingCalendar").removeClass("yk_menuactive").parents().find("#myWeekworkManage").addClass("yk_menuactive");
		}
	})
	/* 根据id获取当前数据 */
	function weekWorkView(viewId,start) {
		$.ajax({
			url : "/weekWorkPlan/do_view",
			type : "get",
			data : {
				'id' : viewId
			},
			error : function(state) {
				new $.zui.Messager('请求发送失败', {
					type : 'danger',
					time : 1500
				}).show();
			},
			success : function(data) {
				var obj = data.data;
				if (data.success) {
					$("#myModal-viewweekworkplan .view-state").text(obj.workState.name);
					$("#myModal-viewweekworkplan .view-title").text(obj.title);
					$("#myModal-viewweekworkplan .view-urgent").text(obj.level);
					$("#myModal-viewweekworkplan .view-starttime").text(obj.workTime);
					$("#myModal-viewweekworkplan .view-content").text(obj.workContent);
					$("#myModal-viewweekworkplan .leftDialog").next().hide();
					if (obj.workState.code == "workState_continue_next_week") {
						$("#myModal-viewweekworkplan .leftDialog").removeClass("col-xs-12").addClass("col-xs-6");
						$("#myModal-viewweekworkplan").find(".modal-dialog").css('width','11rem')
					} else {
						$("#myModal-viewweekworkplan").find(".modal-dialog").css('width','7rem');
						$("#myModal-viewweekworkplan .leftDialog").css({"borderRight" : "0px dashed #ccc"}).removeClass("col-xs-6").addClass("col-xs-12")
					}
					if (obj.workState.code == "workState_continue_next_week") {
						if (obj.nextWeekWorkPlan) {
							$("#myModal-viewweekworkplan").find(".alreadyDelete").remove();
							$("#myModal-viewweekworkplan .view-nextstate").text(obj.nextWeekWorkPlan.workState.name);
							$("#myModal-viewweekworkplan .view-nexttitle").text(obj.nextWeekWorkPlan.title);
							var level;
							if (obj.nextWeekWorkPlan.level == "urgent") {
								level = "紧急"
							} else {
								level = "普通"
							}
							$("#myModal-viewweekworkplan .view-nexturgent").text(level);
							$("#myModal-viewweekworkplan .view-nextstarttime").text(obj.nextWeekWorkPlan.workTime);
							$("#myModal-viewweekworkplan .view-nextcontent").text(obj.nextWeekWorkPlan.workContent);
							$("#myModal-viewweekworkplan").find(".modal-dialog").css({"width" : "11rem"})
							$("#myModal-viewweekworkplan .leftDialog").removeClass("col-xs-12").addClass("col-xs-6").css({"borderRight" : "1px dashed #54c3fe"}).next().show()
						} else {
							$("#myModal-viewweekworkplan .leftDialog").removeClass("col-xs-12").addClass("col-xs-6").css({"borderRight" : "1px dashed #54c3fe"}).next().before('<div class="col-xs-6 alreadyDelete" style="text-align:center"><div class="plan-label">关联计划已被删除<div></div>');
						}
					}
					$("#myModal-viewweekworkplan .enterWeekWorkBtn").data("time",start);
					$("#myModal-viewweekworkplan").modal('show');
				}
			}
		});
	}
}); 