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;

namespace DTable_App.DTableBBS
{
	/// <summary>
	/// _default 的摘要说明。
	/// </summary>
	public class _default : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label L_list;
	    public System.Text.StringBuilder strtext=new System.Text.StringBuilder();
		protected System.Web.UI.WebControls.Label L_loginInfo;
		protected System.Web.UI.WebControls.Label L_hys;
		protected System.Web.UI.WebControls.Label L_lastuser;
		protected System.Web.UI.WebControls.Label L_zts;
		protected System.Web.UI.WebControls.Label L_yesday;
		protected System.Web.UI.WebControls.Label L_zs;
		protected System.Web.UI.WebControls.Label L_today;
		protected System.Web.UI.WebControls.Label L_top5;
		protected System.Web.UI.WebControls.Label L_gg;
		public System.Data.DataTable mytable=new DataTable();
		public string returnChildB(string strID)//一个返回子栏目信息的函数
		{
			

			System.Text.StringBuilder  strThisText=new System.Text.StringBuilder();

         
			mytable.DefaultView.RowFilter ="父栏目=" + strID;//筛选本栏目的子栏目记录
			//myview.Sort="排序,id";//排序
			if(mytable.DefaultView.Count<1)
			{
				mytable.DefaultView.RowFilter ="id=" + strID;//筛选本栏目的子栏目记录
			}
			System.Data.SqlClient.SqlCommand mycmd1=new System.Data.SqlClient.SqlCommand("",oconn);
			System.Data.SqlClient.SqlDataReader myreader;

			string strthisDate=System.DateTime.Now.ToShortDateString(); 
		
			for(int i=0;i<mytable.DefaultView.Count;i++)
			{
				string strfid=mytable.DefaultView[i][2].ToString();//父id			
				string strid=mytable.DefaultView[i][0].ToString();//本id
				string strlmmc=mytable.DefaultView[i][1].ToString();//栏目名称
				string strlmjs=mytable.DefaultView[i][3].ToString();//介绍
				string strlmtb=mytable.DefaultView[i][4].ToString();//图标
				string strbz=mytable.DefaultView[i][5].ToString();//版主
				string strtzzs="";//帖子总数
				string strhfzs="";//回复总数
				string strtdtzzs="";//今日帖子总数
				string strtdhfzs="";//今日回复总数
			//	Page.Response.Write(strid + "<br>");

				//开始获得本栏目的信息
				//开始本栏目帖子总数/回复总数,今日的帖子总数,今日的回复总数
				
				mycmd1.CommandText="select top 1 (select count(*) from DTable_BBS a where a.栏目=" + strid + " and a.父id=0 and a.删除=0) as 帖子总数,(select count(*) from DTable_BBS b where b.栏目=" + strid + " and b.父id>0 and b.删除=0) as 回复总数,(select count(*) from DTable_BBS c where c.栏目=" + strid + " and c.父id=0 and c.删除=0 and c.发布时间 between '" + strthisDate + " 0:00:00' and '" + strthisDate + " 23:59:59') as 今日帖子总数,(select count(*) from DTable_BBS d where d.栏目=" + strid + " and d.父id>0 and d.删除=0 and d.发布时间 between '" + strthisDate + " 0:00:00' and '" + strthisDate + " 23:59:59') as  今日回复总数 from DTable_BBS e where e.栏目=" + strid;
               myreader=mycmd1.ExecuteReader();
				if(myreader.Read())
				{
					 strtzzs=myreader.GetValue(0).ToString();//帖子总数
					 strhfzs=myreader.GetValue(1).ToString();//回复总数
					 strtdtzzs=myreader.GetValue(2).ToString();//今日帖子总数
					 strtdhfzs=myreader.GetValue(3).ToString();//今日回复总数

				}
				if(strtzzs=="")
				{
					strtzzs="0";
				}
				if(strhfzs=="")
				{
					strhfzs="0";
				}
				if(strtdtzzs=="")
				{
					strtdtzzs="0";
				}
				if(strtdhfzs=="")
				{
					strtdhfzs="0";
				}

				myreader.Close();
				//开始TOP5帖子记录
				mycmd1.CommandText="select top 5 id,主题,发布人,发布时间,ip地址,点击率,父id from DTable_BBS where 删除=0 and 栏目=" + strid + " order by id desc";
				myreader=mycmd1.ExecuteReader();
				System.Text.StringBuilder strthisTop5Text=new System.Text.StringBuilder();//TOP记录的循环代码

				while(myreader.Read())
				{
					string strthisfid="";
					
					string strdate=myreader.GetDateTime(3).ToString("yyyy-MM-dd");
					if(strdate.ToString()==System.DateTime.Now.ToString("yyyy-MM-dd"))//今天的帖子
					{
						strdate="<font color=#990000>" +myreader.GetSqlDateTime(3).Value.ToString("yy年MM月dd日 HH:mm" ) + "</font>";
					}
					else
					{
						strdate=myreader.GetSqlDateTime(3).Value.ToString("yy年MM月dd日 HH:mm" );
					}
					

					if(myreader.GetValue(6).ToString()!="0")
					{
						strthisfid=myreader.GetValue(6).ToString();
					}
					else
					{
						strthisfid=myreader.GetValue(0).ToString();
					}
					
strthisTop5Text.Append("<tr><td><img src=\"img_bbs/Forum_readme.gif\" width=\"6\" height=\"6\">" + myreader.GetValue(1).ToString() + "<br></td> <td width=\"100\">作者：" + myreader.GetValue(2).ToString() + "</td> <td width=\"170\">日期：" + strdate  + "<a href=\"show.aspx?addnew=true&rMviewid=" + strthisfid + "#" + myreader.GetValue(6).ToString() + "\"><img src=\"img_bbs/lastpost.gif\" width=\"11\" height=\"10\" border=\"0\"></a></td></tr> ");

				}
				if(strthisTop5Text.Length <10)
				{
strthisTop5Text.Append("<tr><td>暂无帖子</td><td>&nbsp;</td><td>&nbsp;</td></tr>");
				}
				myreader.Close();
				if(strlmtb!="")
				{
					strlmtb="<img src=img_bbsSet/" + strlmtb + " width=80>";
				}
				else
				{
					strlmtb="&nbsp;";
				}

				strThisText.Append("<table width=\"100%\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" bordercolor=\"#111111\" bordercolorlight=\"#666688\" bordercolordark=\"#666688\" style=\"border-collapse: collapse\"> <tr> <td width=\"20\" rowspan=\"2\" align=\"center\"><img src=\"img_bbs/nonews.gif\" width=\"24\" height=\"24\"></td> <td> <table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <tr> <td valign=\"top\"><a href=\"list.aspx?DTable_b=" + strid + "\"><font color=#000099><strong>==" + strlmmc + "==</strong></font></a><br><img src=\"img_bbs/Forum_readme.gif\" width=\"10\" height=\"10\"> " + strlmjs + "</td> <td width=\"80\">" + strlmtb + "</td> </tr> </table></td> <td width=\"23%\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td>一共</td> <td><strong>" + strtzzs + "</strong></td> <td>个主题，</td> <td><strong>" + strhfzs + "</strong></td> <td>篇回复</td> </tr> <tr> <td>今日</td> <td><strong>" + strtdtzzs + "</strong></td> <td>个主题，</td> <td><strong>" + strtdhfzs + "</strong></td> <td>篇回复</td> </tr> <tr> <td colspan=\"5\">板主:" + strbz + "</td> </tr> </table> </td> </tr> <tr> <td colspan=\"2\" bgcolor=\"#E9EDF3\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" + strthisTop5Text.ToString() + "</table></td> </tr></table>");


			}

 mycmd1.Dispose();
		
return strThisText.ToString();
		}
public System.Data.SqlClient.SqlConnection oconn=new System.Data.SqlClient.SqlConnection(sharecode.ConnStr);
		private void Page_Load(object sender, System.EventArgs e)
		{
			

			//开始列表
			string stryhm=sharecode.GetSessionValue(sharecode.strUserSName);

			if(stryhm=="")//如果用户没有登录,就显示登录框
			{
					

				stryhm=sharecode.GetCookieValue(sharecode.strUserNameCookieName);//获取历史的用户迷宫那
				string strmm=sharecode.GetCookieValue(sharecode.strPassWordCookieName);//获取历史密码
				string stryzm=sharecode.GetRndPassword(6);
				
				System.Web.HttpContext.Current.Response.Cookies["login_yzm"].Value=stryzm;

				System.Web.HttpContext.Current.Request.Cookies["login_yzm"].Expires =System.DateTime.Now.AddMinutes(20);
				

				L_loginInfo.Text="<span name='welcome' id='welcome'>欢迎" + stryhm + "访问DTable论坛, 如果您还没有注册,请点击<a href=\"../register/\" target=\"_blank\">这里</a></span><br><TABLE id=\"logintable\" border=0 width='100%' cellspacing='5'><form name='formlogin' target=\"loginframe\" method=\"post\" action=\"../register/userlogin.aspx?type=default\"><tr><td> 用户名: <input type=\"text\" size=\"6\" name=\"yhm\" value=\"" + stryhm + "\">&nbsp;&nbsp;密码: <input type=\"password\" name=\"mm\" value=\"" + strmm + "\" size=\"6\">&nbsp;记住密码: <select id=\"ls\" name=\"ls\"><option value=\"0\">每次登录</option><option value=\"7\">1周</option><option value=\"30\">1月</option><option value=\"365\" selected>1年</option></select>&nbsp;<input type=\"hidden\" name=\"yzm\" value=\"" + stryzm + "\"><input type=\"submit\" name=\"userlogin\" value=\"登录\"></td></tr></form></table><IFRAME name=\"loginframe\" src=\"about:blank\" width=\"1\" height=\"0\"></IFRAME>";
			
			}
			else
			{
				L_loginInfo.Text="欢迎[" + stryhm + "]";
				
			}
		
			oconn.Open();
			//开始总数统计
			string strtoday=System.DateTime.Now.ToString("yyyy-MM-dd");
			string stryesday=System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");

			System.Data.SqlClient.SqlCommand mycmdN=new System.Data.SqlClient.SqlCommand("select top 1 (select count(*) from DTable_User) as 用户总数,(select count(*) from DTable_BBS where 删除=0) as 帖子总数,(select count(*) from DTable_BBS where 删除=0 and 父id=0) as 主题总数,(select top 1 用户名 from DTable_user order by id desc) as 最后注册用户,(select count(*) from DTable_BBS where 删除=0 and 发布时间 between '" + strtoday + " 0:00:00' and '" + strtoday + " 23:59:59') as 今日发帖,(select count(*) from DTable_BBS where 删除=0 and 发布时间 between '" + stryesday + " 0:00:00' and '" + stryesday + " 23:59:59') as 昨日发帖 from DTable_user",oconn);
			System.Data.SqlClient.SqlDataReader myreaderN=mycmdN.ExecuteReader();
			if(myreaderN.Read())
			{
               L_zs.Text=myreaderN.GetValue(1).ToString();
				L_hys.Text="<a href=showuser.aspx>" + myreaderN.GetValue(0).ToString() + "</a>";
				L_lastuser.Text="<a href=showuser.aspx?username=" + myreaderN.GetValue(3).ToString() + ">" + myreaderN.GetValue(3).ToString() + "</a>";
				L_zts.Text=myreaderN.GetValue(2).ToString();
				L_yesday.Text=myreaderN.GetValue(5).ToString();
				L_today.Text=myreaderN.GetValue(4).ToString();
				

			}
			myreaderN.Close();
			//开始广播
			mycmdN.CommandText ="select top 5 id,广播主题 from DTable_BBS_info where '" + System.DateTime.Now + "' between 开始时间 and 结束时间 order by id desc";
			myreaderN=mycmdN.ExecuteReader();
			System.Text.StringBuilder strinfo=new System.Text.StringBuilder();

			while(myreaderN.Read())
			{
				strinfo.Append("<a href=showinfo.aspx?id=" + myreaderN.GetValue(0).ToString() + ">" + myreaderN.GetValue(1).ToString() + "</a>&nbsp;&nbsp;&nbsp;");   

			}
			myreaderN.Close();
			//开始Top5记录



			//end info
			
			L_gg.Text =strinfo.ToString();
			mycmdN.Dispose();
			
			System.Data.SqlClient.SqlDataAdapter myAd=new System.Data.SqlClient.SqlDataAdapter("select id,栏目名称,父栏目,栏目介绍,栏目图标,版主 from DTable_BBS_Board order by 父栏目,排序 desc",oconn);
			System.Data.DataSet rs=new DataSet();
			
			myAd.Fill(rs,"dtable_bor");
			mytable=rs.Tables["dtable_bor"];
			rs.Dispose();
			myAd.Dispose();
		
			

			for(int i=0;i<mytable.Rows.Count;i++)//开始主循环
			{
				
				string strfid=mytable.Rows[i][2].ToString();
				string strid=mytable.Rows[i][0].ToString();
				string strlmmc=mytable.Rows[i][1].ToString();
				if(strfid=="0")//如果是顶级别
				{
					
				//	string strlmjs=mytable.Rows[i][3].ToString();
				//	string strlmtb=mytable.Rows[i][4].ToString();
				//	string strbz=mytable.Rows[i][5].ToString();
					if(i>0)
					{
						strtext.Append("<br>");
					}
					strtext.Append("<table width=\"100%\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\" bordercolor=\"#111111\" bordercolorlight=\"#666688\" bordercolordark=\"#666688\" style=\"border-collapse: collapse\"> <tr> <td colspan=\"3\" background=\"img_bbs/bg1.gif\"><table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\"> <tr> <td height=\"18\"><span class=\"style1\">" + strlmmc + "</span></td> </tr> </table></td> </tr></table>");//写入第一层表格,可以通过修改来实现自己需要的样式
					strtext.Append(returnChildB(strid));

				}
				else
				{
				//	strtext.Append(returnChildB("0"));
				}


			}
			oconn.Close();
			oconn.Dispose();
L_list.Text=strtext.ToString();
		//	Page.Response.Cookies[sharecode.strFromUrlSName].Value=Page.Request.Url.AbsoluteUri.ToString();Page.Response.Cookies[sharecode.strFromUrlSName].Expires=System.DateTime.Now.AddMinutes(20); 





		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
