namespace SK.MemberWeb.Templates.Default
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Web.Security;
	using SK.MemberBLL;

	/// <summary>
	///		cu_MemberMenu 的摘要说明。
	/// </summary>
	public class cu_MemberMenu : BaseControl
	{
		protected System.Web.UI.WebControls.LinkButton btnLogout;
		ProductTypeSystem producttype = new ProductTypeSystem();
		protected System.Web.UI.WebControls.Repeater Repeater1;
		protected System.Web.UI.WebControls.Repeater lstProduct;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbLogon;
		ProductSystem product = new ProductSystem();
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			try
			{
				if(HttpContext.Current.User.Identity.IsAuthenticated)
					tbLogon.Visible = true;
				else
				{
					tbLogon.Visible = false;
					Response.End();
				}

			}
			catch
			{
			}
			BindData();
			
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		设计器支持所需的方法 - 不要使用代码编辑器
		///		修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
			//this.lstProduct.ItemCreated += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.lstProduct_ItemCreated);
			this.lstProduct.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.lstProduct_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);
			/*this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
			this.lstProduct.ItemCreated += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.lstProduct_ItemCreated);
			this.lstProduct.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.lstProduct_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);
			*/

		}
		#endregion
		public string MemberName
		{
			get{return this.CurrentMemberName.ToString();}
		}
		private void BindData()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("ProductName",typeof(String));
			dt.Columns.Add("HomePage",typeof(String));
			dt.Columns.Add("ProductTypeID",typeof(System.Int32));
			dt.Columns.Add("ProductID",typeof(System.Int32));
			dt.Columns.Add("ClickInfo",typeof(String));
			using(DataSet ds = product.List())
			{
				foreach(DataRow row in ds.Tables[0].Rows)
				{
					if((int)row["ProductTypeID"]==5||(int)row["ProductTypeID"]==13||(int)row["ProductTypeID"]==7||(int)row["ProductTypeID"]==11)
					{
						DataRow rownew = dt.NewRow();
						rownew["ProductName"] = row["ProductName"];
						rownew["HomePage"] = row["HomePage"];
						rownew["ProductTypeID"] = row["ProductTypeID"];
						rownew["ProductID"] = row["ProductID"];
						string str = "";
						switch((int)row["ProductTypeID"])
						{
							case 5://论坛
								str = "BBS_"+row["ProductID"]+"_1/";
								str += "BBS_"+row["ProductID"]+"_2/";
								str += "BBS_"+row["ProductID"]+"_3/";
								str += "BBS_"+row["ProductID"]+"_4";
								rownew["ClickInfo"] = str;
								break;
							case 13://供求
								str = "Sale_"+row["ProductID"]+"_1/";
								str += "Sale_"+row["ProductID"]+"_2/";
								str += "Sale_"+row["ProductID"]+"_3/";
								str += "Sale_"+row["ProductID"]+"_4/";
								str += "Sale_"+row["ProductID"]+"_5/";
								str += "Sale_"+row["ProductID"]+"_6";
								rownew["ClickInfo"] = str;
								break;
							case 7://电子商务
								str =  "Shop_"+row["ProductID"]+"_1/";
								str += "Shop_"+row["ProductID"]+"_2/";
								str += "Shop_"+row["ProductID"]+"_3/";
								str += "Shop_"+row["ProductID"]+"_4";
								rownew["ClickInfo"] = str;
								break;
							case 11://人才
								str =  "Job_"+row["ProductID"]+"_1";
								rownew["ClickInfo"] = str;
								break;
						}
						dt.Rows.Add(rownew);
					}
				}
			}
			lstProduct.DataSource = dt;
			lstProduct.DataBind();
		}
		private void btnLogout_Click(object sender, System.EventArgs e)
		{
			FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
				this.CurrentMemberID+";"+this.CurrentMemberName ,
				DateTime.Now,
				DateTime.Now.AddMinutes(-20),
				false,
				"",
				FormsAuthentication.FormsCookiePath);
			string hash = FormsAuthentication.Encrypt(ticket);
			HttpCookie cookie = new HttpCookie(
				FormsAuthentication.FormsCookieName, 
				hash);
			cookie.Domain = this.Domain;

			if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

			System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
			Response.Redirect("~/");
		}

		private void lstProduct_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item)
			{
				DataTable dt = new DataTable();
				dt.Columns.Add("HrefName",typeof(String));
				dt.Columns.Add("Href",typeof(String));
				dt.Columns.Add("ID",typeof(String));
				dt.Columns.Add("ProductID",typeof(String));
				
				DataRow rownew = null;
				string ProductID = ((DataRowView)e.Item.DataItem).Row["ProductID"].ToString();

				switch(Convert.ToInt32(((DataRowView)e.Item.DataItem).Row["ProductTypeID"]))
				{
					case 5://论坛
						rownew = dt.NewRow();
						rownew["HrefName"] = "我的帖子";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/MyTopic.aspx";
						rownew["ID"] = "BBS_"+ProductID+"_1";
						dt.Rows.Add(rownew);
						

						rownew = dt.NewRow();
						rownew["HrefName"] = "我的收藏";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/FavList.aspx";
						rownew["ID"] = "BBS_"+ProductID+"_2";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "个人配置";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/EditProfile.aspx";
						rownew["ID"] = "BBS_"+ProductID+"_3";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "个性签名";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/Signature.aspx";
						rownew["ID"] = "BBS_"+ProductID+"_4";
						dt.Rows.Add(rownew);

						break;

					case 13://供求
						rownew = dt.NewRow();
						rownew["HrefName"] = "发布信息";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/ProductAdd.aspx";
						rownew["ID"] = "Sale_"+ProductID+"_1";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "信息管理";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/Products.aspx";
						rownew["ID"] = "Sale_"+ProductID+"_2";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "购买联系信息";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/MemberInfo.aspx";
						rownew["ID"] = "Sale_"+ProductID+"_3";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "推荐我的信息";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/ProductCommendList.aspx";
						rownew["ID"] = "Sale_"+ProductID+"_4";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "我收到的留言";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/ReceivePM.aspx";
						rownew["ID"] = "Sale_"+ProductID+"_5";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "我发出的留言";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/SendPM.aspx";
						rownew["ID"] = "Sale_"+ProductID+"_6";
						dt.Rows.Add(rownew);

						break;

					case 7://电子商务
						rownew = dt.NewRow();
						rownew["HrefName"] = "订单维护";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/MyAccount_Orders";
						rownew["ID"] = "Shop_"+ProductID+"_1";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "收货地址维护";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/MyAccount_Address.aspx";
						rownew["ID"] = "Shop_"+ProductID+"_2";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "购物卡冲值";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/MyAccount_AddBalance.aspx";
						rownew["ID"] = "Shop_"+ProductID+"_3";
						dt.Rows.Add(rownew);

						rownew = dt.NewRow();
						rownew["HrefName"] = "账户余额";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"]+"Member/MyAccount_Balance.aspx";
						rownew["ID"] = "Shop_"+ProductID+"_4";
						dt.Rows.Add(rownew);

						break;

					case 11://人才
						rownew = dt.NewRow();
						rownew["HrefName"] = "人才首页";
						rownew["Href"] = ((DataRowView)e.Item.DataItem).Row["HomePage"];
						rownew["ID"] = "Job_"+ProductID+"_1";
						dt.Rows.Add(rownew);
						break;

				}
				Repeater lstHref = (Repeater)e.Item.FindControl("lstHref");
				lstHref.DataSource = dt;
				lstHref.DataBind();
																	
			}
		}

		private void lstProduct_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			/*if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item)
			{
				string ProductID = ((DataRowView)e.Item.DataItem).Row["ProductID"].ToString();
				string str = "";
				switch(Convert.ToInt32(((DataRowView)e.Item.DataItem).Row["ProductTypeID"]))
				{
						
					case 5://论坛
						str = "BBS_"+ProductID+"_1/";
						str += "BBS_"+ProductID+"_2/";
						str += "BBS_"+ProductID+"_3/";
						str += "BBS_"+ProductID+"_4";
						((DataRowView)e.Item.DataItem).Row["ClickInfo"] = str;
						break;

					case 13://供求
						str = "Sale_"+ProductID+"_1/";
						str += "Sale_"+ProductID+"_2/";
						str += "Sale_"+ProductID+"_3/";
						str += "Sale_"+ProductID+"_4/";
						str += "Sale_"+ProductID+"_5/";
						str += "Sale_"+ProductID+"_6";
						((DataRowView)e.Item.DataItem).Row["ClickInfo"] = str;
						break;

					case 7://电子商务
						str =  "Shop_"+ProductID+"_1/";
						str += "Shop_"+ProductID+"_2/";
						str += "Shop_"+ProductID+"_3/";
						str += "Shop_"+ProductID+"_4";
						((DataRowView)e.Item.DataItem).Row["ClickInfo"] = str;
						break;
					case 11://人才
						str =  "Job_"+ProductID+"_1";
						((DataRowView)e.Item.DataItem).Row["ClickInfo"] = str;
						break;
				}
			}*/
		}
	}
}
