var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var taskid = $.getUrlParam("taskid");
$(function(){
	initDefault();	
	initXzNameTree();
});
//默认
function initDefault(){
	/*
	$('#xx5').combobox({  
		url:'xtclAction!query_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);
	*/
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	/*
	sParam.LIKEid = $('#LIKEid').textbox("getValue");
	sParam.LIKEtaskid = $('#LIKEtaskid').textbox("getValue");
	sParam.LIKEchannelsetid = $('#LIKEchannelsetid').textbox("getValue");
	sParam.LIKEchannellistid = $('#LIKEchannellistid').textbox("getValue");
	sParam.LIKEvideonum = $('#LIKEvideonum').textbox("getValue");
	sParam.LIKEchannelnum = $('#LIKEchannelnum').textbox("getValue");
	sParam.LIKEchannelname = $('#LIKEchannelname').textbox("getValue");
	sParam.LIKEchannelsort = $('#LIKEchannelsort').textbox("getValue");
	sParam.LIKEvideomodel = $('#LIKEvideomodel').textbox("getValue");
	sParam.LIKErecordxdays = $('#LIKErecordxdays').textbox("getValue");
	sParam.LIKErecordmodel = $('#LIKErecordmodel').textbox("getValue");
	sParam.LIKEdetectionswitch = $('#LIKEdetectionswitch').textbox("getValue");
	sParam.LIKEdetectionstarttime = $('#LIKEdetectionstarttime').textbox("getValue");
	sParam.LIKEcreateaccount = $('#LIKEcreateaccount').textbox("getValue");
	sParam.LIKEcreatetime = $('#LIKEcreatetime').textbox("getValue");
	sParam.LIKEmodifyaccount = $('#LIKEmodifyaccount').textbox("getValue");
	sParam.LIKEmodifytime = $('#LIKEmodifytime').textbox("getValue");
	sParam.LIKEremarks = $('#LIKEremarks').textbox("getValue");
	sParam.LIKEextend1 = $('#LIKEextend1').textbox("getValue");
	sParam.LIKEextend2 = $('#LIKEextend2').textbox("getValue");
	sParam.LIKEextend3 = $('#LIKEextend3').textbox("getValue");
	sParam.LIKEextend4 = $('#LIKEextend4').textbox("getValue");

	*/
	/*
	var xx1 = $("#xx1").textbox("getValue");//文本
	var xx2 = $("#xx2").combobox("getValue");//下拉
	var xx3 = $("#xx3").datetimebox("getValue");//日期
	var xx4 = $("#xx4").numberbox("getValue");//数字
	var LIKEtime1 = $('#LIKEtime1').datetimebox("getValue");	
	var LIKEtime2 = $('#LIKEtime1').datetimebox("getValue");	
	var tableparam = "";
	if(!(LIKEtime1==null||LIKEtime1==""||LIKEtime1==undefined)){
		tableparam += " AND time > '" +LIKEtime1 + "'";
	}
	if(!(LIKEtime2==null||LIKEtime2==""||LIKEtime2==undefined)){
		tableparam += " AND time < '" +LIKEtime2 + "'";
	}
	sParam.tableparam = tableparam;
	sParam.order = "order by time desc ";
	*/
	sParam.LIKEtaskid = taskid
	sParam.LIKEchannelname = $('#LIKEchannelname').textbox("getValue");
	sParam.xzbh = $('#xzmc').textbox("getValue");
	sParam.order = "order by createTime desc ";
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'启用',
        iconCls	:	'icon-start',
        handler	:	doStart
    },{
        text	:	'停止',
        iconCls	:	'icon-end',
        handler	:	doStop
    },{
        text	:	'增加集合通道',
        iconCls	:	'icon-add',
        handler	:	doChannelset
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideotaskbysetlistAction!listYwvideotaskbysetlistByPage_task.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'id',title:'id',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
			{field:'taskid',title:'任务编号',hidden:true,width:120},
			{field:'channelsetid',title:'设备编号',hidden:true,width:120},
			{field:'channellistid',title:'',hidden:true,width:120},
			{field:'channelsetname',title:'集合名称',hidden:false,width:120},
			{field:'channelname',title:'通道名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:120},
			{field:'channelnum',title:'通道编号',hidden:false,width:120},
			{field:'channelsort',title:'通道排序',hidden:true,width:120},
			{field:'videomodel',title:'抽取视频模式',hidden:true,width:120},
			{field:'recordxdays',title:'抽取录像x天前',hidden:true,width:120},
			{field:'recordmodel',title:'抽取录像模式',hidden:true,width:120},
			{field:'detectionswitchname',title:'检测开关',hidden:false,width:120},
			{field:'detectionstarttime',title:'检测开始时间',hidden:true,width:120},
			{field:'createaccount',title:'创建人员',hidden:true,width:120},
			{field:'createtime',title:'创建日期',hidden:true,width:120},
			{field:'modifyaccount',title:'修改人员',hidden:true,width:120},
			{field:'modifytime',title:'修改日期',hidden:true,width:120},
			{field:'remarks',title:'备注',hidden:true,width:120},
			{field:'extend1',title:'扩展1',hidden:true,width:120},
			{field:'extend2',title:'扩展2',hidden:true,width:120},
			{field:'extend3',title:'扩展3',hidden:true,width:120},
			{field:'extend4',title:'扩展4',hidden:true,width:120}
		]]
	});
}
function doStart(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].id);
		}
		top.$.dlg.confirm('你确认要开启吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideotaskbysetlistAction!batch_update_Ywvideotaskbysetlist.do',
				 data:{detectionswitch:1,keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(flag=="success"){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
function doStop(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].id);
		}
		top.$.dlg.confirm('你确认要关闭吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideotaskbysetlistAction!batch_update_Ywvideotaskbysetlist.do',
				 data:{detectionswitch:0,keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(flag=="success"){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
function doChannelset(){
	if(taskid==null || taskid==""){
		$.showMsg('请先双击选择规则! ');
		return;
	}
	windowId = top.$.createWin({
		title	: 	'增加通道集合',
		width	:  	800,
		height	: 	500,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
//			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/videomanage/videotask/ywvideotaskbysetlist/addChannelSet.jsp?taskid="+taskid
	 });
}

//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskbysetlistAction!delete_Ywvideotaskbysetlist.do',
			data:{
				id	:	selRows.id

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].id);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideotaskbysetlistAction!batch_delete_Ywvideotaskbysetlist.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(!$.isNull(flag)){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywvideotaskbysetlist/ywvideotaskbysetlist_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywvideotaskbysetlist/ywvideotaskbysetlist_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(toEdit){//修改
		model.XGTJid = jq('#id').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskbysetlistAction!update_Ywvideotaskbysetlist.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskbysetlistAction!insert_Ywvideotaskbysetlist.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var downFile = baseUrl+"/down/down.xls";
	window.location.href= downFile;
}
//导入
function doImport(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'导入',
		 width	:  	400,
		 height	: 	130,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 url	:	sysUrl+"/im/xx/ywvideotaskbysetlist_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywvideotaskbysetlistAction!count_Ywvideotaskbysetlist.do',
		success : function(value) {
			if(!$.isNull(value)){
				countExport(value);
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
// 导出传参条件
function countExport(totalSize) {
	var limit = 5000;
	if (totalSize > 5000) {
		top.$.dlg.confirm('记录过多支持前5000条,确定要将记录导出Excel为表格吗?',function(){ 
			execExport();
		});		
	} else {
		top.$.dlg.confirm('确认导出?',function(){ 
			execExport();
		})
	}
}
// 调用后台
function execExport() {
	var model = getQueryParams();
	var downParam = "";
	document.excelForm.action = 'ywvideotaskbysetlistAction!exp_Ywvideotaskbysetlist.do?'+downParam;
	document.excelForm.submit(); 
}
function initXzNameTree(){
	$.ajax({
		 type:'post',
		 async:false,
		 dataType : 'json',
		 //url:'ptxzsAction!query_Ptxzs.do'
		 url:'menuConfigAction!query_Ptxzs.do'
	 }).done(function(data){
		if(data!=null && data.length>0) {
			$.each(data, function (i, o) {
				o.state = 'closed';
				o.treeId = o.xzbh;
				o.name = o.xzmc;
				o.treeType = "xz";
				o.id = o.xzbh;
				o.pid = o.sjxzbh;
				o.start = 0; //iconCls
			})
		}
				 $('#xzmc').combotree({
				        checkbox:false,
				    	border:false,
				    	parentField:"pid",  
				    	textFiled:"name",  
				    	idFiled:"id",
				    	panelWidth:148,
				    	data:data,
						onLoadSuccess:function(node,data){
							if(data){
								//$('#LIKExzName').combotree("setValue",data[0].xzId).combotree("setText", data[0].xzName);
//								$('#LIKExzId').val(data[0].xzId);
//								$('#LIKExzName').val(data[0].xzName);
								initDefault();
							}
						},
						onClick: function(node){
//							$('#xzbh').val(node.xzbh);
//							$('#xzmc').val(node.xzmc);
						}
				});
	 });
}