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 zhuti.
	/// </summary>
	public class zhuti : System.Web.UI.Page
	{
		protected System.Data.SqlClient.SqlDataAdapter mydada_1;
		protected System.Data.SqlClient.SqlCommand my_selec_comm;
		protected System.Data.SqlClient.SqlConnection my_conn_1;
		protected System.Web.UI.WebControls.DropDownList DropDownList1;
		int i=0;
		HtmlTableRow myrow;
		HtmlTableCell mycell;
		protected System.Data.DataTable mytable;
		protected System.Data.DataTable mytable_2;
		protected System.Data.DataTable mytable_3;
		protected System.Web.UI.HtmlControls.HtmlInputText page_shu;


		protected  const int  Record_Per_Page=20; //'定义每一页显示的记录数
		protected int nRecCount ; //保存记录总数
		//		protected int nRecrt;   //存放当前页面的起始记录序号
		//		protected int nEndCount;        //保存记录总数
		protected int nPageCount;  //保存总共的数据页面数目
		protected int nPage=1;     //存放要浏览当前数据页面号
		protected int nStart ; // 存放当前页面的起始记录序号
		protected int nEnd;     //存放当前页面的终止记录序号
		protected int  nPageEnd;
		protected System.Web.UI.HtmlControls.HtmlTable Table2;
		protected System.Web.UI.HtmlControls.HtmlTable Table1;
		protected System.Web.UI.WebControls.ListBox ListBox1;
		protected System.Web.UI.HtmlControls.HtmlTable table;
		protected System.Web.UI.HtmlControls.HtmlTable table_1;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Button sou;  //存储当前页面的最后一面的序号
		protected string  ances;
		
		

		
		protected  void Get_zhuti()
		{
			string  sqlstr;
			Session["ancestor_id"]=Request.Params["ancestor_id"];
			ances=Request.Params["ancestor_id"];
			Session["an"]=ances;
			nPage = Convert.ToInt32 ( Request.Params[ "Page"] );//得到当前页号
			sqlstr="SELECT  lujing_pic,islock,isceam,isfix, zhuti, yonghuming, postid, neirong, shijian,renqi FROM posts WHERE (ancestor_id = "+ances+") ORDER BY isfix desc ,postid desc";
			my_conn_1.Open();
			//Try
			mydada_1= new System.Data.SqlClient.SqlDataAdapter(sqlstr,my_conn_1);
			mytable=new DataTable();
			mydada_1.Fill(mytable);
			mytable.TableName="post";
			my_conn_1.Close();
			//			nPage = Convert.ToInt32 ( Request.Params[ "Page"] );//得到当前页号
			nRecCount=mytable.Rows.Count;//得到数据记录总数
			//			Catch ( System.Exception e)
			//		{
			//			Response.Write("错误信息：<b >" + e.Message + "</b > <p > ");
			//			nRecCount = 0;
			//		}
			if (nRecCount > 0)
				// 确定数据记录要显示的页面数
				nPageCount = nRecCount/Record_Per_Page;
			if (nRecCount%Record_Per_Page> 0 )
				nPageCount += 1;
			if(nPageCount==0)
				nPageCount=1;
			//确认浏览命令中的页面参数是否越界，如果越界则重置页面序号
			if( nPage<1 )nPage = 0 ;
			if ( nPage > nPageCount) nPage = nPageCount ;
			//确认当前页面的开始记录和终止记录
			nStart = Record_Per_Page *  ( nPage - 1 );
			nEnd = nStart + Record_Per_Page - 1;
			if (nEnd > nRecCount - 1 )    nEnd = nRecCount - 1;
		}
		protected  void Get_huifu()
		{
			string  str ;
			my_conn_1.Open();
			str="SELECT top 1 neirong, huifu_id, yonghuming, shijian FROM huifu where post_id="+int.Parse(mytable.Rows[i]["postid"].ToString())+" order by huifu_id desc";
			mydada_1= new System.Data.SqlClient.SqlDataAdapter(str,my_conn_1);
			mytable_2=new DataTable();
			mydada_1.Fill(mytable_2);
			mytable_2.TableName="huihu";
			my_conn_1.Close();
		}
		protected  void Get_huifushu()
		{
			string  str ;
			my_conn_1.Open();
			str="SELECT count(*)as huifushu FROM huifu where post_id="+int.Parse(mytable.Rows[i]["postid"].ToString())+" group by post_id";
			mydada_1= new System.Data.SqlClient.SqlDataAdapter(str,my_conn_1);
			mytable_3=new DataTable();
			mydada_1.Fill(mytable_3);
			mytable_3.TableName="huifushu";
			my_conn_1.Close();

		}
		
		protected  string  Set_daohang()
		{
			string daohang;
			string daohang1;
			string daohang2;
			string daohang3;
			string daohang4;
			string daohang5=null;
			daohang1="<A href='zhuti.aspx?ancestor_id="+ances+"&Page=1'><FONT color='#0000f0'>首页</FONT></A>";
			daohang2="&nbsp;<A href='zhuti.aspx?ancestor_id="+ances+"&Page="+(nPage-1)+"'><FONT  color='#0000ff'>上一页</FONT></A>";
			int l;
			l=(nPage-1)/10;
			string fenye;
			for(i=l*10+1;i<=(l+1)*10&&i<=nPageCount;i++)
			{
				if(i==nPage)	
					fenye="<FONT color='red'>"+i+"</FONT>";
				else
					fenye="<FONT color='#0000ff'>"+i+"</FONT>";

				daohang5+="&nbsp;<A href='zhuti.aspx?ancestor_id="+ances+"&Page="+i+"'>"+fenye+"</A>";
			
			} 
			daohang3="&nbsp;<A href='zhuti.aspx?ancestor_id="+ances+"&Page="+(nPage+1)+"'><FONT  color='#0000ff'>下一页</FONT></A> ";
			daohang4="&nbsp;<A href='zhuti.aspx?ancestor_id="+ances+"&Page="+nPageCount+"'><FONT  color='#0000ff'>尾页</FONT></A> "; 
			if( nPage==1)
			{
				daohang1="首页";
				daohang2="&nbsp;上一页";
			}
			if(nPage==nPageCount)
			{
				daohang3="&nbsp;下一页";
				daohang4="&nbsp;尾页";
			}

			daohang=daohang1+daohang2+daohang5+daohang3+daohang4;
			return daohang;
		}
		private void Page_Load(object sender, System.EventArgs e)
		{
			my_conn_1=new SqlConnection(Session["str_connection"].ToString());
			my_conn_1.Open();
			SqlCommand cmd=new SqlCommand("select neirong from ancestor where ancestor_id='"+Request.Params["ancestor_id"].ToString()+"'",my_conn_1);
			SqlDataReader r;
			r=cmd.ExecuteReader();
			if(r.Read())
			{
				Session["title"]="";
				Session["title"]=r["neirong"].ToString().Trim();
				Session["leibie"]=r["neirong"].ToString().Trim();
			}
			r.Close();
			string str;
			ances=Request.Params["ancestor_id"];
			Session["an"]=ances;
			/////nPage = Convert.ToInt32 ( Request.Params[ "Page"] );//得到当前页号
			str="SELECT * FROM posts WHERE (ancestor_id = "+ances+") ";
			SqlCommand mycomm=new SqlCommand(str,my_conn_1);
			SqlDataReader mydra;
			
			mydra=mycomm.ExecuteReader();
			if(mydra.Read())
			{
				mydra.Close();
				my_conn_1.Close();
				Get_zhuti();
				Session["title"]=Session["title"]+"  "+mytable.Rows[i]["zhuti"].ToString();
				for (i=nStart;i<=nEnd;i++)
				{
					Get_huifu();
					string h_neirong,h_yonghuming,h_shijian;
					int h_id;
					if(mytable_2.Rows.Count==0)
					{
						h_neirong=null;
						h_yonghuming=null;
						h_shijian="<font color='#888888'>尚无回复 </font>";;				
						h_id=-1;
					}
					else
					{
						
						h_neirong=mytable_2.Rows[0]["yonghuming"].ToString();
						h_yonghuming=mytable_2.Rows[0]["yonghuming"].ToString();
						h_shijian=Convert.ToDateTime(mytable_2.Rows[0]["shijian"].ToString()).ToShortDateString();
						if(h_shijian==DateTime.Now.ToShortDateString())
							h_shijian="<font color='#ff0000'>"+h_shijian+"</font>";
						h_id=int.Parse(mytable_2.Rows[0]["huifu_id"].ToString());
					}

								
				
					myrow=new HtmlTableRow();
					mycell=new HtmlTableCell();
					mycell.Align="center";
					mycell.Height="25px";
					string addcell=null;
					if(int.Parse(mytable.Rows[i]["islock"].ToString())==1)
						addcell="<TD class='tablebody2'Align='center' height='20'><IMG align='absmiddle' alt='锁定主题' src='./images/lockfolder.gif'></td>";
					else if(int.Parse(mytable.Rows[i]["isceam"].ToString())==1)
						addcell="<TD class='tablebody2'Align='center' height='20'><IMG align='absmiddle' alt='回复数超过1000的主题' src='./images/hotfolder.gif'></td>";
					else if(int.Parse(mytable.Rows[i]["isceam"].ToString())==2)
						addcell="<TD class='tablebody2'Align='center' height='20'><IMG align='absmiddle' alt='精华帖子' src='./images/isbest.gif'></td>";
					else if(int.Parse(mytable.Rows[i]["isceam"].ToString())==3)
						addcell="<TD class='tablebody2'Align='center' height='20'><IMG align='absmiddle' alt='投票帖子' src='./images/closedb.gif'></td>";
					else if(int.Parse(mytable.Rows[i]["isfix"].ToString())==1)
						addcell="<TD class='tablebody2'Align='center' height='20'><IMG align='absmiddle' alt='固顶主题' src='./images/istop.gif'></td>";
					else if(int.Parse(mytable.Rows[i]["isfix"].ToString())==2)
						addcell="<TD class='tablebody2'Align='center' height='20'><IMG align='absmiddle' alt='总固顶主题' src='./images/ztop.gif'></td>";
					else
						addcell="<TD class='tablebody2'Align='center' height='20'><IMG align='absmiddle'alt='开放主题' src='./images/folder.gif'></td>";
					addcell=addcell+"<TD  class='tablebody1' height='20'><IMG runat='sever' style='CURSOR: hand'  src='./images/plus.gif'><IMG src="+mytable.Rows[i]["lujing_pic"].ToString()+" align=absmiddle>"+
						"<A BgColor='#ffffff ' title='《"+mytable.Rows[i]["zhuti"].ToString()+"》作者："+mytable.Rows[i]["yonghuming"].ToString()+" 发表于："+mytable.Rows[i]["shijian"].ToString()+"最后跟贴："+h_neirong+"' "+
						"href='huifu.aspx?post_id="+int.Parse(mytable.Rows[i]["postid"].ToString())+"&amp;ancestor_id="+Request.Params["ancestor_id"]+"&amp;Page=1'><font face='宋体'>"+mytable.Rows[i]["zhuti"].ToString()+"</font></A></td>";
					addcell=addcell+"<TD class='tablebody2'Align='center' height='20'><A href='hyziliaochakan.aspx?UserId="+mytable.Rows[i]["yonghuming"].ToString()+"' target='_blank'>"+mytable.Rows[i]["yonghuming"].ToString()+"</A></td>";
					//
					Get_huifushu();
					string h_huifushu;
					if(mytable_2.Rows.Count==0)
						h_huifushu="0";
					else 
						h_huifushu=mytable_3.Rows[0]["huifushu"].ToString();
					addcell=addcell+"<TD class='tablebody1' Align='center' height='20'>"+h_huifushu+" | "+mytable.Rows[i]["renqi"].ToString()+"</td>";
					addcell=addcell+"<TD class='tablebody2'Align='left' height='20'>&nbsp;&nbsp;"+h_shijian+""+
						"&nbsp;<FONT color='#ff0000'>|</FONT>&nbsp;<A  Align='center' title='点击查看作者资料'  href='hyziliaochakan.aspx?UserId="+h_neirong+"' target'_blank'>"+h_neirong+"</A></td>";
					mycell.Controls.Add(new LiteralControl (""+addcell+""));
					myrow.Cells.Add(mycell);
					table_1.Rows.Add(myrow);		
				
				}
			}
			else 
			{
				myrow=new HtmlTableRow();
				mycell=new HtmlTableCell();
				mycell.Align="center";
				mycell.Height="25px";
				mycell.ColSpan=1;
				mycell.Controls.Add(new LiteralControl ("<TD class='tablebody1' ColSpan=5 Align='center' height='20'><font color='red'>暂无新主题</font></td>"));
				myrow.Cells.Add(mycell);
				table_1.Rows.Add(myrow);
				mydra.Close();
				my_conn_1.Close();
				
			}
			Session["npage"]="";
			Session["nPageCount"]="";
			Session["Record_Per_Page"]="";
			Session["nRecCount"]="";
			Session["fenye"]="";

			Session["npage"]=nPage;
			Session["nPageCount"]=nPageCount;
			Session["Record_Per_Page"]=Record_Per_Page;
			Session["nRecCount"]=nRecCount;
			Session["fenye"]=Set_daohang();
		}
		#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.sou.Click += new System.EventHandler(this.sou_Click);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		private void sou_Click(object sender, System.EventArgs e)
		{
			try
			{
				int i=Convert.ToInt32(TextBox1.Text);
				if(Convert.ToInt32(TextBox1.Text)>nPageCount || Convert.ToInt32(TextBox1.Text)<=1)
				{
					TextBox1.Text="1";
					Response.Write("<script>alert('请输入1到"+nPageCount+"之间的数')</script>");

				}
				else
				{
					TextBox1.Text="";
					Response.Redirect("zhuti.aspx?ancestor_id="+ances+"&Page="+i+"");
				}
			}
			catch{};

		
		}

	}
}
