using System;
using System.Collections;
using System.ComponentModel;
using System.Data.SqlClient;
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;
using System.IO;

namespace free.bbs
{
	/// <summary>
	/// Summary description for fatiezi.
	/// </summary>
	public class fatiezi : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlTextArea Info;
		protected System.Web.UI.WebControls.TextBox zhuti;
		protected System.Web.UI.WebControls.TextBox yonghuming;
		protected System.Web.UI.HtmlControls.HtmlSelect select1;
		protected System.Web.UI.HtmlControls.HtmlInputButton Submit2;

		protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.CheckBox qianming;
		protected System.Web.UI.WebControls.CheckBox tongzhi;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing1;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing2;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing3;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing4;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing5;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing6;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing7;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing8;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing9;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing10;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing11;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing12;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing13;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton biaoqing14;
		protected System.Web.UI.HtmlControls.HtmlSelect stdaxiao;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.HtmlControls.HtmlInputFile fileup;
		protected System.Data.SqlClient.SqlConnection myconn;		
		protected System.Data.SqlClient.SqlCommand mycomm;
		
		string path="";
		protected System.Web.UI.HtmlControls.HtmlSelect stziti;
		protected System.Web.UI.HtmlControls.HtmlGenericControl FONT1;
		protected System.Web.UI.HtmlControls.HtmlSelect styanse;
		
		int shu1,shu2,shu3,shu4;
		protected System.Web.UI.WebControls.RadioButtonList face;
		private void Page_Load(object sender, System.EventArgs e)
		{
			
//			if(Page.IsPostBack)
//			{
//				Session["filename"]="";
//				Session["houzui"]="";
//				Session["path"]="";
//			}
			try
			{
				yonghuming.Text=Request.Cookies["free_huiyuan"].Value.ToString();				
				Session["id"]=int.Parse(Request.Params["ancestor_id"].ToString());
				if(yonghuming.Text=="dengchu")
				{
					Response.Redirect("error.aspx");
				}
				
				
			}
			catch 
			{
				Response.Redirect("error.aspx");
			}
			myconn=new SqlConnection(Session["str_connection"].ToString());
			myconn.Open();
			mycomm=new SqlCommand("select islock from ancestor where ancestor_id="+Request.Params["ancestor_id"]+"",myconn);
			SqlDataReader myread=mycomm.ExecuteReader();
			try
			{
				myread.Read();
				
				if(Request.Cookies["free_huiyuan"].Value.ToString()!="free911")
				{
					
					if(myread["islock"].ToString()=="1")
					{
						Response.Write("<script>alert('对不起，该论坛模块已经被锁定，不能发表新主题>')</script>");
						
						myread.Close();
						myconn.Close();
						Response.Write("<script>window.close()</script>");
					}
					else
					{
						myread.Close();
						myconn.Close();
					}
				
				}
				else
				{
					myread.Close();
					myconn.Close();
				}
			}
			catch
			{
				myread.Read();
				myconn.Close();}
			
			
		}

		private void Submit2_ServerClick(object sender, System.EventArgs e)
		{
			//文件上传
			
			if (fileup.PostedFile.ContentLength==0)
			{
				Response.Write("<script>alert('你没有选择要上载的文件或者上传的文件长度为0!')</script>");
			}
			else if(fileup.PostedFile.ContentLength>300000)
			{
				Response.Write("<script>alert('你上载的文件或者上传的文件长度不能超过300K!')</script>");
			}
			else
			{
				 
				Session["filename"]=Path.GetFileName(fileup.PostedFile.FileName);
				path=Server.MapPath("upload//");
				if( Directory.Exists(path)==false)
					Directory.CreateDirectory (path) ;
				path+=Session["filename"].ToString();
				if( File.Exists(path)==true)
						
				{
					Response.Write("<script>alert('该文件名已经存在')</script>");
						
				}
				else
				{
					fileup.PostedFile.SaveAs(path);
					Session["filename"]=Path.GetFileName(fileup.PostedFile.FileName);
					Session["houzui"]=fileup.PostedFile.ContentType.ToString();
					Session["path"]=Server.MapPath("upload//");
					Response.Write("<script>alert('恭喜，文件上传成功！')</script>");
					
					
				}
				
			}      
		

		}

		private void Submit1_ServerClick(object sender, System.EventArgs e)
		{
			
			myconn.Open();
			if (zhuti.Text=="")
			{
				Response.Write("<script>alert('***主题不能为空！')</script>");
			}
			else if (Info.Value=="")
			{
				Response.Write("<script>alert('***发表的内容不能为空！')</script>");
			}
		    else if(zhuti.Text.Length>100)
				Response.Write("<script>alert('对不起，你输入的主题过长！')</script>");
			else 
			{				
				//得到主题
				string zhuti1;
				if(select1.Value=="")
				{
					zhuti1=zhuti.Text;
				}
				else
				{
					zhuti1="【"+select1.Value+"】"+zhuti.Text;
				}

				string lujing="";
				if(biaoqing1.Checked==true)
				{
			
					lujing=@"./emotion/emotion-1.gif";

				}   
				else if(biaoqing2.Checked==true)
				{
			
					lujing=@"./emotion/emotion-2.gif";
				}
				else if(biaoqing3.Checked==true)
				{
			
					lujing=@"./emotion/emotion-3.gif";
				}
				else if(biaoqing4.Checked==true)
				{
		
					lujing=@"./emotion/emotion-4.gif";
				}
				else if(biaoqing5.Checked==true)
				{
					lujing=@"./emotion/emotion-5.gif";
				}
				else if(biaoqing6.Checked==true)
				{
					lujing=@"./emotion/emotion-6.gif";
				}
				else if(biaoqing7.Checked==true)
				{
					lujing=@"./emotion/emotion-7.gif";
				}
				else if(biaoqing8.Checked==true)
				{
					lujing=@"./emotion/emotion-8.gif";
				}
				else if(biaoqing9.Checked==true)
				{				
					lujing=@"./emotion/emotion-9.gif";
				}
				else if(biaoqing10.Checked==true)
				{				
					lujing=@"./emotion/emotion-10.gif";
				}
				else if(biaoqing11.Checked==true)
				{				
					lujing=@"./emotion/emotion-11.gif";
				}
				else if(biaoqing12.Checked==true)
				{
					lujing=@"./emotion/emotion-12.gif";
				}
				else if(biaoqing13.Checked==true)
				{				
					lujing=@"./emotion/emotion-13.gif";
				}
				else if(biaoqing14.Checked==true)
				{				
					lujing=@"./emotion/emotion-14.gif";
				}
				string isqianming;
				if(qianming.Checked==true)
					isqianming="1";
				else
					isqianming="0";
				
				DateTime shijian=DateTime.Now;
				
				try
				{
					string a=Session["houzui"].ToString();					
				}
				catch 
				{
					Session["filename"]="";
					Session["houzui"]="";
					Session["path"]="";

				}
				if(Request.Params["ancestor_id"].ToString()!="124")
				{
					string mysql="insert into posts(zhuti,neirong,yonghuming,shijian,islock,isceam,isfix,ancestor_id,attachfile,attachfileext,lujing,lujing_pic,showqianming)VALUES(@chuqu,@neirong,'"+yonghuming.Text+"','"+shijian+"',0,0,0,'"+Session["id"]+"','"+Session["filename"].ToString()+"','"+Session["houzui"].ToString()+"','"+Session["path"].ToString()+"','"+lujing+"','"+isqianming+"')";
					mycomm=new SqlCommand(mysql,myconn);
					mycomm.Parameters.Add(new SqlParameter("@chuqu", SqlDbType.NVarChar, 50));
					mycomm.Parameters["@chuqu"].Value = htmlencode(zhuti1);

					mycomm.Parameters.Add(new SqlParameter("@neirong", SqlDbType.NText));
					mycomm.Parameters["@neirong"].Value =Info.Value;
			   
					if(mycomm.ExecuteNonQuery()==1)
					{				
					
						string strsle="select jifen,fatieshu,jinqian,meili from huiyuan WHERE yonghuming='"+yonghuming.Text+"'";
						mycomm=new SqlCommand(strsle,myconn);
						SqlDataReader myread=mycomm.ExecuteReader();
						try
						{
							if(myread.Read())
							{
								shu1=(int)myread.GetInt32(0)+10;
								shu2=(int)myread.GetInt32(1)+1;
								shu3=(int)myread.GetInt32(2)+10;
								shu4=(int)myread.GetInt32(3)+2;
							}
							myread.Close();

						}

						catch
						{ 
							myread.Close();
						}
						string strupd="UPDATE huiyuan SET jifen="+shu1+",fatieshu="+shu2+",jinqian="+shu3+",meili="+shu4+" WHERE yonghuming='"+yonghuming.Text+"'";
						mycomm=new SqlCommand(strupd,myconn);
						try
						{
							if(mycomm.ExecuteNonQuery()==1)
							{
								Response.Redirect("fatiechenggong.aspx?ancestor_id="+Request.Params["ancestor_id"]+"&Page=1");
							}
						}
						catch{};
					}
					
				}
				else
				{
					SqlDataAdapter dda;
					DataSet ds;
					ds=new DataSet();
					string sqlselect="select ancestor_id from ancestor";
					try
					{
						dda=new SqlDataAdapter(sqlselect,myconn);
						dda.Fill(ds,"wenzhang");
						try
						{
							for(int i=0;i<=ds.Tables["wenzhang"].Rows.Count-1;i++)
							{
								string mysql="insert into posts(zhuti,neirong,yonghuming,shijian,islock,isceam,isfix,ancestor_id,attachfile,attachfileext,lujing,lujing_pic,showqianming)VALUES(@chuqu,@neirong,'"+yonghuming.Text+"','"+shijian+"',0,0,2,'"+ds.Tables["wenzhang"].Rows[i]["ancestor_id"].ToString()+"','"+Session["filename"].ToString()+"','"+Session["houzui"].ToString()+"','"+Session["path"].ToString()+"','"+lujing+"','"+isqianming+"')";
								mycomm=new SqlCommand(mysql,myconn);
								mycomm.Parameters.Add(new SqlParameter("@chuqu", SqlDbType.NVarChar, 50));
								mycomm.Parameters["@chuqu"].Value =htmlencode(zhuti1);
								mycomm.Parameters.Add(new SqlParameter("@neirong", SqlDbType.NText));
								mycomm.Parameters["@neirong"].Value =Info.Value;
								if(mycomm.ExecuteNonQuery()==1)
								{		
									string strupd="UPDATE huiyuan SET jifen=jifen+10,fatieshu=fatieshu+1,jinqian=jinqian+10,meili=meili+2  WHERE yonghuming='"+yonghuming.Text+"'";
									mycomm=new SqlCommand(strupd,myconn);
									mycomm.ExecuteNonQuery();
								}
						
							}
						}
						catch
						{}
						Response.Redirect("fatiechenggong.aspx?ancestor_id="+Request.Params["ancestor_id"]+"&Page=1");
					}
					catch
					{}

				}
				myconn.Close();
			}
				

		}
		private string htmlencode(string str)
		{
			str=Server.HtmlEncode(str);
			str=str.Replace("\r\n","<br>");
			char ch;
			ch=(char)32;
			str=str.Replace(ch.ToString(),"");
			return str;
		}

	

		private void Button1_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("bbs_zhutiyulan.aspx");
		}

	

		private void Button2_Click(object sender, System.EventArgs e)
		{
			//		    yonghuming.Text="";
			zhuti.Text="";
			Info.Value="";
		}

		#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.Button2.Click += new System.EventHandler(this.Button2_Click);
			this.Submit2.ServerClick += new System.EventHandler(this.Submit2_ServerClick);
			this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
