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.SqlClient;

namespace WebApplication2
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public partial class UserModif : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				if(this.Session["USERNAME"]==null||this.Session["Role"]==null||this.Session["USERNAME"].ToString()==""||this.Session["Role"].ToString()=="")
				{
					Response.Redirect("login.aspx");
				}
				else
				{
					string [] role=this.Session["Role"].ToString().Split('#');
					bool exists=false;
					for(int i=0;i<role.Length;i++)
					{
                        if (role[i].Equals("18") || role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
					{
					
						this.Button2.Attributes.Add("onclick","return confirm(\"确认要修改!?\");");
						if((Request.Params["name"]!=null)&&(Request.Params["db"]!=null))
						{
							string database=Request.Params["db"].ToString();
							string username=Request.Params["name"].ToString();
							RLbind(username,database);
							this.Panel1.Visible=true;
						}
						else
						{
							this.Panel1.Visible=false;
						}
		
					
					}
					else
					{
						Response.Redirect("NoAuthor.aspx");
					}
				}
			}
			// 在此处放置用户代码以初始化页面
		}

		private void RLbind(string username,string database)
		{
			
					string Username=username;
				string Database=database;
				SB.ADOHelper help=new SB.ADOHelper();
				SqlDataReader rd=help.getReader("select UserName,Appearance from "+database+" where UserName='"+username+"'");
			if(rd.Read())
			{
				this.Panel1.Visible=true;
				this.Lbaccount.Text=username;
				this.Lbname.Text=rd["UserName"].ToString();
				switch(int.Parse(rd["Appearance"].ToString()))
				{
					case 1:this.RadioButtonList1.SelectedIndex=0;this.Label2.Text="正常";break;
					case 2:this.RadioButtonList1.SelectedIndex=1;this.Label2.Text="禁用";;break;
					case 3:this.RadioButtonList1.SelectedIndex=2;this.Label2.Text="待审核";break;
				}
				if(this.DropDownList1.Items[0].Value.ToString()==database)
				{
					this.DropDownList1.SelectedIndex=0;
				}
				else
				{
					this.DropDownList1.SelectedIndex=1;
				}
			}
			else
			{
				this.Panel1.Visible=false;
				Response.Write("<script>alert(\"没有找到相关数据!!请更换类户类型重试!!\")</script>");
				this.Label2.Text="";
				this.Lbaccount.Text="";
				this.Lbname.Text="";
                this.RadioButtonList1.SelectedIndex=-1;
			}
			rd.Close();
		}


		protected void Button1_Click(object sender, System.EventArgs e)
		{
			string database=this.DropDownList1.SelectedValue.ToString();
			string username=SB.ADOHelper.inputText(this.TextBox1.Text.ToString());
			RLbind(username,database);
		}


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

		}
		#endregion

		protected void Button2_Click(object sender, System.EventArgs e)
		{
			SB.ADOHelper help=new SB.ADOHelper();
			string username=this.Session["USERNAME"].ToString();
			string affairstype="审核";
			DateTime dt=DateTime.Now;
			string ip=Request.UserHostAddress.ToString();
			string url=Request.Url.ToString();
            string reurl = Request.UrlReferrer.ToString();
			string accesslog=SB.ADOHelper.inputText(this.TextBox2.Text.ToString());
			int appearance=Convert.ToInt32(this.RadioButtonList1.SelectedValue);
			string db=this.DropDownList1.SelectedValue.ToString();
			string Useraccount=this.Lbaccount.Text.ToString(); 
			SqlConnection conn=help.conn;
			if(conn.State==ConnectionState.Closed)
			{
				conn.Open();
			}
			  SqlTransaction trans=conn.BeginTransaction();
			   SqlCommand cmd=new SqlCommand("update "+db+" set Appearance='"+appearance+"' where UserName='"+Useraccount+"'",conn);
			 
			cmd.Transaction=trans;

			try
			{
				cmd.ExecuteNonQuery();
			    cmd.CommandText="insert into FinanceLogin(Affairs,AffairsType,UserName,DataAndTime,ip,webPage,fromPage,Remarks) values('3','"+affairstype+"','"+username+"','"+dt.ToString()+"','"+ip+"','"+url+"','"+reurl+"','"+accesslog+"')";					
				cmd.ExecuteNonQuery();
				trans.Commit();
				Response.Write("<script>alert(\"操作成功!!!\")</script>");
                RLbind(Useraccount, db);
				}
			catch
			{
                  trans.Rollback();
					Response.Write("<script>alert(\"操作失败!!!\")</script>");
			}

			    
		}
	}
}
