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_Tools : 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) {
        int iRowCount = 5, iCount = 0;
        bool bHaveData = true;
        string sIsMultiTools = "False";

        if (Request["DialogClientID"] != null) {
            string sParentID = Request["DialogClientID"].ToString().Trim();
            string sFABID = Request["FABID"].ToString();
            string sCondition = null;// = Request["FABID"].ToString() == "" ? null : "FABID='" + Request["FABID"].ToString()+ "'";
            string[] arDistinctName = null;

            // 回傳目標的控制項是不是允許多許
            if (Request["IsMultiTools"] != null)
                sIsMultiTools = Request["IsMultiTools"].ToString() ;


            //FAB 的條件
            if (sFABID != "") {
                //sCondition = "FABID='" + Request["FABID"].ToString().Replace("'", "").Replace("--", "") + "'";
                // 0805008: 一個工番可以對應多個 FAB
                sCondition = "FABID in ('" + Request["FABID"].ToString().Replace(",","','") + "') ";
            }

            // JobNo 的條件
            string sJobNo = "";
            if (Request["JobNo"] != null) {
                // 先取得 JobNo 所包含的機台
                sJobNo = Request["JobNo"].ToString().Trim().ToUpper();
                if (sJobNo == "") {
                    sJobNo = "";//"NoJobNoInput";
                    //sCondition = "1=0";
                }
                else {
                    // 有 JobNo 的資料
                    // 如果是 TW, JW, 或 JWL 則再判斷該工番的所有機台是不是全部設定完成了,如果全設完則不show出
                    if ( (sJobNo.Substring(0,2) == "TW" || sJobNo.Substring(0,2) == "JW" || sJobNo.Substring(0,3) == "JWL") && sJobNo.Substring(0,3) != "TWL") {
                        string sTools = new TEC_JobNoData().SelectDistinct("Tools", "JobNo='" + sJobNo.Replace("'", "").Replace("--", "") + "'")[0];
                        string[] ar = sTools.Replace("\r", "").Split("\n".ToCharArray());
                        string sJobNoContainTools = "";
                        for (int i = 0; i < ar.Length; i++) {
                            sJobNoContainTools += "','" + ar[i];
                        }
                        if (sJobNoContainTools != "")
                            sJobNoContainTools = sJobNoContainTools.Substring(2) + "'";
                        else
                            sJobNoContainTools = "''";


                        if (sCondition != null)
                            sCondition += " and SES_ToolNo in (" + sJobNoContainTools + 
                                ") and SES_ToolNo not in (Select distinct SES_ToolNo from TEC_MainWorks where JobNo='" + sJobNo.Replace("'", "").Replace("--", "") + "' )";
                    }
                }
            }
            arDistinctName = new TEC_ToolsData().SelectDistinct("SES_ToolNo", sCondition);


            // 判斷是否有找到資料, 沒有則return 
            if (arDistinctName.Length <= 0) {
                Label lbl = new Label();
                if (sJobNo == "NoJobNoInput")   // 沒有輸入工番
                    lbl.Text = Resources.Universal.WorksList_NoJobNoInput;
                else if ( sJobNo != "")         // 有工番的條件時, 但該工番的值己被設完了
                    lbl.Text = Resources.Universal.Msg_NotFindDataOrSet;
                else                            // 沒有工番的條件時
                    lbl.Text = Resources.Universal.Msg_NotFindData;

                lbl.Text += "<br>";
                this.form1.Controls.Add(lbl);
                bHaveData = false ;
            }

            if (bHaveData) {
                // show data with table
                Table tblDisplay = new Table();
                tblDisplay.GridLines = System.Web.UI.WebControls.GridLines.Both;
                //tblDisplay.Width = System.Web.UI.WebControls.Unit.Pixel(400);
                tblDisplay.ForeColor = System.Drawing.Color.White;
                tblDisplay.Font.Bold = true;
                tblDisplay.BackColor = System.Drawing.Color.SkyBlue;
                TableRow tableRow = null;
                for (int i = 0; i < arDistinctName.Length; i++) {
                    string sName = arDistinctName[i];
                    if ((iCount % iRowCount) == 0)
                        tableRow = new TableRow();
                    TableCell tableCell = new TableCell();
                    Label lbl = new Label();
                    lbl.Text = sName;
                    //lbl.Attributes.Add("onclick", "window.opener.document.getElementById('" + sParentID + "').value ='" + sName + "'; window.close();");
                    lbl.Attributes.Add("onclick", "AssignValueToParent('" + sParentID + "','" + sName + "','" + sIsMultiTools + "')");
                    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);
            }   // show data


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