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_AddPhoto 的摘要说明。
	/// </summary>
	public class Admin_AddPhoto : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DropDownList ChannelID;
		protected System.Web.UI.WebControls.DropDownList ParentID;
		protected System.Web.UI.WebControls.TextBox Soft_Name;
		protected System.Web.UI.WebControls.TextBox Soft_WebSiteName;
		protected System.Web.UI.WebControls.TextBox Soft_WWW;
		protected System.Web.UI.WebControls.TextBox Soft_WWWtest;
		protected System.Web.UI.WebControls.TextBox Soft_huanjin;
		protected System.Web.UI.WebControls.TextBox Soft_Mail;
		protected System.Web.UI.WebControls.TextBox Soft_daxiao;
		protected System.Web.UI.WebControls.TextBox Soft_admin;
		protected System.Web.UI.WebControls.TextBox Soft_language;
		protected System.Web.UI.WebControls.TextBox Soft_jibie;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList2;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Image Image1;
		protected System.Web.UI.WebControls.TextBox txthot;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.HtmlControls.HtmlInputFile File1;
		protected System.Web.UI.WebControls.TextBox Soft_pass;
		protected System.Web.UI.WebControls.Button Button3;
		protected System.Web.UI.WebControls.TextBox Textbox5;
		protected System.Web.UI.HtmlControls.HtmlInputFile File2;
		public int CID,PID,ClassID;
		public string Addtime;
		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 ispic=false and isphoto=true";
			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.Button2.Click += new System.EventHandler(this.Button2_Click);
			this.Button3.Click += new System.EventHandler(this.Button3_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();
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			int face=Convert.ToInt32(Request.Form["face"]);
			Addtime=DateTime.Now.ToString().Substring(0,10);
			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 * from photo where Soft_Name='"+this.Soft_Name.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();
					this.TextBox1.Text=(string)ACdrII["Soft_uploadpic"];
					this.Textbox5.Text=(string)ACdrII["Soft_uploadsoft"];
					//					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;
					}
					
					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 photo(ChannelID,ClassID,Soft_Name,Soft_WebSiteName,Soft_Mail,Soft_WWW,Soft_WWWtest,Soft_uploadpic,Soft_uploadsoft,Soft_daxiao,Soft_jianjie,Soft_huanjin,Soft_admin,Soft_language,Soft_pass,Soft_star,Soft_jibie,iscmd,ishot,UpdateTime,hits) values ('"+ChannelID.SelectedValue+"','"+ParentID.SelectedValue
					+"','"+this.Soft_Name.Text+"','"+Soft_WebSiteName.Text+"','"+Soft_Mail.Text+"','"+Soft_WWW.Text+"','"+Soft_WWWtest.Text+"','"+this.TextBox1.Text+"','"+this.Textbox5.Text+"','"+Soft_daxiao.Text+"','"+this.Editor1.Text+"','"+Soft_huanjin.Text+"','"+Soft_admin.Text+"','"+Soft_language.Text+"','"+Soft_pass.Text+"',"+face+",'"+Soft_jibie.Text+"',"+this.RadioButtonList1.SelectedItem.Value+","+this.RadioButtonList2.SelectedItem.Value+",'"+Addtime+"',"+i+")";
						
				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 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 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>");
			}
		}

		private void Button3_Click(object sender, System.EventArgs e)
		{
			string fullname=this.File2.PostedFile.FileName;
			string filename=fullname.Substring(fullname.LastIndexOf("\\")+1);
			string type=fullname.Substring(fullname.LastIndexOf(".")+1);
			if(type=="rar"||type=="zip"||type=="gzip")
			{
				this.File2.PostedFile.SaveAs(Server.MapPath("uploadpic")+"\\"+filename);
				
				this.Textbox5.Text=filename;
			}
			else
			{
				Response.Write("<script language=javascript>alert('上传格式只能是rar,zip,gzip');</script>");
			}
		}
	}
}
