<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" Inherits="Ycbw.Data.OleDbClient"  %>

<%@ Register TagPrefix="Rajah" TagName="Head" src="../ctrls/Head.ascx" %>
<%@ Register TagPrefix="Rajah" TagName="Foot" src="../ctrls/Foot.ascx" %>

<script language="c#" runat="server">

private void Page_Load(object sender,EventArgs e)
{
	if( !Page.IsPostBack )
	{
		this.Bind_Grid();
	}
}

private void Bind_Grid()
{
	string strSql = "select * from [b_admin] where adminid > 0";
	
	this.BindGrid( strSql + " Order By uid Desc", "data_list", this.data_list );
}

private void button_add_click(object sender,EventArgs e)
{
	string err = "";
	
	//一大堆判断
	if( err.Length > 0 )
	{
		Response.Write( err );
	}
	else
	{
		this.Fill( "Select Top 1 * From b_admin where username like '"+ this.text_username.Text +"'", "data_Users" );
		
		if( this.Data.Tables["data_Users"].Rows.Count > 0 )
		{
			this.JWrite( "alert('添加失败, 新账号与现有账号重复!');history.go(-1);" );
		}
		else
		{
			string username = this.text_username.Text;
			string nickname = this.text_nickname.Text;
			string password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.text_password.Text,"MD5").ToLower();
			
			string adminid  = this.text_adminid.Text;

			this.Execute( "Insert into b_admin ([username], [password], [nickname], [adminid], [groupid]) values ( '"+ username +"', '"+ password +"', '"+ nickname +"', '"+ adminid +"', 0 )" );

			this.Bind_Grid();
		}
	}
}

private void data_list_PageChanged(object sender,DataGridPageChangedEventArgs e)
{
	((DataGrid)sender).CurrentPageIndex = e.NewPageIndex;
	this.Bind_Grid();
}

private void data_edit_click(object sender,DataGridCommandEventArgs e)
{
	((DataGrid)sender).EditItemIndex = (int)e.Item.ItemIndex;
	this.Bind_Grid();
}

private void data_update_click(object sender,DataGridCommandEventArgs e)
{
	string uid       = ((Label)e.Item.FindControl("uid")).Text;
	string username = ((TextBox)e.Item.FindControl("text_username")).Text;
	string nickname = ((TextBox)e.Item.FindControl("text_nickname")).Text;
	string adminid  = ((DropDownList)e.Item.FindControl("text_adminid")).Text;

	this.Execute( "Update b_admin set [username] = '"+ username +"', [nickname] = '"+ nickname +"', [adminid] = '"+ adminid +"' where uid = "+ uid );
	
	string password = ((TextBox)e.Item.FindControl("text_password")).Text;
	if( password != "" )
	{
		password = FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5").ToLower();
		
		this.Execute( "Update b_admin set [password] = '"+ password +"' where uid = "+ uid );
	}	

	((DataGrid)sender).EditItemIndex = -1;
	this.Bind_Grid();	
}

private void data_cancel_click(object sender,DataGridCommandEventArgs e)
{
	((DataGrid)sender).EditItemIndex = -1;
	this.Bind_Grid();	
}

private void data_delete_click(object sender,DataGridCommandEventArgs e)
{
	string uid    = ((Label)e.Item.FindControl("uid")).Text;
	
	this.Execute( "Delete from b_admin where uid = "+ uid );

	this.Bind_Grid();	
}
</script>

<Rajah:Head ID="Head" Runat="Server" />
<script language="javascript" src="../js/drag.js"></script>
<form id="form1" runat="server">
<div id="body">
	<div class="title3">后台用户管理</div>
	<div>
	<table width="100%" align="center" border="0" cellspacing="8" cellpadding="0">
	  <tr>
		<td>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table3">
		  <tr>
			<td><input type="button" onclick="javascript:showAdd();" value=" 添加 " /></td>
			<td align="right"><asp:Label ID="text_pages1" runat="server" /></td>
		  </tr>
		</table>
		<asp:DataGrid ID="data_list" Width="100%" BorderWidth="0" CellPadding="0" CellSpacing="0" AutoGenerateColumns="false" runat="server" 
		CssClass="autotext" HeaderStyle-CssClass="header1" ItemStyle-CssClass="item1" 
		OnEditCommand="data_edit_click" OnUpdateCommand="data_update_click" OnCancelCommand="data_cancel_click" OnDeleteCommand="data_delete_click">
		  <columns>
			<asp:TemplateColumn HeaderStyle-Width="24">
				<headertemplate><input type="checkbox" onclick="checkAll(this)"></headertemplate>
				<itemtemplate><input type="checkbox" name="ids" value="<%# Eval("uid") %>"><asp:Label ID="uid" Text='<%# Eval("uid") %>' Visible="false" runat="server" /></itemtemplate>
			</asp:TemplateColumn>
	
			<asp:TemplateColumn HeaderStyle-Width="100">
				<headertemplate>账号</headertemplate>
				<itemtemplate>
				  <span style="width:90px"><%# Eval("username") %></span>
				</itemtemplate>
				<edititemtemplate>
				  <asp:TextBox ID="text_username" CssClass="text2" Width="90" Text='<%# Eval( "username" ) %>' runat="server" />
				</edititemtemplate>
			</asp:TemplateColumn>
	
			<asp:TemplateColumn HeaderStyle-Width="100">
				<headertemplate>密码</headertemplate>
				<itemtemplate>
				  <span style="width:90px"></span></a>
				</itemtemplate>
				<edititemtemplate>
				  <asp:TextBox ID="text_password" CssClass="text2" Width="90" runat="server" />
				</edititemtemplate>
			</asp:TemplateColumn>
	
			<asp:TemplateColumn HeaderStyle-Width="100">
				<headertemplate>名称</headertemplate>
				<itemtemplate>
				  <span style="width:90px"><%# Eval("nickname") %></span>
				</itemtemplate>
				<edititemtemplate>
				  <asp:TextBox ID="text_nickname" CssClass="text2" Width="90" Text='<%# Eval( "nickname" ) %>' runat="server" />
				</edititemtemplate>
			</asp:TemplateColumn>
	
			<asp:TemplateColumn HeaderStyle-Width="100%">
				<headertemplate>权限</headertemplate>
				<itemtemplate>
				  <asp:DropDownList ID="text_adminid" text='<%# Eval( "adminid" ) %>' Enabled="false" runat="server">
						<asp:ListItem Value="1" Text="普通管理员" />
						<asp:ListItem Value="4" Text="超级管理员" />
					</asp:DropDownList>
				</itemtemplate>
				<edititemtemplate>
				  <asp:DropDownList ID="text_adminid" text='<%# Eval( "adminid" ) %>' runat="server">
						<asp:ListItem Value="1" Text="普通管理员" />
						<asp:ListItem Value="4" Text="超级管理员" />
					</asp:DropDownList>
				</edititemtemplate>
			</asp:TemplateColumn>
	
			<asp:TemplateColumn HeaderStyle-Width="70">
				<headertemplate></headertemplate>
				<itemtemplate>
					<asp:LinkButton Text="修改" CommandName="edit" runat="server" />
					<span onclick="return confirm('确定要删除该条资料吗?')"><asp:LinkButton Text="删除" CommandName="delete" runat="server" /></span>
				</itemtemplate>
				<edititemtemplate>
					<asp:LinkButton Text="确定" ForeColor="#FF0000" CommandName="update" runat="server" />
					<asp:LinkButton Text="取消" ForeColor="#FF0000" CommandName="cancel" runat="server" />			
				</edititemtemplate>
			</asp:TemplateColumn>
		  </columns>
		</asp:DataGrid>	</td>
	  </tr>
	  <tr>
		<td>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table3">
		  <tr>
			<td>&nbsp;</td>
			<td align="right"><asp:Label ID="text_pages" runat="server" /></td>
		  </tr>
		</table>
		</td>
	  </tr>
	</table>
	</div>
</div>


<div id="add" style="position:absolute; left:100px; top:100px; display:none; border:1px solid #4E86C4;">
	<div class="title4" onmousedown="setMove(this);" style="cursor:move;">新增管理员</div>
	<div>
		<table border="0" align="center" cellpadding="5" cellspacing="0">
		  <tr bgcolor="#FFFFFF">
			<td width="60" height="24" align="right">账号</td>
			<td width="180"><asp:TextBox ID="text_username" CssClass="text2" Width="90%" runat="server" /></td>
			<td width="60" align="right">密码</td>
			<td width="180" style="padding-left:3px;"><asp:TextBox ID="text_password" CssClass="text2" Width="90%" runat="server" /></td>
		  </tr>
		  <tr bgcolor="#FFFFFF">
			<td height="24" align="right">名称</td>
			<td><asp:TextBox ID="text_nickname" CssClass="text2" Width="90%" runat="server" /></td>
			<td align="right">级别</td>
			<td><asp:DropDownList ID="text_adminid" runat="server">
							<asp:ListItem Value="1" Text="普通管理员" />
							<asp:ListItem Value="4" Text="超级管理员" />
							</asp:DropDownList></td>
		  </tr>
		  <tr bgcolor="#FFFFFF">
			<td colspan="4" class="table4"><span onclick="return checkAdd()"><asp:Button Text=" 新增 " CssClass="Button1" runat="server" OnClick="button_add_click" /></span> <input type="button" class="Button1" value=" 取消 " onclick="hideAdd()"></td>
		  </tr>
		</table>
		<script language="javascript">
		function checkSubmit()
		{
			if( document.form1.text_username.value == "" )
			{
				alert('名称必须填写');
				return false;
			}

			return true;
		}
		</script>
	</div>
</div>

</form>
<Rajah:Foot ID="Foot" Runat="Server" />