var proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var winId	= $.getUrlParam("winId");
var fwsbh = $.getUrlParam("fwsbh");
var xcdh = $.getUrlParam("xcdh");
$(function(){
	if(!fwsbh){
		fwsbh = "-1";//
	}
	/*$('#xcsblx').combobox({  
		url:'xtclAction!query_Xtcl.do?csdl=nhywjtsblx',
		textField:"csmc",  
		 valueField:"csdm",
		 panelWidth:148,
		 loadFilter:function(data){
//			data.unshift({"csmc":"全部","csdm":""});
			if(data && data.length>0){
				data[0].selected = true;
			}
			return data;
		}
	}); */
	//加载下拉选
	initSelect();
	initDefault();	
});
function initSelect(){
	//加载下拉框
	$.ajax({
		url:'xtclAction!query_Xtcl.do',
		type:"post",
		dataType:"JSON",
		async:false,
		data : {"page":1,"rows":1000,"csdl":"nhywjtsblx"},
		success:function(res){
			if(res && res.length>0){
				var lxArr = [{"id":res[0].csdm,"text":res[0].csmc,"selected":true}];
				for(var i=1;i<res.length;i++){
					lxArr.push({"id":res[i].csdm,"text":res[i].csmc})
				}
				$("#xcsblx").combobox("loadData",lxArr);
			}
		}
	});
}
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEchannelname = $('#LIKEtdmc').textbox("getValue");
	sParam.LIKEvideonum = $('#LIKEsbbh').textbox("getValue");
	sParam.xcsblx = $('#xcsblx').combobox("getValue");
	sParam.fwsbh = fwsbh;
	sParam.xcdh = xcdh;
	sParam.order = " order by videonum desc ";
	
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'nhywjtssxcsbAction!listJtsbByFwsbh.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:'channelname',title:'设备名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:120},
			{field:'pp',title:'设备品牌',hidden:false,width:120},
		]]
	});
}

function getSelectedRows(){
	var rows = $('#tableGrid').datagrid('getChecked'); 
	return rows;
}
//获取选中元素
parent.parent.$(".l-btn-text").on("click",function(){
	if($(this).text()=="关闭"){
		return;
	}
	var insertParam = {};
//	insertParam.channelsetid = channelsetid;
	var rows = $('#tableGrid').datagrid('getChecked'); 
	if(rows && rows.length>0){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjtssxcsbAction!batch_add_Nhywjtssxcsb.do',
			data:{xcdh:xcdh,xcsblx:$('#xcsblx').combobox("getValue"),param:JSON.stringify(rows)},
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+winId).dialog('close');
					top.$("#"+winId).panel("options").data = true;
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
//	top.$("#"+winId).panel("options").data.insertParam = insertParam;
//	top.$("#"+winId).panel("options").data.channelsetid = channelsetid;
	
	
	
});
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//保存
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("操作失败！");
				}
			}
		});
	}
}
