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 SK.MemberBLL;

namespace SK.MemberWeb.admin
{
	/// <summary>
	/// Member_List 的摘要说明。
	/// </summary>
	public class Member_List : BasePage
	{
		protected System.Web.UI.WebControls.Repeater lstMember;
		protected Wuqi.Webdiyer.AspNetPager pager;
		protected System.Web.UI.WebControls.Button btnSearch;
		protected System.Web.UI.WebControls.TextBox txtKeyword;
		MemberSystem member = new MemberSystem();
		protected System.Web.UI.WebControls.Button btnDelete;
		RightMemberSystem rightmember = new RightMemberSystem();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			txtKeyword.Attributes["onkeypress"]=this.GetExecuteControlScript(btnSearch);
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Member",this.CurrentMemberID))
					Response.Redirect("noright.aspx");

				if(Request.QueryString["k"]!=null)
					txtKeyword.Text = Request.QueryString["k"];
				pager.PageSize = Convert.ToInt32(Main.GetValue("PageSize_MemberList"));
				BindData();
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
			this.lstMember.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.lstMember_ItemDataBound);
			this.lstMember.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.lstMember_ItemCommand);
			this.pager.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.pager_PageChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		protected void Delete_Load(object sender, System.EventArgs e)
		{
			((LinkButton)sender).Attributes["onclick"]="return confirm('您确定要删除这个会员吗？');";
		}
		public string ShowDenied(object IsDenied)
		{
			return Convert.ToBoolean(IsDenied)?"<font color='red'>禁用</font>":"正常";
		}
		private void BindData()
		{
			pager.RecordCount = member.Search_Count(txtKeyword.Text);
			lstMember.DataSource = member.Search(txtKeyword.Text,pager.PageSize,pager.CurrentPageIndex);
			lstMember.DataBind();
		}

		private void btnSearch_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(String.Format("Member_List.aspx?k={0}",txtKeyword.Text));
		}

		private void lstMember_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
			if(e.CommandName=="Delete")
			{
				member.Delete(e.CommandArgument);
			}
			Response.Redirect(String.Format("Member_List.aspx?k={0}&page={1}",txtKeyword.Text,pager.CurrentPageIndex));

		}

		private void pager_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
		{
			pager.CurrentPageIndex = e.NewPageIndex;
			BindData();
		}

		private void lstMember_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType==ListItemType.AlternatingItem)
			{
				if(((DataRowView)e.Item.DataItem).Row["Roles"].ToString().IndexOf("Administrator")>-1)
					((Label)e.Item.FindControl("lblMemberName_A")).ForeColor = Color.Red;
			}
			if(e.Item.ItemType==ListItemType.Item)
			{
				if(((DataRowView)e.Item.DataItem).Row["Roles"].ToString().IndexOf("Administrator")>-1)
					((Label)e.Item.FindControl("lblMemberName_I")).ForeColor = Color.Red;
			}
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			/*
			foreach(System.Web.UI.WebControls.RepeaterItem ri in lstMember.Items)
			{
				if(ri.ItemType == ListItemType.AlternatingItem)
				{
					CheckBoxPlus del_a = (CheckBoxPlus)ri.FindControl("del_a");
					if(del_a.Checked)
						member.Delete(del_a.CommandArgument);
				}
				if(ri.ItemType == ListItemType.Item)
				{
					CheckBoxPlus del_i = (CheckBoxPlus)ri.FindControl("del_i");
					if(del_i.Checked)
						member.Delete(del_i.CommandArgument);
				}
			}
			BindData();
			*/
		}
	}
}
