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.Threading;

namespace WebApplication2.Admin
{
	/// <summary>
	/// AdvCount 的摘要说明。
	/// </summary>
	public partial class AdvCount : 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("12") || role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
					{
					
						cmd_str=" where AdvCount.WebsiteId=Website.id and AdvCount.AdvListId=AdvList.id";
						this.Label3.Visible=false;
                        begdate.Value = DateTime.Now.ToShortDateString();
                        enddate.Value = DateTime.Now.ToShortDateString();
                        ddlbind();
						Bind(1);
						SB.ADOHelper help=new SB.ADOHelper();	
						SqlDataReader rd=help.getReader("select sum(AdvCount.Quantity)as hitcount,sum(AdvCount.ShowQuantity)as appcount,sum(AdvCount.money)as momeycount from AdvCount,Website,AdvList"+cmd_str);
						if(rd.Read())
						{
							this.Label4.Text=rd["hitcount"].ToString();
							this.Label5.Text=rd["appcount"].ToString();
							this.Label6.Text=rd["momeycount"].ToString();	
						}
						rd.Close();
                        if (help.conn.State == ConnectionState.Open)
                        {
                            help.conn.Close();
                         }
					
					}
					else
					{Response.Redirect("NoAuthor.aspx");}
				}
			}
			// 在此处放置用户代码以初始化页面
		}
        private void ddlbind()
        {
            Dropdown3.Items.Add("=选择=");
            Dropdown3.Items[0].Value = " is not null ";
            //dropdown4.Items.Add("=选择=");
            //dropdown4.Items[0].Value = " is not null ";
            SB.ADOHelper help = new SB.ADOHelper();
            if (help.conn.State == ConnectionState.Closed)
            { help.conn.Open(); }
            SqlDataReader rd=null;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select id,AdvTitle from AdvList";
            cmd.Connection = help.conn;
            rd = cmd.ExecuteReader();
            int index=1;
            while (rd.Read())
            {   
                Dropdown3.Items.Add(rd["AdvTitle"].ToString());
                Dropdown3.Items[index].Value ="='"+rd["id"].ToString()+"'";
                index++;
            }
            rd.Close();
            help.conn.Close();
        }
		private void Bind(int record)
		{//Response.Write("select * from AdvCount,Website,AdvList "+cmd_str+" order by AdvCount.id desc");
			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 AdvCount,Website,AdvList "+cmd_str+" order by AdvCount.id desc",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 AdvCount,Website,AdvList "+cmd_str,conn);
		
			recordcount=(int)cmd.ExecuteScalar();

			page=((recordcount%maxrecord)==0)?(recordcount/maxrecord):(recordcount/maxrecord+1);
            conn.Close();
			
			this.Label2.Text=page.ToString();
			if(this.DataGrid1.Items.Count>0)
			{
				this.Label1.Text=record.ToString();
				this.Label7.Text=recordcount.ToString();
				this.DropDownList2.Items.Clear();
				for(int i=1;i<=int.Parse(this.Label2.Text);i++)
				{
					this.DropDownList2.Items.Add(i.ToString());
				}
				this.DropDownList2.SelectedIndex=int.Parse(this.Label1.Text)-1;
				
			}
			else
			{
				this.Label1.Text="0";
				this.Label7.Text="0";
				this.DropDownList2.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;
			dataset.Clear();
					
		}
		public string GetTitle(string id,string table,string title)
		{
			string titles="无";
			SB.ADOHelper help=new SB.ADOHelper();
			if(help.conn.State==ConnectionState.Closed)
			{
				help.conn.Open();
			}
			SqlCommand cmd=new SqlCommand("select "+title+" from "+table+" where id='"+id+"'",help.conn);
			SqlDataReader rd=cmd.ExecuteReader();
			if(rd.Read())
			{
				titles=rd[title].ToString();
			}
		    
			rd.Close();
			help.conn.Close();
			return titles;
		}
		private 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);
		}

		#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);
            this.Button1.Click += new EventHandler(Button1_Click);
            this.LinkButton1.Click+=new EventHandler(gopage);
            this.LinkButton2.Click += new EventHandler(gopage);
            this.LinkButton3.Click += new EventHandler(gopage);
            this.LinkButton4.Click += new EventHandler(gopage);
            this.DropDownList2.SelectedIndexChanged+=new EventHandler(DropDownList2_SelectedIndexChanged);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
		    string count=SB.ADOHelper.inputText(this.TextBox1.Text.Trim());
            string begindate = begdate.Value.Trim() != "" ? ">='" + begdate.Value + "'" : " is not null ";
            string overdate = enddate.Value.Trim() != "" ? "<='" + enddate.Value + "'" : " is not null ";
			string state=this.DropDownList1.SelectedValue;
            if (RadioButton1.Checked)
            {
                cmd_str = " where WebUserName" + (count==""? " is not null":("='"+count+"'")) + " and AdvList.id" + Dropdown3.SelectedValue + " and  AdvCount.WebsiteId=Website.id and AdvCount.AdvListId=AdvList.id and convert(smalldatetime,convert(varchar(10),advcount.AddTime))" + begindate + " and convert(smalldatetime,convert(varchar(10),advcount.AddTime))" + overdate + "  and IsPayment " + state;
            }
            else
            {
                cmd_str = " where Website.WebsiteName" + (count == "" ? " is not null" : ("='" + count + "'")) + " and AdvList.id" + Dropdown3.SelectedValue + " and  AdvCount.WebsiteId=Website.id and AdvCount.AdvListId=AdvList.id and convert(smalldatetime,convert(varchar(10),advcount.AddTime))" + begindate + " and convert(smalldatetime,convert(varchar(10),advcount.AddTime))" + overdate + "  and IsPayment " + state;
            }
			Bind(1);
			if(this.DataGrid1.Items.Count>0)
			{
				this.Label3.Visible=false;
			}
			else
			{
				this.Label3.Visible=true;
			}
			SB.ADOHelper help=new SB.ADOHelper();	
		SqlDataReader rd=help.getReader("select sum(AdvCount.Quantity)as hitcount,sum(AdvCount.ShowQuantity)as appcount,sum(AdvCount.money)as momeycount from AdvCount,Website,AdvList"+cmd_str);
			if(rd.Read())
			{
				this.Label4.Text=rd["hitcount"].ToString();
				this.Label5.Text=rd["appcount"].ToString();
				this.Label6.Text=rd["momeycount"].ToString();
			}
			rd.Close();
            if (help.conn.State == ConnectionState.Open)
            { help.conn.Close(); }
		}

	

		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();
			if(e.CommandName.ToLower()=="delete")
			{
				string id=Convert.ToString(this.DataGrid1.DataKeys[e.Item.ItemIndex]);
				help.execSql("delete from AdvCount where id='"+id+"'");
                Bind(1);
                if (help.conn.State == ConnectionState.Open)
                { help.conn.Close(); }
			}
			if(e.CommandName.ToLower()=="edit")
			{string id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();            
			Response.Redirect("ModifAdvCount.aspx?id="+id);
				
			}
		}

		private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Bind(int.Parse(this.DropDownList2.SelectedItem.Text));
		}


        //protected void Dropdown3_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //   // dropdown4.Items.Clear();
        //    dropdown4.Items.Add("=选择=");
        //    dropdown4.Items[0].Value = " is not null ";
        //    if(Dropdown3.SelectedIndex>0)
        //    {   
        //        string advlistid = Dropdown3.SelectedValue.ToString();
        //        SB.ADOHelper help = new SB.ADOHelper();
        //        if (help.conn.State == ConnectionState.Closed)
        //        {
        //            help.conn.Open();
        //        }
        //        SqlDataReader rd = null;
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandText = "SELECT WebsiteId,WebsiteName FROM Website,ApplicationAdv where AdvListId" + advlistid + " and Website.id=ApplicationAdv.WebsiteId";
        //        cmd.Connection = help.conn;
        //        rd = cmd.ExecuteReader();
                
        //        int index=1;
        //        while (rd.Read())
        //        {
        //            dropdown4.Items.Add(rd["WebsiteName"].ToString());
        //            dropdown4.Items[index].Value = "='" + rd["WebsiteId"].ToString() + "'";
        //            index++;
        //        }
        //        rd.Close();
        //        help.conn.Close();
                
        //    }
        //}
}
}
