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_TEC_WhiteBoardDetail : 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 Page_Load(object sender, EventArgs e) {
        lblPageTitle.Text = Request["Year"].ToString() + " / " + Request["Month"].ToString() + " / " + Request["Day"].ToString() ;
    }

    public void btnAddNew_Click(object sender, EventArgs e) {
        if (GridView1.Rows.Count <= 0) { 
        }
        GridView1.ShowFooter = true;
        btnAddNew.Enabled = false;
    }
    public void Insert_Click(object sender, EventArgs e) {
        GridView1.ShowFooter = false;
        btnAddNew.Enabled = true;
    }
    public void InsertCancel_Click(object sender, EventArgs e) {
        GridView1.ShowFooter = false;
        btnAddNew.Enabled = true;
    }


    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 == "Insert") {
            string Year = Request["Year"].ToString();
            string Month = Request["Month"].ToString();
            string Day = Request["Day"].ToString();
            try {
                DateTime.Parse(Year + "/" + Month + "/" + Day);
            }
            catch { 
                
                return;
            }
            string Contents = ((TextBox)GridView1.FooterRow.FindControl("txtContents")).Text ?? "";
            new TEC_WhiteBoardData().Insert(int.Parse(Year), int.Parse(Month), int.Parse(Day),Contents);

            ////GridView1.DataBind();
        }
        if (e.CommandName == "Update") {
        }


        // 當選擇編輯時不允許新增, 當按取消編輯時再允許
        if (e.CommandName == "Edit") {
            InsertCancel_Click(null, null);
            GridView1.ShowFooter = false;
            btnAddNew.Enabled = false;
        }
        // 動作完成或取消後將 AddNew 按鈕 enable
        if (e.CommandName == "Update" || e.CommandName == "Cancel")
            btnAddNew.Enabled = true;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        // 沒有資料時會回傳一個空的 Row , 此時不允許將編輯的動作
        if (e.Row.RowIndex == 0 && ((Label)e.Row.FindControl("lblWhiteBoardID")).Text == "") { 
            ((LinkButton)e.Row.FindControl("LinkButtonEdit")).Visible = false;
            ((LinkButton)e.Row.FindControl("LinkButtonDelete")).Visible = false;
        }

        // 不是來自管理的路徑 (功能 hidden)
        if (Request.Url.ToString().ToUpper().IndexOf("SEST/MAINTENANCE/") < 0) {
            btnAddNew.Visible = false;                  //新增按鈕
            GridView1.Columns[6].Visible = false;       //刪除,編輯
        }

    }

}
