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;
using Microsoft.VisualBasic;

public partial class TEC_OnCall : 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");
        }
    }

    
    private int iDay = 0;
    private string sCurrentMonth = "";
    private DateTime dtCurrentDate ;
    //private int iMaxDayOfMonth = 0;
    protected void Page_Load(object sender, EventArgs e) {

        // 不是來自管理的路徑 (功能 hidden)
        if (Request.Url.ToString().ToUpper().IndexOf("SEST/MAINTENANCE/") < 0) {
            this.Title = Resources.SiteMapLocalizations.TEC + " - " + Resources.SiteMapLocalizations.Maintenance_OnCall;
            tblCommand.Rows[1].Cells[0].ColSpan = 3;
        }
        else {
            this.Title = Resources.SiteMapLocalizations.Maintenance + " - " + Resources.SiteMapLocalizations.Maintenance_OnCallEdit;
            tblCommand.Rows[1].Cells[1].Visible = true;
            tblCommand.Rows[1].Cells[2].Visible = true;
            trPrompt.Visible = true;
        }


        // 取得 current date 的值
        if (Request["CurrentDay"] != null)
            dtCurrentDate = Convert.ToDateTime(Request["CurrentDay"]);
        else
            dtCurrentDate = DateTime.Today;
        lblCurrentMonth.Text = dtCurrentDate.ToString("yyyy/MM");

        // 取得資料
        if (!IsPostBack) {
            ddlArea.DataSourceID = "ObjectDataSource3";
            ddlArea.DataBind();
            ddlArea.SelectedIndex = 2;  // 新竹

            ObjectDataSource1.SelectParameters["Year"].DefaultValue = dtCurrentDate.Year.ToString();
            ObjectDataSource1.SelectParameters["Month"].DefaultValue = dtCurrentDate.Month.ToString();
            ObjectDataSource1.SelectParameters["Area"].DefaultValue = ddlArea.SelectedItem.Text;
            GridView1.DataBind();
            //GridView1.EditIndex = -1;
        }


        hlPreviousMonth.NavigateUrl = "OnCall.aspx?CurrentDay=" + dtCurrentDate.AddMonths(-1).ToString("yyyy/MM/dd");
        hlNextMonth.NavigateUrl = "OnCall.aspx?CurrentDay=" + dtCurrentDate.AddMonths(1).ToString("yyyy/MM/dd");
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.FindControl("lblDay") != null) {
            iDay++;
            //if (int.Parse(((Label)e.Row.FindControl("lblDay")).Text) <= iMaxDayOfMonth) {
            DateTime dtCurrentDay = DateTime.Parse(dtCurrentDate.ToString("yyyy/MM") + @"/" + ((Label)e.Row.FindControl("lblDay")).Text);
            int iWeekDay = (int)dtCurrentDay.DayOfWeek;
            // 填入相對應的星期到 display table
            switch (iWeekDay % 7) {
                case 0: {
                        ((Label)e.Row.FindControl("lblWeekDay")).Text = DateAndTime.WeekdayName(1, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday);
                        e.Row.Cells[2].ForeColor = System.Drawing.Color.Gray;
                        e.Row.BackColor = System.Drawing.Color.FromName("#FF99FF");
                        ((Label)e.Row.FindControl("lblWeekDay")).ForeColor = System.Drawing.Color.Gray;
                        break;
                    }
                case 1: {
                        ((Label)e.Row.FindControl("lblWeekDay")).Text = DateAndTime.WeekdayName(2, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday);
                        break;
                    }
                case 2: {
                        ((Label)e.Row.FindControl("lblWeekDay")).Text = DateAndTime.WeekdayName(3, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday);
                        break;
                    }
                case 3: {
                        ((Label)e.Row.FindControl("lblWeekDay")).Text = DateAndTime.WeekdayName(4, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday);
                        break;
                    }
                case 4: {
                        ((Label)e.Row.FindControl("lblWeekDay")).Text = DateAndTime.WeekdayName(5, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday);
                        break;
                    }
                case 5: {
                        ((Label)e.Row.FindControl("lblWeekDay")).Text = DateAndTime.WeekdayName(6, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday);
                        break;
                    }
                case 6: {
                        ((Label)e.Row.FindControl("lblWeekDay")).Text = DateAndTime.WeekdayName(7, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday);
                        break;
                    }
            }


            // 不是來自管理路徑
            if (Request.Url.ToString().ToUpper().IndexOf("SEST/MAINTENANCE/") < 0) {
                ((Label)e.Row.FindControl("lblMechanicMain")).Visible = true;
                ((Label)e.Row.FindControl("lblMechanicSub")).Visible = true;
                ((Label)e.Row.FindControl("lblElectricMain")).Visible = true;
                ((Label)e.Row.FindControl("lblElectricSub")).Visible = true;
            }
            else {
                ((TextBox)e.Row.FindControl("txtMechanicMain")).Visible = true;
                ((TextBox)e.Row.FindControl("txtMechanicSub")).Visible = true;
                ((TextBox)e.Row.FindControl("txtElectricMain")).Visible = true;
                ((TextBox)e.Row.FindControl("txtElectricSub")).Visible = true;
            }

        }

    }
    protected void btnSubmit_Click(object sender, EventArgs e) {
        string sArea = ddlArea.SelectedItem.ToString();
        string sEmployee = ddlEmployee.SelectedItem.ToString();

        if (sArea == "") {
            Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=" + Resources.Universal.Msg_SelAreaAndEmp);
            return;
        }

        string Year = dtCurrentDate.Year.ToString();
        string Month = dtCurrentDate.Month.ToString();
        string Day = "", MM = "", MS = "", EM = "", ES = "";
        TEC_OnCallData oOncall = new TEC_OnCallData();
        int iIdx = 0; // 目前 GridView 的index
        foreach (GridViewRow gvr in GridView1.Rows) {
            MM = ""; MS = ""; EM = ""; ES = "";

            // GridView1.Rows[iIdx] 取代 gvr (這樣才有辦法取到真正的 ClientID)
            if (Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtMechanicMain")).ClientID.Replace("_", "$")] != null) //{
                MM = Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtMechanicMain")).ClientID.Replace("_", "$")].ToString();
            if (Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtMechanicSub")).ClientID.Replace("_", "$")] != null) //{
                MS = Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtMechanicSub")).ClientID.Replace("_", "$")].ToString();
            if (Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtElectricMain")).ClientID.Replace("_", "$")] != null) //{
                EM = Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtElectricMain")).ClientID.Replace("_", "$")].ToString();
            if (Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtElectricSub")).ClientID.Replace("_", "$")] != null) //{
                ES = Request[((TextBox)GridView1.Rows[iIdx].FindControl("txtElectricSub")).ClientID.Replace("_", "$")].ToString();

            iIdx ++;
            oOncall.UpdateOrInsert(Year, Month, iIdx.ToString(), sArea, MM, MS, EM, ES);

        }

        ddlArea_SelectedIndexChanged(null, null);
        //Response.Redirect("OnCall.aspx?CurrentDay=" + dtCurrentDate.ToString("yyyy/MM/dd"));  
    }


    protected void ContentTextBox_Load(object sender, EventArgs e) {
        TextBox txt = (TextBox)sender;
        txt.Attributes.Add("style", "cursor: hand");
        txt.Attributes.Add("onclick", "AssignOnCallStaff('" + txt.ClientID + "','" + ddlEmployee.ClientID + "')");
        txt.Attributes.Add("onkeydown", "javascript: this.value=''");
    }

    protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e) {
        ObjectDataSource1.SelectParameters["Year"].DefaultValue = dtCurrentDate.Year.ToString();
        ObjectDataSource1.SelectParameters["Month"].DefaultValue = dtCurrentDate.Month.ToString();
        ObjectDataSource1.SelectParameters["Area"].DefaultValue = ddlArea.SelectedItem.Text;
        GridView1.DataBind();
    }
}
