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_Pay 的摘要说明。
	/// </summary>
	public class cu_Pay : BaseControl
	{
		ProductSystem product = new ProductSystem();
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder4;
		PayLocalSystem paylocal = new PayLocalSystem();
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder6;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder5;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder7;
		protected System.Web.UI.WebControls.Button btnPay;
		protected SK.MemberWeb.Controls.NumericTextBox txtMonth;
		protected SK.MemberWeb.Controls.NumericTextBox txtPoint;
		protected System.Web.UI.WebControls.DropDownList rbProduct;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbPoint;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbMonthly;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder3;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder2;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder1;
		MemberSystem member = new MemberSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			btnPay.Attributes["onclick"]="return pay_CheckInput();";
			rbProduct.Attributes["onchange"] = "ChangeShow();";
			if(!IsPostBack)
			{
				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.rbProduct.SelectedIndexChanged += new System.EventHandler(this.rbProduct_SelectedIndexChanged);
			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 rbProduct_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			/*if(rbProduct.SelectedValue == "0")
			{
				panelPoint.Visible = true;
			}
			else
			{
				panelPoint.Visible = false;
			}*/
		}

		private void btnPay_Click(object sender, System.EventArgs e)
		{
			string year=DateTime.Now.Year.ToString();
			string month="00"+DateTime.Now.Month.ToString();
			month=month.Substring(month.Length-2);
			string day="00"+DateTime.Now.Day.ToString();
			day=day.Substring(day.Length-2);

			string time=DateTime.Now.ToShortTimeString().Replace(" ","").Replace(":","");
			Random rnd=new Random();
			string rndstr=rnd.Next(100000).ToString();
			string v_oid=year+month+day+"-2010-"+time+rndstr;

	
			string v_mid=Main.GetValue("Pay_Account").ToString();
			//string v_oid="20040620-2010-000000099";
			string SEmail="";
			string v_rcvaddr="";
			string v_rcvtel="";
			string v_rcvpost="";
			string v_amount=String.Empty;
			object ProductID = null;
			if(rbProduct.SelectedValue=="0")
			{
				v_amount = txtPoint.Text;
				paylocal.Add(this.CurrentMemberID,v_oid,v_amount,null,Convert.ToInt32(Main.GetValue("MoneyPoint"))*Convert.ToInt32(txtPoint.Text),null);

			}
			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(this.CurrentMemberID,v_oid,v_amount,ProductID,null,txtMonth.Text);
						break;
					}
				}
			}
			DataRow drMember = member.Info(this.CurrentMemberID);
			string v_ymd =year+month+day;
			string v_orderstatus="0";
			string v_rcvname=this.CurrentMemberName.ToString();
			string v_ordername=this.CurrentMemberID.ToString();
			string v_orderemail=drMember["Email"].ToString();
			string v_moneytype="0";
			string v_url = Main.GetValue("Pay_Url").ToString();
			string md5=v_moneytype+v_ymd+v_amount+v_rcvtel+v_oid+v_mid+v_url+Main.GetValue("Pay_MD5").ToString();
			string v_md5info =FormsAuthentication.HashPasswordForStoringInConfigFile(md5,"md5").ToUpper();

			string str=String.Format("paybank.aspx?productname={0}&v_mid={1}&v_oid={2}&v_url={3}&v_md5info={4}&v_ordername={5}&v_amount={6}&SEmail={7}&STel={8}&v_rcvaddr={9}&v_rcvname={10}&v_rcvpost={11}&v_orderemail={12}&v_orderstatus={13}&v_ymd={14}&v_rcvtel={15}&v_moneytype={16}","缴费",v_mid,v_oid,v_url,v_md5info,Server.UrlEncode(v_ordername),v_amount,SEmail,v_rcvtel,v_rcvaddr,Server.UrlEncode(v_rcvname),v_rcvpost,v_orderemail,v_orderstatus,v_ymd,v_rcvtel,v_moneytype);
			Response.Redirect(str);
		}
	}
}
