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>
	/// Mail_SendGroup 的摘要说明。
	/// </summary>
	public class Mail_SendGroup : BasePage
	{
		protected System.Web.UI.WebControls.Button btnSend;
		protected System.Web.UI.WebControls.RadioButtonList InHtml;
		protected System.Web.UI.WebControls.TextBox Content;
		protected System.Web.UI.WebControls.TextBox EmailTest;
		protected System.Web.UI.WebControls.TextBox Subject;
		protected System.Web.UI.WebControls.DropDownList dpMailingList;
		protected System.Web.UI.WebControls.DropDownList dpGroup;
		protected System.Web.UI.WebControls.Button btnSendTest;
		MailingListSystem mailinglist = new MailingListSystem();
		MailingListMemberSystem mailinglistmember = new MailingListMemberSystem();
		RightMemberSystem rightmember = new RightMemberSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Mail",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.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			this.btnSendTest.Click += new System.EventHandler(this.btnSendTest_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		private void BindData()
		{
			dpMailingList.DataSource = mailinglist.List();
			dpMailingList.DataTextField = "Name";
			dpMailingList.DataValueField = "MailingListID";
			dpMailingList.DataBind();
		}

		private void btnSendTest_Click(object sender, System.EventArgs e)
		{
			
		}

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			int emailcount = 0;
			int successemail =0;
			foreach(DataRow dr in mailinglistmember.List(dpMailingList.SelectedValue).Tables[0].Rows)
			{
				string subject = Subject.Text;
				string body = Content.Text;
				emailcount++;
				try
				{
					//subject = subject.Replace("[[username]]",Email.Text).Replace("[[password]]",Password.Text);
					//body = body.Replace("[[username]]",Email.Text).Replace("[[password]]",Password.Text);
					bool inhtml = Convert.ToBoolean(Convert.ToInt16(InHtml.SelectedValue));
					Utils.SendMail(dr["EmailAddress"].ToString(),dr["EmailAddress"].ToString(),subject,body,inhtml?PatNorSoft.Mail.Message.Build.BodyPartFormat.HTML:PatNorSoft.Mail.Message.Build.BodyPartFormat.Plain);
					successemail++;
				}
				catch
				{
					
				}
			}
			this.AlertAndRedir(String.Format("共{0}个Email，成功发送{1}。",emailcount,successemail),"Mail_SendGroup.aspx");
		}
	}
}
