using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                              Started Date  : 12-Aug-2007
* Module          : Entrance Master                                        Last Modified : 21-Aug-2007
* Programmer      : Kumuthini.L
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_EntranceModify : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    EntranceEventData objEntrance = new EntranceEventData();
    TerminalEventData objTerminal = new TerminalEventData();
    cls_Common objCommon = new cls_Common(); int intEntranceID;
    int intOrgLevelID = 0;
    int gintEmployeeID = 0;
    string strEntranceName = string.Empty;
    string strEntranceDesc = string.Empty;
    string gstrUserGroupName = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        GC.Collect();
        base.Page_Load(sender, e);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        gintEmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SelectInTerminal") + "','" + rm.GetString("Alert_SelectOutTerminal") + "','" + rm.GetString("Alert_ConfirmSave") + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        UserRights();
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        intEntranceID = Convert.ToInt32(Request.QueryString["Entrance_Id"]);
        if (!IsPostBack)
        {
            FillEntranceDetails(intEntranceID);
            objCommon.menu("Device", "15");
        }
        else
        {
            GenerateHtmlTableForInTerminals(Convert.ToInt16(ddl_NoOfINTerminals.SelectedValue));
            FillInTerminalValues(Convert.ToInt16(ddl_NoOfINTerminals.SelectedValue));
            GenerateHtmlTableForOutTerminals(Convert.ToInt16(ddl_NoOfOUTTerminals.SelectedValue));
            FillOutTerminalValues(Convert.ToInt16(ddl_NoOfOUTTerminals.SelectedValue));
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_save_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToInt32(Session["parentLevelId"]) != 0)
            {
                intOrgLevelID = Convert.ToInt32(Session["parentLevelId"]);
                Session["parentLevelId"] = null;
                strEntranceName = txt_EntranceName.Text.Replace("'", "''");
                strEntranceDesc = txt_EntranceDescription.Text.Replace("'", "''");
                int count = objEntrance.CheckEntrance_Update(intEntranceID, strEntranceName, intOrgLevelID);
                if (count > 0)
                {
                    base.Message(rm.GetString("EntranceMaster_Entrance_Name_Duplicate", ci));
                    return;
                }
                else
                {
                    int intLoop;
                    string strInTerminals = string.Empty;
                    string strOutTerminals = string.Empty;
                    int intNoOfInTerminals = Convert.ToInt16(ddl_NoOfINTerminals.SelectedValue);
                    int intNoOfOutTerminals = Convert.ToInt16(ddl_NoOfOUTTerminals.SelectedValue);
                    for (intLoop = 1; intLoop <= intNoOfInTerminals; intLoop++)
                    {
                        DropDownList dlist = new DropDownList();
                        dlist = (DropDownList)Table1.FindControl("ddl_InTerminal" + intLoop + "");
                        if (dlist.SelectedItem.Text != "-Select")
                        {
                            if (strInTerminals == string.Empty)
                            {
                                strInTerminals = dlist.SelectedValue;
                            }
                            else
                            {
                                strInTerminals = strInTerminals + "," + dlist.SelectedValue;
                            }
                        }
                    }
                    for (intLoop = 1; intLoop <= intNoOfOutTerminals; intLoop++)
                    {
                        DropDownList dlist = new DropDownList();
                        dlist = (DropDownList)Table2.FindControl("ddl_OutTerminal" + intLoop + "");
                        if (dlist.SelectedItem.Text != "-Select")
                        {
                            if (strOutTerminals == string.Empty)
                            {
                                strOutTerminals = dlist.SelectedValue;
                            }
                            else
                            {
                                strOutTerminals = strOutTerminals + "," + dlist.SelectedValue;
                            }
                        }
                    }
                    objEntrance.InsertEntranceDetails('U', intEntranceID, strEntranceName, strEntranceDesc, intOrgLevelID, intNoOfInTerminals, intNoOfOutTerminals, strInTerminals, strOutTerminals, gintEmployeeID);
                }
            }
            else
            {
                base.Message(rm.GetString("Alert_Selectallorganizationlevels", ci));
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        string strRedirectPage = "TA_UI_Master_Entrance.aspx";
        base.MessageWithRedirect(rm.GetString("Alert_Entrancemodifiedsuccessfully", ci), strRedirectPage);
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_Entrance.aspx");
    }
    #endregion
    #region UserDefinedFunctions
    private void FillEntranceDetails(int EntranceID)
    {
        DataSet ds_Entrance = new DataSet();
        ds_Entrance = objEntrance.SelectEntranceDetailsByID(EntranceID, gstrUserGroupName);
        if (ds_Entrance.Tables[0].Rows.Count > 0)
        {
            Session["parentLevelId_modify"] = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["ID"]);
            txt_EntranceName.Text = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["Entrance_Name"]);
            txt_EntranceDescription.Text = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["Entrance_Description"]);
            ddl_NoOfINTerminals.SelectedValue = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["NoOfInTerminals"]);
            GenerateHtmlTableForInTerminals(Convert.ToInt16(ddl_NoOfINTerminals.SelectedValue));
            FillInTerminalValues(Convert.ToInt16(ddl_NoOfINTerminals.SelectedValue));
            ddl_NoOfOUTTerminals.SelectedValue = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["NoOfOutTerminals"]);
            GenerateHtmlTableForOutTerminals(Convert.ToInt16(ddl_NoOfOUTTerminals.SelectedValue));
            FillOutTerminalValues(Convert.ToInt16(ddl_NoOfOUTTerminals.SelectedValue));
        }
        ds_Entrance.Dispose();
    }
    private DataSet FillTerminalsDropdown()
    {
        DataSet ds_Terminal = new DataSet();
        try
        {
            ds_Terminal = objTerminal.SelectTerminalDetailsForEntrance("U", intEntranceID);
        }
        catch
        {
        }
        return (ds_Terminal);
    }
    private void GenerateHtmlTableForInTerminals(int inNoOfINTerminals)
    {
        try
        {
            int numrows;
            int numcells;
            int i;
            int j;
            int count = 1;
            int cellcount;
            numrows = inNoOfINTerminals;
            cellcount = inNoOfINTerminals;
            numcells = 3;
            if (numrows >= 1)
                if ((numrows % 1) == 0)
                    numrows = numrows / 1;
                else
                    numrows = numrows + 1;
            else
                numrows = 1;
            for (j = 1; j <= numrows; j++)
            {
                TableRow r = new TableRow();
                for (i = 1; i <= numcells; i++)
                {
                    if (count <= cellcount)
                    {
                        TableCell c = new TableCell();
                        DropDownList ddl_InTerminals = new DropDownList();
                        ddl_InTerminals.ID = "ddl_InTerminal" + count;
                        ddl_InTerminals.CssClass = "form-control select2";
                        ddl_InTerminals.DataSource = FillTerminalsDropdown();
                        ddl_InTerminals.DataTextField = "Terminal_ReaderID";
                        ddl_InTerminals.DataValueField = "Terminal_Id";
                        ddl_InTerminals.DataBind();
                        ListItem liSelect = new ListItem();
                        liSelect.Value = "-Select-";
                        liSelect.Text = rm.GetString("Select", ci);
                        ddl_InTerminals.Items.Insert(0, liSelect);
                        foreach (ListItem item in ddl_InTerminals.Items)
                        {
                            item.Attributes.Add("title", item.Text);
                        }
                        if (count < 10)
                        {
                            c.Controls.Add(new LiteralControl("<lable>" + rm.GetString("EntranceMaster_In_Terminal", ci) + " " + count + "</lable>"));
                        }
                        else
                        {
                            c.Controls.Add(new LiteralControl("<lable>" + rm.GetString("EntranceMaster_In_Terminal", ci) + " " + count + "</lable>"));
                        }
                        c.Controls.Add(ddl_InTerminals);
                        c.CssClass = "col-md-4";
                        r.Cells.Add(c);
                        r.CssClass = "row";
                        ddl_InTerminals.Attributes.Add("onchange", "changeEntranceIN(" + count + ", this.id )");
                        count = count + 1;
                    }
                    Table1.Rows.Add(r);
                }
            }
        }
        catch
        { }
    }
    private void FillInTerminalValues(int inNoOfINTerminals)
    {
        try
        {
            DataSet ds_InTerminals = new DataSet();
            if (!IsPostBack)
            {
                ds_InTerminals = objEntrance.SelectEntranceINOUTDetailsByIDview(intEntranceID, "In");
            }
            else
            {
                ds_InTerminals = (DataSet)Session["ds_InTerminals"];
            }
            Session["ds_InTerminals"] = ds_InTerminals;
            int i = 0; int j = 0; for (i = 0; i < inNoOfINTerminals; i++)
            {
                for (j = 0; j < ds_InTerminals.Tables[0].Rows.Count; j++)
                {
                    if (i == j)
                    {
                        DropDownList ddl_InTerminals = new DropDownList();
                        string DropDownId = "ddl_InTerminal" + (i + 1);
                        ddl_InTerminals = (DropDownList)Table1.FindControl(DropDownId);
                        ddl_InTerminals.SelectedValue = Convert.ToString(ds_InTerminals.Tables[0].Rows[j]["Reader_Id"]);
                        ddl_InTerminals.Attributes.Add("onchange", "changeEntranceIN(" + (i + 1) + ", '" + Convert.ToString(ddl_InTerminals.ClientID) + "' )");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    private void GenerateHtmlTableForOutTerminals(int inNoOfOUTTerminals)
    {
        try
        {
            int numrows;
            int numcells;
            int i;
            int j;
            int count = 1;
            int cellcount;
            numrows = inNoOfOUTTerminals;
            cellcount = inNoOfOUTTerminals;
            numcells = 3;
            if (numrows >= 1)
                if ((numrows % 1) == 0)
                    numrows = numrows / 1;
                else
                    numrows = numrows + 1;
            else
                numrows = 1;
            for (j = 1; j <= numrows; j++)
            {
                TableRow r = new TableRow();
                for (i = 1; i <= numcells; i++)
                {
                    if (count <= cellcount)
                    {
                        TableCell c = new TableCell();
                        DropDownList ddl_OutTerminals = new DropDownList();
                        ddl_OutTerminals.ID = "ddl_OutTerminal" + count;
                        ddl_OutTerminals.CssClass = "form-control select2";
                        ddl_OutTerminals.DataSource = FillTerminalsDropdown();
                        ddl_OutTerminals.DataTextField = "Terminal_ReaderID";
                        ddl_OutTerminals.DataValueField = "Terminal_Id";
                        ddl_OutTerminals.DataBind();
                        ListItem liSelect = new ListItem();
                        liSelect.Value = "-Select-";
                        liSelect.Text = rm.GetString("Select", ci);
                        ddl_OutTerminals.Items.Insert(0, liSelect);
                        foreach (ListItem item in ddl_OutTerminals.Items)
                        {
                            item.Attributes.Add("title", item.Text);
                        }
                        if (count < 10)
                        {
                            c.Controls.Add(new LiteralControl("<label>" + rm.GetString("EntranceMaster_Out_Terminal", ci) + " " + count + "</lable>"));
                        }
                        else
                        {
                            c.Controls.Add(new LiteralControl("<label>" + rm.GetString("EntranceMaster_Out_Terminal", ci) + " " + count + "</lable>"));
                        }
                        c.Controls.Add(ddl_OutTerminals);
                        c.CssClass = "col-md-4";
                        r.Cells.Add(c);
                        r.CssClass = "row";
                        ddl_OutTerminals.Attributes.Add("onchange", "changeEntranceOUT(" + count + ", this.id )");
                        count = count + 1;
                    }
                    Table2.Rows.Add(r);
                }
            }
        }
        catch
        { }
    }
    private void FillOutTerminalValues(int inNoOfOUTTerminals)
    {
        try
        {
            DataSet ds_OutTerminals = new DataSet();
            if (!IsPostBack)
            {
                ds_OutTerminals = objEntrance.SelectEntranceINOUTDetailsByIDview(intEntranceID, "Out");
            }
            else
            {
                ds_OutTerminals = (DataSet)Session["ds_OutTerminals"];
            }
            Session["ds_OutTerminals"] = ds_OutTerminals;
            int i = 0; int j = 0; for (i = 0; i < inNoOfOUTTerminals; i++)
            {
                for (j = 0; j < ds_OutTerminals.Tables[0].Rows.Count; j++)
                {
                    if (i == j)
                    {
                        DropDownList ddl_OutTerminals = new DropDownList();
                        string DropDownId = "ddl_OutTerminal" + (i + 1);
                        ddl_OutTerminals = (DropDownList)Table2.FindControl(DropDownId);
                        ddl_OutTerminals.SelectedValue = Convert.ToString(ds_OutTerminals.Tables[0].Rows[j]["Reader_Id"]);
                        ddl_OutTerminals.Attributes.Add("onchange", "changeEntranceOUT(" + (i + 1) + ", '" + Convert.ToString(ddl_OutTerminals.ClientID) + "' )");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    // Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_EntranceName.Text = rm.GetString("EntranceMaster_Entrance_Name", ci);
        lbl_EntranceDescription.Text = rm.GetString("EntranceMaster_Entrance_Desc", ci); ;
        lbl_NoOfINTerminals.Text = rm.GetString("EntranceMaster_NoOfINTerminals", ci);
        lbl_NoOfOUTTerminals.Text = rm.GetString("EntranceMaster_NoOfOUTTerminals", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Validator_Entrance_EntranceName", ci);
        rfvNoOfINTerminals.ErrorMessage = rm.GetString("Validator_Entrance_NoOfINTerminals", ci);
        rfvNoOfOUTTerminals.ErrorMessage = rm.GetString("Validator_Entrance_NoOfOUTTerminals", ci);
        lbl_EntranceMaster.Text = rm.GetString("Masters_Entrance_EntranceMaster", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        ddl_NoOfINTerminals.Items[0].Text = rm.GetString("Select", ci);
        ddl_NoOfOUTTerminals.Items[0].Text = rm.GetString("Select", ci);
        lblImportNote.Text = rm.GetString("Field_ImportantNote", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(15, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
}