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 TEC_Forms_WorkRecordDetail : 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) {

        // 選項變更時的 Client action
        rdoAddNew.Attributes.Add("onclick", 
            GridView1.ClientID + ".style.display='none';" + 
            lblSelectWorkIDPrompt.ClientID + ".style.display='none';" +
            tblNewInfo.ClientID + ".style.display='inline';");// + btnNext.ClientID + ".click=" + Resources.Universal.Msg_InsertToWorkList);
        rdoSelectWorkID.Attributes.Add("onclick", 
            GridView1.ClientID + ".style.display='inline';" + 
            lblSelectWorkIDPrompt.ClientID + ".style.display='inline';" +
            tblNewInfo.ClientID + ".style.display='none';");// + btnNext.ClientID + ".onclick=''");
        
        hlResponsibility.Attributes.Add("onclick", "openSubPageEmployee('" + txtResponsibility.ClientID + "')");
        hlResponsibility.Attributes.Add("style", "cursor: hand");

        if (!IsPostBack) {
            //txtExpectStartDate.Text = DateTime.Today.ToString("yyyy/MM/dd");
            //txtExpectEndDate.Text = DateTime.Today.ToString("yyyy/MM/dd");
            txtResponsibility.Text = (new HR_EmployeesData().getUserNameByAccount(Profile.UserName));


            string sJobNo = "";
            if (Request["JobNo"] != null)
                sJobNo = Request["JobNo"].ToString().ToUpper();
            if (sJobNo == "" || (sJobNo.Substring(0,3) != "AJW" && sJobNo.Substring(0,3) != "AJM" &&sJobNo.Substring(0,3) != "AZZ") ) {
                rdoAddNew.Enabled = false;
            }

            btnNext.Attributes.Add("onclick", "if (" + tblNewInfo.ClientID + ".style.display!='none') return checkExpectDate('" + txtExpectStartDate.ClientID + "','" + txtExpectEndDate.ClientID + "', true)");
        }


    }

    protected void GridView1_DataBound(object sender, EventArgs e) {
        // 判斷資料做元件的顯示與否
        if (GridView1.Rows.Count <= 0) {
            string sJobNo = "";
            if (Request["JobNo"] != null)
                sJobNo = Request["JobNo"].ToString();
            if (sJobNo == ""){
                Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=Miss Data of JobNo");
                return;
            }
            // 除了AJM,AJW,AZZ 才可自此新增工作清單
            if (sJobNo.ToUpper().IndexOf("AJM") < 0 && sJobNo.ToUpper().IndexOf("AJW") < 0 && sJobNo.ToUpper().IndexOf("AZZ") < 0) {
                Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=" + Resources.Universal.WorkRecord_NoWorkID);
                return;
            }
            
            rdoAddNew.Checked = true;
            //rdoAddNew.Enabled = false;
            rdoSelectWorkID.Enabled = false;
            lblSelectWorkIDPrompt.Visible = false;

            Form.Attributes.Add("onload", GridView1.ClientID + ".style.display='none';");
        }
        else {
            rdoSelectWorkID.Checked = true;

            //Form.Attributes.Add("onload", tblNewInfo.ClientID + ".style.display='none';");
            //tblNewInfo.Attributes.Add("onunload", tblNewInfo.ClientID + ".style.display='none';");
            tblNewInfo.Attributes.Add("style", "display: none;background-color: #eeeeee; border: solid 3px #3399ff");
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Back", "<script language='javascript' type='text/javascript'>" + tblNewInfo.ClientID + ".style.display='none'</script>;");

        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
    }

    protected void rdoAddNew_CheckedChanged(object sender, EventArgs e) {
        if (rdoSelectWorkID.Checked)
            GridView1.Visible = true;
        else
            GridView1.Enabled = false;
    }


    protected void btnNext_Click(object sender, EventArgs e) {
        
        if (rdoAddNew.Checked) {
            // 新增工作清單
            string sJobNo = "", sFABID = "", sToolID = "";
            string sDescription = "", sRootCause = "", sResponsibilities = "", sStartDate = "", sEndDate = "", sGroups = "";
            if (Request["JobNo"] != null)
                sJobNo = Request["JobNo"].ToString();
            if (Request["FABID"] != null)
                sFABID = Request["FABID"].ToString();
            if (Request["ToolID"] != null)
                sToolID = Request["ToolID"].ToString();

            sDescription = txtDescription.Text;
            sRootCause = txtRootCause.Text;
            sResponsibilities = txtResponsibility.Text;
            sStartDate = txtExpectStartDate.Text;
            sEndDate = txtExpectEndDate.Text;
            // 取得負責人所對應的 Group
            sGroups = SES_Common.getMatchGroup( sResponsibilities);


            // 判斷值
            if (sJobNo == "" || sFABID == "" || sToolID == "") {
                Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=Miss Data of JobNo or FABID or ToolID");
                return;
            }
            try {
                DateTime.Parse(sStartDate);
                DateTime.Parse(sEndDate);
            }
            catch {
                Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=Incorrect Date Format");
                return;
            }

            // 新增至工作清單
            int iWorkID = new TEC_MainWorksData().Insert(sJobNo, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), "WorkRecord", Profile.UserName,
                sFABID, sToolID, sDescription, sRootCause, "Processing", null, null, null, null);
            // 避免工程師亂打日期, 不在這裡新增, 而在 WorkRecord.aspx
            //// 新增 Schedule
            //string sWhiteBoardContent = "[" + iWorkID.ToString() + "] => " + sJobNo + "_" + sFABID + "_" + sToolID + " : " +
            //        sDescription + "(" + sResponsibilities.Replace("\r\n", ",") + ")";
            //int iScheduleID = SES_Common.saveScheduleAndWhiteBoard(iWorkID.ToString(), sStartDate, sEndDate, sResponsibilities, sGroups, sWhiteBoardContent);
            btnNext.Enabled = false;
            Response.Redirect("WorkRecord.aspx?WorkID=" + iWorkID.ToString() + "&ShowDate=true" );
        }
        else if (rdoSelectWorkID.Checked) {
            // 選擇已存在的工作清單
            if (GridView1.SelectedIndex < 0)
                return;
            if ( GridView1.SelectedRow.Cells[1].Text != "")
                Response.Redirect("WorkRecord.aspx?WorkID=" + GridView1.SelectedRow.Cells[1].Text + "&ShowDate=true");
        }
    }

}
