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 myhuifu.
	/// </summary>
	public class myhuifu : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlTable table_1;
		protected  const int  Record_Per_Page=40; //'定义每一页显示的记录数
		protected int nRecCount ; //保存记录总数
		protected int nPageCount;  //保存总共的数据页面数目
		protected int nPage;     //存放要浏览当前数据页面号
		protected int nStart ; // 存放当前页面的起始记录序号
		protected int nEnd;     //存放当前页面的终止记录序号
		protected int  nPageEnd;  //存储当前页面的最后一面的序号 

		protected System.Data.SqlClient.SqlConnection myconn;
		protected System.Data.SqlClient.SqlCommand mycomm;
		protected System.Data.SqlClient.SqlDataReader reader;
		protected System.Data.SqlClient.SqlDataAdapter mydda;
		HtmlTableRow myrow;
		HtmlTableCell mycell;
		string h_yonghuming,h_shijian;
		
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				string yonghuming=Request.Cookies["free_huiyuan"].Value.ToString();
				if(yonghuming=="dengchu")
				{
					Response.Redirect("error.aspx");
				}
			}
			catch 
			{
				Response.Redirect("error.aspx");
			}
			myconn=new SqlConnection(Session["str_connection"].ToString());
			string sqlselect="SELECT  islock,isceam,isfix, zhuti, yonghuming, postid, neirong, shijian,ancestor_id FROM posts WHERE (postid in (select post_id from huifu where yonghuming='"+Request.Cookies["free_huiyuan"].Value.ToString()+"')) ORDER BY isfix desc ,postid desc";
			mydda=new SqlDataAdapter(sqlselect,myconn);
			myconn.Open();
			DataSet ds=new DataSet(); 
			mydda.Fill(ds,"myzhuti");
			nPage = Convert.ToInt32 ( Request.Params[ "Page"] );//得到当前页号
			try
			{
				nRecCount=ds.Tables["myzhuti"].Rows.Count;//得到数据记录总数
			}
			catch
			{
				nRecCount = 0;
			}
			if (nRecCount > 0)
				// 确定数据记录要显示的页面数
				nPageCount = nRecCount/Record_Per_Page;
			if (nRecCount%Record_Per_Page> 0 )
				nPageCount += 1;
			//确认浏览命令中的页面参数是否越界，如果越界则重置页面序号
			if( nPage<1 )nPage = 1 ;
			if ( nPage > nPageCount) nPage = nPageCount ;
			//确认当前页面的开始记录和终止记录
			nStart = Record_Per_Page *  ( nPage - 1 );
			nEnd = nStart + Record_Per_Page - 1;
			if (nEnd > nRecCount - 1 )    nEnd = nRecCount - 1;
			//////////////////////////////////////////////////////////
			/////////////////////////////////////////////////////////
			if(ds.Tables["myzhuti"].Rows.Count>0)
			{   
				
				for (int i=nStart;i<=nEnd;i++)
				{
					myrow=new HtmlTableRow();
					mycell=new HtmlTableCell();
					mycell.Align="center";
					mycell.Height="25px";
					string addcell=null;
					if(int.Parse(ds.Tables["myzhuti"].Rows[i]["islock"].ToString())==1)
						addcell="<TD class='tablebody1'Align='center' height='20'><IMG align='absmiddle' alt='锁定主题' src='./images/lockfolder.gif'></td>";
					else if(int.Parse(ds.Tables["myzhuti"].Rows[i]["isceam"].ToString())==1)
						addcell="<TD class='tablebody1'Align='center' height='20'><IMG align='absmiddle' alt='回复数超过1000的主题' src='./images/hotfolder.gif'></td>";
					else if(int.Parse(ds.Tables["myzhuti"].Rows[i]["isceam"].ToString())==2)
						addcell="<TD class='tablebody1'Align='center' height='20'><IMG align='absmiddle' alt='精华帖子' src='./images/isbest.gif'></td>";
					else if(int.Parse(ds.Tables["myzhuti"].Rows[i]["isceam"].ToString())==3)
						addcell="<TD class='tablebody1'Align='center' height='20'><IMG align='absmiddle' alt='投票帖子' src='./images/closedb.gif'></td>";
					else if(int.Parse(ds.Tables["myzhuti"].Rows[i]["isfix"].ToString())==1)
						addcell="<TD class='tablebody1'Align='center' height='20'><IMG align='absmiddle' alt='固顶主题' src='./images/istop.gif'></td>";
					else if(int.Parse(ds.Tables["myzhuti"].Rows[i]["isfix"].ToString())==2)
						addcell="<TD class='tablebody1'Align='center' height='20'><IMG align='absmiddle' alt='总固顶主题' src='./images/ztop.gif'></td>";
					else
						addcell="<TD class='tablebody1'Align='center' height='20'><IMG align='absmiddle'alt='开放主题' src='./images/folder.gif'></td>";
					
					////////////////////////////////////////////////////////////////////////////////
					addcell=addcell+"<TD   class='tablebody2' height='20'><IMG runat='sever' style='CURSOR: hand'  src='./images/plus.gif'>"+
						"<A BgColor='#ffffff ' href='huifu.aspx?post_id="+int.Parse(ds.Tables["myzhuti"].Rows[i]["postid"].ToString())+"&amp;ancestor_id="+ds.Tables["myzhuti"].Rows[i]["ancestor_id"]+"&amp;Page=1'><font face='宋体'>"+ds.Tables["myzhuti"].Rows[i]["zhuti"].ToString()+"</font></A></td>";
					addcell=addcell+"<TD class='tablebody1'Align='center' height='20'>"+ds.Tables["myzhuti"].Rows[i]["yonghuming"].ToString()+"</td>";
					//
					sqlselect="SELECT count(*)as huifushu FROM huifu where post_id="+int.Parse(ds.Tables["myzhuti"].Rows[i]["postid"].ToString())+" ";
					mydda= new System.Data.SqlClient.SqlDataAdapter(sqlselect,myconn);
					DataTable mytable=new DataTable();
					mydda.Fill(mytable);
					mytable.TableName="huifushu";					
					addcell=addcell+"<TD Align='center' class='tablebody2' height='20'>"+mytable.Rows[0]["huifushu"]+"</td>";
					///////////////////////////////////////////////////////////////////////////////////
					sqlselect="SELECT top 1  yonghuming, shijian FROM huifu where post_id="+int.Parse(ds.Tables["myzhuti"].Rows[i]["postid"].ToString())+" order by huifu_id desc";
					mydda= new System.Data.SqlClient.SqlDataAdapter(sqlselect,myconn);
					DataTable mytable1=new DataTable();
					mydda.Fill(mytable1);
					mytable1.TableName="huifu";
					try
					{
						h_yonghuming=mytable1.Rows[0]["yonghuming"].ToString();
						h_shijian=mytable1.Rows[0]["shijian"].ToString();
						
					}
					catch
					{
						h_yonghuming=null;
						h_shijian=null;				
					}
					addcell=addcell+"<TD class='tablebody1'Align='center' height='20'><A  Align='center'>"+h_shijian+"</A>"+
						"&nbsp;<FONT color='#ff0000'>|</FONT>&nbsp;<A  Align='center' href='hyziliaochakan.aspx?UserId="+h_yonghuming+"' target'_blank'>"+h_yonghuming+"</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'>您还参与主题</td>"));
				myrow.Cells.Add(mycell);
				table_1.Rows.Add(myrow);
			}
		
			/////////////////////////////////
			Session["npage"]=nPage;
			Session["nPageCount"]=nPageCount;
			Session["Record_Per_Page"]=Record_Per_Page;
			Session["nRecCount"]=nRecCount;
			string daohang2;
			string daohang3;
			
			
			daohang2="&nbsp;<A href='myzhuti.aspx?name="+Request.Cookies["free_huiyuan"].Value.ToString()+"&nbsp;Page="+(nPage-1)+"'><FONT  color='#0000ff'>上一页</FONT> </A>";
			daohang3="&nbsp;<A href='myzhuti.aspx?name="+Request.Cookies["free_huiyuan"].Value.ToString()+"&nbsp;Page="+(nPage+1)+"'><FONT  color='#0000ff'>下一页</FONT></A> ";
			if( nPage==1 || nPage==0)
			{
				daohang2="上一页";
			}
			if(nPage==nPageCount)
			{
				daohang3="&nbsp;下一页";
			}
			Session["fenye"]=daohang2+daohang3;
				
		}


		#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.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
