using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace free.bbs
{
	/// <summary>
	/// Summary description for admin_delhuihuan.
	/// </summary>
	public class admin_delhuihuan : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.TextBox name1;
		protected System.Web.UI.WebControls.DropDownList bankuai;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		SqlConnection monn;
		protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
		protected System.Web.UI.WebControls.TextBox huiyuanming;
		SqlCommand mycomm;
		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				if(Session["free_quanxian"].ToString()!="0")
				{
					Response.Write("<script>alert('您无权使用！！')</script>");
					Response.Redirect("../admin/default.aspx");
				}
			}
			catch
			{
				Response.Write("<script>alert('您无权使用！！')</script>");
				Response.Redirect("../admin/default.aspx");
				
			}
			Session["chuli"]="";
			monn=new SqlConnection(Session["str_connection"].ToString());
			monn.Open();
			string mysql="select neirong from ancestor";
			SqlCommand command=new SqlCommand(mysql,monn);
			SqlDataReader reader=command.ExecuteReader();
			if(!IsPostBack)
			{
				while(reader.Read())
				{
					bankuai.Items.Add(reader["neirong"].ToString());
				}
			}
			reader.Close();
			monn.Close();
			bangding();
		}

		private void bangding()
		{
			DataSet mydata=new DataSet();
			string mysql="select yonghuming,xingbie,jifen,wenzhangshu,zhuceshijian,lastlogin,fatieshu,dengji,jinqian,meili from huiyuan order by zhuceshijian";
			SqlDataAdapter mypeter=new SqlDataAdapter(mysql,monn);
			mypeter.Fill(mydata,"huiyuan");
			DataGrid1.DataSource=mydata.Tables["huiyuan"].DefaultView;
			DataGrid1.DataBind();
			
		}
		

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			if(name1.Text==""||bankuai.SelectedValue=="")
			{
				Response.Write("<script>alert('您必须输入用户名！')</script>");
				Session["chuli"]="您必须输入用户名！";
			}
			else
			{ 
				monn.Open();
				string sss="select banzhu from ancestor where neirong='"+bankuai.SelectedValue+"'";
				SqlCommand ccc=new SqlCommand(sss,monn);
				SqlDataReader mmm=ccc.ExecuteReader();
				mmm.Read();
				if(mmm["banzhu"].ToString().Trim()!="")
				{
					Response.Write("<script>alert('该版块的版主已经存在！')</script>");
					mmm.Close();
				}
				else
				{
					mmm.Close();
					string mysql2,mysql4,mysql5;
					mysql5="select yonghuming from huiyuan where yonghuming='"+name1.Text.ToString().Trim()+"'";
					ccc=new SqlCommand(mysql5,monn);
						SqlDataReader readd=ccc.ExecuteReader();
					if(readd.Read())
					{
						readd.Close();
						
						//monn.Open();
					
						mysql2="update ancestor set banzhu='"+name1.Text.Trim()+"' where neirong='"+bankuai.SelectedValue.ToString()+"'";
						mysql4="update huiyuan set dengji='版主' where yonghuming='"+name1.Text.Trim()+"'";
						try
						{
							SqlCommand command2=new SqlCommand(mysql2,monn);
							command2.ExecuteNonQuery();
							SqlCommand command4=new SqlCommand(mysql4,monn);
							command4.ExecuteNonQuery();
							monn.Close();
							Session["chuli"]="升级版主成功！";
							Response.Write("<script>alert('升级版主成功！')</script>");
							bangding();
						}
						catch
						{
							Response.Write("<script>alert('请检查该用户是否存在！')</script>");
							Session["chuli"]="该用户不存在！";
						}
					}
					else
					{
						readd.Close();
						Response.Write("<script>alert('您输入的用户不是会员！')</script>");
					}
					
				}
				
				
			}
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex=e.NewPageIndex;
			bangding();
		}


		private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex=e.Item.ItemIndex;
			bangding();
		}

		private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex=-1;
			bangding();
		
		}

		private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			mycomm=new SqlCommand("update huiyuan SET dengji='"+e.Item.Cells[3].Text.ToString()+"' WHERE yonghuming='"+e.Item.Cells[1].Text.ToString()+"' ",monn);
			mycomm.Connection.Open();
			mycomm.ExecuteNonQuery();
			mycomm.Connection.Close();
			DataGrid1.EditItemIndex=-1;
			bangding();
		
		}

		private void Submit1_ServerClick(object sender, System.EventArgs e)
		{
			if(huiyuanming.Text=="")
				Response.Write("<script>alert('请先输入要删除的会员名！')</script>");
			else
			{
				monn.Open();
				string sql="delete from huiyuan where yonghuming='"+huiyuanming.Text.ToString().Trim()+"'";
				SqlCommand liu=new SqlCommand(sql,monn);
				if(liu.ExecuteNonQuery()==1)
				{
					Response.Write("<script>alert('该会员删除成功！')</script>");
					bangding();
					huiyuanming.Text="";
				}
				else
					Response.Write("<script>alert('该会员不存在！')</script>");
				monn.Close();
			}
		}
	}
}
