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_lookbanzhu.
	/// </summary>
	public class admin_lookbanzhu : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid mydatagrid;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.TextBox name;
		protected System.Web.UI.WebControls.TextBox name1;
		protected System.Web.UI.WebControls.DropDownList bankuai;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.DropDownList jiebie;
		protected System.Web.UI.WebControls.DropDownList jibie1;
		protected System.Web.UI.WebControls.DropDownList mokuai;
		SqlConnection monn;
		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["jieguo"]="";
			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());
					mokuai.Items.Add(reader["neirong"].ToString());
				}
			}
			reader.Close();
			monn.Close();
			bangding();
			// Put user code to initialize the page here
		}


		#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.Button2.Click += new System.EventHandler(this.Button2_Click);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		private void bangding()
		{
			string mysql="select banzhu,banzhu1,banzhu2,banzhu3,banzhu4,neirong from ancestor ";
			DataSet mydata=new DataSet();
			monn.Open();
			SqlDataAdapter mypeter=new SqlDataAdapter(mysql,monn);
			mypeter.Fill(mydata,"ancestor");
			mydatagrid.DataSource=mydata.Tables["ancestor"].DefaultView;
			mydatagrid.DataBind();
			monn.Close();
		}


		public void ChangePage(Object sender,DataGridPageChangedEventArgs e)
		{
			mydatagrid.CurrentPageIndex = e.NewPageIndex;
			bangding();
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			DateTime time=DateTime.Now;
			if(name.Text=="")
			{
				Response.Write("<script>alert('您必须输入用户名！')</script>");
				Session["jieguo"]="您必须输入用户名！";
			}
			else
			{
				try
				{
//					
					string mysql2="update huiyuan set dengji='普通会员' where yonghuming='"+name.Text.Trim()+"'";
					string mysql4="";
					switch(Convert.ToUInt32(jibie1.SelectedValue.ToString()))
					{
						case 1:
							mysql4="update ancestor set banzhu='' where neirong='"+mokuai.SelectedValue+"'";
							break;
						case 2:
							mysql4="update ancestor set banzhu1='' where neirong='"+mokuai.SelectedValue+"'";
							break;
						case 3:
							mysql4="update ancestor set banzhu2='' where neirong='"+mokuai.SelectedValue+"'";
							break;
						case 4:
							mysql4="update ancestor set banzhu3='' where neirong='"+mokuai.SelectedValue+"'";
							break;
						case 5:
							mysql4="update ancestor set banzhu4='' where neirong='"+mokuai.SelectedValue+"'";
							break;
					}
				monn.Open();
					SqlCommand command2=new SqlCommand(mysql2,monn);
					command2.ExecuteNonQuery();
					SqlCommand command4=new SqlCommand(mysql4,monn);
					command4.ExecuteNonQuery();
					string sqlins="INSERT INTO duanxiaoxi(neirong,faqiren,jieshoufang,shijian,zhuti) VALUES('   海纳百川管理团队的调动，您已退出<<"+bankuai.SelectedValue.ToString()+">>版主职责。海纳百川建站资讯网于2005年2月9日发布，希望您继续参与和支持我们。','free911','"+name.Text.Trim()+"','"+time+"','对不起，您的工作不到位。')";
					command2= new SqlCommand(sqlins,monn);
					try
					{
						command2.ExecuteNonQuery();
					}
					catch{}
					monn.Close();
					Response.Write("<script>alert('操作成功，该版主已经被降为普通会员！')</script>");
					bangding();
				}
				catch
				{
					Response.Write("<script>alert('该版主不存在！')</script>");
				}
			}
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			DateTime time=DateTime.Now;
			if(name1.Text=="")
			{
				Response.Write("<script>alert('您必须输入用户名！')</script>");
				//Session["chuli"]="您必须输入用户名！";
			}
			else
			{ 
				monn.Open();
				string sss="";
				switch(Convert.ToUInt32(jiebie.SelectedValue.ToString()))
				{
					case 1:
						sss="select banzhu from ancestor where neirong='"+bankuai.SelectedValue+"'";
						break;
					case 2:
						sss="select banzhu1 from ancestor where neirong='"+bankuai.SelectedValue+"'";
						break;
					case 3:
						sss="select banzhu2 from ancestor where neirong='"+bankuai.SelectedValue+"'";
						break;
					case 4:
						sss="select banzhu3 from ancestor where neirong='"+bankuai.SelectedValue+"'";
						break;
					case 5:
						sss="select banzhu4 from ancestor where neirong='"+bankuai.SelectedValue+"'";
						break;

				}
				SqlCommand ccc=new SqlCommand(sss,monn);
				SqlDataReader mmm=ccc.ExecuteReader();
				mmm.Read();
				if(mmm.GetValue(0).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();
						switch(Convert.ToUInt32(jiebie.SelectedValue.ToString()))
						{
							case 1:
								mysql2="update ancestor set banzhu='"+name1.Text.Trim()+"' where neirong='"+bankuai.SelectedValue.ToString()+"'";
								break;
							case 2:
								mysql2="update ancestor set banzhu1='"+name1.Text.Trim()+"' where neirong='"+bankuai.SelectedValue.ToString()+"'";
								break;
							case 3:
								mysql2="update ancestor set banzhu2='"+name1.Text.Trim()+"' where neirong='"+bankuai.SelectedValue.ToString()+"'";
								break;
							case 4:
								mysql2="update ancestor set banzhu3='"+name1.Text.Trim()+"' where neirong='"+bankuai.SelectedValue.ToString()+"'";
								break;
							case 5:
								mysql2="update ancestor set banzhu4='"+name1.Text.Trim()+"' where neirong='"+bankuai.SelectedValue.ToString()+"'";
								break;
						}
						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();
							string sqlins="INSERT INTO duanxiaoxi(neirong,faqiren,jieshoufang,shijian,zhuti) VALUES('   恭喜您成为海纳百川<<"+bankuai.SelectedValue.ToString()+">>新任版主。海纳百川建站资讯网于2005年2月9日发布，海纳百川全体管理员欢迎您的加盟，海纳百川的每一步成长都离不开您的参与和支持。我们相信因您的加入，海纳百川的成长会更快。让我们一起共建海纳百川吧！','free911','"+name1.Text.Trim()+"','"+time+"','欢迎您加入海纳百川管理团队')";
							command2= new SqlCommand(sqlins,monn);
							try
							{
								command2.ExecuteNonQuery();
							}
							catch{}
							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>");
					}
					
				}
				
				
			}
		}
	}
}
