using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Maintenance_HR_Department : System.Web.UI.Page {
    //the function code for localization on this page
    protected override void InitializeCulture() {
        ProfileCommon p = (ProfileCommon)this.Context.Profile;

        if (Request.Form.Get("ctl00$selLanguage") != null)
            p.lang = Request.Form.Get("ctl00$selLanguage").ToString();
        if (p.lang == String.Empty)
            p.lang = Session["Localization"].ToString();

        if (p.lang.Length > 0) {
            this.Culture = p.lang;
            this.UICulture = System.Globalization.CultureInfo.CreateSpecificCulture(p.lang).Name;
        }
    }
    //select theme
    protected void Page_PreInit(object sender, EventArgs e) {
        Page.Theme = Profile.UI.Theme;

        // 沒有權限
        if (!SES_Common.Authorize(Request.Url.ToString(), Profile.UserName)) {
            Response.Redirect("/SEST/Common/NoAuthorization.aspx");
        }
    }

    
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
     
    }



    public void AddButton1_Click(object sender, EventArgs e) {
        GridView1.ShowFooter = true;
        btnAddNew.Enabled = false;
    }
    public void btnSave_Click(object sender, EventArgs e) {
        GridView1.ShowFooter = false;
        btnAddNew.Enabled = true;
    }
    public void btnInsertCancel_Click(object sender, EventArgs e) {
        GridView1.ShowFooter = false;
        btnAddNew.Enabled = true;
    }






    protected void Page_Load(object sender, EventArgs e) {        
        if (!IsPostBack) {
            //  set the key names from my data class.
            //GridViewMain.DataKeyNames = new LocationData().PrimaryKeyColumnNames;
            GridView1.DataKeyNames  = new HR_DepartmentsData().PrimaryKeyColumnNames;
        }
    }

    private bool LoadDataEmpty {
        //  some controls that are used within a GridView, such as the calendar control, can cuase post backs.
        //  we need to preserve LoadDataEmpty across post backs.
        get { return (bool)(ViewState["LoadDataEmpty"] ?? false); }
        set { ViewState["LoadDataEmpty"] = value; }
    }

    protected void ObjectDataSourceMain_Selected(object sender, ObjectDataSourceStatusEventArgs e) {
        //  bubble exceptions before we touch e.ReturnValue
        if (e.Exception != null) {
            throw e.Exception;
        }

        //  get the DataTable from the ODS select mothod
        DataTable dataTable = (DataTable)e.ReturnValue;

        //  if rows=0 then add a dummy (null) row and set the LoadDataEmpty flag.
        if (dataTable.Rows.Count == 0) {
            dataTable.Rows.Add(dataTable.NewRow());
            LoadDataEmpty = true;
        }
        else {
            LoadDataEmpty = false;
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
        //  when binding a row, look for a zero row condition based on the flag.
        //  if we have zero data rows (but a dummy row), hide the grid view row
        //  and clear the controls off of that row so they don't cause binding errors
        if (LoadDataEmpty && e.Row.RowType == DataControlRowType.DataRow) {
            e.Row.Visible = false;
            e.Row.Controls.Clear();
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
        //  handle the save button on the footer row. this is the only manual data operation
        //  that must be done. update and delete are handled by the gridview and ODS
        if (e.CommandName == "Save") {
            string DeptID = ((TextBox)GridView1.FooterRow.FindControl("txtAddDeptID")).Text;
            string Name = ((TextBox)GridView1.FooterRow.FindControl("txtAddName")).Text;
            string Organization = ((TextBox)GridView1.FooterRow.FindControl("txtAddGrade")).Text;
            string Description = ((TextBox)GridView1.FooterRow.FindControl("DescriptionBox")).Text ?? "";

            new HR_DepartmentsData().Insert(DeptID, Name, Organization, Description);

            GridView1.DataBind();
        }
        if (e.CommandName == "Edit" || e.CommandName == "Delete") { 
            GridView1.ShowFooter = false;
            btnAddNew.Enabled = true;
        }

        // 往前移一頁(避免重新整理時再執行一次)再重新載入
        //Page_Load(this, new EventArgs());
        string sBackCount = "0";
        if ( e.CommandName == "Delete")
            sBackCount = "-1";
        else if (e.CommandName == "Save")
            sBackCount = "-2";

        if (int.Parse(sBackCount) < 0) {
            string sScript = "";
            sScript = "<script language='javascript' type='text/javascript'>\r\n";
            sScript += "window.history.go(" + sBackCount + ");";
            sScript += "window.location.replace('Department.aspx');";
            sScript += "</script>\r\n";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Back", sScript);
            //Response.Redirect("Department.aspx");
        }
    }

}
    