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 ComponentArt.Web.UI;
using SK.MemberBLL;

namespace SK.MemberWeb.admin
{
	/// <summary>
	/// Member_Edit 的摘要说明。
	/// </summary>
	public class Member_Edit : BasePage
	{
		protected System.Web.UI.WebControls.TextBox txtZip;
		protected System.Web.UI.WebControls.TextBox txtCity;
		protected System.Web.UI.WebControls.TextBox txtTrueName;
		protected System.Web.UI.WebControls.TextBox txtEmail2;
		protected System.Web.UI.WebControls.TextBox txtAnswer;
		protected System.Web.UI.WebControls.DropDownList dpQuestion;
		protected System.Web.UI.WebControls.DropDownList dpSex;
		protected System.Web.UI.WebControls.Label lblEmail;
		protected Dune5.WebControls.DateTimePicker cpBirthDay;
		MemberSystem member = new MemberSystem();
		QuestionSystem question = new QuestionSystem();
		CountrySystem country = new CountrySystem();
		protected System.Web.UI.WebControls.Label lblMemberName;
		protected System.Web.UI.WebControls.CheckBox chkManager;
		ProvinceSystem province = new ProvinceSystem();
		protected System.Web.UI.WebControls.CheckBoxList chkRight;
		RightMemberSystem rightmember = new RightMemberSystem();
		protected System.Web.UI.WebControls.DataList lstProduct;
	RightSystem right = new RightSystem();
		protected System.Web.UI.WebControls.DropDownList dpProvince;
		protected System.Web.UI.WebControls.DropDownList dpCountry;
		protected System.Web.UI.HtmlControls.HtmlTable tbAdmin;
		protected System.Web.UI.WebControls.Button btnRight;
		protected System.Web.UI.WebControls.CheckBox chkDenied;
		protected SK.MemberWeb.Controls.NumericTextBox txtPoint;
		protected System.Web.UI.WebControls.Button btnSave;
		ProductSystem product = new ProductSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			btnSave.Attributes["onclick"] = "return Check_Input();";
			tbAdmin.Visible = false;
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Member",this.CurrentMemberID))
					Response.Redirect("noright.aspx");

				ViewState["MemberID"] = Request.QueryString["id"];
				BindData();
			}
		}

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

		}
		#endregion
		private void BindData()
		{
			using(DataSet ds = question.List())
			{
				dpQuestion.Items.Add(new ListItem("[选择一个问题]","0"));
				foreach(DataRow dr in ds.Tables[0].Rows)
				{
					dpQuestion.Items.Add(new ListItem(dr["QuestionName"].ToString(),dr["QuestionID"].ToString()));
				}
			}
			using(DataSet ds = country.List())
			{
				foreach(DataRow dr in ds.Tables[0].Rows)
				{
					dpCountry.Items.Add(new ListItem(dr["CountryName"].ToString(),dr["CountryID"].ToString()));
				}
			}
			using(DataSet ds = province.List())
			{
				foreach(DataRow dr in ds.Tables[0].Rows)
				{
					dpProvince.Items.Add(new ListItem(dr["ProvinceName"].ToString(),dr["ProvinceID"].ToString()));
				}
			}

			DataRow drMember = member.Info(ViewState["MemberID"]);
			lblMemberName.Text = drMember["MemberName"].ToString();
			lblEmail.Text = drMember["Email"].ToString();
			dpSex.SelectedValue = drMember["Sex"].ToString();
			
			txtAnswer.Text = drMember["Answer"].ToString();
			cpBirthDay.Value = Convert.ToDateTime(drMember["BirthDay"]);
			txtEmail2.Text = drMember["Email2"].ToString();
			txtTrueName.Text = drMember["TrueName"].ToString();
			txtPoint.Text = drMember["Point"].ToString();
			//txtScore.Text = drMember["Score"].ToString();
			try
			{
				dpQuestion.SelectedValue = drMember["QuestionID"].ToString();
				dpCountry.SelectedValue = drMember["CountryID"].ToString();
				dpProvince.SelectedValue = drMember["ProvinceID"].ToString();
			}
			catch
			{
			}
			txtCity.Text = drMember["City"].ToString();
			txtZip.Text = drMember["Zip"].ToString();
			chkDenied.Checked = Convert.ToBoolean(drMember["IsDenied"]);

			chkManager.Checked = drMember["Roles"].ToString().IndexOf("Administrator")>-1;

			if(chkManager.Checked)
			{
				DataTable dt = new DataTable();
				dt.Columns.Add("ProductID",typeof(Int32));
				dt.Columns.Add("ProductName",typeof(String));
				dt.Columns.Add("ProductNO",typeof(String));

				DataRow dr = dt.NewRow();
				dr["ProductID"] = 0;
				dr["ProductName"] = "会员管理站";
				dt.Rows.Add(dr);

				using(DataSet ds = product.List())
				{
					foreach(DataRow drPro in ds.Tables[0].Rows)
					{
						dr = dt.NewRow();
						dr["ProductID"] = drPro["ProductID"];
						dr["ProductName"] = drPro["ProductName"];
						dr["ProductNO"] = drPro["ProductNO"];
						dt.Rows.Add(dr);
					}
				}
				lstProduct.DataSource = dt;
				lstProduct.DataBind();

				tbAdmin.Visible = true;
			}
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			member.Update(ViewState["MemberID"],"",lblMemberName.Text,txtTrueName.Text,dpSex.SelectedValue,txtEmail2.Text,dpQuestion.SelectedValue=="0"?null:dpQuestion.SelectedValue,txtAnswer.Text,cpBirthDay.Value,txtZip.Text,dpCountry.SelectedValue,dpProvince.SelectedValue,txtCity.Text,txtPoint.Text,0,chkDenied.Checked);
			member.SetRoles(ViewState["MemberID"],chkManager.Checked?"Administrator,User":"User");
			//Response.Write("修改成功");
			//Response.End();
			this.AlertAndRedir("修改成功","Member_Edit.aspx?id="+ViewState["MemberID"].ToString());
		}
		private void btnRight_Click(object sender, System.EventArgs e)
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("ProductID",typeof(Int32));
			dt.Columns.Add("RightID",typeof(Int32));
			dt.Columns.Add("MemberID",typeof(Int32));

			DataRow dr;

			foreach(DataListItem di in lstProduct.Items)
			{
				CheckBoxList chkRight = (CheckBoxList)di.FindControl("chkRight");
				foreach(ListItem li in chkRight.Items)
				{
					if(li.Selected)
					{
						dr = dt.NewRow();
						dr["ProductID"] = ((Label)di.FindControl("ProductID")).Text;
						dr["RightID"] = li.Value;
						dr["MemberID"] = ViewState["MemberID"];
						dt.Rows.Add(dr);
					}
				}
			}
			rightmember.Delete_Member(ViewState["MemberID"]);
			foreach(DataRow drRight in dt.Rows)
			{
				rightmember.Add(drRight["ProductID"],drRight["RightID"],drRight["MemberID"]);
			}
			this.AlertAndRedir("权限设置成功","Member_Edit.aspx?id="+ViewState["MemberID"].ToString());
		}

		private void lstProduct_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
		{
		if(e.Item.ItemType == ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
			{
				CheckBoxList chkRight = (CheckBoxList)e.Item.FindControl("chkRight");
				if(((DataRowView)e.Item.DataItem).Row["ProductID"].ToString()!="0")
					chkRight.DataSource = right.List(product.Info(((DataRowView)e.Item.DataItem).Row["ProductID"])["ProductTypeID"]);
				else
					chkRight.DataSource = right.List(0);
				chkRight.DataTextField = "RightName";
				chkRight.DataValueField = "RightID";
				chkRight.DataBind();
				using(DataSet ds = rightmember.List(((DataRowView)e.Item.DataItem).Row["ProductID"],ViewState["MemberID"]))
				{
					foreach(DataRow dr in ds.Tables[0].Rows)
					{
						foreach(ListItem li in chkRight.Items)
						{
							if(li.Value == dr["RightID"].ToString())
							{
								li.Selected = true;
							}
						}
					}
				}
			}
		}
	}
}
