var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.detectortype= $('#detectortype').val();
	sParam.LIKEname = $('#LIKEname').textbox("getValue");
	sParam.order = " order by updatetime desc"
	
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'detectorAction!listDetectorCurrentstateByPage.do',
		columns : [[
		   /* {
				field: 'A',
				checkbox:'true',
				width:30
			},*/		    
			{field:'name',title:'名称',hidden:false,width:120},
			{field:'bianma',title:'备案编码18位',hidden:false,width:120},
			{field:'detfrequency',title:'数据采集频率',hidden:true,width:60	},			
			{field:'longitude',title:'经度',hidden:false,width:60},
			{field:'latitude',title:'纬度',hidden:false,width:60},
			{field:'roadname',title:'道路名称',hidden:false,width:110},
			{field:'roadid',title:'道路编码',hidden:false,width:120},
			{field:'bianma2',title:'设备私有编码',hidden:false,width:60},
			{field:'factoryid',title:'厂商编码',hidden:false,width:120},
			{field:'regionid',title:'行政区划编码',hidden:false,width:120},
			{field:'updatetime',title:'更新时间',hidden:false,width:250},
			{field:'state',title:'当前状态',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					if(value==1){//正常
						return "<div style='height:13px;width:13px;background-color:green;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
					}else if(value==2){//2停用，//3报废
						return "<div style='height:13px;width:13px;background-color:red;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
					}else {
						return  "报废";
					}
				}
			},
			{field:'create_time',title:'检测时间',hidden:false,width:120},
			{field:'currentstateid',title:'操作',hidden:false,width:70, 
				formatter:function showClick(value,record,num){ 
        			return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="lookHistory('+num+')"  group="" id=""><span class="l-btn-left"><span class="l-btn-text">详情</span></span></a>';
				}
 			},
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'detectorAction!delete_Detectorcurrentstate.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:'detectorAction!batch_delete_Detectorcurrentstate.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/ywgl/detectorcurrentstate_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/ywgl/detectorcurrentstate_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.XGTJid = jq('#id').val();
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'detectorAction!update_Detectorcurrentstate.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_Detectorcurrentstate.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/detectorcurrentstate_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'detectorAction!count_Detectorcurrentstate.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_Detectorcurrentstate.do?'+downParam;
	document.excelForm.submit(); 
}

// 查看历史记录
function lookHistory(index){
	
	//console.log("历史跳转：",index);
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'历史记录',
		width	:  	800,
		height	: 	500, 
		data  :  selRows,
		url	:	sysUrl+"/yw/recordalarm/detectorstate/detectorhistorystate.jsp",
//		onClose : closeRecordVideo
	});
}

