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_WhiteBoard : 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) {
        // 取得 current date 的值
        if (Request["CurrentDay"] != null)
            dtCurrentDate = Convert.ToDateTime(Request["CurrentDay"]);
        else
            dtCurrentDate = DateTime.Today;
        lblCurrentMonth.Text = dtCurrentDate.ToString("yyyy/MM");

        //// 取得資料
        //DataTable dt = new TEC_WhiteBoardData().SelectByMonth(dtCurrentDate.Year, dtCurrentDate.Month);
        ////// 刪除超過本月最大天數的資料
        ////iMaxDayOfMonth = new Root.pcCommon().getMaxDayOfMonth(dtCurrentDate); // 取得月的最大天數
        ////for (int i = iMaxDayOfMonth; i < 31; i++) {
        ////    dt.Rows.RemoveAt(iMaxDayOfMonth);
        ////}
        //GridView1.DataSource = dt;
        //GridView1.DataBind();

        // 取得資料
        if (!IsPostBack) {
            ObjectDataSource1.SelectParameters["Year"].DefaultValue = dtCurrentDate.Year.ToString();
            ObjectDataSource1.SelectParameters["Month"].DefaultValue = dtCurrentDate.Month.ToString();
            GridView1.EditIndex = -1;
        }


        hlPreviousMonth.NavigateUrl = "WhiteBoard.aspx?CurrentDay=" + dtCurrentDate.AddMonths(-1).ToString("yyyy/MM/dd");
        hlNextMonth.NavigateUrl = "WhiteBoard.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");
                        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);
                        e.Row.Cells[2].ForeColor = System.Drawing.Color.Gray;
                        e.Row.BackColor = System.Drawing.Color.Pink;
                        break;
                    }
            }
            // 轉換 Contents 的換行符號
            if (e.Row.FindControl("lblContents") != null) {
                string sContent = ((Label)e.Row.FindControl("lblContents")).Text;
                if (sContent.Length > 0)
                    ((Label)e.Row.FindControl("lblContents")).Controls.Add(getContentTable(sContent));
                //((Label)e.Row.FindControl("lblContents")) .Text = sContent.re
                //    ((Label)e.Row.FindControl("lblContents")).Text.Replace("\r\n", "<br>");
            }


            // 不是來自管理的路徑 (功能 hidden)
            if (Request.Url.ToString().ToUpper().IndexOf("SEST/MAINTENANCE/") < 0) {
                GridView1.Columns[0].Visible = false;       //刪除,編輯
                this.Title = Resources.SiteMapLocalizations.TEC + " - " + Resources.SiteMapLocalizations.TEC_WhiteBoard;
            }
            else
                this.Title = Resources.SiteMapLocalizations.Maintenance + " - " + Resources.SiteMapLocalizations.Maintenance_WhiteBoardEdit;
        }
    }
    private System.Web.UI.HtmlControls.HtmlTable getContentTable(string Content) {
        string[] arContents = Content.Split('\n');
        System.Web.UI.HtmlControls.HtmlTable tbl = new HtmlTable();
        System.Web.UI.HtmlControls.HtmlTableRow tr = new HtmlTableRow();
        System.Web.UI.HtmlControls.HtmlTableCell td = new HtmlTableCell();
        tbl.CellPadding = 0;
        tbl.CellSpacing = 0;
        tbl.Border = 1;
        tbl.Width = "100%";
        int Idx = 0;
        foreach (string s in arContents) {
            tr = new HtmlTableRow();
            td = new HtmlTableCell();
            if (s.IndexOf("=>") > 0)
                td.InnerText = s.Substring(s.IndexOf("=>") + 2);
            else
                td.InnerText = s;
            if (Idx % 2 == 0)
                td.BgColor = "#ffffff";
            else
                td.BgColor = "#aaffaa";
            tr.Cells.Add(td);
            tbl.Rows.Add(tr);
            Idx++;
        }

        return tbl;
    }
}
