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.OleDb;
using System.Configuration;
using System.IO;
namespace baoge.Admin
{
	/// <summary>
	/// Admin_AddNews 的摘要说明。
	/// </summary>
	public class Admin_AddNews : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox Title;
		protected System.Web.UI.WebControls.TextBox LinkUrl;
		protected System.Web.UI.WebControls.TextBox Intro;
		protected System.Web.UI.WebControls.TextBox Author;
		protected System.Web.UI.WebControls.TextBox MY_Email;
		protected System.Web.UI.WebControls.TextBox CopyFrom;
		protected System.Web.UI.WebControls.TextBox MY_Site;
		protected System.Web.UI.WebControls.TextBox Inputer;
		protected System.Web.UI.WebControls.TextBox MY_QQ;
		protected System.Web.UI.WebControls.DropDownList ChannelID;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.DropDownList ParentID;

		public int CID,PID,ClassID;
		protected System.Web.UI.WebControls.RequiredFieldValidator TRr;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator5;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator6;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator3;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList2;
		protected System.Web.UI.WebControls.TextBox txthot;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator7;

		public string Addtime;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList3;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.HtmlControls.HtmlInputFile File1;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Image Image1;
		protected CuteEditor.Editor Editor1;
		public int i;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面

			//判断是否为合法用户
			if(Session["AdminName"]==null)
			{
			Response.Redirect("Admin_Login.aspx");
			}
			if(!IsPostBack)
			{
				ChannelDataBind();

			}


		}

		private void ChannelDataBind()
	      {
	

			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义数据库连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库连接对像
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
			ACconn.Open();
			string ACsql="select ChannelID,ChannelName from Channel where issoft=false";
			OleDbDataAdapter ACda=new OleDbDataAdapter(ACsql,ACconn);
			DataTable ACdt=new DataTable();
			ACda.Fill(ACdt);

			//频道的数据绑定
			ChannelID.DataSource=ACdt;
			ChannelID.DataTextField="ChannelName";
			ChannelID.DataValueField="ChannelID";
			ChannelID.DataBind();
		
			ACda.Dispose();
			ACconn.Close();

	}







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

		}
		#endregion

		private void ChannelID_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		

			int CID=Convert.ToInt32(ChannelID.SelectedValue);
			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义数据库连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库连接对像
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
			ACconn.Open();
			//取得栏目数据
			string ACsql2="select * from Class where ParentID=2 and ChannelID="+CID;
			OleDbDataAdapter ACda2=new OleDbDataAdapter(ACsql2,ACconn);
			DataTable ACdt2=new DataTable();
			ACda2.Fill(ACdt2);

			//栏目的数据绑定
			ParentID.DataSource=ACdt2;
			ParentID.DataTextField="ClassName";
			ParentID.DataValueField="ClassID";
			ParentID.DataBind();				
			ACconn.Close();

//			int PID=Convert.ToInt32(ParentID.SelectedValue);
//			//取得二级栏目数据
//			string ACsql3="select * from Class where ParentID="+PID;
//			OleDbDataAdapter ACda3=new OleDbDataAdapter(ACsql3,ACconn);
//			DataTable ACdt3=new DataTable();
//			ACda3.Fill(ACdt3);
//
//			//栏目的数据绑定
//			SID.DataSource=ACdt3;
//			SID.DataTextField="ClassName";
//			SID.DataValueField="ClassID";
//			SID.DataBind();		

		}

//		private void ParentID_SelectedIndexChanged(object sender, System.EventArgs e)
//		{
//		
//
//			int PID=Convert.ToInt32(ParentID.SelectedValue);
//			//取得数据库路径
//			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
//			//定义数据库连接字符串
//			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
//			//创建数据库连接对像
//			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
//			//取得二级栏目数据
//			string ACsql2="select * from Class where ParentID="+PID;
//			OleDbDataAdapter ACda2=new OleDbDataAdapter(ACsql2,ACconn);
//			DataTable ACdt2=new DataTable();
//			ACda2.Fill(ACdt2);
//
//			//栏目的数据绑定
//			SID.DataSource=ACdt2;
//			SID.DataTextField="ClassName";
//			SID.DataValueField="ClassID";
//			SID.DataBind();	
//
//			
//		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
		
			
            Addtime=DateTime.Now.ToString().Substring(0,8);
			int CID=Convert.ToInt32(ChannelID.SelectedValue);
			int PID=Convert.ToInt32(ParentID.SelectedValue);
			

			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义数据库连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库连接对像
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);

			try
			{
				//打开数据库
				ACconn.Open();

				
//				int SSID=Convert.ToInt32(SID.SelectedValue);
				//判断添加新闻是否已存在
				string ACsqlII="select ArticleID,iscmd,ishot,ispic,uploadpic from Article where Title='"+Title.Text+"'";
				OleDbCommand ACcommII=new OleDbCommand(ACsqlII,ACconn);
				OleDbDataReader ACdrII=ACcommII.ExecuteReader();
//				string y="";
				while(ACdrII.Read())
				{
					this.TextBox1.Text=(string)ACdrII["uploadpic"];
					this.txthot.Text=ACdrII["Hits"].ToString();
					if((bool)ACdrII["iscmd"])
					{
						
						RadioButtonList1.Items[0].Selected=true;
					}
					else
					{
						RadioButtonList1.Items[1].Selected=true;
					}
					if((bool)ACdrII["ishot"])
					{
//						i=(int)ACdrII["Hits"];
						RadioButtonList2.Items[0].Selected=true;
					}
					else
					{
						RadioButtonList2.Items[1].Selected=true;
					}
					if((bool)ACdrII["ispic"])
					{
						//						i=(int)ACdrII["Hits"];
						this.RadioButtonList3.Items[0].Selected=true;
					}
					else
					{
						this.RadioButtonList3.Items[1].Selected=true;
					}
					Response.Write("<script>alert('此信息已添加过');</script>");
					Response.Write("<script>history.go(-2);</script>");

				}
//				else
//				{
				    
//					this.TextBox1.Text=y;
					ACdrII.Close();



					if(RadioButtonList2.SelectedItem.Value=="1")
					{
						i+=10;
					}
					else if(RadioButtonList2.SelectedItem.Value=="0")
					{
						i=Convert.ToInt32(this.txthot.Text);
					}

					
//					if (Convert.ToInt32(SID.SelectedValue)==0)
//					{
						string ACsqlI="insert into Article(ClassID,ChannelID,Title,LinkUrl,Hits,Content,Intro,Author,MY_Email,CopyFrom,MY_Site,Inputer,MY_QQ,UpdateTime,iscmd,ishot,uploadpic,ispic) values ('"+ParentID.SelectedValue+"','"+ChannelID.SelectedValue+"','"+Title.Text+"','"+LinkUrl.Text+"',"+i+",'"+this.Editor1.Text+"','"+Intro.Text+"','"+Author.Text+"','"+MY_Email.Text+"','"+CopyFrom.Text+"','"+MY_Site.Text+"','"+Inputer.Text+"','"+MY_QQ.Text+"','"+Addtime+"',"+this.RadioButtonList1.SelectedItem.Value+","+this.RadioButtonList2.SelectedItem.Value+",'"+this.TextBox1.Text+"',"+this.RadioButtonList3.SelectedItem.Value+")";
						
						OleDbCommand ACcommI=new OleDbCommand(ACsqlI,ACconn);
						ACcommI.ExecuteNonQuery();
						Response.Write("<script>alert('新闻成功添加');</script>");

//					}
//					else
//					{
//						string ACsqlI="insert into Article(ClassID,ChannelID,Title,LinkUrl,Content,Intro,Author,MY_Email,CopyFrom,MY_Site,Inputer,MY_QQ,UpdateTime) values ('"+SID.SelectedValue+"','"+ChannelID.SelectedValue+"','"+Title.Text+"','"+LinkUrl.Text+"','"+Content.Text+"','"+Intro.Text+"','"+Author.Text+"','"+MY_Email.Text+"','"+CopyFrom.Text+"','"+MY_Site.Text+"','"+Inputer.Text+"','"+MY_QQ.Text+"','"+Addtime+"')";
//
//						OleDbCommand ACcommI=new OleDbCommand(ACsqlI,ACconn);
//						ACcommI.ExecuteNonQuery();
//						Response.Write("<script>alert('信息成功添加');</script>");	
//
//					}
					
 
					
					
																													
				
			}
			catch(Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				ACconn.Close();
			}
		}

		private void SID_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}

		private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int iscmd;
			if(RadioButtonList1.SelectedItem.Value=="1")
			{
				iscmd=1;
			}
			else
			{
				iscmd=0;
			}
		}

		private void RadioButtonList2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int ishot;
			if(RadioButtonList2.SelectedItem.Value=="1")
			{
				ishot=1;
			}
			else
			{
				ishot=0;
			}
		}

		private void RadioButtonList3_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int ispic;
			if(RadioButtonList3.SelectedItem.Value=="1")
			{
				ispic=1;
			}
			else
			{
				ispic=0;
			}
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			string fullname=this.File1.PostedFile.FileName;
			string filename=fullname.Substring(fullname.LastIndexOf("\\")+1);
			string type=fullname.Substring(fullname.LastIndexOf(".")+1);
			if(type=="jpg"||type=="gif"||type=="bmp")
			{
				this.File1.PostedFile.SaveAs(Server.MapPath("uploadpic")+"\\"+filename);
				this.Image1.ImageUrl="uploadpic/"+filename;
				this.TextBox1.Text=filename;
				
			}
			else
			{
				Response.Write("<script language=javascript>alert('图片格式不正确');</script>");
			}
		}
	}
}

