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 WebApplication2.Admin
{
	/// <summary>
	/// WebList 的摘要说明。
	/// </summary>
	public partial class WebList : System.Web.UI.Page
	{
		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("4") || role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
					{
					
						if(Request.Params["webid"]==null||Request.Params["webid"].ToString()=="")
						{
							cmd_str="";
							this.Label3.Visible=false;
							Bind(1);
						}
						else
						{
							cmd_str=" where id='"+Request.Params["webid"].ToString()+"'";
							this.Label3.Visible=false;
							Bind(1);
					
						}
					
					}
					else{Response.Redirect("NoAuthor.aspx");}
				}
			}
			// 在此处放置用户代码以初始化页面
		}
		private void Bind(int record)
		{
			int maxrecord=20;
			int recordcount;
			int page;
			int startrecord=(record-1)*maxrecord;
			string connstr=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection conn=new SqlConnection (connstr);
			SqlDataAdapter da=new SqlDataAdapter("select * from Website"+cmd_str+" order by id desc",conn);
			DataSet dataset=new DataSet();
			da.Fill(dataset,startrecord,maxrecord,"websit");
			this.DataGrid1.DataSource=dataset;
			this.DataGrid1.DataBind();
			conn.Close();

			SqlCommand cmd=new SqlCommand("select count(*) from Website"+cmd_str,conn);
			conn.Open();
			recordcount=(int)cmd.ExecuteScalar();

			page=((recordcount%maxrecord)==0)?(recordcount/maxrecord):(recordcount/maxrecord+1);
			
			this.Label1.Text=record.ToString();
			this.Label2.Text=page.ToString();
			
			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);
		}
		public string WebType(int type)
		{
			switch(type)
			{
				case 1:return "娱乐休闲";
				case 2:return "地方门户";
				case 3:return "下载资源";
				case 4:return "行业资讯";
				case 5:return "影视宽带";
				case 6:return "论坛博客";
				case 7:return "网址导航";
			    case 8:return "聊天交友";
				case 9:return "壁纸图片";
				case 10:return "教育培训";
				case 11:return "医疗保健";
				case 12:return "衣食住行";
				case 13:return "军事国防";
				case 14:return "其他站点";
				default:return " ";
					
			}
		}
		public string Stars(int star)
		{string stars="";
			for(int i=0;i<=star;i++)
			{
              stars+="☆";
			}
        return stars;
		}

		#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.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

		}
		#endregion

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			string webstype=this.DropDownList1.SelectedValue.ToString();
			string webstate=this.DropDownList2.SelectedValue.ToString();
			string account=SB.ADOHelper.inputText(this.TextBox1.Text);
			cmd_str=" where WebsiteType "+webstype+" and Appearance "+webstate+" and UserName like '%"+account+"%'";
			Bind(1);
			if(this.DataGrid1.Items.Count>0)
			{
				this.Label3.Visible=false;
			}
			else
			{
				this.Label3.Visible=true;
			}
		}

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			 ImageButton btn=(ImageButton)e.Item.FindControl("ImageButton2");
			if(btn!=null)
			{
				btn.Attributes.Add("onclick","return confirm('确认要删除吗！？');");
			
			}
			
		}

		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{SB.ADOHelper help=new SB.ADOHelper();
			
			string id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); 
			if(e.CommandName.ToLower()=="delete")
			{
				help.execSql("delete from Website where id='"+id+"'");
					Bind(1);
			}
			if(e.CommandName.ToLower()=="edit")
			{
				Response.Redirect("ModifWebList.aspx?id="+id);
			}

		}

		protected void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}
