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>
	/// Product_Edit 的摘要说明。
	/// </summary>
	public class Product_Edit : BasePage
	{
		protected System.Web.UI.WebControls.TextBox txtProductName;
		protected System.Web.UI.WebControls.TextBox txtProductNO;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.DropDownList dpProductType;
		ProductSystem product = new ProductSystem();
		protected System.Web.UI.WebControls.CheckBox chkAllow;
		protected SK.MemberWeb.Controls.NumericTextBox txtMoney;
		protected System.Web.UI.WebControls.TextBox txtHomePage;
		ProductTypeSystem producttype = new ProductTypeSystem();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			this.txtProductNO.Attributes["onkeypress"] = this.GetExecuteControlScript(btnSave);
			btnSave.Attributes["onclick"] = "return CheckInput();";
			if(!IsPostBack)
			{
				ViewState["ProductID"] = Request.QueryString["id"];
				BindDrop();
				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.btnSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		private void BindDrop()
		{
			dpProductType.DataSource = producttype.List();
			dpProductType.DataTextField = "ProductTypeName";
			dpProductType.DataValueField = "ProductTypeID";
			dpProductType.DataBind();
		}
		private void BindData()
		{
			DataRow dr = product.Info(ViewState["ProductID"]);
			txtProductName.Text = dr["ProductName"].ToString();
			dpProductType.SelectedValue = dr["ProductTypeID"].ToString();
			txtProductNO.Text = dr["ProductNO"].ToString();
			txtMoney.Text = dr["MonthlyPayment"].ToString();
			if(!dr.IsNull("HomePage"))
				txtHomePage.Text = dr["HomePage"].ToString();
			chkAllow.Checked = Convert.ToBoolean(dr["AllowMonthly"]);
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if(!txtHomePage.Text.ToLower().StartsWith("http://")&&!txtHomePage.Text.ToLower().StartsWith("https://"))
			{
				this.DisplayAlert("站点首页必须以http://或https://开始");
			}
			else
			{
				if(product.ExistsProductNO(ViewState["ProductID"],txtProductNO.Text))
				{
					this.DisplayAlert("此序列号已经使用，请另外选择序列号");
					return;
				}
				string homepage = txtHomePage.Text;
				if(!homepage.EndsWith("/"))
					homepage+="/";
				product.Update(ViewState["ProductID"],txtProductName.Text,dpProductType.SelectedValue,homepage,txtProductNO.Text,chkAllow.Checked,txtMoney.Text);
				this.AlertAndRedir("保存成功","Product_List.aspx");
			}
		}
	}
}
