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.Data.OleDb;
using System.IO;
using System.Web.Caching;
using System.Text;
namespace baoge.Admin
{
	/// <summary>
	/// Admin_Login 的摘要说明。
	/// </summary>
	public class Admin_Login : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ImageButton Submit;
		protected System.Web.UI.WebControls.TextBox AdminName;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.TextBox password;
//		protected System.Web.UI.WebControls.TextBox textbox1;
		protected System.Web.UI.WebControls.TextBox Textbox1;
		protected System.Web.UI.WebControls.ImageButton ImageButton1;
	    protected string sql;
		protected other isadmin=new other();
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			
		}


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

		}
		#endregion

		

		private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.Textbox1.Text="";
			this.password.Text="";
		}


//		private void log()
//		{
////			if(!Page.IsValid) 
////			{
////				ErrorMsg.Text = "必需输入登录信息";
////				return;
////			}
//			DataSet ds = new DataSet();
//			FileStream fs = new FileStream(Server.MapPath("LoginMsg.xml"),FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//			StreamReader reader = new StreamReader(fs);
//			ds.ReadXml(reader);
//			fs.Close();
//			DataTable dt = ds.Tables[0];
//			DataRow newrow = dt.NewRow();
//			newrow["custom_username"] = this.Textbox1.Text;
//			newrow["custom_ip"] = Request.UserHostAddress;
//			newrow["login_time"] = DateTime.Now.ToString();
//			fs = new FileStream(Server.MapPath("LoginMsg.xml"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
//			TextWriter writer = new StreamWriter(fs);
//			writer = TextWriter.Synchronized(writer);
//			ds.Tables[0].Rows.Add(newrow);
//			ds.WriteXml(writer);
//			writer.Close();
//			Cache.Remove("MyData");
//		}
		private void Submit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			string inputname=this.Textbox1.Text;
			System.IO.StreamWriter fs;
			//取得web.config中的数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义数据库连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);

			string logfile=System.Configuration.ConfigurationSettings.AppSettings["logfile"];
			string log="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+logfile;
			//创建数据库连接对象
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
			try
			{
				ACconn.Open();
				//定义SQL语句
				string ACsql="select id,Purview from admin where AdminName='"+inputname+"' and  Password='"+password.Text+"'";

				//创建Command对像
				OleDbCommand ACcomm=new OleDbCommand(ACsql,ACconn);
				//实例化DataReader()对像
				OleDbDataReader ACdr=ACcomm.ExecuteReader();
				if(ACdr.Read())
				{
					Session["AdminName"]=inputname;
					Session["Password"]=password.Text;
					Session["Purview"]=ACdr["Purview"];
					ACdr.Close();
//					this.log();
					fs=new StreamWriter(Server.MapPath(log),true,Encoding.Default);
					fs.WriteLine("ip:"+isadmin.getip()+"在时间:"+System.DateTime.Now.ToString()+"使用合法用户名:"+inputname+"登陆成功");
					fs.Close();
					Response.Redirect("../Admin/Admin_index.aspx");

				}
//				else
//				{
//					ErrorStr.Text="用户名或密码错误";
//
//				}

			}
			catch(Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				ACconn.Close();
			}
		}
	}
}
