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 Personal_Calendar : 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) {
        hdiAccount.Value = Profile.UserName;

        //hlDetail.NavigateUrl = "javascript:location.replace('CalendarDetail.aspx?Account=" + Profile.UserName + "&Date='+aspnetForm." + txtModifyDate.ClientID.Replace("_", "$") + ".value)";
        DateTime dtCurrentDate = DateTime.Today;

        // 取得目前的日期
        if (Request[hidToday.ClientID.ToString().Replace("_", "$")] != null) {
            dtCurrentDate = DateTime.Parse(Request[hidToday.ClientID.ToString().Replace("_", "$")]);
        }
        // 將日期填入 hidden 的欄位
        hidToday.Value = dtCurrentDate.ToString();


        int iYear = dtCurrentDate.Year;
        int iMonth = dtCurrentDate.Month;
        int iWeekDay = (int)DateTime.Parse(iYear + @"/" + iMonth + @"/1").DayOfWeek;
        int iMaxDayOfMonth = new Root.pcCommon().getMaxDayOfMonth(dtCurrentDate); // 取得月的最大天數

        for (int i = 1; i < 32; i++) {
            // 將大於目前月數的最大天數的控制項 disabled(item of over 28)
            if (i > iMaxDayOfMonth) {
                ((Label)tblBody.FindControl("lblWeek" + i)).Visible = false;
                ((Label)tblBody.FindControl("lblDay" + i)).Visible = false;
                ((CheckBox)tblBody.FindControl("chkModify" + i)).Visible = false;
                ((TextBox)tblBody.FindControl("txtContents" + i)).Visible = false;
            }
            else {
                // 將可能已經被 disabled 的控制項打開 (item of over 28)
                if (i >= 28) {
                    ((Label)tblBody.FindControl("lblWeek" + i)).Visible = true;
                    ((Label)tblBody.FindControl("lblDay" + i)).Visible = true;
                    ((CheckBox)tblBody.FindControl("chkModify" + i)).Visible = true;
                    ((TextBox)tblBody.FindControl("txtContents" + i)).Visible = true;
                }
                // 填入相對應的星期到 display table
                switch (iWeekDay % 7) {
                    case 0: {((Label)tblBody.FindControl("lblWeek" + i)).Text = DateAndTime.WeekdayName(1, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday); 
                        tblBody.Rows[i+1].BgColor = "pink"; break; }
                    case 1: {((Label)tblBody.FindControl("lblWeek" + i)).Text = DateAndTime.WeekdayName(2, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday); 
                        tblBody.Rows[i + 1].BgColor = "Linen"; break; }
                    case 2: {((Label)tblBody.FindControl("lblWeek" + i)).Text = DateAndTime.WeekdayName(3, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday); 
                        tblBody.Rows[i + 1].BgColor = "Linen"; break; }
                    case 3: {((Label)tblBody.FindControl("lblWeek" + i)).Text = DateAndTime.WeekdayName(4, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday); 
                        tblBody.Rows[i + 1].BgColor = "Linen"; break; }
                    case 4: {((Label)tblBody.FindControl("lblWeek" + i)).Text = DateAndTime.WeekdayName(5, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday); 
                        tblBody.Rows[i + 1].BgColor = "Linen"; break; }
                    case 5: {((Label)tblBody.FindControl("lblWeek" + i)).Text = DateAndTime.WeekdayName(6, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday); 
                        tblBody.Rows[i + 1].BgColor = "Linen"; break; }
                    case 6: {((Label)tblBody.FindControl("lblWeek" + i)).Text = DateAndTime.WeekdayName(7, false, Microsoft.VisualBasic.FirstDayOfWeek.Sunday); 
                        tblBody.Rows[i + 1].BgColor = "pink"; break; }
                }
            }

            // contents default value 
            ((TextBox)tblBody.FindControl("txtContents" + i)).Text = "";
            ((TextBox)tblBody.FindControl("txtContents" + i)).Rows = 1;
            ((TextBox)tblBody.FindControl("txtContents" + i)).Attributes.Add("onchange", "javascript: this.rows='5';" + ((CheckBox)tblBody.FindControl("chkModify" + i)).ClientID + ".checked=true;");
            ((TextBox)tblBody.FindControl("txtContents" + i)).Attributes.Add("onclick", "javascript: this.rows='5';" + ((CheckBox)tblBody.FindControl("chkModify" + i)).ClientID + ".checked=true;");
            ((CheckBox)tblBody.FindControl("chkModify" + i)).Checked = false;
            ((CheckBox)tblBody.FindControl("chkModify" + i)).Attributes.Add("onclick", "javascript: if (this.checked){" + ((TextBox)tblBody.FindControl("txtContents" + i)).ClientID +
                ".rows=5;} else {" + ((TextBox)tblBody.FindControl("txtContents" + i)).ClientID + ".rows=1;}");
            iWeekDay++;
        }


        // 如果是 PostBack 回來則先做儲存的動作
        if (IsPostBack) {  // IsPostBack
            string sContnets = "";
            Personal_CalendarData perCal = new Personal_CalendarData();
            for (int i = 1; i < 32; i++) {
                if (Request[tblBody.FindControl("chkModify" + i).ClientID.Replace("_", "$")] != null) {
                    sContnets = Request[tblBody.FindControl("txtContents" + i).ClientID.Replace("_", "$")];
                    perCal.Save(Profile.UserName, iYear, iMonth, i, sContnets);
                    //sTmp += "," + iDay.ToString();
                }
            }
        }


        // 取得資料庫中的資料
        DataTable dt = new Personal_CalendarData().Select(Profile.UserName, iYear, iMonth);
        // 取得欄位的位置
        int iDay = 0, iContents = 0;
        if (dt != null) {
            iDay = dt.Columns.IndexOf("Day");
            iContents = dt.Columns.IndexOf("Contents");
            // 填入相對應的內容到 display table
            for (int i = 0; i < dt.Rows.Count; i++) {
                ((TextBox)tblBody.FindControl("txtContents" + Convert.ToInt32(dt.Rows[i][iDay]))).Text = dt.Rows[i][iContents].ToString();
                ((TextBox)tblBody.FindControl("txtContents" + Convert.ToInt32(dt.Rows[i][iDay]))).Rows = 5;
            }
        }

        lblYearMonth.Text = dtCurrentDate.ToString("yyyy / MM");
        hlPreviousMonth.NavigateUrl = "javascript: document.getElementById('" + hidToday.ClientID + "').value='" + dtCurrentDate.AddMonths(-1) + "'; aspnetForm.submit();";//"';window.location.href=window.location.href";
        hlNextMonth.NavigateUrl = "javascript: document.getElementById('" + hidToday.ClientID + "').value='" + dtCurrentDate.AddMonths(1) + "'; aspnetForm.submit();";//"';window.location.href=window.location.href";
        //}
    }


    protected void btnAddNewModify_Click(object sender, EventArgs e) {

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

        string sContnets = "";
        for (int i = 0; i < 32; i++) {
            if (tblBody.FindControl("chkModify" + i) != null && tblBody.FindControl("txtContents" + i) != null){
                if (((CheckBox)tblBody.FindControl("chkModify" + i)).Checked == true) {
                    sContnets = ((TextBox)tblBody.FindControl("txtContents" + i)).Text.Trim();
                }
            }
        }
    }
}
