namespace Employee_Directory
{
	
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

	public class EmpsRecord : System.Web.UI.Page
	
    { 

//更新窗口定义控件的声名
		protected CCUtility Utility;
		
		//更新记录表单、变量等的声名
		protected System.Web.UI.WebControls.Label emps_ValidationSummary;
		protected System.Web.UI.HtmlControls.HtmlInputButton emps_insert;
		protected System.Web.UI.HtmlControls.HtmlInputButton emps_update;
		protected System.Web.UI.HtmlControls.HtmlInputButton emps_delete;
		protected System.Web.UI.HtmlControls.HtmlInputButton emps_cancel;
		protected System.Web.UI.HtmlControls.HtmlInputHidden emps_emp_id;
		protected System.Web.UI.WebControls.TextBox emps_name;
		protected System.Web.UI.WebControls.TextBox emps_title;
		protected System.Web.UI.WebControls.TextBox emps_emp_login;
		protected System.Web.UI.WebControls.TextBox emps_emp_password;
		protected System.Web.UI.WebControls.DropDownList emps_emp_level;
		protected System.Web.UI.WebControls.DropDownList emps_dep_id;
		protected System.Web.UI.WebControls.TextBox emps_address;
		protected System.Web.UI.WebControls.TextBox emps_email;
		protected System.Web.UI.WebControls.TextBox emps_work_phone;
		protected System.Web.UI.WebControls.TextBox emps_home_phone;
		protected System.Web.UI.WebControls.TextBox emps_cell_phone;
		protected System.Web.UI.WebControls.CheckBox emps_manmonth;
		protected System.Web.UI.WebControls.TextBox emps_picture;
		
		//定义各表单事件保护字符串
		protected string emps_FormAction="EmpsGrid.aspx?";
		protected System.Web.UI.HtmlControls.HtmlInputHidden p_emps_emp_id;protected String[] emps_emp_level_lov = "0;None;3;Admin".Split(new Char[] {';'});

//初始化事件	
	public EmpsRecord()
	{
	this.Init += new System.EventHandler(Page_Init);
	}
// EmpsRecord中的自定义包含控件结束

	public void ValidateNumeric(object source, ServerValidateEventArgs args) {
			try{
				Decimal temp=Decimal.Parse(args.Value);
				args.IsValid=true;
		        }catch{
				args.IsValid=false;	}
		}

//定义登录窗口显示控件过程
//初始化页面过程，创建一个Utility实例，并调用其相应的各方法
        protected void Page_Load(object sender, EventArgs e)
        {	
		Utility=new CCUtility(this);
		Utility.CheckSecurity(3);
		// 完成窗口安全验证

		if (!IsPostBack){
			
			p_emps_emp_id.Value = Utility.GetParam("emp_id");Page_Show(sender, e);
		}
	}

//页面关闭过程
	protected void Page_Unload(object sender, EventArgs e)
	{
		if(Utility!=null) Utility.DBClose();
	}

//窗口中控件定义事件处理过程
	protected void Page_Init(object sender, EventArgs e)
	{
		InitializeComponent();
	}
        private void InitializeComponent()
        {
		this.Load += new System.EventHandler(this.Page_Load);
		this.Unload += new System.EventHandler(this.Page_Unload);
emps_insert.ServerClick += new System.EventHandler (this.emps_Action);
		emps_update.ServerClick += new System.EventHandler (this.emps_Action);
		emps_delete.ServerClick += new System.EventHandler (this.emps_Action);
		emps_cancel.ServerClick += new System.EventHandler (this.emps_Action);
		
        }

//定义整体显示页面过程
        protected void Page_Show(object sender, EventArgs e)
        {
		emps_Show();
		
        }



// EmpsRecord Show end
//完成表单初始化


//定义控制函数，控制按钮的的显示与否
//如，当插入记录时，应显示插入、取消按钮，而修改时应显示修改、删除、取消按钮
private bool emps_Validate(){
	bool result=true;
	emps_ValidationSummary.Text="";

	for(int i=0;i<Page.Validators.Count;i++){
		if(((System.Web.UI.WebControls.BaseValidator)Page.Validators[i]).ID.ToString().StartsWith("emps")){
			if(!Page.Validators[i].IsValid){
				emps_ValidationSummary.Text+=Page.Validators[i].ErrorMessage+"<br>";
				result=false;
			}
		}
	}

	emps_ValidationSummary.Visible=(!result);
	return result;
}

//更新记录显示过程
void emps_Show() {
	
	Utility.buildListBox(emps_emp_level.Items,emps_emp_level_lov,null,"");
Utility.buildListBox(emps_dep_id.Items,"select dep_id,name from deps order by 2","dep_id","name",null,"");

	bool ActionInsert=true;
	
	if (p_emps_emp_id.Value.Length > 0 ) {
		string sWhere = "";
		
		sWhere += "emp_id=" + CCUtility.ToSQL(p_emps_emp_id.Value, CCUtility.FIELD_TYPE_Number);
		
		string sSQL = "select * from emps where " + sWhere;
		OleDbDataAdapter dsCommand = new OleDbDataAdapter(sSQL, Utility.Connection);
		DataSet ds = new DataSet();
		DataRow row;

		if (dsCommand.Fill(ds, 0, 1, "emps") > 0) {
		row = ds.Tables[0].Rows[0];
				
	emps_emp_id.Value = CCUtility.GetValue(row, "emp_id");
		

	emps_name.Text = CCUtility.GetValue(row, "name");
	emps_title.Text = CCUtility.GetValue(row, "title");
	emps_emp_login.Text = CCUtility.GetValue(row, "emp_login");
	emps_emp_password.Text = CCUtility.GetValue(row, "emp_password");
	
		{string s;
		s=CCUtility.GetValue(row, "emp_level");
		
		try {emps_emp_level.SelectedIndex=emps_emp_level.Items.IndexOf(emps_emp_level.Items.FindByValue(s));
		}catch{}}
		

	
		{string s;
		s=CCUtility.GetValue(row, "dep_id");
		
		try {emps_dep_id.SelectedIndex=emps_dep_id.Items.IndexOf(emps_dep_id.Items.FindByValue(s));
		}catch{}}
		

	emps_address.Text = CCUtility.GetValue(row, "address");
		

	emps_email.Text = CCUtility.GetValue(row, "email");
	emps_work_phone.Text = CCUtility.GetValue(row, "work_phone");
	emps_home_phone.Text = CCUtility.GetValue(row, "home_phone");
	emps_cell_phone.Text = CCUtility.GetValue(row, "cell_phone");
	emps_manmonth.Checked=CCUtility.GetValue(row, "manmonth").ToLower().Equals("1".ToLower());
	
	emps_picture.Text = CCUtility.GetValue(row, "picture");
	emps_insert.Visible=false;
		ActionInsert=false;

	}}

		if(ActionInsert){

		String pValue;

		pValue = Utility.GetParam("emp_id");emps_emp_id.Value = pValue;emps_delete.Visible=false;
	emps_update.Visible=false;
	
}
	}

//插入新记录事件代码
bool emps_insert_Click(Object Src, EventArgs E) {
		string sSQL="";
		bool bResult=emps_Validate();
		
		{int iCount = Utility.DlookupInt("emps", "count(*)", "emp_login=" + CCUtility.ToSQL(Utility.GetParam("emps_emp_login"), CCUtility.FIELD_TYPE_Text));
		if (iCount!=0){
			emps_ValidationSummary.Visible=true;
			emps_ValidationSummary.Text+="The value in field Login is already in database."+"<br>";
			bResult=false;}}
		
		if(bResult){	
		        
			
			string p2_name=CCUtility.ToSQL(Utility.GetParam("emps_name"), CCUtility.FIELD_TYPE_Text) ;
			string p2_title=CCUtility.ToSQL(Utility.GetParam("emps_title"), CCUtility.FIELD_TYPE_Text) ;
			string p2_emp_login=CCUtility.ToSQL(Utility.GetParam("emps_emp_login"), CCUtility.FIELD_TYPE_Text) ;
			string p2_emp_password=CCUtility.ToSQL(Utility.GetParam("emps_emp_password"), CCUtility.FIELD_TYPE_Text) ;
			string p2_emp_level=CCUtility.ToSQL(Utility.GetParam("emps_emp_level"), CCUtility.FIELD_TYPE_Number) ;
			string p2_dep_id=CCUtility.ToSQL(Utility.GetParam("emps_dep_id"), CCUtility.FIELD_TYPE_Number) ;
			string p2_address=CCUtility.ToSQL(Utility.GetParam("emps_address"), CCUtility.FIELD_TYPE_Text) ;
			string p2_email=CCUtility.ToSQL(Utility.GetParam("emps_email"), CCUtility.FIELD_TYPE_Text) ;
			string p2_work_phone=CCUtility.ToSQL(Utility.GetParam("emps_work_phone"), CCUtility.FIELD_TYPE_Text) ;
			string p2_home_phone=CCUtility.ToSQL(Utility.GetParam("emps_home_phone"), CCUtility.FIELD_TYPE_Text) ;
			string p2_cell_phone=CCUtility.ToSQL(Utility.GetParam("emps_cell_phone"), CCUtility.FIELD_TYPE_Text) ;
			string c1_manmonth=CCUtility.getCheckBoxValue(Utility.GetParam("emps_manmonth"), "1", "0", CCUtility.FIELD_TYPE_Number) ;
			string p2_picture=CCUtility.ToSQL(Utility.GetParam("emps_picture"), CCUtility.FIELD_TYPE_Text) ;

			sSQL = "insert into emps (" +
				"name," +
				"title," +
				"emp_login," +
				"emp_password," +
				"emp_level," +
				"dep_id," +
				"address," +
				"email," +
				"work_phone," +
				"home_phone," +
				"cell_phone," +
				"manmonth," +
				"picture)" +
				" values (" +
				p2_name + "," + 
				p2_title + "," + 
				p2_emp_login + "," + 
				p2_emp_password + "," + 
				p2_emp_level + "," + 
				p2_dep_id + "," + 
				p2_address + "," + 
				p2_email + "," + 
				p2_work_phone + "," + 
				p2_home_phone + "," + 
				p2_cell_phone + "," + 
				c1_manmonth + "," + 
				p2_picture + ")";
			OleDbCommand cmd = new OleDbCommand(sSQL, Utility.Connection);
			try {
				cmd.ExecuteNonQuery();
			} catch(Exception e) {
				emps_ValidationSummary.Text += e.Message;
				emps_ValidationSummary.Visible = true;
				return false;
			}
		}
		return bResult;
	}



//更新记录事件代码
	bool emps_update_Click(Object Src, EventArgs E) {
	    string sWhere = "";
		string sSQL ="";
		
		bool bResult=emps_Validate();
		if(bResult){
		
	        if (p_emps_emp_id.Value.Length > 0) {
        	    sWhere = sWhere + "emp_id=" + CCUtility.ToSQL(p_emps_emp_id.Value, CCUtility.FIELD_TYPE_Number);
		    }
		
	        {int iCount = Utility.DlookupInt("emps", "count(*)", "emp_login=" + CCUtility.ToSQL(Utility.GetParam("emps_emp_login"), CCUtility.FIELD_TYPE_Text) + " and not(" + sWhere + ")");
		if (iCount!=0){
		    emps_ValidationSummary.Visible=true;
		    emps_ValidationSummary.Text+="The value in field Login is already in database."+"<br>";
		    bResult=false;}}
		
		if (bResult){
		
		sSQL = "update emps set " +
		"[name]=" +CCUtility.ToSQL(Utility.GetParam("emps_name"),CCUtility.FIELD_TYPE_Text)  +
		",[title]=" +CCUtility.ToSQL(Utility.GetParam("emps_title"),CCUtility.FIELD_TYPE_Text)  +
		",[emp_login]=" +CCUtility.ToSQL(Utility.GetParam("emps_emp_login"),CCUtility.FIELD_TYPE_Text)  +
		",[emp_password]=" +CCUtility.ToSQL(Utility.GetParam("emps_emp_password"),CCUtility.FIELD_TYPE_Text)  +
		",[emp_level]=" +CCUtility.ToSQL(Utility.GetParam("emps_emp_level"),CCUtility.FIELD_TYPE_Number)  +
		",[dep_id]=" +CCUtility.ToSQL(Utility.GetParam("emps_dep_id"),CCUtility.FIELD_TYPE_Number)  +
		",[address]=" +CCUtility.ToSQL(Utility.GetParam("emps_address"),CCUtility.FIELD_TYPE_Text)  +
		",[email]=" +CCUtility.ToSQL(Utility.GetParam("emps_email"),CCUtility.FIELD_TYPE_Text)  +
		",[work_phone]=" +CCUtility.ToSQL(Utility.GetParam("emps_work_phone"),CCUtility.FIELD_TYPE_Text)  +
		",[home_phone]=" +CCUtility.ToSQL(Utility.GetParam("emps_home_phone"),CCUtility.FIELD_TYPE_Text)  +
		",[cell_phone]=" +CCUtility.ToSQL(Utility.GetParam("emps_cell_phone"),CCUtility.FIELD_TYPE_Text)  +
		",[manmonth]=" +CCUtility.getCheckBoxValue(Utility.GetParam("emps_manmonth"),"1","0",CCUtility.FIELD_TYPE_Number)  +
		",[picture]=" +CCUtility.ToSQL(Utility.GetParam("emps_picture"),CCUtility.FIELD_TYPE_Text) ;

		
	        sSQL = sSQL + " where " + sWhere;
		
//创建与数据库联结的命令集
		OleDbCommand cmd = new OleDbCommand(sSQL, Utility.Connection);
			try {
				cmd.ExecuteNonQuery();
			} catch(Exception e) {
				emps_ValidationSummary.Text += e.Message;
				emps_ValidationSummary.Visible = true;
				return false;
			}
		}
		}
		return bResult;
	}

//删除记录代码
bool emps_delete_Click(Object Src, EventArgs E) {
	string sWhere = "";
	
	if (p_emps_emp_id.Value.Length > 0) {
		sWhere += "emp_id=" + CCUtility.ToSQL(p_emps_emp_id.Value, CCUtility.FIELD_TYPE_Number);
	}
	
	string sSQL = "delete from emps where " + sWhere;
	OleDbCommand cmd = new OleDbCommand(sSQL, Utility.Connection);
	try {
		cmd.ExecuteNonQuery();
	} catch(Exception e) {
		emps_ValidationSummary.Text += e.Message;
		emps_ValidationSummary.Visible = true;
		return false;
	}
	return true;
}

//取消更新代码
bool emps_cancel_Click(Object Src, EventArgs E) {
	return true;
	}


//为各按钮点击事件参数函数代码
void emps_Action(Object Src, EventArgs E) {
bool bResult=true;
if(((HtmlInputButton)Src).ID.IndexOf("insert")>0) bResult=emps_insert_Click(Src,E);
if(((HtmlInputButton)Src).ID.IndexOf("update")>0) bResult=emps_update_Click(Src,E);
if(((HtmlInputButton)Src).ID.IndexOf("delete")>0) bResult=emps_delete_Click(Src,E);
if(((HtmlInputButton)Src).ID.IndexOf("cancel")>0) bResult=emps_cancel_Click(Src,E);

if(bResult)Response.Redirect(emps_FormAction+"");
}

    }
}