var winId	= $.getUrlParam("winId");
var records = top.$("#"+winId).panel("options").data;
$(function(){
	//加载下拉选项
	//setTimeout(loadValue,500);
	loadValue();
	$("#taskmodel").combobox({
		onChange:function(newValue,oldValue){
			changeDisplayByModel(newValue);
    	}
	}); 
	// 获取审核人
	$.ajax({
		url : "ywvideotaskAction!getApproveUser.do",
		type : "POST",
		dataType : "JSON",
		success : function(res){
			if(res && res.length>0){
				var peopleArr = [{"id":"","text":"请选择审核人","selected":true}];
				for(var i in res){
					peopleArr.push( {"id":res[i].yhbh+"-"+res[i].sjhm,"text":res[i].yhzh});
				}
				$("#extend2").combobox({disabled:false});
				$("#extend2").combobox("loadData",peopleArr);
			}else{
				$.showMsg("未关联到可审批单位！")
			}
		},
		error : function(res){ }
	});

	if(!records.flag){
		
		$("#taskmodel").combobox("setValue",records.taskmodel)
	}else{
		$("#taskmodel").combobox("setValue",Constants.everydayonce)
	}
	//插入规则
	parent.parent.$(".l-btn-text").on("click",function(){ 
		if($(this).text()=="关闭"){
			return;
		}
		var insertParam = {};
//		insertParam.catalogid = $("#catalogid").val();
//		insertParam.catalogname = $("#catalogname").val();
		insertParam.taskname = $("#taskname").val();
		if(insertParam.taskname==""){
			$.showMsg("任务名称不能为空！");
			return;
		}
		var extend2id = $("#extend2").combobox("getValue");
		if (extend2id == "") {
			$.showMsg("请选择审核人！");
			return;
		}
		insertParam.extend2 = $("#extend2").combobox("getText");
		insertParam.extend1 = "0";
		insertParam.tasktype = $("#tasktype").val();
		insertParam.tsaklevel = $("#tsaklevel").val();
		insertParam.detectionswitch = $("#detectionswitch").val();
//		insertParam.detectionstarttime = $("#detectionstarttime").val();
		insertParam.taskdescribe = $("#taskdescribe").val();
		insertParam.taskmodel = $("#taskmodel").val();
		if(insertParam.taskmodel==Constants.everydayonce){
			insertParam.daystarttime = $("#daystarttime").val();
		}else if(insertParam.taskmodel==Constants.everydaymanytimes){
			insertParam.daystarttime = $("#daystarttime").val();
			insertParam.daystarttimebyhour = $("#daystarttimebyhour").val();
		}else if(insertParam.taskmodel==Constants.customizingtime){
			if($("#mondayswitch").prop("checked",this.checked)){
				insertParam.mondayswitch = 1;
				insertParam.mondayruntime = $("#mondayruntime").val();
				insertParam.mondayrunbyhour = $("#mondayrunbyhour").val();
			}else{
				insertParam.mondayswitch = 0;
			}
			if($("#tuesdayswitch").prop("checked",this.checked)){
				insertParam.tuesdayswitch = 1;
				insertParam.tuesdayruntime = $("#tuesdayruntime").val();
				insertParam.tuesdayrunbyhour = $("#tuesdayrunbyhour").val();
			}else{
				insertParam.tuesdayswitch = 0;
			}
			if($("#wednesdayswitch").prop("checked",this.checked)){
				insertParam.wednesdayswitch = 1;
				insertParam.wednesdayuntime = $("#wednesdayuntime").val();
				insertParam.wednesdayunbyhour = $("#wednesdayunbyhour").val();
			}else{
				insertParam.wednesdayswitch = 0;
			}
			if($("#thursdayswitch").prop("checked",this.checked)){
				insertParam.thursdayswitch = 1;
				insertParam.thursdayruntime = $("#thursdayruntime").val();
				insertParam.thursdayrunbyhour = $("#thursdayrunbyhour").val();
			}else{
				insertParam.thursdayswitch = 0;
			}
			if($("#fridayswitch").prop("checked",this.checked)){
				insertParam.fridayswitch = 1;
				insertParam.fridayruntime = $("#fridayruntime").val();
				insertParam.fridayrunbyhour = $("#fridayrunbyhour").val();
			}else{
				insertParam.fridayswitch = 0;
			}
			if($("#saturdayswitch").prop("checked",this.checked)){
				insertParam.saturdayswitch = 1;
				insertParam.saturdatruntime = $("#saturdatruntime").val();
				insertParam.saturdatrunbyhour = $("#saturdatrunbyhour").val();
			}else{
				insertParam.saturdayswitch = 0;
			}
			if($("#sundayswitch").prop("checked",this.checked)){
				insertParam.sundayswitch = 1;
				insertParam.sundayruntime = $("#sundayruntime").val();
				insertParam.sundayrunbyhour = $("#sundayrunbyhour").val();
			}else{
				insertParam.sundayswitch = 0;
			}
		}
		var url = "";
		if(!records.flag){
			insertParam.XGTJtaskid = $("#taskid").val();
			url = "ywvideotaskAction!update_Ywvideotask.do";
			if(insertParam.detectionswitch!=(records.detectionswitch+"")){
				insertParam.detectionstarttime = getNowFormatDate();
			}
		}else{
			url = "ywvideotaskAction!insert_Ywvideotask.do"
		}
		$.ajax({
			type:'post',
			dataType : 'json',
			url:url,
			data:insertParam,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+winId).dialog('close');
//					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
		
	});
});
function loadValue(){
	//加载下拉框
	$.ajax({
		url:" xtclAction!query_Xtcl_task.do",
		type:"post",
		dataType:"JSON",
		async:false,
		data : {"csdl":"ywVideoTaskType,ywVideoTaskModel,ywVideoTaskLevel,ywTaskSwitch,ywTaskEnable"},
		success:function(res){
			if(res && res.length>0){
				var taskTypeArr = [];
				var taskModelArr = [];
				var taskLevelArr = [];
				var taskSwitchArr = [];
				var taskEnableArr = [];
				for(i in res){
					if(res[i].csdl=="ywVideoTaskType"){
						if(res[i].csdm=="1"){
							taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc,"selected":true})
						}else{
							taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
					}else if(res[i].csdl=="ywVideoTaskModel"){
					//	console.log("任务模式：",res);  暂时注释掉   自定义时间
						if(res[i].csdm !='3'){
							if(res[i].csdm=="1"){							
								taskModelArr.push({"id":res[i].csdm,"text":res[i].csmc,"selected":true})
							}else{
								taskModelArr.push({"id":res[i].csdm,"text":res[i].csmc})
							}
						}						
					}else if(res[i].csdl=="ywVideoTaskLevel"){
						if(res[i].csdm=="0"){
							taskLevelArr.push({"id":res[i].csdm,"text":res[i].csmc,"selected":true})
						}else{
							taskLevelArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
					}else if(res[i].csdl=="ywTaskEnable"){
						if(res[i].csdm=="0"){
							taskEnableArr.push({"id":res[i].csdm,"text":res[i].csmc,"selected":true})
						}else{
							taskEnableArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
					}else{
						if(res[i].csdm=="0"){
							taskSwitchArr.push({"id":res[i].csdm,"text":res[i].csmc,"selected":true})
						}else{
							taskSwitchArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
					}
				}
				$("#tasktype").combobox( "loadData", taskTypeArr);
				$("#tsaklevel").combobox("loadData", taskLevelArr);
				$("#taskmodel").combobox("loadData", taskModelArr);
				$("#detectionswitch").combobox({disabled:true});
				$("#detectionswitch").combobox("loadData",taskEnableArr);
			}
		}
	});
	if(!records.flag){
		changeDisplayByModel(records.taskmodel);
		$("#taskid").val(records.taskid);
		$("#taskname").textbox("setValue",records.taskname);
		$("#tasktype").combobox("setValue",records.tasktype+"");
		$("#tsaklevel").combobox("setValue",records.tsaklevel+"");
		$("#taskmodel").combobox("setValue",records.taskmodel+"");
		$("#taskdescribe").textbox("setValue",records.taskdescribe);
		$("#detectionswitch").combobox("setValue",records.detectionswitch+"");
		$("#daystarttime").timespinner("setValue",records.daystarttime );
		
		$("#daystarttimebyhour").numberbox("setValue",records.daystarttimebyhour);
		$("#channeltotals").numberbox("setValue",records.channeltotals);
		
		if(records.mondayswitch==1){
			$('#mondayswitch').attr("checked",true); 
		}else{
			$('#mondayswitch').attr("checked",false); 
			
		}
		$("#mondayruntime").timespinner("setValue",records.mondayruntime);
		$("#mondayrunbyhour").textbox("setValue",records.mondayrunbyhour);
		
		if(records.tuesdayswitch==1){
			$('#tuesdayswitch').attr("checked",true); 
		}else{
			$('#tuesdayswitch').attr("checked",false); 
			
		}
		$("#tuesdayruntime").timespinner("setValue",records.tuesdayruntime);
		$("#tuesdayrunbyhour").textbox("setValue",records.tuesdayrunbyhour);
		
		if(records.wednesdayswitch==1){
			$('#wednesdayswitch').attr("checked",true); 
		}else{
			$('#wednesdayswitch').attr("checked",false); 
			
		}
		$("#wednesdayuntime").timespinner("setValue",records.wednesdayuntime);
		$("#wednesdayunbyhour").textbox("setValue",records.wednesdayunbyhour);
		
		if(records.thursdayswitch==1){
			$('#thursdayswitch').attr("checked",true); 
		}else{
			$('#thursdayswitch').attr("checked",false); 
			
		}
		$("#thursdayruntime").timespinner("setValue",records.thursdayruntime);
		$("#thursdayrunbyhour").textbox("setValue",records.thursdayrunbyhour);
		
		if(records.fridayswitch==1){
			$('#fridayswitch').attr("checked",true); 
		}else{
			$('#fridayswitch').attr("checked",false); 
			
		}
		$("#fridayruntime").timespinner("setValue",records.fridayruntime);
		$("#fridayrunbyhour").textbox("setValue",records.fridayrunbyhour);
		
		if(records.saturdatswitch==1){
			$('#saturdatswitch').attr("checked",true); 
		}else{
			$('#saturdatswitch').attr("checked",false); 
			
		}
		$("#saturdatruntime").timespinner("setValue",records.saturdatruntime);
		$("#saturdatrunbyhour").textbox("setValue",records.saturdatrunbyhour);
		
		if(records.sundayswitch==1){
			$('#sundayswitch').attr("checked",true); 
		}else{
			$('#sundayswitch').attr("checked",false); 
			
		}
		$("#sundayruntime").timespinner("setValue",records.sundayruntime);
		$("#sundayrunbyhour").textbox("setValue",records.sundayrunbyhour);
	}else{
		
		$("#daystarttime").timespinner("setValue",'08:00' );
		$("#daystarttimebyhour").numberbox("setValue",4);
		$("#channeltotals").numberbox("setValue",records.channeltotals);
		$("#mondayruntime").timespinner("setValue",'08:00');
		$("#mondayrunbyhour").textbox("setValue",4);
		$("#tuesdayruntime").timespinner("setValue",'08:00');
		$("#tuesdayrunbyhour").textbox("setValue",4);
		$("#wednesdayuntime").timespinner("setValue",'08:00');
		$("#wednesdayunbyhour").textbox("setValue",4);
		$("#thursdayruntime").timespinner("setValue",'08:00');
		$("#thursdayrunbyhour").textbox("setValue",4);
		$("#fridayruntime").timespinner("setValue",'08:00');
		$("#fridayrunbyhour").textbox("setValue",4);
		$("#saturdatruntime").timespinner("setValue",'08:00');
		$("#saturdatrunbyhour").textbox("setValue",4);
		$("#sundayruntime").timespinner("setValue",'08:00');
		$("#sundayrunbyhour").textbox("setValue",4);
	}
}
function changeDisplayByModel(newValue){
	if(newValue==Constants.everydayonce){
		$("#startTime").css("display","block");
		$("#intervalTime").css("display","none");
		$("#monday").css("display","none");
		$("#tuesday").css("display","none");
		$("#wednesday").css("display","none");
		$("#thursday").css("display","none");
		$("#friday").css("display","none");
		$("#saturday").css("display","none");
		$("#sunday").css("display","none");
	}else if(newValue==Constants.everydaymanytimes){
		$("#startTime").css("display","block");
		$("#intervalTime").css("display","block");
		$("#monday").css("display","none");
		$("#tuesday").css("display","none");
		$("#wednesday").css("display","none");
		$("#thursday").css("display","none");
		$("#friday").css("display","none");
		$("#saturday").css("display","none");
		$("#sunday").css("display","none");
		
	}else if(newValue==Constants.customizingtime){
		$("#startTime").css("display","none");
		$("#intervalTime").css("display","none");
		$("#monday").css("display","block");
		$("#tuesday").css("display","block");
		$("#wednesday").css("display","block");
		$("#thursday").css("display","block");
		$("#friday").css("display","block");
		$("#saturday").css("display","block");
		$("#sunday").css("display","block");
		
	}
}
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}
/*
var xx1 = $("#xx1").textbox("getValue");//文本
var xx2 = $("#xx2").combobox("getValue");//下拉
var xx3 = $("#xx3").datetimebox("getValue");//日期
var xx4 = $("#xx4").numberbox("getValue");//数字
$('#xx5').combobox({  
	url:'xtclAction!select_Xtcl.do?csdl=xx5&page=1&rows=1000', 
	textField:"csmc",  
	valueField:"csdm",
	panelWidth:148,
	loadFilter:function(data){
		//data.unshift({"csmc":"选择","csdm":""});
		return data;
	}
});
$("#xx6").textbox("setValue",records.xx6);
$("#xx7").combobox("setValue",records.xx7);
$("#xx8").combobox("setValue",records.xx8);
$("#xx9").datetimebox("setValue",$.getNowTime());
$("#xx10").datetimebox("setValue",$.getTodayStart('00:00:00',-3));//往前推3天  +'00:00:00'
$("#xx11").datetimebox("setValue",$.getTodayEnd());//当天 +'23:59:59'	
$("#xx12").datetimebox("setValue",$.getDateNumToStr(records.xx12));
$("#xx13").numberbox("setValue",records.xx13);
*/