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>
	/// Payment_Hand 的摘要说明。
	/// </summary>
	public class Payment_Hand : BasePage
	{
		protected System.Web.UI.WebControls.Button btnPay;
		protected System.Web.UI.WebControls.DropDownList rbProduct;
		protected SK.MemberWeb.Controls.NumericTextBox txtPoint;
		protected SK.MemberWeb.Controls.NumericTextBox txtMonth;
		ProductSystem product = new ProductSystem();
		PaymentSystem payment = new PaymentSystem();
		protected System.Web.UI.WebControls.TextBox txtMemberName;
		protected System.Web.UI.HtmlControls.HtmlTable tbMonthly;
		protected System.Web.UI.HtmlControls.HtmlTable tbPoint;
		MemberSystem member = new MemberSystem();
		WebLogSystem log = new WebLogSystem();
		RightMemberSystem rightmember = new RightMemberSystem();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			btnPay.Attributes["onclick"]="return pay_CheckInput();";
			rbProduct.Attributes["onchange"] = "ChangeShow();";
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Payment",this.CurrentMemberID))
					Response.Redirect("noright.aspx");

				BindData();
			}
			if(rbProduct.SelectedValue=="0")
			{
				tbPoint.Attributes["style"]="display:";
				tbMonthly.Attributes["style"]="display:none";
			}
			else
			{
				tbPoint.Attributes["style"]="display:'none'";
				tbMonthly.Attributes["style"]="display:";
			}

		}

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

		}
		#endregion
		private void BindData()
		{
			using(DataSet ds = product.List_AllowMonthly())
			{
				rbProduct.Items.Add(new ListItem(String.Format("购买{0}(每元{1}个)",Main.GetValue("PointName").ToString(),Main.GetValue("MoneyPoint").ToString()),"0"));
				rbProduct.SelectedIndex = 0;
				foreach(DataRow dr in ds.Tables[0].Rows)
				{
					rbProduct.Items.Add(new ListItem(dr["ProductName"].ToString()+String.Format("(包月{0}/月)",dr["MonthlyPayment"].ToString()),dr["ProductID"].ToString()));
				}
			}
		}

		private void btnPay_Click(object sender, System.EventArgs e)
		{
			DataRow drMember = member.Info_Name(txtMemberName.Text);
			if(drMember==null)
			{
				this.DisplayAlert("此用户名不存在");
				return;
			}
			string v_amount = null;
			object ProductID = null;
			if(rbProduct.SelectedValue=="0")
			{
				v_amount = txtPoint.Text;
				//paylocal.Add(drMember["MemberID"].ToString(),null,v_amount,null,Convert.ToInt32(Main.GetValue("MoneyPoint"))*Convert.ToInt32(txtPoint.Text),null);
				payment.Add(drMember["MemberID"].ToString(),v_amount,null,null,null,null,null,null,null,Convert.ToInt32(Main.GetValue("MoneyPoint"))*Convert.ToInt32(txtPoint.Text),0);

			}
			else
			{
				foreach(DataRow dr in product.List().Tables[0].Rows)
				{
					if(rbProduct.SelectedValue==dr["ProductID"].ToString())
					{
						ProductID = dr["ProductID"];
						v_amount = (Convert.ToInt32(dr["MonthlyPayment"])*Convert.ToInt32(txtMonth.Text)).ToString();
						//paylocal.Add(drMember["MemberID"].ToString(),null,v_amount,ProductID,null,txtMonth.Text);
						payment.Add(drMember["MemberID"].ToString(),v_amount,null,null,null,null,null,ProductID,txtMonth.Text,null,0);
						break;
					}
				}
			}
			log.Add(this.CurrentMemberID,String.Format("为会员编号{0}续费{1}元",drMember["MemberID"].ToString(),v_amount),WebLogType.Money);
			this.AlertAndRedir("续费成功","Payment_Hand.aspx");
		}
	}
}
