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;

namespace baoge.Admin
{
	/// <summary>
	/// edit 的摘要说明。
	/// </summary>
	public class edit : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.HyperLink homeLink;
		protected System.Web.UI.WebControls.Label Header;
		protected System.Web.UI.WebControls.Button SaveBtn;
		protected System.Web.UI.WebControls.Button CancelBtn;
		protected System.Web.UI.WebControls.Button SaveAs;
		protected System.Web.UI.WebControls.TextBox SaveAsTxt;
		protected System.Web.UI.WebControls.TextBox CodeText;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{	
				
				// check for the File querystring parameter
				if(Request.QueryString["File"] != null)
				{
					try
					{
						// read the text from the file
						FileStream fs=new FileStream(Server.MapPath(Server.UrlDecode(Request.QueryString["File"].ToString())),FileMode.Open,FileAccess.Read);
						StreamReader sr = new StreamReader(fs,System.Text.Encoding.Default);
						// fill the CodeText text field with the file text
						CodeText.Text = sr.ReadToEnd().ToString();
						sr.Close();
						// set the header appropriately
						Header.Text = "编辑文件: <b>" + Request.QueryString["File"].ToString() + "</b>";
					}
					catch(Exception ex)
					{
						FTP.ReportError("不能打开刚建立的文件", ex.ToString(), "需重新编编译一次");
					}
				}
				else
					Response.Write("没有找到此文件");
			}
		}

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

		}
		#endregion

		private void SaveBtn_Click(object sender, System.EventArgs e)
		{
			try
			{
				FileInfo file = new FileInfo(Server.MapPath(Server.UrlDecode(Request.QueryString["File"].ToString())));				
				StreamWriter sw = file.CreateText();
				// write the contents of the text field to the file
				sw.Write(CodeText.Text);
				sw.Close();	
				// notify user of success
				Response.Write("<script>alert('文件保存成功\\nName: " + Request.QueryString["File"].ToString() + "\\nTime: " + DateTime.Now.ToString() + "'); document.location=\"edit.aspx?File=" + Request.QueryString["File"].ToString() + "\";</script>");
			}
			catch(Exception ex)
			{
				FTP.ReportError("不能保存该文件.", ex.ToString(), "单击返回按钮继续");
			}
		}

		private void CancelBtn_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("webfile.aspx");	
		}

		private void SaveAs_Click(object sender, System.EventArgs e)
		{
			if(this.SaveAsTxt.Text == "")	// user gave no filename to save as
				Response.Write("<script>alert('请输入“另存为”的文件名!'); </script>");
			else
			{
				try
				{
					FileInfo file = new FileInfo(Server.MapPath(Session["Path"].ToString() + this.SaveAsTxt.Text));				
					StreamWriter sw = file.CreateText();
					// write the contents of the text field to the file
					sw.Write(CodeText.Text);
					sw.Close();																
					// notify user of success
					Response.Write("<script>alert('文件保存成功\\nName: " + SaveAsTxt.Text + "\\nTime: " + DateTime.Now.ToString() + "'); document.location=\"edit.aspx?File=" + Server.UrlEncode(Session["Path"].ToString() + SaveAsTxt.Text) + "\";</script>");
				}
				catch(Exception ex)
				{
					FTP.ReportError("不能保存该文件.", ex.ToString(), "当前目录下已有一个相同的文件.");
				}
			}
		}
	}
}
