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 SubPages_JobNos : 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;
    }



    protected void Page_Load(object sender, EventArgs e) {
        if (Request["JobNoClientID"] != null) {
            string sParentJobNoID = Request["JobNoClientID"].ToString().Trim();
            // 以下為選擇性參數
            string sParentFABID = "";
            string sParentDescriptionID = "";
            if (Request["FABClientID"] != null)
                sParentFABID = Request["FABClientID"].ToString();
            if (Request["DescriptionClientID"] != null)
                sParentDescriptionID = Request["DescriptionClientID"].ToString();


            string sSelectType = "";
            if (Request["SelectType"] != "")
                sSelectType = Request["SelectType"].ToString();



            DataTable dtJobNos;
            //////// 只針對 JWL or JW or TW (有配置的機台)/////////////////////////////////////////////////////////////////
            if (sSelectType == "Unassigned") { // 新增
                dtJobNos = new TEC_JobNoData().SelectUnassigned("((substring(JobNo,1,2) in ('JW','TW') and substring(JobNo,1,3)<>'TWL') or substring(JobNo,1,3)='JWL') and IsClosed<>1");      // 只針對 JWL or JW or TW
            }
            else {
                dtJobNos = new TEC_JobNoData().Select("((substring(JobNo,1,2) in ('JW','TW') and substring(JobNo,1,3)<>'TWL') or substring(JobNo,1,3)='JWL') and IsClosed<>1");      // data source
            }

            // Show data
            if (dtJobNos != null) {
                ShowTable(dtJobNos, "(JWL or JW or TW)", sParentJobNoID, sParentFABID, sParentDescriptionID);
            }
            //////////////////////////////////////////////////////////////////////////////////////////////////


            //////// 針對 JWL or JW or TW 其它以外的 AJW, AZZ, TWL: (工番指定廠別內的所有機台) //////////////////////////////////////////////////////////
            dtJobNos = new TEC_JobNoData().Select("( (substring(JobNo,1,2) not in ('JW','TW')) and substring(JobNo,1,3)<>'JWL' or substring(JobNo,1,3)='TWL') and IsClosed<>1");      // data source

            // Show data 判斷是否有找到資料
            if (dtJobNos == null) {
                Label lbl = new Label();
                lbl.Text = Resources.Universal.Msg_NotFindData;
                this.form1.Controls.Add(lbl);
            }
            else {
                ShowTable(dtJobNos, "(Others - Without JWL/JW/TW)", sParentJobNoID, sParentFABID, sParentDescriptionID);
            }
            //////////////////////////////////////////////////////////////////////////////////////////////////


            // back button
            Button btn = new Button();
            btn.Text = Resources.Universal.Msg_Close;
            btn.Attributes.Add("onclick", "window.close();");
            this.form1.Controls.Add(btn);
        }
    }


    private void ShowTable(DataTable dtJobNos, string Title, string sParentJobNoID, string sParentFABID, string sParentDescriptionID) {
        int iRowCount = 5, iCount = 0;

        // show data with table
        Table tblDisplay = new Table();
        tblDisplay.GridLines = System.Web.UI.WebControls.GridLines.Both;
        //tblDisplay.Width = System.Web.UI.WebControls.Unit.Pixel(500);
        tblDisplay.ForeColor = System.Drawing.Color.White;
        tblDisplay.Font.Bold = true;
        tblDisplay.BackColor = System.Drawing.Color.SkyBlue;
        TableRow tableRow = new TableRow();

        // table title
        TableCell tableCell = new TableCell();
        Label lbl = new Label();
        lbl.Text = Title;
        tableCell.Controls.Add(lbl);
        tableCell.ColumnSpan = 5;
        tableRow.Cells.Add(tableCell);
        tblDisplay.Rows.Add(tableRow);
        
        
        //for (int i = 0; i < arDistinctName.Length; i++) {
        for (int i = 0; i < dtJobNos.Rows.Count; i++) {
            //string sName = arDistinctName[i];
            string sName = dtJobNos.Rows[i]["JobNo"].ToString();
            string sFABID = dtJobNos.Rows[i]["FABID"].ToString();
            string sDescription = dtJobNos.Rows[i]["Description"].ToString();

            // client onclick 時的執行命令字串
            string sOnClickCommand = "window.opener.document.getElementById('" + sParentJobNoID + "').value ='" + sName + "'; ";
            //if (sParentFABID != "")   // 一個 JobNo 已改可以對應多個 FAB 所以無法自動填入
            //    sOnClickCommand += "window.opener.document.getElementById('" + sParentFABID + "').value ='" + sFABID + "'; ";
            if (sParentDescriptionID != "")
                sOnClickCommand += "window.opener.document.getElementById('" + sParentDescriptionID + "').value ='" + sDescription.Replace("\r\n", "").Replace("'", "\\\'") + "'; ";
            sOnClickCommand += "window.close();window.opener.focus();";

            if ((iCount % iRowCount) == 0)
                tableRow = new TableRow();
            tableCell = new TableCell();
            lbl = new Label();
            lbl.Text = sName;
            lbl.Attributes.Add("onclick", sOnClickCommand);
            lbl.Attributes.Add("title", sDescription.Replace("\r\n", "").Replace("'", "\'"));
            lbl.Attributes.Add("style", "cursor:hand;");
            tableCell.Controls.Add(lbl);
            tableRow.Controls.Add(tableCell);
            if (((iCount + 1) % iRowCount) == 0)
                tblDisplay.Controls.Add(tableRow);
            iCount++;
        }
        if ((iCount % iRowCount) != 0)
            tblDisplay.Controls.Add(tableRow);

        this.form1.Controls.Add(tblDisplay);
        Literal li = new Literal();
        li.Text = "<br>";
        this.form1.Controls.Add(li);
    }
}
