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;
using System.Data.OleDb;
namespace free.bbs
{
	/// <summary>
	/// Summary description for goodfriend.
	/// </summary>
	public class goodfriend : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		int start;
		SqlConnection myconn;
		protected System.Web.UI.WebControls.TextBox name;
		protected System.Web.UI.WebControls.TextBox name1;
		protected System.Web.UI.HtmlControls.HtmlInputButton Button2;
		SqlCommand mycomm;
		protected void DataBind1()
		{			
			
			SqlDataAdapter DR = new SqlDataAdapter("SELECT yonghuming,xingbie,chushengriqi,qq,dianhua,email FROM huiyuan WHERE yonghuming IN (SELECT haoyou FROM friend WHERE yonghuming='"+Request.Cookies["free_huiyuan"].Value+"')",myconn);
			DataSet ds = new DataSet();
			DR.Fill(ds,"haoyou");
			DataGrid1.DataSource=ds.Tables["haoyou"];
			DataGrid1.DataBind();
			
		}
		private void Page_Load(object sender, System.EventArgs e)
		{
			myconn=new SqlConnection(Session["str_connection"].ToString());
			myconn.Open();
			if(!IsPostBack)
			{
				start=0;
				DataBind1();
				try
				{
					name.Text=Request.Params["friend"].ToString();
				}
				catch
				{
					name.Text="";
				}
			}
			myconn.Close();
			////////////////////////
			
		}

		#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.Button1.ServerClick += new System.EventHandler(this.Button1_ServerClick);
			this.Button2.ServerClick += new System.EventHandler(this.Button2_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		public void Page_Changed(object send,DataGridPageChangedEventArgs E)
		{
			start=E.NewPageIndex*DataGrid1.PageSize;
			DataGrid1.CurrentPageIndex=E.NewPageIndex;
			DataBind1();
		}

		private void Button1_ServerClick(object sender, System.EventArgs e)
		{
			
			if(name.Text!="")
			{
				myconn.Open();
				try
				{
					string sqlsle="select * from huiyuan where yonghuming='"+name.Text.Trim().ToString()+"'";
					mycomm=new SqlCommand(sqlsle,myconn);
					SqlDataReader myread=mycomm.ExecuteReader();
					if(myread.Read())
					{
						myread.Close();
						string sqlins="insert into friend(yonghuming,haoyou) VALUES ('"+Request.Cookies["free_huiyuan"].Value+"','"+name.Text.Trim().ToString()+"')";
						mycomm= new SqlCommand(sqlins,myconn);
						if(mycomm.ExecuteNonQuery()>0)
						{
							Response.Write("<script>alert('恭喜，已加为好友！>')</script>");
							DataBind1();
							myconn.Close();
						}
						else
						{
							myconn.Close();
							Response.Write("<script>alert('对不起，该会员已经在您的好友列表中！>')</script>");
						}
					}
					else
					{
						myread.Close();
						myconn.Close();
						Response.Write("<script>alert('对不起，您输入的不是会员名！>')</script>");
					}
				}
				catch
				{
					myconn.Close();
				}
			}
			else
			{
				Response.Write("<script>alert('会员名不能为空！>')</script>");
			}
		}

		private void Button2_ServerClick(object sender, System.EventArgs e)
		{
			if(name1.Text=="")
			{
				Response.Write("<script>alert('请先输入好友的用户名>')</script>");
			}
			else
			{
				myconn.Open();
				string sqlsle="select haoyou from friend where yonghuming='"+Request.Cookies["free_huiyuan"].Value.ToString()+"'and haoyou='"+name1.Text.Trim()+"'";
				mycomm=new SqlCommand(sqlsle,myconn);
				SqlDataReader myread1=mycomm.ExecuteReader();
				if(myread1.Read())
				{
					string mysql="delete from friend where yonghuming='"+Request.Cookies["free_huiyuan"].Value.ToString()+"' and haoyou='"+name1.Text.Trim()+"'";
					myread1.Close();
					SqlCommand command=new SqlCommand(mysql,myconn);
					if(command.ExecuteNonQuery()>0)
					{
						myconn.Close();
						DataBind1();
						Response.Write("<script>alert('好友删除成功！>')</script>");
					}
					else
						myconn.Close();
				}
				else
				{
					Response.Write("<script>alert('输入为非好友姓名！>')</script>");
				}
			}
		
		}
	}
}
