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

namespace SK.MemberWeb.admin
{
	/// <summary>
	/// Payment_Member 的摘要说明。
	/// </summary>
	public class Payment_Member : BasePage
	{
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.DataGrid lstProduct;
		protected System.Web.UI.WebControls.TextBox txtMemberName;
		MemberSystem member = new MemberSystem();
		ProductSystem product = new ProductSystem();
		protected System.Web.UI.WebControls.Button btnSearch;
		protected SK.MemberWeb.Controls.NumericTextBox txtPoint;
		protected System.Web.UI.HtmlControls.HtmlTable tbInfo;
		protected System.Web.UI.WebControls.Button btnSave;
		MonthlySystem monthly = new MonthlySystem();
		WebLogSystem log = new WebLogSystem();
		RightMemberSystem rightmember = new RightMemberSystem();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			txtMemberName.Attributes["onkeypress"] = this.GetExecuteControlScript(btnSearch);
			btnSearch.Attributes["onclick"] = "return CheckInput();";
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Payment",this.CurrentMemberID))
					Response.Redirect("noright.aspx");

				tbInfo.Visible = false;
				//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.lstProduct.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.lstProduct_ItemDataBound);
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		private void BindData()
		{
			lstProduct.DataSource = product.List();
			lstProduct.DataBind();
		}

		private void lstProduct_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item)
			{
				DataRow dr = monthly.Info_ProductID(ViewState["MemberID"],((DataRowView)e.Item.DataItem).Row["ProductID"]);
				if(dr!=null)
					((DateTimePicker)e.Item.FindControl("cpExpiredTime")).Value = Convert.ToDateTime(dr["ExpiredTime"]);
				else
					((DateTimePicker)e.Item.FindControl("cpExpiredTime")).Value = DateTime.Now;
			}
		}

		private void btnSearch_Click(object sender, System.EventArgs e)
		{
			DataRow drMember = member.Info_Name(txtMemberName.Text);
			if(drMember==null)
			{
				tbInfo.Visible = false;
				this.DisplayAlert("此用户名不存在");
				return;
			}
			txtPoint.Text = drMember["Point"].ToString();
			ViewState["MemberID"] = drMember["MemberID"];
			tbInfo.Visible = true;
			BindData();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			member.UpdatePoint(ViewState["MemberID"],txtPoint.Text);
			for(int i=0;i<lstProduct.Items.Count;i++)
			{
				if(lstProduct.Items[i].ItemType==ListItemType.AlternatingItem||lstProduct.Items[i].ItemType==ListItemType.Item)
				{
					monthly.Update(ViewState["MemberID"],((Label)lstProduct.Items[i].FindControl("ProductID")).Text,((DateTimePicker)lstProduct.Items[i].FindControl("cpExpiredTime")).Value);
				}
			}
			//this.AlertAndRedir("保存成功","Payment_Member.aspx");
			log.Add(this.CurrentMemberID,String.Format("修改会员{0}的{1}和包月时间",ViewState["MemberID"].ToString(),Main.GetValue("PointName").ToString()),WebLogType.Money);
			this.DisplayAlert("保存成功");
		}
	}
}
