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 Personal_PersonalHome : 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) {
        string sAccount = Profile.UserName;
        try {
            DataTable dt = new HR_EmployeesData().SelectExcludeLeave("Name,FirstName", "Account='" + sAccount + "'");
            if (dt.Rows.Count == 1) {
                string sUserName = dt.Rows[0]["Name"].ToString();
                string sUserEName = dt.Rows[0]["FirstName"].ToString();

                // List the works not reocrd yet
                dt = new TEC_MainWorksData().GetWorkNoRecord(sAccount);
                //int iWorkID = dt.Columns.IndexOf("WorkID");
                //int iJobNo = dt.Columns.IndexOf("JobNo");
                //int iSESToolNo = dt.Columns.IndexOf("SES_ToolNo");
                //int iResponsibility = dt.Columns.IndexOf("Responsibility");
                foreach (DataRow dr in dt.Rows) {
                    HtmlTableRow tr = new HtmlTableRow();
                    HtmlTableCell tc = new HtmlTableCell(); tr.Cells.Add(tc);
                    tc = new HtmlTableCell(); tc.BgColor = "Linen"; tc.InnerText = dr["JobNo"].ToString(); tr.Cells.Add(tc);
                    tc = new HtmlTableCell(); tc.BgColor = "Linen"; tc.InnerText = dr["SES_ToolNo"].ToString(); tr.Cells.Add(tc);
                    tc = new HtmlTableCell(); tc.BgColor = "Linen"; tc.InnerText = dr["Description"].ToString(); tr.Cells.Add(tc);
                    tc = new HtmlTableCell(); tc.BgColor = "Linen"; tc.InnerText = dr["ExpectEndDate"].ToString(); tr.Cells.Add(tc);
                    //tc = new HtmlTableCell(); tc.BgColor = "Linen"; tc.InnerHtml = dr["Responsibility"].ToString().Replace("\r\n", "<br>"); tr.Cells.Add(tc);
                    // hyperlink
                    tc = new HtmlTableCell(); HtmlAnchor a = new HtmlAnchor();
                    a.InnerText = Resources.Universal.WorksList_Record;
                    a.HRef = "~/TEC/Forms/WorkRecord.aspx?WorkID=" + dr["WorkID"].ToString() + "&ScheduleID=" + dr["ScheduleID"].ToString();
                    tc.BgColor = "Linen"; tc.Controls.Add(a); tr.Cells.Add(tc);

                    tblWorkNoRecord.Rows.Add(tr);
                }

                if (!IsPostBack) {
                    // 白板
                    lblTitleWhiteBoard_Yesterday.Text += "(" + DateTime.Today.AddDays(-1).ToString("yyyy/MM/dd") + ")";
                    string[] arWhiteBoard = new TEC_WhiteBoardData().Select(DateTime.Today.AddDays(-1), sUserName, sUserEName);
                    if (arWhiteBoard != null) {
                        if (arWhiteBoard.Length > 0) lblContentsWhiteBoard_Yesterday.Text = "";//clear
                        for (int i = 0; i < arWhiteBoard.Length; i++) {
                            lblContentsWhiteBoard_Yesterday.Text += "&#8226" + arWhiteBoard[i] + "<br>";
                        }
                    }
                    // today
                    lblTitleWhiteBoard_Today.Text += "(" + DateTime.Today.ToString("yyyy/MM/dd") + ")";
                    arWhiteBoard = new TEC_WhiteBoardData().Select(DateTime.Today, sUserName, sUserEName);
                    if (arWhiteBoard != null) {
                        if (arWhiteBoard.Length > 0) lblContentsWhiteBoard_Today.Text = "";//clear
                        for (int i = 0; i < arWhiteBoard.Length; i++) {
                            lblContentsWhiteBoard_Today.Text += "&#8226" + arWhiteBoard[i] + "<br>";
                        }
                    }
                    // tomorrow
                    lblTitleWhiteBoard_Tomorrow.Text += "(" + DateTime.Today.AddDays(1).ToString("yyyy/MM/dd") + ")";
                    arWhiteBoard = new TEC_WhiteBoardData().Select(DateTime.Today.AddDays(1), sUserName, sUserEName);
                    if (arWhiteBoard != null) {
                        if (arWhiteBoard.Length > 0) lblContentsWhiteBoard_Tomorrow.Text = "";//clear
                        for (int i = 0; i < arWhiteBoard.Length; i++) {
                            lblContentsWhiteBoard_Tomorrow.Text += "&#8226" + arWhiteBoard[i] + "<br>";
                        }
                    }


                    // 個人行事曆
                    //today
                    lblTitleCalendar_Today.Text += "(" + DateTime.Today.ToString("yyyy/MM/dd") + ")";
                    string[] arCalendar = new Personal_CalendarData().Select(sUserEName, DateTime.Today);
                    if (arCalendar != null) {
                        if (arCalendar.Length > 0) lblContentsCalendar_Today.Text = ""; //clear
                        for (int i = 0; i < arCalendar.Length; i++) {
                            lblContentsCalendar_Today.Text += "&#8226" + arCalendar[i] + "<br>";
                        }
                    }
                    // tomorrow
                    lblTitleCalendar_Tomorrow.Text += "(" + DateTime.Today.AddDays(1).ToString("yyyy/MM/dd") + ")";
                    arCalendar = new Personal_CalendarData().Select(sUserEName, DateTime.Today.AddDays(1));
                    if (arCalendar != null) {
                        if (arCalendar.Length > 0) lblContentsCalendar_Tomorrow.Text = ""; //clear
                        for (int i = 0; i < arCalendar.Length; i++) {
                            lblContentsCalendar_Tomorrow.Text += "&#8226" + arCalendar[i] + "<br>";
                        }
                    }
                }
            }
        }
        catch (Exception ex) {
            Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=" + ex.Message.Replace("\r\n", "=>"));
        }
    }
}
