var windowId,sysUrl = top.sysUrl,toEdit;
var sbbh= $.getUrlParam("sbbh");
var fromSbxx= $.getUrlParam("fromSbxx");
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	/*
	var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate()-7);
	$("#LIKEsj1").datetimebox("setValue",datenow.format("yyyy-MM-dd")+" 00:00:00");
	$("#LIKEsj2").datetimebox("setValue",datenow.format("yyyy-MM-dd")+" 23:59:59");
	*/
	
	if((sbbh && sbbh.length)||fromSbxx=="1"){
		$('#LIKEsbbh').textbox("setValue",sbbh);
		$('#LIKEsbbh').textbox("disable",true);
		$('#sbtd_s_div').css('height',"350px;");
		var h = $('body').layout('panel','center').panel("options").height;
		$('body').layout('panel','north').panel("resize", {height: 38});
		$('body').layout('panel','center').panel("resize", {height: (h + 38)})
		$("body").layout("resize");
	}else{
		$("#tdlx").removeClass("hidden");
		$("#tdzt").removeClass("hidden");
		$("#ipdz").removeClass("hidden");
	}

	$.ajax({
		 type:'post',
		 dataType : 'json',
		 url:'ptxzsAction!query_Ptxzs.do',
		 success:function(data){
			ptxzTree = $('#LIKExzbh').combotree({
				 checkbox:false,
			     border:false,
			     parentField:"sjxzbh",
			   	 textFiled:"xzmc",
			   	 idFiled:"xzbh",
			   	 data:data,
			   	 value: "请选择",
			   	 panelWidth:148,
			     onLoadSuccess:function(node,data){
			    	 if(data&&data.length>0){
							$('#xzbh').combotree("setValue",data[0].xzbh).combotree("setText", data[0].xzmc);
			    	 }
			     },
				 onClick: function(node){
					$('#xzbh').combotree("setValue",node.xzbh).combotree("setText", node.xzmc);
				 }
			}); 
		 }
	 });

	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	if(sbbh && sbbh.length){
		sParam.sbbh = $('#LIKEsbbh').textbox("getValue");
	}else{
		sParam.LIKEsbbh = $('#LIKEsbbh').textbox("getValue");
	}
	sParam.LIKEtdmc = $('#LIKEtdmc').textbox("getValue");
	sParam.order = " order by tdbh ,cjrq desc  ";
	return sParam;
}
//加载列表
function initGrid(){
	var toolbar = [{
        text:'查询',
        iconCls:'icon-search',
        handler:doSearch
    },{
    	id:"addSbtdspsr",
        text:'增加',
        iconCls:'icon-add',
        handler:function(){doAdd();}
    },{
        text:'修改',
        iconCls:'icon-edit',
        handler:doEdit
    },{
        text:'删除',
        iconCls:'icon-delete',
        handler:doDelete
    }];
	
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'sbtdspsrAction!query_Sbtdspsr_tdxx_ext.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'xh',title:'xh',width:120,hidden:true},
			{field:'sbbh',title:'设备编号',hidden:false,width:155},
			{field:'tdmc',title:'通道名称',hidden:false,width:170,
				formatter:function(value,record,num){
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
			},
			{field:'tdbh',title:'通道编号',hidden:false,width:60},
			{field:'tdlx',title:'通道类型',hidden:true,width:80},
			{field:'tdtypename',title:'通道类型',hidden:false,width:80},
			{field:'tdztmc',title:'通道状态',hidden:false,width:60},
			{field:'sfzxmc',title:'在线状态',hidden:false,width:60},
			{field:'sfzlx',title:'录像状态',hidden:false,width:60,formatter:function(value,record,num){
				var sfzlxmc = "未录像";
				if(value=="1"){
					sfzlxmc = "录像中";
				}
				return sfzlxmc;
			}},
			{field:'cjmc',title:'设备厂家',hidden:false,width:80},
			{field:'sbip',title:'IP地址',hidden:false,width:150},
			{field:'lxfsname',title:'录像方式',hidden:false,width:100},
			{field:'zxcczmc',title:'中心存储组',hidden:false,width:110},
			{field:'xztreename',title:'行政名称',hidden:false,width:80},
			{field:'wbbh',title:'外部编号',hidden:false,width:80},
			{field:'jd',title:'经度',hidden:false,width:60},
			{field:'wd',title:'纬度',hidden:false,width:60},
			{field:'squrllj',title:'上墙地址',hidden:false,width:350},
			{field:'lxblts',title:'录像保留天数',hidden:true,width:100},
			{field:'lxblts',title:'录像保留天数',hidden:true,width:100},
			{field:'zxcczbh',title:'中心存储组编号',hidden:true,width:110},
			{field:'tdazwz',title:'安装位置',hidden:true,width:80},
			{field:'jlbh',title:'级联编号',hidden:true,width:80} 
			
		]],
		onLoadSuccess:function(){
		    if(!(sbbh && sbbh.length)||fromSbxx=="1"){
		    	 $('#addSbtdspsr').hide();
		    }
		}
	});
}


//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(selRows===null){
		 $.showMsg('请先选择记录! ');
		 return;
	}
	top.$.dlg.confirm('确认删除所选通道?',function(){
		var delMap = {};
		delMap.sbbh = selRows.sbbh;
		delMap.tdbh = selRows.tdbh;
		console.log(delMap);
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'sbtdspsrAction!delete_Sbtdspsr.do',
			data:delMap,
			success:function(flag){
				if(flag == 'success'){
					$.showMsg('删除成功!');
					doSearch();
				}
			}
		});
	});
	/*
	top.$.dlg.confirm('确认删除所选通道?',function(){
		for (var i = 0;i < selRows.length; i++) {
			$.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'sbtdspsrAction!delete_Sbtdspsr.do',
				 data:{xh	:	selRows[i].xh},
				 success:function(flag){
					 j++;
					 if( selRows.length == j ){
						 $.showMsg('删除成功!');
						 doSearch();
					 }
				 }
			 });
		}
	});
	*/
}

//导出
function doExport(){
	countTotal();
}
//统计数据总量
function countTotal() {
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'sbtdspsrAction!count_Sbtdspsr.do',
		success : function(value) {
			exportTj(value);
		}
	});
}

//导出传参条件
function exportTj(totalSize) {
	var model = getQueryParams();
	var tableparam = "";
	var start = 0;
	var limit = 5000;
	if (totalSize > 5000) {
		tableparam += " limit " + start + "," + limit;
		model.tableparam = tableparam;
		var qd = confirm("记录过多支持前5000条,确定要将记录导出Excel为表格吗？");
		if (qd) {
			downloadExcel(model);
		}
	} else {
		model.tableparam = tableparam;
			downloadExcel(model);
	}
}

//调用后台
function downloadExcel(model) {
	var downLoadExcelPar = "order="+model.order;
	if(model.tableparam!="" && model.tableparam!=undefined){
		downLoadExcelPar = downLoadExcelPar+"&tableparam="+model.tableparam;
	}else if(model.sbbh!="" && model.sbbh!=undefined){
		downLoadExcelPar = downLoadExcelPar+"&sbbh="+model.sbbh;
	}
	console.log(downLoadExcelPar);
	top.$.dlg.confirm('确认导出?',function(){ 
        document.excelForm.action = 'sbtdspsrAction!exoprtExcel.do?'+downLoadExcelPar;
	    document.excelForm.submit(); 
	});
	
}

//查询
function doSearch(){
	$('#tableGrid').datagrid('load',getQueryParams());
}

//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		
		 id		:	$.uniqueId(),
		 title	: 	'增加通道',
		 width	:  	650,
		 height	: 	640,
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/yw/nhyw/nhywdzjkgl/nhywsbcsglxx_show.jsp?sbbh="+sbbh
	 });
}

//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'编辑通道',
		 width	:  	650,
		 data   :   selRows,
		 height	: 	650,
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/yw/nhyw/nhywdzjkgl/nhywsbcsglxx_show.jsp?sbbh="+sbbh
	 });
}
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	model.xzbh  =   jq('#xzbh').combotree('getValue');
	if(!model.tdmc){
		$.showMsg('通道名称不能为空! ');
		return;
	} 
	if(!model.tdzt  || model.tdzt=='请选择' ){
		$.showMsg('通道状态不能为空! ');
		return;
	} 
	if(!model.tdlx  || model.tdlx=='请选择' ){
		$.showMsg('通道类型不能为空! ');
		return;
	} 
	if(toEdit){//修改
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'sbtdspsrAction!update_Sbtdspsr.do',
			 data:model,
			 success:function(value){
				 if(value=="success"){
					 $.showMsg("操作成功！");
					 top.$("#"+windowId).dialog('close');
					 doSearch();
				 }
			 }
		 });
	}else{
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'sbtdspsrAction!insert_Sbtdspsr.do',
			 data:model,
			 success:function(value){
				 if(value=="success"){
					 $.showMsg("操作成功！");
					 top.$("#"+windowId).dialog('close');
					 doSearch();
				 }
			 }
		 });
	}
}

//打开修改
function doEdit(){
	if($("#tableGrid").datagrid('getSelections').length > 1){
		$.showMsg('请仅选择一行进行修改! ');
		 return;
	}
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
function doConfig(){
	if($("#tableGrid").datagrid('getSelections').length > 1){
		$.showMsg('请仅选择一行进行修改! ');
		 return;
	}
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		var selRows = $('#tableGrid').datagrid('getRows')[selIndex];
		if(selRows.lxfs=="2"){
			doTimeConfig(selRows.sbbh,selRows.tdbh,selRows.tdlx);
		}else{
			$.showMsg('录像方式只能是按计划录像! ');
			return;
		}
	}		
}
//告警时间级别设置弹窗
function doTimeConfig(sbbh,tdbh,tdlx){
	var mm = {};
	mm.sbbh = sbbh;
	mm.tdbh = tdbh;
	mm.tdlx = tdlx;
	mm.page = 1;
	mm.rows = 1000;
	$.ajax({
		 type:'post',
		 dataType : 'json',
		 async:false,
		 url:'ptlxjhbAction!query_Ptlxjhb.do',
		 data:mm,
		 success:function(data){
			if(data){
				windowId = top.$.createWin({
					title	: 	'录像计划设置',
					width	:  	800,
					data    :   data,
					height	: 	450,
					buttons:  [{
						text:$.fn.textbox.defaults.ok,
						handler:saveTimeConfig
					}],
					url	:	sysUrl+"/im/global/setRecordPlan.jsp"
				 });
			}
		 }
	 });
}
//时间级别保存
function saveTimeConfig(){
	var selRows = $('#tableGrid').datagrid("getSelected");
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model = win.getValueGroup();
	console.log(model)
	//$.showMsg('功能暂未完成');
	var mm = {};
	mm.sbbh = selRows.sbbh;
	mm.tdbh = selRows.tdbh;
	mm.tdlx = selRows.tdlx;
	$.ajax({
		 type:'post',
		 dataType : 'json',
		 async:false,
		 url:'ptlxjhbAction!delete_Ptlxjhb.do',
		 data:mm,
		 success:function(flag){
			 if(flag){
				 for(var i=0;i<model.length;i++){
					var tmpdata = model[i];
					tmpdata.sbbh = selRows.sbbh;
					tmpdata.tdbh = selRows.tdbh;
					tmpdata.tdlx = selRows.tdlx;
					$.ajax({
						 type:'post',
						 dataType : 'json',
						 async:false,
						 url:'ptlxjhbAction!insert_Ptlxjhb.do',
						 data:tmpdata,
						 success:function(flag){
							
						 }
					 });
				}
			 }			
		 }
	 });	
	top.$("#"+windowId).dialog('close');
	$.showMsg('录像计划设置成功!');
	doSearch();
}