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 System.IO;
using SK.MemberBLL;

namespace SK.MemberWeb.admin
{
	/// <summary>
	/// MainSet 的摘要说明。
	/// </summary>
	public class MainSet : BasePage
	{
		protected System.Web.UI.WebControls.Button btnSave;
		MainSystem main = new MainSystem();
		WebLogSystem log = new WebLogSystem();
		protected System.Web.UI.WebControls.TextBox FilePicture_MaxNum;
		protected System.Web.UI.WebControls.TextBox VoteChoice_MaxNum;
		protected SK.MemberWeb.Controls.NumericTextBox PageSize_PaymentList;
		protected SK.MemberWeb.Controls.NumericTextBox PageSize_WebLog;
		protected System.Web.UI.WebControls.CheckBox NeedValidEmail;
		protected SK.MemberWeb.Controls.NumericTextBox PageSize_MemberList;
		protected System.Web.UI.WebControls.DropDownList dpTemplate;
		protected System.Web.UI.WebControls.TextBox txtPay_MD5;
		protected System.Web.UI.WebControls.TextBox txtPay_Url;
		protected System.Web.UI.WebControls.TextBox txtPay_Account;
		protected System.Web.UI.WebControls.TextBox Regex_MemberName;
		protected System.Web.UI.WebControls.TextBox PointName;
		protected SK.MemberWeb.Controls.NumericTextBox RegPoint;
		RightMemberSystem rightmember = new RightMemberSystem();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			txtPay_Url.Attributes["onkeypress"] = this.GetExecuteControlScript(btnSave);
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Main",this.CurrentMemberID))
					Response.Redirect("noright.aspx");

				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 BindData()
		{
			string[] dirs = System.IO.Directory.GetDirectories(Server.MapPath("../templates"));
			foreach(string dir in dirs)
			{
				DirectoryInfo di = new DirectoryInfo(dir);
				dpTemplate.Items.Add(new ListItem(di.Name,di.Name));
			}
			try
			{
				dpTemplate.SelectedValue = Main.GetValue("TemplateName").ToString();
			}
			catch
			{
			}

			PageSize_PaymentList.Text = Main.GetValue("PageSize_PaymentList").ToString();
			PageSize_WebLog.Text = Main.GetValue("PageSize_WebLog").ToString();
			PageSize_MemberList.Text = Main.GetValue("PageSize_MemberList").ToString();
			NeedValidEmail.Checked = Convert.ToBoolean(Convert.ToInt32(Main.GetValue("NeedValidEmail")));
			Regex_MemberName.Text = Main.GetValue("Regex_MemberName").ToString();
			txtPay_Account.Text = Main.GetValue("Pay_Account").ToString();
			txtPay_MD5.Text = Main.GetValue("Pay_MD5").ToString();
			txtPay_Url.Text = Main.GetValue("Pay_Url").ToString();
			PointName.Text = Main.GetValue("PointName").ToString();
			RegPoint.Text = Main.GetValue("RegPoint").ToString();
			/*if(Main.GetValue("Score2Point")==null)
			{
				MainSystem main = new MainSystem();
				main.Add("Score2Point","20");
			}
			Score2Point.Text = Main.GetValue("Score2Point").ToString();*/
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if(!Utils.IsValidInt(PageSize_PaymentList.Text))
			{
				this.DisplayAlert("后台公告列表页每页行数必须为整数");
				return;
			}
			if(!Utils.IsValidInt(PageSize_WebLog.Text))
			{
				this.DisplayAlert("后台专题列表页每页行数必须为整数");
				return;
			}
			if(!Utils.IsValidInt(PageSize_MemberList.Text))
			{
				this.DisplayAlert("前台专题列表页每页行数必须为整数");
				return;
			}
			main.Update("TemplateName",dpTemplate.SelectedValue);
			main.Update("PageSize_PaymentList",PageSize_PaymentList.Text);
			main.Update("PageSize_WebLog",PageSize_WebLog.Text);
			main.Update("PageSize_MemberList",PageSize_MemberList.Text);
			main.Update("NeedValidEmail",NeedValidEmail.Checked?1:0);
			main.Update("Pay_Account",txtPay_Account.Text);
			main.Update("Pay_MD5",txtPay_MD5.Text);
			main.Update("Pay_Url",txtPay_Url.Text);
			main.Update("Regex_MemberName",Regex_MemberName.Text);
			main.Update("PointName",PointName.Text);
			main.Update("RegPoint",RegPoint.Text);

			log.Add(this.CurrentMemberID,"修改了系统基本参数",WebLogType.Update);
			this.AlertAndRedir("基本参数保存成功","MainSet.aspx");
		}
	}
}
