<script language="javascript">
$(function(){
	var th = $(".top").height();
	th = 111-th
	var wh = $(window).height()-th;
	var pr = '<?php echo $page_row ?>';
	var pn = false;
	if(pr>0){
		pn = true;
	}
	$("#Users").datagrid({
		//title:'用户列表',
		idField:'id',
		height:wh,
		treeField:'text',
		autoRowHeight:false,
		singleSelect:true,
		striped:true,
		rownumbers:true,
		pagination:pn,
		pageSize:pr,
		pageList:[30,50,80,100,100000000000],
		method:'get',
		url:'__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=<?php echo ACTION_NAME; ?>&json=1',
		fitColumns:true,
		nowrap:Number('{$TC.config.DATA_NOWRAP}'),
		selectOnCheck:false,
		checkOnSelect:true,
		onBeforeLoad: function () {  
			 if($("#UserCon .datagrid-toolbar table tr #sersSearchBox").length==0){
				 var grid = $("#UserCon .datagrid-toolbar table tr");  
				 var date = '<td>'+$("#selectInputUser").html()+'</td>';    
				 grid.append(date); 
				 $("#sersSearchBox").combobox({
					url:'__ITEM__/__RUNTIME__/Data/Json/User_data.json',
					editable:true,
					method:'get',
					valueField:'id',
					textField:'text',
					filter: function(q,r){
						var opts = $(this).combobox('options');
						var v = r[opts.textField];
						var vp = ','+String(getPinYin(v));
						var qp = ','+q.toUpperCase();
						if(vp.indexOf(qp)>=0 || v.indexOf(q) == 0){
							return r[opts.textField];
						}
					},
					onSelect:function(q){
						$.post('__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=change&act=username', {val:q.text}, function(data){
							$("#Users").datagrid('reload');
						});
					}
				 });
			 }
		},
		onHeaderContextMenu:function(e,f){
			if(f!='id'){
				$("#searchUsers").dialog({
					title:'快速搜索',
					resizable:true,
					width:430,
					height:80,
					href:'__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=search&field='+f
				});
			}
			e.preventDefault();
		},
		onDblClickRow:function(e,rowIndex,rowData){
			//var se = $("#Project").datagrid('getSelected');
			var se = $("#Users").datagrid('getChecked');
			var se_len = se.length;
			var idd = se[0]['id'];
			if(se_len==1 && idd!=1){
				$("#addUsers").dialog({
					title:'编辑用户',
					resizable:true,
					width:520,
					height:250,
					href:'__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=add&act=edit&id='+idd,
					onOpen:function(){
						cancel['User'] = $(this);
					},
					onClose:function(){
						//$("#Users").datagrid('reload');
						cancel['User'] = null;
					}
				});
			}
			
		},
		onUncheck:function(i,d){
			$("#Users").datagrid('unselectRow',i);
		},
		toolbar:[{
		iconCls: 'icon-add',
			text : '新增',
			handler: function(){
				$("#addUsers").dialog({
					title:'新增用户',
					resizable:true,
					width:520,
					height:250,
					href:'__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=add&act=add',
					onOpen:function(){
						cancel['User'] = $(this);
					},
					onClose:function(){
						//$("#Users").datagrid('reload');
						cancel['User'] = null;
					}
				});
			}
		},'-',{
			iconCls: 'icon-edit',
			text : '编辑',
			handler: function(){
				//var se = $("#Project").datagrid('getSelected');
				var se = $("#Users").datagrid('getChecked');
				var se_len = se.length;
				var idd = se[0]['id'];
				if(se_len==1 && idd!=1){
					$("#addUsers").dialog({
						title:'编辑用户',
						resizable:true,
						width:520,
						height:250,
						href:'__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=add&act=edit&id='+idd,
						onOpen:function(){
							cancel['User'] = $(this);
						},
						onClose:function(){
							//$("#Users").datagrid('reload');
							cancel['User'] = null;
						}
					});
				}else if(se_len>1){
					$.messager.alert('提示','不能同时编辑两行数据！','warning');
				}
			}
		},'-',{
			iconCls: 'icon-cancel',
			text : '删除',
			handler: function(){
				var se = $("#Users").datagrid('getChecked');
				var s = "";  
				for (var property in se) {  
					s = s + se[property]['id']+',' ;  
				}
				if(s){
					$.messager.confirm('提示','确定删除吗！',function(r){
						if(r==true){
							$.messager.progress();
							$.post('__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=del',{id:s}, function(data){
								$.messager.progress('close');
								if(data==1){
									$.messager.alert('提示','删除数据成功！','info',function(){
										$("#Users").datagrid('reload');
									});
								}else if(data==0){
									$.messager.alert('提示','删除数据失败！','warning');
								}else{
									$.messager.alert('提示','您没有删除权限！','warning');
								}
							});
						}
					});	
				}
			}
		},'-',{
			iconCls: 'icon-json',
			text : '生成JSON',
			handler: function(){
				$.get('__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=json', function(data){
					if(data==1){
						$.messager.alert('提示','生成json数据成功！','info');
					}else{
						$.messager.alert('提示',data,'warning');
					}
				});
			}
		},'-',{
			iconCls: 'icon-excel',
			text : '导出',
			handler: function(){				
				window.location = '__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=excel';
			}
		},'-',{
			iconCls: 'icon-search',
			text : '高级搜索',
			handler: function(){
				$("#searchUsers").dialog({
					title:'高级搜索',
					resizable:true,
					width:500,
					height:220,
					href:'__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=advsearch'
				});
			}
		},'-',{
			iconCls: 'icon-reload',
			text : '重载',
			handler: function(){
				$.get('__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=clear', function(data){
					$("#sersSearchBox").combobox('setValue','');
					$("#Users").datagrid('reload');
				});
			}
		}],
		frozenColumns:[[
			{checkbox:true}, 
			{field:'id',title:'ID号',width:45,sortable:true},
			{field:'username',title:'<span class="tit-bl" title="单击右键，进入快速搜索">帐号</span>',width:110,sortable:true}
		]],
		columns:[[  
			{field:'union_group_name',title:'<span class="tit-bl" title="单击右键，进入快速搜索">角色</span>',width:100,sortable:true}, 
			{field:'union_comy_name',title:'<span class="tit-bl" title="单击右键，进入快速搜索">公司</span>',width:105,sortable:true,hidden:Boolean(<?php echo !C('MORE_COMY') ?>)}, 
			{field:'union_part_name',title:'<span class="tit-bl" title="单击右键，进入快速搜索">部门</span>',width:115,sortable:true},   
			{field:'email',title:'<span class="tit-bl" title="单击右键，进入快速搜索">邮箱</span>',width:200},
			{field:'login_count',title:'<span class="tit-bl" title="单击右键，进入快速搜索">登录次数</span>',width:80,sortable:true},
			{field:'last_visit',title:'<span class="tit-bl" title="单击右键，进入快速搜索">最后登录时间</span>',width:170,sortable:true},
			//{field:'report',title:'<span class="tit-bl" title="单击右键，进入快速搜索">周报</span>',width:45,sortable:true},
			{field:'status',title:'<span class="tit-bl" title="单击右键，进入快速搜索">状态</span>',width:60,sortable:true}
		]]
	});
	
	 var dataview = '<?php echo C("DATAGRID_VIEW") ?>';
	 if(dataview!='0'){
		var pager = $('#Users').datagrid('getPager');
		pager.pagination({
			layout: 'list,sep,first,prev,sep,manual,sep,next,last,sep,refresh',
			displayMsg: '共{total}记录'
		});
	 }
	
	$("#rightTabs").tabs({
		onClose:function(t,i){
			$.ajaxSetup({  
				async : false  
			});
			if(t=='用户管理'){
				$.get('__APP__?g=<?php echo GROUP_NAME; ?>&m=<?php echo MODULE_NAME; ?>&a=clear', function(data){});
			}	
			$.ajaxSetup({  
				async : true  
			});
		}
	});
	
	$("#rightTabs").tabs('select','用户管理');
});
function toSe(value,name){   
	alert(value+":"+name)   
}
</script>
<div class="con" id="UserCon" onselectstart="return false;" style="-moz-user-select:none;">
 <if condition="C('DATAGRID_VIEW')!='0'">
 <table id="Users" data-options="view:<?php echo C("DATAGRID_VIEW") ?>"></table>
 <else />
 <table id="Users"></table>
 </if>
</div>
<div id="searchUsers"></div>
<div id="addUsers"></div>
<div id="selectInputUser" style="display:none">
 <span class="datagrid-btn-separator-nofloat"  style="margin-right:2px;"></span>
 <input id="sersSearchBox" style="width:100px;" />
</div>
