var windowId,sysUrl = top.sysUrl,toEdit;
$(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");
	
	$("#LIKEsj1").datetimebox("setValue",$.getTodayStart(-30)); //往前推30天+00:00:00 
	$("#LIKEsj2").datetimebox("setValue",$.getTodayEnd());//当天 +23:59:59
	
	$('#status').combobox({  
		 data : $.obj2ary(CommonData.userStatusObj),  
		 textField:1,  
		 valueField:0,
		 panelWidth:148,
		 value:"" 
	});
	*/
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	/*
	//RH_DEMO_SEARCH_PARAM
	*/
	//
	/*
	var xx1 = $("#LIKEtext").textbox("getValue");
	var xx2 = $("#LIKEtext").datetimebox("getValue");
	var xx3 = $("#LIKEtext").combobox("getValue");
	var LIKEtime1 = $('#LIKEtime1').datetimebox("getValue");	
	var LIKEtime2 = $('#LIKEtime1').datetimebox("getValue");	
	var tableparam = "";
	if(!(LIKEtime1==null||LIKEtime1==""||LIKEtime1==undefined)){
		tableparam += " AND time > '" +LIKEtime1 + "'";
	}
	if(!(LIKEtime2==null||LIKEtime2==""||LIKEtime2==undefined)){
		tableparam += " AND time < '" +LIKEtime2 + "'";
	}
	sParam.tableparam = tableparam;
	sParam.order = "order by time 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
    }];
	//return CommonData.userStatusObj[value];
	
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'RH_DEMO_NAMEAction!listRH_DEMO_NAMEByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    //RH_DEMO_GRID
		]]
	});
}

//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		 return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'RH_DEMO_NAMEAction!delete_RH_DEMO_NAME.do',
			 data:{keyword	:	selRows.keyword},
			 success:function(flag){
				 if(flag == "success"){
					 $.showMsg("操作成功！");
					 doSearch();
				 }
			 }
		 });
	});
}

//批量删除
function batch_deleteUser(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = "";
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			if(i == selRows.length -1){
				bianhao += selRows[i].KEYWORD;	
			}else{
				bianhao += selRows[i].KEYWORD+",";	
			}
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'RH_DEMO_NAMEAction!batch_delete_RH_DEMO_NAME.do',
				 data:{keyword	:	keyword},
				 success:function(flag){
					 $.showMsg("操作成功！");
					 doSearch();
				 }
			 });
		}); 
	}
}

//导出
function doExport(){
	
}

//查询
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+"/RH_PACKAGE/RH_JSPFILE_show.jsp"
	 });
}

//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		 title	: 	'修改信息',
		 width	:  	650,
		 data   :   selRows,
		 height	: 	300,
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/RH_PACKAGE/RH_JSPFILE_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;
//	}
//	var isSuper 	= 	jq(comboboxs[0]).combobox('getValue') ;
//	var status 		= 	jq(comboboxs[1]).combobox('getValue') ;
	
//	if(!model.adminName){
//		$.showMsg('用户名称不能为空! ');
//		return;
//	}if(!model.password){
//		$.showMsg('密码不能为空! ');
//		return;
//	}if(!model.password2){
//		$.showMsg('请输入确认密码! ');
//		return;
//	}
//	if(password!=password2){
//		$.showMsg('两次密码不一致! ');
//		return;
//	}
//	if(!model.shouji){
//		var r =/^1[3|4|5|8][0-9]\d{4,8}$/;   
//		if(!r.test(shouji)){
//			$.showMsg('手机请输入11位纯数字!');
//			return;
//		}
//	}
//	if(model.zhaidian){
//		var r =/^[0-9]+[0-9]*]*$/;   
//		if(!r.test(model.zhaidian)){
//			$.showMsg('电话请输入纯数字!');
//			return;
//		}if(model.zhaidian.length<6){
//			$.showMsg('电话请输入至少6位纯数字!');
//			return;
//		}
//	}
	if(toEdit){//修改
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'RH_DEMO_NAMEAction!update_RH_DEMO_NAME.do',
			 data:model,
			 success:function(value){
				 if(value=="success"){
					 $.showMsg("操作成功！");
					 top.$("#"+windowId).dialog('close');
					 doSearch();
				 }
			 }
		 });
	}else{
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'RH_DEMO_NAMEAction!insert_RH_DEMO_NAME.do',
			 data:model,
			 success:function(value){
				 if(value=="success"){
					 $.showMsg("操作成功！");
					 top.$("#"+windowId).dialog('close');
					 doSearch();
				 }
			 }
		 });
	}
}

//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}