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;
using System.Text;


namespace temp
{
	/// <summary>
	/// UserSearch 的摘要说明。
	/// </summary>
	public partial class UserSearchweb : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Button Button1;
	private static string cmdstr;
		public string cmd_str
		{
			get
			{
				return cmdstr.ToString();
			}
			set
			{
				cmdstr=value;
			}
		}
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				if(this.Session["USERNAME"]==null||this.Session["Role"]==null||this.Session["USERNAME"].ToString()==""||this.Session["Role"].ToString()=="")
				{
					Response.Redirect("login.aspx");
				}
				else
				{
					string [] role=this.Session["Role"].ToString().Split('#');
					bool exists=false;
					for(int i=0;i<role.Length;i++)
					{
						if(role[i].Equals("3")||role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
					{	
						this.cmd_str="";
						this.Label3.Visible=false;
						Bind(1, " order by id desc");
						dpdatebind();
						pageeEnable();				
					}
					else
					{
						Response.Redirect("NoAuthor.aspx");
					}
				}

			}

				// 在此处放置用户代码以初始化页面
		}
		private void pageeEnable()
		{
			if(!(this.DataGrid1.Items.Count>0))
			{
				this.LinkButton1.Enabled=false;
				this.LinkButton2.Enabled=false;
				this.LinkButton3.Enabled=false;
				this.LinkButton4.Enabled=false;
			}
		}
//
		private void Bind(int record,string dbname)
		{
			
			
			
			
			int maxrecord=20;
			int recordcount;
			int page;
			int startrecord=(record-1)*maxrecord;

		
			string connstr=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn=new SqlConnection (connstr);
            conn.Open();
			SqlDataAdapter da=new SqlDataAdapter("select * from WebsiteHost"+dbname,conn);
			DataSet dataset=new DataSet();
			da.Fill(dataset,startrecord,maxrecord,"websit");
			this.DataGrid1.DataSource=dataset;
			this.DataGrid1.DataBind();
			SqlCommand cmd=new SqlCommand("select count(*) from WebsiteHost"+cmd_str,conn);
			recordcount=(int)cmd.ExecuteScalar();
            conn.Close();
			page=((recordcount%maxrecord)==0)?(recordcount/maxrecord):(recordcount/maxrecord+1);
			this.Label2.Text=page.ToString();
			if(this.DataGrid1.Items.Count>0)
			{
				this.Label1.Text=record.ToString();
				this.Label4.Text=recordcount.ToString();
				this.DropDownList1.Items.Clear();
				for(int i=1;i<=int.Parse(this.Label2.Text);i++)
				{
					this.DropDownList1.Items.Add(i.ToString());
				}
				this.DropDownList1.SelectedIndex=int.Parse(this.Label1.Text)-1;
				
			}
			else
			{
				this.Label1.Text="0";
				this.Label4.Text="0";
				this.DropDownList1.Items.Clear();
			}
			this.LinkButton1.Enabled=record<=1?false:true;
			this.LinkButton2.Enabled=record<=1?false:true;
			this.LinkButton3.Enabled=record<page?true:false;
			this.LinkButton4.Enabled=record<page?true:false;

					
		}

		protected void gopage(object sender, System.EventArgs e)
		{
			int prepage=Convert.ToInt32(this.Label1.Text.ToString());
		
			int nextpage=Convert.ToInt32(this.Label2.Text.ToString());
			string cmdname=((LinkButton)sender).CommandArgument.ToString();
			switch(cmdname)
			{
				case "pre":
					if(prepage>1)
						prepage--;break;
				case "next":
					if(prepage<nextpage)
						prepage++;break;
				case "frist":prepage=1;break;
				case "end":prepage=nextpage;break;
			}
			
			Bind(prepage,this.cmd_str.ToString());
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
			this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
            this.Button2.Click += new EventHandler(Button2_Click);
            this.DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
		}
		#endregion

		
		private bool isnumber(string str)
		{int leng=str.Length;
			for(int i=0;i<leng;i++)
			{
				if(!char.IsNumber(str[i]))
				{
					return false;
				}
			}
			return true;

		}
        protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Bind(int.Parse(this.DropDownList1.SelectedItem.Text), cmd_str);
        }
		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			SB.ADOHelper help=new SB.ADOHelper();
			if(e.CommandName.ToLower()=="delete")
            {
               
                    int index = int.Parse(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
                    try
                    {
                        help.execSql("delete from WebsiteHost where id='" + index + "'");
                    }
                    catch
                    {
                        Response.Write("<script>alert('数据存在关联不能被删除！')</script>"); return;
                    }
                    Bind(1, cmd_str.ToString());
               
			}
			else
			{
				if(e.CommandName.ToLower()=="modif")
				{
					string Name=help.getDataset("select UserName from WebsiteHost where id='"+this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()+"'").Tables[0].Rows[0]["UserName"].ToString();

					Response.Redirect("ModifyWebUserDetail.aspx?name="+Name);
				}
			}		
		}

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			ImageButton deleteBtn=(ImageButton)e.Item.FindControl("deleteBtn");
			if(deleteBtn!=null)
			{
				deleteBtn.Attributes.Add("onclick","return confirm('确认要删除吗？');");
				
			}
		}
		private bool haveRecod()
		{
			if(this.DataGrid1.Items.Count>0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			
			Bind(1,cmd_str+" order by "+e.SortExpression+" desc");

			
		}
		private void dpdatebind()
		{
			DateTime dt=DateTime.Now;
			this.dpyear1.Items.Add("--");
			this.dpyear1.Items[0].Value="2004";
			this.dpyear2.Items.Add("--");
			this.dpyear2.Items[0].Value=dt.Year.ToString();
			this.dpmonth1.Items.Add("--");
			this.dpmonth1.Items[0].Value="1";
			this.dpmonth2.Items.Add("--");
			this.dpmonth2.Items[0].Value=dt.Month.ToString();
			this.dpday1.Items.Add("--");
			this.dpday1.Items[0].Value="1";
			this.dpday2.Items.Add("--");
			this.dpday2.Items[0].Value=(dt.Day+1)>31? (dt.Day).ToString():((dt.Day)+1).ToString();
			for(int i=2004;i<=dt.Year;i++)
			{
				this.dpyear1.Items.Add(i.ToString());
				this.dpyear2.Items.Add(i.ToString());
			}
			for(int j=1;j<12;j++)
			{
				this.dpmonth1.Items.Add(j.ToString());
			
				this.dpmonth2.Items.Add(j.ToString());
			
			}
			for(int k=1;k<=31;k++)
			{
				this.dpday1.Items.Add(k.ToString());
		
				this.dpday2.Items.Add(k.ToString());
		
			}

		}

		protected void Button2_Click(object sender, System.EventArgs e)
		{
			SB.ADOHelper help=new SB.ADOHelper();
			string account=SB.ADOHelper.inputText(this.TextBox2.Text.ToString());
			string username=SB.ADOHelper.inputText(this.TextBox4.Text.ToString());
			string usertype=this.dptype.SelectedValue.ToString();
			string appearance=this.dpstate.SelectedValue.ToString();
			string date1=SB.ADOHelper.inputText(this.dpyear1.SelectedValue+"-"+this.dpmonth1.SelectedValue+"-"+this.dpday1.SelectedValue);
			string date2=SB.ADOHelper.inputText(this.dpyear2.SelectedValue+"-"+this.dpmonth2.SelectedValue+"-"+this.dpday2.SelectedValue);
		            cmd_str=" where UserName like '%"+account+"%' and Name like '%"+username+"%'  and UserType "+usertype+" and Appearance "+appearance+" and RegDate>='"+date1+"' and  RegDate<='"+date2+"'";
					Bind(1,cmd_str.ToString() );	
					this.Label3.Visible=haveRecod();
				}
			}	
		}

