var windowId,sysUrl = top.sysUrl,toEdit;
$(function(){
	initDefault();	
});
//默认
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.LIKEname = $('#LIKEname').textbox("getValue");
	sParam.LIKEid = $('#LIKEid').textbox("getValue");
//	sParam.LIKEsccs = $('#LIKEsccs').textbox("getValue");
	sParam.LIKEroadname = $('#LIKEroadname').textbox("getValue");
	sParam.detectortype=$('#detectortype').val();
	sParam.order = "order by updatetime desc ";
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	doAdd
    },{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    },{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete
    }/*,{
        text	:	'模板',
        iconCls	:	'icon-export-excel',
        handler	:	doTemplate
    },{
        text	:	'导入',
        iconCls	:	'icon-update',
        handler	:	doImport
    },{
        text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }*/];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'detectorAction!listDetectorByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'name',title:'名称',hidden:false,width:120},
			{field:'id',title:'编码',hidden:false,width:120},
			{field:'bianma',title:'备案编码',hidden:false,width:120},
			{field:'roadid',title:'道路id',hidden:false,width:120},
			{field:'roadname',title:'道路名称',hidden:false,width:120},
			{field:'longitude',title:'经度',hidden:false,width:120},
			{field:'latitude',title:'纬度',hidden:false,width:120},
			{field:'regionid',title:'行政区划编码',hidden:false,width:120},
			{field:'regionname',title:'行政区划名称',hidden:false,width:120},
			{field:'bianma2',title:'设备私有编码',hidden:false,width:120},
			{field:'updatetime',title:'更新时间',hidden:false,width:120}
		]]
	});
}


//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'detectorAction!delete_Detector.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].xh);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'detectorAction!batch_delete_Detector.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	: 	530,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/detector/detector_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	530,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/detector/detector_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	//区别地磁和微波
	model.detectortype=$('#detectortype').val();
	console.log("增加",model);
	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:'detectorAction!update_Detector.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:'detectorAction!insert_Detector.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+"/yw/nhyw/detector/detector_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'detectorAction!count_Detector.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 = 'detectorAction!exp_Detector.do?'+downParam;
	document.excelForm.submit(); 
}
