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.SqlClient;

namespace WebApplication2.Admin
{
	/// <summary>
	/// NewsManage 的摘要说明。
	/// </summary>
	public partial class NewsAdd : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				if(this.Session["USERNAME"]==null||this.Session["Role"]==null||this.Session["USERNAME"].ToString()==""||this.Session["Role"].ToString()=="")
				{
					Response.Redirect("login.aspx");
				}
				else
				{
					string [] role=this.Session["Role"].ToString().Split('#');
					bool exists=false;
					for(int i=0;i<role.Length;i++)
					{
                        if (role[i].Equals("16") || role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
					{
					
						this.RadioButtonList1.SelectedIndex=0;
						if(this.RadioButtonList1.Items[4].Selected)
						{
							this.pl.Visible=true;
						}
						else
						{
							this.pl.Visible=false;
						}

					
					}
					else
					{Response.Redirect("NoAuthor.aspx");}
			
				}
			}
		}

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

		}
		#endregion

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			if(Request.Params["content"]==null||Request.Params["content"].ToString()=="")
			{
				Response.Write("<script>alert(\"请输入新闻内容！！\")</script>");// 在此处放置用户代码以初始化页面
			}
			else
			{
				if(this.Txtnewtitle.Text==null||this.Txtnewtitle.Text=="")
				{
					Response.Write("<script>alert(\"请输入新闻标题！！\")</script>");
				}
				else
				{
					
						if(this.Txtreceive.Text==null&&this.pl.Visible==true||this.Txtreceive.Text==""&&this.pl.Visible==true)
						{
							Response.Write("<script>alert(\"请输入接收者账号！！\")</script>");
						}
					
					else
					{
						SB.ADOHelper help=new SB.ADOHelper();
						DateTime dt=DateTime.Now;
						string newtitle=this.Txtnewtitle.Text;
						string receive=this.Txtreceive.Text.Trim();
						string receivetype=this.DropDownList1.SelectedValue;
						string username=this.Session["USERNAME"].ToString();
						string newtype=this.RadioButtonList1.SelectedValue;
						string content=Server.HtmlEncode(Request.Params["content"].ToString());
						string datatime=dt.ToShortDateString();	
						SqlConnection conn=new SqlConnection(SB.ADOHelper.ConnectionString());
						SqlDataAdapter da=new SqlDataAdapter("select * from News ",conn);
						conn.Open();
						DataSet ds=new DataSet();
						da.Fill(ds);
						SqlCommandBuilder sb=new SqlCommandBuilder(da);
						DataRow row=ds.Tables[0].NewRow();
						row["Title"]=newtitle;
						row["Appearance"]=1;
						row["UserName"]=username;
					    if(this.pl.Visible)
					    {   row["ToUserName"]=receive;
							row["ToAffairs"]=receivetype;
						}
						row["Affairs"]=3;
						row["NewsType"]=newtype;
						row["Contents"]=content;
						row["AddTima"]=datatime;
						row["hits"]=0;
						ds.Tables[0].Rows.Add(row);
						da.Update(ds,ds.Tables[0].ToString());
						Response.Write("<script>alert(\"新闻成功添加！\")</script>");
						this.Txtnewtitle.Text="";
						this.Txtreceive.Text="";
						this.RadioButtonList1.SelectedIndex=0;
						
							
					}
				}

				
			}
		
		}

		protected void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.RadioButtonList1.Items[4].Selected)
			{
				this.pl.Visible=true;

			}
			else
			{
				this.pl.Visible=false;
			}
		}
	

		
	}
}
