var proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var winId	= $.getUrlParam("winId");
var xzbh = $.getUrlParam("xzbh");
//var deviceChoose=$.getUrlParam("deviceChoose");
var channelsetid = $.getUrlParam("channelsetid");
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	if(xzbh!=""){
		sParam.xzbh = xzbh;
	}
	sParam.LIKEtdmc = $('#LIKEtdmc').textbox("getValue");
	sParam.LIKEsbbh = $('#LIKEsbbh').textbox("getValue");
	sParam.channelsetid = channelsetid;
	sParam.order = "order by sbbh,tdbh desc ";
	
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
//		url : 'menuConfigAction!getChannelListFromSystem.do',
		url : 'sbtdspsrAction!listSbtdspsrToChannelSet.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'xh',title:'xh',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
 			{field:'tdmc',title:'通道名称',hidden:false,width:120},
			{field:'sbbh',title:'设备编号',hidden:false,width:120},
			{field:'tdbh',title:'通道编号',hidden:false,width:120},
			{field:'xzmc',title:'行政目录',hidden:false,width:120},
			{field:'tdlx',title:'通道类型',hidden:true,width:120},
			{field:'wbbh',title:'外部编号',hidden:true,width:120},
			{field:'tdmcpy',title:'通道名称拼音',hidden:true,width:120},
			{field:'tdzt',title:'通道状态',hidden:true,width:120},
			{field:'tdazwz',title:'通道安装位置',hidden:true,width:120},
			{field:'jd',title:'经度',hidden:true,width:120},
			{field:'wd',title:'纬度',hidden:true,width:120},
			{field:'tpwzx',title:'图片位置x',hidden:true,width:120},
			{field:'tpwzy',title:'图片位置y',hidden:true,width:120},
			{field:'spbmgs',title:'视频编码格式',hidden:true,width:120},
			{field:'spxsgs',title:'视频显示格式',hidden:true,width:120},
			{field:'ssspsfzzf',title:'实时视频是否走转发',hidden:true,width:120},
			{field:'spzt',title:'视频状态',hidden:true,width:120},
			{field:'lxzt',title:'录像状态',hidden:true,width:120},
			{field:'lxsfzzf',title:'录像是否走转发',hidden:true,width:120},
			{field:'spzl',title:'视频帧率',hidden:true,width:120},
			{field:'gjzjg',title:'关键帧间隔',hidden:true,width:120},
			{field:'ml',title:'码率',hidden:true,width:120},
			{field:'spmlyxgs',title:'视频码率优先格式',hidden:true,width:120},
			{field:'sfzcptzkz',title:'是否支持ptz控制',hidden:true,width:120},
			{field:'zxcczbh',title:'中心存储组编号',hidden:true,width:120},
			{field:'zxccxe',title:'中心存储限额',hidden:true,width:120},
			{field:'zxccyyrl',title:'中心存储已用容量',hidden:true,width:120},
			{field:'zxccsyrl',title:'中心存储剩余容量',hidden:true,width:120},
			{field:'lxblts',title:'录像保留天数',hidden:true,width:120},
			{field:'sfqy',title:'是否启用',hidden:true,width:120},
			{field:'jlbh',title:'级联编号',hidden:true,width:120},
			{field:'fxms',title:'方向描述',hidden:true,width:120},
			{field:'jbms',title:'基本描述',hidden:true,width:120},
			{field:'xzbh',title:'行政编号',hidden:true,width:120},
			{field:'ggbh',title:'公共编号',hidden:true,width:120},
			{field:'znbh',title:'职能编号',hidden:true,width:120},
			{field:'lxfs',title:'录像方式',hidden:true,width:120},
			{field:'jpkzdh',title:'键盘控制短号',hidden:true,width:120},
			{field:'jdpx',title:'节点排序',hidden:true,width:120},
			{field:'sfznfxjd',title:'是否智能分析节点',hidden:true,width:120},
			{field:'znfxlx',title:'智能分析类型',hidden:true,width:120},
			{field:'sqms',title:'上墙模式',hidden:true,width:120},
			{field:'squrllj',title:'上墙url路径',hidden:true,width:120},
			{field:'ssmlxz',title:'实时码流选择',hidden:true,width:120},
			{field:'lxmlxz',title:'录像码流选择',hidden:true,width:120},
			{field:'zburllj',title:'组播url路径',hidden:true,width:120},
			{field:'zbdk',title:'组播端口',hidden:true,width:120},
			{field:'dqx',title:'读权限',hidden:true,width:120},
			{field:'xqx',title:'写权限',hidden:true,width:120},
			{field:'kqx',title:'控权限',hidden:true,width:120},
			{field:'pqx',title:'配权限',hidden:true,width:120},
			{field:'lxhfqx',title:'录像回放权限',hidden:true,width:120},
			{field:'lxxzqx',title:'录像下载权限',hidden:true,width:120},
			{field:'lxdjqx',title:'录像冻结权限',hidden:true,width:120},
			{field:'sfzlx',title:'是否在录像',hidden:true,width:120},
			{field:'sfzx',title:'是否在线',hidden:true,width:120},
			{field:'hb',title:'海拔',hidden:true,width:120},
			{field:'jmcj',title:'解码插件',hidden:true,width:120},
			{field:'sj',title:'视角',hidden:true,width:120},
			{field:'lxlrcyfs',title:'录像录入采用方式',hidden:true,width:120},
			{field:'khdosdxsnr',title:'客户端osd显示内容',hidden:true,width:120},
			{field:'khdosdxswz',title:'客户端osd显示位置',hidden:true,width:120},
			{field:'cjry',title:'创建人员',hidden:true,width:120},
			{field:'cjrq',title:'创建日期',hidden:true,width:120},
			{field:'xgry',title:'修改人员',hidden:true,width:120},
			{field:'xgrq',title:'修改日期',hidden:true,width:120},
			{field:'bz',title:'备注',hidden:true,width:120},
			{field:'kz1',title:'扩展1',hidden:true,width:120},
			{field:'kz2',title:'扩展2',hidden:true,width:120},
			{field:'kz3',title:'扩展3',hidden:true,width:120},
			{field:'kz4',title:'扩展4',hidden:true,width:120}
		]]
	});
}

function getSelectedRows(){
	var rows = $('#tableGrid').datagrid('getChecked'); 
	return rows;
}
//获取选中元素

function getSelected(){
	var rows = $('#tableGrid').datagrid('getChecked'); 
//	console.log("勾选：",rows);
	return rows;
}
/*parent.parent.$(".l-btn-text").on("click",function(){
	if($(this).text()=="关闭"){
		return;
	}
	var insertParam = {};
//	insertParam.channelsetid = channelsetid;
	var rows = $('#tableGrid').datagrid('getChecked'); 
//	top.$("#"+winId).panel("options").data.insertParam = insertParam;
//	top.$("#"+winId).panel("options").data.channelsetid = channelsetid;
	
 	
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideochannelsetlistAction!batch_add_Ywvideochannelsetlist.do',
		data:{channelsetid:channelsetid,param:JSON.stringify(rows)},
		success:function(value){
			if(value=="success"){
				$.showMsg("操作成功！");
//				top.$("#"+winId).dialog('close');
//				doSearch();
			}else if(value=="same"){
				$.showMsg("已存在！");
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
	
});*/
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'sbtdspsrAction!delete_Sbtdspsr.do',
			data:{
				xh	:	selRows.xh

			},
			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].xh);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'sbtdspsrAction!batch_delete_Sbtdspsr.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,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/sbtdspsr/sbtdspsr_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/sbtdspsr/sbtdspsr_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	console.log(model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(toEdit){//修改
		model.XGTJxh = jq('#xh').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.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 if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'sbtdspsrAction!insert_Sbtdspsr.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,
		 url	:	sysUrl+"/im/xx/sbtdspsr_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'sbtdspsrAction!count_Sbtdspsr.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 = 'sbtdspsrAction!exp_Sbtdspsr.do?'+downParam;
	document.excelForm.submit(); 
}
