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_SelectionPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sSelectValue = "", sCondition = "", sJobNo = "";
        bool bEnableManualInput = false;
        if (Request["SelectValue"] != null)
            sSelectValue = Request["SelectValue"].ToString();
        if (Request["EnableManualInput"] != null)
            bEnableManualInput = Convert.ToBoolean(Request["EnableManualInput"]);
        if (Request["JobNo"] != null)
            sJobNo = Request["JobNo"].ToString();
        if (Request["AssignObjName"] != null)
            hidTargetObjectName.Value = Request["AssignObjName"].ToString();
        if (Request["MaxCount"] != null)
            hidMaxCount.Value = Request["MaxCount"].ToString();
        // Condition : Ex. JobNo|TW00195,TW00196@FAB|UMC8C
        if (Request["Condition"] != null)
            sCondition = Request["Condition"].ToString();



        if (!bEnableManualInput)
            txtNewValue.Disabled = true;
        switch (sSelectValue) {
            case "FABs":
                divTitle.InnerText = "Select FABs";
                SelectFABs(sJobNo);
                break;
            case "Tools":
                divTitle.InnerText = "Select Tools";
                SelectTools(sCondition);
                break;
        }

    }

    // show data
    private void showData(string[] values) {
        if (values.Length == 0) {
            selAvailable.Items.Clear();
            selAvailable.Items.Add("No data be found");
            selAvailable.Disabled = true;
        }
        else {
            selAvailable.DataSource = values;
            selAvailable.DataBind();
        }
    }

    private string[] getStringArray(DataTable dt, string ColumnName) {
        if (dt == null || dt.Rows.Count == 0) return null;
        string[] arRtn = new string[dt.Rows.Count];
        int iIdx = 0;
        foreach (DataRow dr in dt.Rows) {
            arRtn[iIdx] = dr[ColumnName].ToString();
            iIdx++;
        }

        return arRtn;
    }
    private void SelectFABs(string JobNo) {
        DataTable dt = null;
        string[] arResult = null;
        if (JobNo.Length <= 0) {
            dt = new Customer_FABData().Select();
            arResult = getStringArray(dt, "FABID");
        }
        else
            arResult = new Customer_FABData().SelectFABs(JobNo);

        showData(arResult);
    }
    private void SelectTools(string Condition) {
        string[] arItems = Condition.Split('@');
        string sColumn = "", sValueIn = "", sCondition = "";
        foreach (string Item in arItems) {
            sColumn = Item.Substring(0, Item.IndexOf("|"));
            sValueIn = Item.Substring(Item.IndexOf("|") + 1);

            sCondition = " and " + sColumn + " in ('" + sValueIn.Replace(",", "','") + "') ";
        }
        if (sCondition.Length > 0) {
            sCondition = sCondition.Substring(4);
            string[] arResult = new TEC_ToolsData().SelectDistinct("SES_ToolNo", sCondition);
            showData(arResult);
        }
    }
}
