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>
	/// show 的摘要说明。
	/// </summary>
	public class show : System.Web.UI.Page
	{
		protected dtable.dTable DTable1;
		protected System.Web.UI.WebControls.Label L_login;
		protected System.Web.UI.WebControls.Label L_script;
		protected System.Web.UI.WebControls.Label L_list;
		protected System.Web.UI.WebControls.Image Img_addnew;
		//protected System.Web.UI.WebControls.Image Img_search;
		protected System.Web.UI.WebControls.Label L_user;
		protected System.Web.UI.WebControls.Label L_title;
		
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label L_read;
		protected System.Web.UI.WebControls.Label L_hideinput;
		protected System.Web.UI.WebControls.Label L_bz;
		protected System.Web.UI.WebControls.Label L_bbsname;
		protected HtmlGenericControl thistitle;
		
		protected System.Web.UI.WebControls.Label L_thistitle;
	
		private void Page_Load(object sender, System.EventArgs e)
		{   L_bbsname.Text=sharecode.strBBSName;
			string strthisid=sharecode.GetQueryStringValue("rMviewid");
			string strkeyword=sharecode.GetQueryStringValue("keyword").Replace("；","");
			strkeyword=strkeyword.Replace("<","").Replace(">","");//去处html标签
			
			
			// 在此处放置用户代码以初始化页面
			DTable1.ConnStr =sharecode.ConnStr;
			DTable1.List_Enable =false;
			DTable1.Edit_EnableEdt =false;
			DTable1.Edit_EnableAdd =true;
			DTable1.Edit_EnableDel =false;
			DTable1.Edit_EnablePostAndAddNewButton=false;

			DTable1.TableName="DTable_BBS";
			DTable1.List_SQL="select top 1 * from DTableBBS";
			DTable1.List_Enable=false;
			DTable1.Edit_URL_AddEnd=Page.Request.RawUrl.ToString() ; 
			DTable1.Edit_FieldList="id,主题 as 回复主题,内容 as 回复内容,相关图片,相关文件,发布人,IP地址,父ID,栏目";
			
				
			DTable1.List_Sty_Table="width=\"100%\" height=\"28\" border=\"1\" align=\"center\" cellPadding=\"3\" cellSpacing=\"0\" borderColor=\"#111111\" borderColorLight=\"#666688\" borderColorDark=\"#666688\" style=\"BORDER-COLLAPSE: collapse\""; 
			DTable1.List_Sty_Row2="bgcolor=\"#eef3f9\""; 
			DTable1.List_Sty_Row1=""; 
			

            string stryhm="";
			if(sharecode.GetQueryStringValue("addnew")=="true" && sharecode.GetQueryStringValue("submitaddnew")=="" )//如果是添加状态,并且不是提交状态
			{
				if(sharecode.GetSessionValue(sharecode.strUserSName)=="")//如果用户没有登录,就显示登录框
				{
//					
					stryhm=sharecode.GetCookieValue(sharecode.strUserNameCookieName);//获取历史的用户迷宫那
					L_user.Text=stryhm;
					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_login.Text="<TABLE id=\"logintable\" width=\"100%\" height=\"28\"	border=\"1\" align=\"center\" cellPadding=\"3\" cellSpacing=\"0\" borderColor=\"#111111\" borderColorLight=\"#666688\" borderColorDark=\"#666688\" style=\"BORDER-COLLAPSE: collapse\"><form name='formlogin' target=\"loginframe\" method=\"post\" action=\"../register/userlogin.aspx\"><tr><td> 用户名: <input type=\"text\" size=\"10\" name=\"yhm\" value=\"" + stryhm + "\">&nbsp;&nbsp;密码: <input type=\"password\" name=\"mm\" value=\"" + strmm + "\" size=\"10\">&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>";
					L_script.Text="<script language=javascript>document.all.submitaddnew.disabled=true;</script>";
				}
				else
				{
				L_login.Text="";
				stryhm=sharecode.GetSessionValue(sharecode.strUserSName);
				L_user.Text=stryhm;
				}
			}
		
			
			
			
			
			
			
			
			
			
			string strb="";
			

			if(strthisid!="" && sharecode.GetFormValue("submitaddnew")=="")//显示信息
			{
				Page.Response.Cookies[sharecode.strFromUrlSName].Value=Page.Request.Url.AbsoluteUri.ToString();Page.Response.Cookies[sharecode.strFromUrlSName].Expires=System.DateTime.Now.AddMinutes(20); 
				System.Text.StringBuilder strtext=new System.Text.StringBuilder();	
				System.Data.SqlClient.SqlConnection oconn=new System.Data.SqlClient.SqlConnection(sharecode.ConnStr);
				oconn.Open();
				System.Data.SqlClient.SqlCommand mycmd=new System.Data.SqlClient.SqlCommand("select a.id,a.主题,a.内容,a.发布人,a.发布时间,a.相关文件,a.相关图片,a.父id,a.栏目,b.积分,(select count(*) from DTable_BBS c where c.发布人=a.发布人) as 发贴数,b.门派,b.注册时间,b.最后一次登录,b.性别,b.签名,b.头像,a.点击率,c.版主,a.置顶 from DTable_BBS a inner join DTable_user b on a.发布人=b.用户名 inner join DTable_BBS_Board c on a.栏目=c.id where a.删除=0 and (a.id=" + strthisid + " or a.父id=" + strthisid + ") order by a.id" ,oconn);
				
				System.Data.SqlClient.SqlDataReader myreader=mycmd.ExecuteReader();
				int theno=-1;
				string strthisztid="";//本主题的id
				while(myreader.Read())
				{
					theno++;
					string strtmpid=myreader.GetValue(0).ToString();
					string strzt="";//主题
					string strnr="";//内容
					string strfbr="";//发布人
					string strfbsj="";//发布时间
					string strxgwj="";//相关文件
					string strxgtp="";//相关图片
					string strfid="";//父id
					//string strb="";//栏目
					string strjf="";//积分
					string strfts="";//发贴数
					string strmp="";//门派
					string strzcsj="";//注册时间
					string strzhycdl="";//最后一次登录
					string strxb="";//性别
					string strqm="";//签名
					string strtx="";//头像
					string strthisbz="";//版主
					string strzd="";//置顶
					
			if(!myreader.IsDBNull(19)){strzd=myreader.GetValue(19).ToString();}
					if(!myreader.IsDBNull(18)){strthisbz=myreader.GetValue(18).ToString();}

					if(!myreader.IsDBNull(16)){strtx=myreader.GetValue(16).ToString();}
					if(!myreader.IsDBNull(15)){strqm=myreader.GetValue(15).ToString();}
					if(!myreader.IsDBNull(14)){strxb=myreader.GetValue(14).ToString();}
					if(!myreader.IsDBNull(13)){strzhycdl=myreader.GetValue(13).ToString();}
					if(!myreader.IsDBNull(12)){strzcsj=myreader.GetValue(12).ToString();}
					if(!myreader.IsDBNull(11)){strmp=myreader.GetValue(11).ToString();}
					if(!myreader.IsDBNull(10)){strfts=myreader.GetValue(10).ToString();}
					if(!myreader.IsDBNull(9)){strjf=myreader.GetValue(9).ToString();}
					//if(!myreader.IsDBNull(8)){strb=myreader.GetValue(8).ToString();}
					if(!myreader.IsDBNull(5)){strxgwj=myreader.GetValue(5).ToString();}
					if(!myreader.IsDBNull(6)){strxgtp=myreader.GetValue(6).ToString();}
					if(!myreader.IsDBNull(7)){strfid=myreader.GetValue(7).ToString();}
					strzt=myreader.GetValue(1).ToString();
					strnr=myreader.GetValue(2).ToString();
					strfbr=myreader.GetValue(3).ToString();
					strfbsj=myreader.GetValue(4).ToString();
					
				
					if(strxgwj.Length>5)
					{
strxgwj="<strong>相关文件：</strong><a href=file_up/" + strxgwj + " target=_blank>下载</a>";

					}
					if(strxgtp.Length>5)
					{
						strxgtp="<strong>相关图片：</strong><br><img src=img_up/" + strxgtp + ">";
					}
					string strthisl="";
					if(theno==0)
					{
						strthisl="楼主";
						L_title.Text=strzt;
						L_thistitle.Text=strzt;
						L_read.Text=myreader.GetValue(17).ToString();
						strb=myreader.GetValue(8).ToString();
						strthisztid=strtmpid;
					
						thistitle.InnerText=strzt + "";//设置Title
						//赋值搜索所需的隐含控件
						L_hideinput.Text="<input type=\"hidden\" name=\"DTable_b\" value=\"" + strb + "\">";
					}
					else
					{
						strthisl="第" + theno.ToString() + "楼";
					}
					
					if(strkeyword!="")
					{
						strzt=strzt.Replace(strkeyword,"<font color=#FF0000>" + strkeyword + "</font>");
						strnr=strnr.Replace(strkeyword,"<font color=#FF0000>" + strkeyword + "</font>");
					}
					if(strqm.Length>0)
					{
						strqm="-----------------------------------------------------------------------------------------------------------------<br>" + strqm;
					}
					string strmg="";//生成管理连接
					string strtmpuser=sharecode.GetSessionValue(sharecode.strUserSName);
					string stradministrator=sharecode.GetSessionValue(sharecode.strSuperAdminSName);
					if((strthisbz!="" && sharecode.GetSessionValue(sharecode.strUserSName)!="") || stradministrator!="")//如果有版主,并且当前用户已经登录或者是超级管理员
					{
						string strdel="del";//设置删除的代码
						string strzdtext="<a href=update.aspx?b=" + strb + "&id=" + strthisztid + "&op=zd title=把本主题置顶>置顶</a> | ";

						if(theno==0)
						{
							strdel="delete";
						
						
						if(strzd.ToLower()=="true")
						{
                           strzdtext="<a href=update.aspx?b=" + strb + "&id=" + strthisztid + "&op=unzd title=解除本主题置顶>解除置顶</a> | ";
						}
						}

						
						if(System.Convert.ToString("," + strthisbz + ",").IndexOf("," + strtmpuser + ",")>-1 || stradministrator!="")//如果是本栏目管理员或者是超级管理员
						{
							strmg="<strong>管理选项:</strong> " + strzdtext + " <a href=\"update.aspx?meditid=" + strtmpid + "&b=" + strb + "&id=" + strthisztid + "&op=edt&furl=" + Page.Request.Url.AbsoluteUri.ToString() + "\" title=编辑本贴>编辑</a> | <a href=update.aspx?b=" + strb + "&id=" + strtmpid + "&op=" + strdel + " onclick=\"{if(confirm('即将删除帖子,是否继续(按取消放弃删除)?')){return true;}return false;}\" title=删除本贴>删除</a> | <a href=\"update.aspx?b=" + strb + "&id=" + strthisztid + "&op=gg&addnew=true&furl=list.aspx?DTable_b=" + strb + "\" title=发布本栏目公告>发布公告</a>";
						}

					}
					
					strtext.Append("<TABLE width='100%' border=\"1\" cellpadding=\"3\" cellspacing='0' bordercolor='#111111' bordercolorlight='#666688' bordercolordark='#666688' style='border-collapse: collapse'><TBODY><tr valign='top'><td rowspan=\"2\" align=\"center\" VALIGN=\"top\" width=150><table width=\"150\" border=\"0\" cellspacing=\"0\"> <tr> <td colspan=\"2\" align=\"left\" style=\"filter:glow(color=#9898BA,strength=2)\"><font color=#000066>" + strfbr + "</font><a name=" + strtmpid + "></a></td> </tr> <tr> <td colspan=\"2\" align=\"left\"><img src=\"img_face/" + strtx + "\" width=\"70\"></td> </tr> <tr> <td align=\"right\">&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td align=\"right\" width=30>积分:</td> <td width=\"54\">" + strjf + "</td> </tr> <tr> <td align=\"right\">文章:</td> <td>" + strfts + "</td> </tr> <tr> <td align=\"right\">门派:</td> <td>" + strmp + "</td> </tr> <tr> <td align=\"right\">注册:</td> <td>" + strzcsj + "</td> </tr> <tr> <td align=\"right\" valign=top>最后:</td> <td align=\"left\">" + strzhycdl + "</td> </tr> </table></td><td ><table width=\"100%\" border=\"0\"> <tr> <td width=\"18\">&nbsp;</td> <td width=\"58\"><a href=\"#re\"><img src=\"img_bbsSet/reply_a.gif\" width=\"45\" height=\"18\" border=\"0\"></a></td> <td width=\"59\"><a href=# onclick=\"window.location.reload();\"><img src=\"img_bbsSet/refresh.gif\" width=\"40\" height=\"12\" border=0></a></td> <td align=\"right\">" + strfbsj + "&nbsp;&nbsp;&nbsp;" + strthisl + "</td> </tr> </table></td>	 </tr>		 <tr valign='top'>		 <td><strong>主题:" + strzt + "</strong><br><br>" + strnr.Replace("‘","'") + "<br><br>" + strxgwj + "<br>" + strxgtp + "<br>" + strqm + "<br><br><div align=right>" + strmg + "</div></td>	 </tr>		</TBODY></TABLE>");
					
				}
				myreader.Close();
				//开始更新记录点击率

				mycmd.CommandText="update DTable_BBS set 点击率=点击率+1 where id=" + strthisid;
				try
				{
					mycmd.ExecuteNonQuery();
				}
				catch
				{

				}

 


				myreader.Close();
//开始获取栏目信息
				//开始处理发贴的链接
				Img_addnew.Attributes.Add("onclick","window.open('add.aspx?addnew=true&DTable_b=" + strb + "','_self','')");
			//	Img_search.Attributes.Add("onclick","window.open('search.aspx?DTable_b=" + strb + "','_self','')");

			


				//开始获取栏目信息
mycmd.CommandText ="select 栏目名称,栏目介绍,栏目图标,版主,(select top 1 b.栏目名称 from DTable_BBS_Board b where b.id=a.父栏目) as 父栏目名称 from DTable_BBS_Board a where id=" + strb;

				myreader=mycmd.ExecuteReader();
				string strlmmc="";//栏目名称
				string strlmjs="";//栏目介绍
				string strlmtb="";//图标
				string strbz="";//版主
				string strflmmc="";//父栏目名称
				if(myreader.Read())
				{
					if(!myreader.IsDBNull(4))
					{
						strflmmc=myreader.GetValue(4).ToString();
					}
					if(!myreader.IsDBNull(3))
					{
						strbz=myreader.GetValue(3).ToString();
					}
					if(!myreader.IsDBNull(2))
					{
						strlmtb=myreader.GetValue(2).ToString();
					}
					if(!myreader.IsDBNull(1))
					{
						strlmjs=myreader.GetValue(1).ToString();
					}
					if(!myreader.IsDBNull(0))
					{
						strlmmc=myreader.GetValue(0).ToString();
					}
                
				}
				myreader.Close();
				L_bz.Text+=strbz;
				
				if(strflmmc!="")
				{
					L_list.Text=strflmmc + " → ";
				}
				if(strlmmc!="")
				{
					L_list.Text+="<a href=list.aspx?DTable_b=" + strb + ">" + strlmmc + "</a>";
				}


				mycmd.Dispose();
				oconn.Close();
				oconn.Dispose();
				
				Label2.Text= strtext.ToString();
				DTable1.Edit_FieldFormat="2||htmledit|img_bbs/||||^3||urlfile|./img_up||./file_up/mini/|||^4||urlfile|./file_up/|||||^5||autovalue|" + sharecode.GetSessionValue(sharecode.strUserSName) + "||||^7||autovalue|" + strthisid + "||||^6||autovalue|" + Page.Request.UserHostAddress.ToString()  +"||||^8||autovalue|" + strb + "||||";
				
				if(sharecode.GetFormValue("field5")!="")
				{
					DTable1.Edit_SQL_AddEnd ="update DTable_User set 积分=积分+7 where 用户名='" + sharecode.GetFormValue("field5") + "'";
				}
				
			}
			else if(strthisid!="" && sharecode.GetFormValue("submitaddnew")!="" && sharecode.GetFormValue("field7")!="")//如果是提交状态,且传入了父id,开始为本贴的主题排序

			{//DTable1.Edit_FieldList="id,主题,内容,相关图片,相关文件,发布人,IP地址,父ID,栏目,排序";				//开始获取最大id
System.Data.SqlClient.SqlConnection oconn=new System.Data.SqlClient.SqlConnection(sharecode.ConnStr);
				oconn.Open();
				System.Data.SqlClient.SqlCommand mycmd=new System.Data.SqlClient.SqlCommand("update DTable_BBS set 排序=(select top 1 max(排序) from DTable_BBS b) +1 where id=" + sharecode.GetFormValue("field7"),oconn);
				try
				{
					mycmd.ExecuteNonQuery();
				}
				catch
				{
				}
				mycmd.Dispose();
				oconn.Close();
				oconn.Dispose();


			}
		
			
		}

		#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
	}
}
