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 EmpsGrid : System.Web.UI.Page
	
    { 

//职工管理窗口定义控件的声名
		protected CCUtility Utility;
		
		//表格表单，职工变量等的声名
		protected System.Web.UI.HtmlControls.HtmlTableRow emps_no_records;
		protected string emps_sSQL;
		protected string emps_sCountSQL;
		protected int emps_CountPage;
		protected CCPager emps_Pager;protected System.Web.UI.WebControls.LinkButton emps_insert;
		protected System.Web.UI.WebControls.Repeater emps_Repeater;
		protected int i_emps_curpage=1;	
	
		//查询表单、变量等的声名
		protected System.Web.UI.WebControls.Button Search_search_button;
		protected System.Web.UI.WebControls.TextBox Search_emp_login;
		protected System.Web.UI.WebControls.TextBox Search_name;
		protected System.Web.UI.WebControls.DropDownList Search_manmonth;
		
		//定义各表单事件保护字符串
		protected string emps_FormAction="EmpsRecord.aspx?";
		protected String[] emps_emp_level_lov = "0;;3;Admin".Split(new Char[] {';'});protected String[] emps_manmonth_lov = "0;;1;Yes".Split(new Char[] {';'});
		protected string Search_FormAction="EmpsGrid.aspx?";
		protected String[] Search_manmonth_lov = ";All;0;No;1;Yes".Split(new Char[] {';'});

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

	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){
			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.Click += new System.EventHandler (this.emps_insert_Click);
		emps_Pager.NavigateCompleted+=new NavigateCompletedHandler(this.emps_pager_navigate_completed);
		Search_search_button.Click += new System.EventHandler (this.Search_search_Click);
		
        }

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



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

//定义表格每页显示职工记录条数
const int emps_PAGENUM = 20;

//创建数据库种职工数据记录的数据源集合
ICollection emps_CreateDataSource() {

       
       //开始定义查询数据Sql字符串
	emps_sSQL = "";
	emps_sCountSQL = "";
	string sWhere = "", sOrder = "";

	bool HasParam = false;

	//建立排序方式
	sOrder = " order by e.emp_login Asc";
	if(Utility.GetParam("Formemps_Sorting").Length>0&&!IsPostBack)
	{ViewState["SortColumn"]=Utility.GetParam("Formemps_Sorting");
	 ViewState["SortDir"]="ASC";}
	if(ViewState["SortColumn"]!=null) sOrder = " ORDER BY " + ViewState["SortColumn"].ToString()+" "+ViewState["SortDir"].ToString();
	
	//建立职工属性
	System.Collections.Specialized.StringDictionary Params =new System.Collections.Specialized.StringDictionary();
	
	
	if(!Params.ContainsKey("emp_login")){
	string temp=Utility.GetParam("emp_login");
	Params.Add("emp_login",temp);}
	
	if(!Params.ContainsKey("manmonth")){
	string temp=Utility.GetParam("manmonth");
	if (Utility.IsNumeric(null,temp) && temp.Length>0) { temp = CCUtility.ToSQL(temp, CCUtility.FIELD_TYPE_Number);} else {temp = "";}
	Params.Add("manmonth",temp);}
	
	if(!Params.ContainsKey("name")){
	string temp=Utility.GetParam("name");
	Params.Add("name",temp);}
	
	  if (Params["emp_login"].Length>0) {
	    HasParam = true;
	    sWhere +="e.[emp_login] like '%" + Params["emp_login"].Replace( "'", "''") +  "%'";
	  }
	  if (Params["manmonth"].Length>0) {
	    if (sWhere.Length >0) sWhere +=" and ";
	    HasParam = true;
	    sWhere +="e.[manmonth]=" + Params["manmonth"];
	  }
	  if (Params["name"].Length>0) {
	    if (sWhere.Length >0) sWhere +=" and ";
	    HasParam = true;
	    sWhere +="e.[name] like '%" + Params["name"].Replace( "'", "''") +  "%'";
	  }

	
	  if(HasParam)
	    sWhere = " WHERE (" + sWhere + ")";

	//定制Sql语句

	emps_sSQL = "select [e].[emp_id] as e_emp_id, " +
    "[e].[emp_level] as e_emp_level, " +
    "[e].[emp_login] as e_emp_login, " +
    "[e].[manmonth] as e_manmonth, " +
    "[e].[name] as e_name " +
    " from [emps] e ";
	
	//装配出完整的Sql语句

	  emps_sSQL = emps_sSQL + sWhere + sOrder;
	  if (emps_sCountSQL.Length== 0) {
	    int iTmpI = emps_sSQL.ToLower().IndexOf("select ");
	    int iTmpJ = emps_sSQL.ToLower().LastIndexOf(" from ")-1;
	    emps_sCountSQL = emps_sSQL.Replace(emps_sSQL.Substring(iTmpI + 7, iTmpJ-6), " count(*) ");
	    iTmpI = emps_sCountSQL.ToLower().IndexOf(" order by");
	    if (iTmpI > 1) emps_sCountSQL = emps_sCountSQL.Substring(0, iTmpI);
	  }
	  
	  
	//联结数据库，读取数据	
	OleDbDataAdapter command = new OleDbDataAdapter(emps_sSQL, Utility.Connection);
	DataSet ds = new DataSet();
	
	command.Fill(ds, (i_emps_curpage - 1) * emps_PAGENUM, emps_PAGENUM,"emps");
	OleDbCommand ccommand = new OleDbCommand(emps_sCountSQL, Utility.Connection);
	int PageTemp=(int)ccommand.ExecuteScalar();
	emps_Pager.MaxPage=(PageTemp%emps_PAGENUM)>0?(int)(PageTemp/emps_PAGENUM)+1:(int)(PageTemp/emps_PAGENUM);
	bool AllowScroller=emps_Pager.MaxPage==1?false:true;
	
	DataView Source;
        Source = new DataView(ds.Tables[0]);

		if (ds.Tables[0].Rows.Count == 0){
			emps_no_records.Visible = true;
			AllowScroller=false;}
		else
			{emps_no_records.Visible = false;
			AllowScroller=AllowScroller&&true;}
		
		emps_Pager.Visible=AllowScroller;
		return Source;
		
	}

	//显示哪一页的数据
	protected void emps_pager_navigate_completed(Object Src, int CurrPage)
		{
			i_emps_curpage=CurrPage;
			emps_Bind();
		}
	
	//绑定数据
	void emps_Bind() {
		emps_Repeater.DataSource = emps_CreateDataSource();
		emps_Repeater.DataBind();
		
	}

	//加入新职工记录过程
	void emps_insert_Click(Object Src, EventArgs E) {
		string sURL = emps_FormAction+"emp_login=" + Server.UrlEncode(Utility.GetParam("emp_login")) + "&manmonth=" + Server.UrlEncode(Utility.GetParam("manmonth")) + "&name=" + Server.UrlEncode(Utility.GetParam("name")) + "&";
		Response.Redirect(sURL);
	}
	//职工记录排序方式改变
	protected void emps_SortChange(Object Src, EventArgs E) {
		if(ViewState["SortColumn"]==null || ViewState["SortColumn"].ToString()!=((LinkButton)Src).CommandArgument){
			ViewState["SortColumn"]=((LinkButton)Src).CommandArgument; 
			ViewState["SortDir"]="ASC";
		}else{
			ViewState["SortDir"]=ViewState["SortDir"].ToString()=="ASC"?"DESC":"ASC";
		}
		emps_Bind();
	}


//查询显示过程
void Search_Show() {
	Utility.buildListBox(Search_manmonth.Items,Search_manmonth_lov,null,"");


	string s;
	
	s=Utility.GetParam("emp_login");
	Search_emp_login.Text = s;
	
	s=Utility.GetParam("name");
	Search_name.Text = s;
	
	s=Utility.GetParam("manmonth");
	
	try {Search_manmonth.SelectedIndex=Search_manmonth.Items.IndexOf(Search_manmonth.Items.FindByValue(s));
	}catch{}
}

//查询过程事件
void Search_search_Click(Object Src, EventArgs E) {
	string sURL = Search_FormAction + "emp_login="+Search_emp_login.Text+"&"
	 + "name="+Search_name.Text+"&"
	 + "manmonth="+Search_manmonth.SelectedItem.Value+"&"
	;
	sURL += "";
	Response.Redirect(sURL);
}

    }
}