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>
	/// AdvFinance 的摘要说明。
	/// </summary>
	public partial class AdvFinance : System.Web.UI.Page
	{
		
		
	    
		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("6") || role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
					{
					
						Bind(1);
				
						if(this.Datagrid1.Items.Count>0)
						{
							this.Label3.Visible=false;
						}
						else
						{
							this.Label3.Visible=true;
						}
					
					}
					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);
            conn.Open();
			SqlDataAdapter da=new SqlDataAdapter("select * from AdvList where AcceptAppearance>0 order by 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 AdvList where AcceptAppearance>0",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);
		}

		#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

		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()=="edit")
			{
			
				Response.Redirect("AdvBalance.aspx?id="+id);
			}
		}

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

		protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Bind(int.Parse(this.DropDownList1.SelectedItem.Text));
		}

		private void Datagrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{   string id;
            SB.ADOHelper help=new SB.ADOHelper();
			for(int i=0;i<this.Datagrid1.Items.Count;i++)
			{
				id=this.Datagrid1.DataKeys[this.Datagrid1.Items[i].ItemIndex].ToString();
				
				SqlDataReader rd=help.getReader("SELECT MAX(AddTime)as AddTime FROM AdvCount where IsPayment=2 and AdvListId='"+id+"'");
				if(rd.Read())
				{
						if(rd["AddTime"].ToString()!="")
				 {
					 this.Datagrid1.Items[i].Cells[7].Text=Convert.ToDateTime(rd["AddTime"].ToString()).ToShortDateString();
					
				 }
				}
				else
				{
					this.Datagrid1.Items[i].Cells[7].Text="无";
					
				}
                rd.Close();
			
			}
            if (help.conn.State == ConnectionState.Open)
            { help.conn.Close(); }
		}

		

	
	
		
	}
}
