$(function(){
	var start=getFirstDay();//获取本周一的日期
	var end=getLastDay();//获取本周日的日期
	var nextfirst=getNextWeekFirstDay();//获取下周一的日期
	var nextlast=getNextWeekLastDay();//获取本周日的日期
	function addweekInit(){
		$("#inputdateselect1").val(doGetNowDate());//将今天日期作为下拉框选项的初始值
		dateInit("#myModal-addweekwork .leftformdate",start,"2116-12-31");//设置左侧默认时间范围
		dateInit("#myModal-addweekwork .rightformdate",nextfirst,nextlast);//设置右侧默认时间范围
	}
	addweekInit();
	//新增按钮工作计划状态点击事件
	$("#myModal-addweekwork .radio-inline").click(function(){
		var radioclass=$(this).attr("class");
		var inputclass=$(this).find("input").attr("class");
		if((radioclass=="workState_working")||(inputclass=="workState_working")){//点击的是进行中的状态
			dateInit("#myModal-addweekwork .leftformdate",start,"2116-12-31");
		}else {//点击的是非进行中的状态
			//如果点击的是下周继续，右侧的时间选择也需要初始化
			if((radioclass=="workState_continue_next_week")||(inputclass=="workState_continue_next_week")){
				dateInit(".rightformdate",nextfirst,nextlast);
			}
			//非进行中的状态左侧时间需要初始化
			var compare=compareDate($("#myModal-addweekwork .leftformdate").val(),end);    
			if(compare){
				$("#myModal-addweekwork .leftformdate").val(doGetNowDate());
			}
			dateInit("#myModal-addweekwork .leftformdate",start,end);
		}
		//点击下周继续
		if((radioclass=="workState_continue_next_week")||(inputclass=="workState_continue_next_week")){
			$("#addWorkForm .row>.col-xs-12").removeClass("col-xs-12").addClass("col-xs-6").css({"borderRight":"1px dashed #5AC4FC"}).next().css({"display":"block"}).animate({"right":"-7rem"});
			//复制初始值
			console.log($("#addWorkForm .nextweek_sign").val());
			if(!$("#addWorkForm .nextweek_sign").val()){
				$("#inputtitle2").val($("#inputtitle1").val());
				var sel=$("#inputAddress2").find("option:selected").val();
				$("#inputAddress1").find("option[value='"+sel+"']").attr("selected",true);
				$("#inputdateselect2").val(getNextWeekFirstDay());
				$("#inputcontent2").val($("#inputcontent1").val());
				$("#addWorkForm .nextweek_sign").val("alreadyClicked");
			}
			$("#myModal-addweekwork").find(".modal-dialog").animate({"width":"12rem"});
		}else {
			$("#addWorkForm .row .leftDialog").removeClass("col-xs-6").addClass("col-xs-12").css({"borderRight":"0px dashed #ccc"}).next().css({"display":"none"});
			$("#myModal-addweekwork").find(".modal-dialog").animate({"width":"7rem"});
			$("#addWorkForm").find(".notEmptyLabel").remove().end().find(".has-error").removeClass("has-error");
		}
	})
	//新增表单初始化
	$("#myModal-addweekwork").on("click",".close",function(){
		$("#addWorkForm").resetForm();
		addweekInit();
	})
	$("#myModal-addweekwork").on("click",".closeAddWeekWorkBtn",function(){
		$("#addWorkForm").resetForm();
		addweekInit();
	})
	//修改表单初始化
	$("#myModal-modifyweekwork").on("click",".close",function(){
		$("#updateWorkForm").resetForm();
		$("#updateWorkForm .row .leftDialog").next().data("flag",'');
	})
	$("#myModal-modifyweekwork").on("click",".closeUpdateWeekWorkBtn",function(){
		$("#updateWorkForm").resetForm();
		$("#updateWorkForm .row .leftDialog").next().data("flag",'');
	})
	//修改工作计划状态点击事件
	$("#myModal-modifyweekwork").delegate(".updateAllState .radio-inline","click",function(e){
		var radioclass=$(this).attr("class");
		var inputclass=$(this).find("input").attr("class");
		if((radioclass=="workState_working")||(inputclass=="workState_working")){//点击的是进行中的状态
			var mydatefirst='';var mydateend='';
			if($("#updateInputdateSelect").data("parent-data")){//下周继续的数据
				if($("#updateInputdateSelect").data("weekth-data")){//本周
					mydatefirst=start;mydateend=end;
				}else {
					mydatefirst=nextfirst;mydateend=nextlast;
				}
			}else {   //正常新增中的进行中
				mydatefirst=start;mydateend="2116-12-31";
			}
			dateInit("#myModal-modifyweekwork .leftformdate",mydatefirst,mydateend);
		}else {//点击的是非进行中的状态
			var compare=compareDate($("#myModal-modifyweekwork .leftformdate").val(),end);    
			if(compare){
				$("#myModal-modifyweekwork .leftformdate").val(doGetNowDate());
			}
			dateInit("#myModal-modifyweekwork .leftformdate",start,end)
		}
		//点击下周继续
		if((radioclass=="workState_continue_next_week")||(inputclass=="workState_continue_next_week")){
			var hasdata=$("#updateWorkForm .row .leftDialog").next().data("flag");
			console.log(hasdata);
			if(!hasdata) {//右侧没有数据，需要复制左侧数据给右侧
				$("#next_updateInputTitle").val($("#updateInputTitle").val());
				$("#updateInputAddress option").each(function(index,item){
					if($(this).is(":checked")){
						$("#updateWorkForm .row .leftDialog").next().find("#next_updateInputAddress option").eq(index).attr("selected",true);
					}
				})
				$("#next_updateInputdateSelect").val(getNextWeekFirstDay());
				$("#next_updateInputContent").val($("#updateInputContent").val());
				$("#inputtitle2").val($("#inputtitle1").val());
				$("#myModal-modifyweekwork").find(".modal-dialog").animate({"width":"12rem"});
				$("#updateWorkForm .row .leftDialog").next().data("flag",true);
			}
			$("#updateWorkForm .row .leftDialog").removeClass("col-xs-12").addClass("col-xs-6").css({"borderRight":"1px dashed #5AC4FC"}).next().css({"display":"block"}).animate({"marginLeft":"0px"});
			$("#myModal-modifyweekwork").find(".modal-dialog").animate({"width":"12rem"});
		}else {
			$("#updateWorkForm .row .leftDialog").removeClass("col-xs-6").addClass("col-xs-12").css({"borderRight":"0px dashed #5AC4FC"}).next().css({"display":"none"});
			$("#myModal-modifyweekwork").find(".modal-dialog").animate({"width":"7rem"});
		}
		e.stopPropagation();
	})
	//“新增计划”按钮点击显示弹框
	$(".add-week-work").click(function(){
		//初始化表单
		$("#addWorkForm").resetForm();
		$("#addWorkForm").find(".notEmptyLabel").remove().end().find(".has-error").removeClass("has-error");
		$("#inputdateselect1").val(doGetNowDate());
		$("#addWorkForm .row .leftDialog").removeClass("col-xs-6").addClass("col-xs-12").css({"borderRight":"0px dashed #ccc"}).next().css({"display":"none"});
		$("#myModal-addweekwork").find(".modal-dialog").css({"width":"7rem"});
		$("#myModal-addweekwork").modal('show');
	});
	//表单的鼠标按下事件
	$("#myModal-addweekwork").delegate(".form-control","mousedown",function(){
		$(this).parent().find(".notEmptyLabel").remove();
		$(this).parent().parent().removeClass("has-error");
	})
	$("#myModal-modifyweekwork").delegate(".form-control","mousedown",function(){
		$(this).parent().find(".notEmptyLabel").remove();
		$(this).parent().parent().removeClass("has-error");
	})
	function dateInit(obj,start,end){
		$(obj).datetimepicker('remove');
		$(obj).datetimepicker({//左侧日期选择的初始化
		    language:  "zh-CN",
		    weekStart: 1,
		    todayBtn:  0,
		    autoclose: 1,
		    todayHighlight: 1,
		    startView: 2,
		    minView: 2,
		    forceParse: 0,
		    format: "yyyy-mm-dd",
		    startDate:start,
		    endDate:end
		});
	}
})