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>
	/// Mail_Export 的摘要说明。
	/// </summary>
	public class Mail_Export : BasePage
	{
		protected System.Web.UI.WebControls.Button btnExport;
		RightMemberSystem rightmember = new RightMemberSystem();
		protected System.Web.UI.WebControls.HyperLink lnkEmail;
		MailingListMemberSystem member = new MailingListMemberSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Mail",this.CurrentMemberID))
					Response.Redirect("noright.aspx");

			}
		}

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

		}
		#endregion

		private void btnExport_Click(object sender, System.EventArgs e)
		{
			using(DataSet ds = member.List(1))
			{
				string rnd = Utils.CreateRndStr(5);
				string path = Server.MapPath("~/admin/emaillist/"+rnd+".txt");
				if (!File.Exists(path)) 
				{
					// Create a file to write to.
					using (StreamWriter sw = File.CreateText(path)) 
					{
						foreach(DataRow dr in ds.Tables[0].Rows)
						{
							sw.WriteLine(dr["EmailAddress"].ToString());
						}
					}
				}
				btnExport.Visible = false;
				lnkEmail.Text = "右键另存邮件列表";
				lnkEmail.NavigateUrl = "~/admin/emaillist/"+rnd+".txt";
			}
		}
	}
}
