jQuery(document).ready(function() {
	
		
	function loadData(){
		$('#grid-table').setGridParam({
			mtype:'GET',
			url : '/user/do_list',
			datatype : 'json',
			postData: { 'org.id':sxUtil.tree.getSelectOrgId() },
			page : 1
		});
		$('#grid-table').trigger('reloadGrid');
	}
	
	//初始化组织架构
	sxUtil.tree.navTreeMap({
		selectedAfter:loadData
	});
	sxUtil.jqGrid('#grid-table',{
		colNames:['ID','登录名','用户名', '管理员','手机','组织机构','操作'],
		colModel:[
			{name:'id',index:'id', width:120,key:true,hidden:true,hidedlg:true},
			{name:'loginName',index:'loginName',width:100},
			{name:'name',index:'name', width:70},
			{name:'admin',index:'admin', width:90,formatter: adminFormatter},
			{name:'phone',index:'phone', width:150, sortable:false},
			{name:'org.name',index:'orgName', width:150, sortable:false},
			{name:'locked',index:'locked', width:150, sortable:false,formatter:lockFormatter}
		], 
		mtype:'GET',
		url : '/user/do_list',
		pager : "#grid-pager",
		postData:{
			'org.id':sxUtil.tree.getSelectOrgId()
		},
		onSelectRow: function (rowId, status, e) {  
	        var rowIds = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
	        var id = $("#grid-table").jqGrid('getGridParam', 'selrow');
	        if(rowIds.length==1){
	        	var locking = $("#locking");
	        	var unlock = $("#unlock");
	        	var rowData = $("#grid-table").jqGrid('getRowData',id);
	        	if(rowData.locked=="否"){
	        		locking.show();
	        		unlock.hide();
	        	}
	        	if(rowData.locked=="是"){
	        		locking.hide();
	        		unlock.show();
	        	}
	        }
		}
	},true);
	
//	 function lockFormatter(cellvalue, options, rowObject) {  
//	     return cellvalue == false ? "否":"是";  
//	 }
	 function adminFormatter(cellvalue, options, rowObject) {  
		 return cellvalue == false ? "否":"是";  
	 }
	 
	 function lockFormatter(cellvalue,options,rowObject){
		// debugger;
		 if(cellvalue==undefined || cellvalue == false){
			 return '<a href="javascript:void(0)" onclick="lockuser(\''+rowObject.id+'\',true)">禁用</a>';
		 }
		 if(cellvalue==true){
			 return '<a href="javascript:void(0)" onclick="lockuser(\''+rowObject.id+'\',false)">启用</a>';
		 }
		 return '';
	 }
	//添加用户与角色的信息
	$('#userAdd').click(function(){
		sxUtil.dialog.window({
			url:'/user/add?org.id='+sxUtil.tree.getSelectOrgId(),
			layer:{
				  title:'新增',
				  area: ['950px', '500px'],
				  success:function(dom,index){
					  
				  },
				  btn: ['确定', '关闭'],
				  yes: function(index, dom){
					  $("#userForm").data('index',index).submit(); 
				  }
			}
		});
	});
	
	//修改用户与角色的关系
	$('#userUpdate').click(function(){
		 var ids = $("#grid-table").jqGrid('getGridParam','selarrrow');
		 var id = $("#grid-table").jqGrid('getGridParam','selrow');
		 if(null != id && null != ids && ids.length == 1){
			 sxUtil.dialog.window({
					url:'/user/update',
					data:{"id":id},
					layer:{
						  title:'修改',
						  area: ['950px', '400px'],
						  success:function(dom,index){
							  
						  },
						  btn: ['确定', '关闭'],
						  yes: function(index, dom){
							  $("#userForm").data('index',index).submit(); 
						  }
					}
			});  
		 }else{
			 layer.msg('请选中一条数据！', {icon: 0});
		 }
		 
	});
	
	//用户解锁
//	$('#modifyLock').click(function(){
//
//		var bo = false;
//		var ids = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
//		var rowData = $("#grid-table").jqGrid('getRowData',ids[0]);
//		if(rowData.locked=="是"){
//			bo = false;
//		}else{
//			bo = true;
//		}
//		if (ids && ids.length == 1) {
//			$.ajax({
//				url : "/user/do_modifylock",
//				data : {
//					"id" : ids[0],
//					"locked" : bo
//				},
//				success : function(msg) {
//					if (msg.success == true) {
//						$("#grid-table").trigger("reloadGrid"); // 重新载入
//						if(bo == false){
//							layer.msg('解锁成功！', {icon : 1});
//						}else{
//							layer.msg('锁定成功！', {icon : 1});
//						}
//					} else {
//						if(bo == false){
//							layer.msg('解锁失败！', {
//								icon : 2
//							});
//						}else{
//							layer.msg('锁定失败！', {
//								icon : 2
//							});
//						}
//					}
//				}
//			});
//		} else {
//			layer.msg('请选中一条数据！', {
//				icon : 0
//			});
//		}
//	});
	
	
	
	//重置密码
	$('#resetPassword').click(function(){
		 var ids = $("#grid-table").jqGrid('getGridParam','selarrrow');
		 var id = $("#grid-table").jqGrid('getGridParam','selrow');
		 if(null != id && null != ids && ids.length == 1){
			 sxUtil.dialog.window({
					url:'/user/resetpassword',
					layer:{
						  title:'重置密码',
						  area: ['950px', '200px'],
						  success:function(dom,index){
							  
						  },
						  btn: ['确定', '关闭'],
						  yes: function(index, dom){
							 $.ajax({ 
					              type: "POST", 
					              url: "/user/do_resetpassword/", 
					              data: {"id":id,"password":$("#password").val()}, 
					              success: function(msg){ 
					            	  if(msg.success==true){ 
					                       layer.close(index);
					                       layer.msg('重置密码成功！', {icon: 1});
					                   }else{ 
					                       layer.msg('新密码和旧密码相同！', {icon: 2});
					                   } 
					              } 
					         }); 
						  }
					}
				});
		 }else{
			 layer.msg('请选中一条数据！', {icon: 0});
		 }
	});
	
	//删除用户，如果有关联用户则提醒
	$('#userDelete').click(function(){
		 var ids = $("#grid-table").jqGrid('getGridParam','selarrrow');
		 var id = $("#grid-table").jqGrid('getGridParam','selrow');
		 if(null != id && null != ids && ids.length >= 1){
			 layer.confirm('您是否确认删除？', {
				  btn: ['确定','关闭'] //按钮
				}, function(){
				  $.ajax({ 
		              url: "/user/do_deletes/", 
		              data: {"ids":ids.join(",")}, 
		              success: function(msg){ 
		            	  if(msg.success==true){ 
		            		   $("#grid-table").trigger("reloadGrid"); //重新载入 
		                       layer.msg('删除成功！', {icon: 1});
		                   }else{ 
		                       layer.msg('删除失败！', {icon: 2});
		                   } 
		              } 
		         }); 
			});	 
		 }else{
			 layer.msg('请选中一条数据！', {icon: 0});
		 }
	});
	
	//查询用户
	$('#userSearch').click(function(){
		sxUtil.dialog.window({
			url:'/user/userSearch?org.id='+sxUtil.tree.getSelectOrgId(),
			layer:{
				  title:'查询',
				  area: ['950px', '400px'],
				  btn: ['确定', '关闭'],
				  yes: function(index, dom){
					  $('#searchForm').data('index',index).submit(); 
				  }
			}
		});
	});	
	
	function refresh(){
		$('#grid-table').jqGrid('setGridParam',{'postData':null});
		$("#grid-table").jqGrid('setGridParam',{ 
            mtype:'GET',
			url:"/user/do_list", 
            postData:{'org.id':sxUtil.tree.getSelectOrgId(),'level':$('#chosen-level').val()}, //发送数据 
            page:1 
        }).trigger("reloadGrid"); //重新载入
	}
	
	//刷新
	$('#refresh').click(function(){
		refresh();
	});
	
	$('#chosen-level').on('change',function(){
		refresh();
	});
});

function lockuser(id,bol){
	$.ajax({
		url : "/user/do_modifylock",
		data : {
			"id" : id,
			"locked" : bol
		},
		success : function(msg) {
			if (msg.success == true) {
				$("#grid-table").trigger("reloadGrid"); // 重新载入
					layer.msg('操作成功！', {icon : 1});
			} else {
					layer.msg('操作失败！', {
						icon : 2
					});
			}
		}
	});
	
}