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 : 15-Mar-2007
* Programmer      : Kumuthini.L
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_EntranceAdd : Base.BasePage
{
    #region variable declaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    TerminalEventData objTerminal = new TerminalEventData();
    cls_Common objcommon = new cls_Common();
    EntranceEventData objentrance = new EntranceEventData();
    string strEntranceName = string.Empty;
    string strEntranceDesc = string.Empty;
    int gintOrgLevel = 0;
    int gintEmployeeID;
    #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"]);
        gintEmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        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);
        if (!IsPostBack)
        {
            txt_EntranceName.Focus();
            objcommon.menu("Device", "15");
        }
        UserRights();
        if (IsPostBack)
        {
            gintOrgLevel = Convert.ToInt32(Session["parentLevelId"]);
        }
        GenerateHtmlTableForInTerminals();
        GenerateHtmlTableForOutTerminals();
    }
    #endregion
    #region UserDefinedFunctions
    private DataSet FillTerminalsDropdown()
    {
        DataSet ds_Terminal = new DataSet();
        try
        {
            ds_Terminal = objTerminal.SelectTerminalDetailsForEntrance("I", 0);
        }
        catch
        {
        }
        return (ds_Terminal);
    }
    private void GenerateHtmlTableForInTerminals()
    {
        try
        {
            int numrows;
            int numcells;
            int i;
            int j;
            int count = 1;
            numrows = Convert.ToInt16(ddl_NoOfINTerminals.SelectedValue);
            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 <= Convert.ToInt16(ddl_NoOfINTerminals.SelectedValue))
                    {
                        TableCell c = new TableCell();
                        DropDownList ddl_INTerminals = new DropDownList();
                        ddl_INTerminals.CssClass = "form-control select2";
                        ddl_INTerminals.ID = "ddl_InTerminal" + count;
                        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";
                        count = count + 1;
                    }
                }
                Table1.Rows.Add(r);
            }
        }
        catch (Exception ex)
        {
            Response.Write(Convert.ToString(ex.Message));
        }
    }
    private void GenerateHtmlTableForOutTerminals()
    {
        try
        {
            int numrows;
            int numcells;
            int i;
            int j;
            int count = 1;
            numrows = Convert.ToInt16(ddl_NoOfOUTTerminals.SelectedValue);
            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 <= Convert.ToInt16(ddl_NoOfOUTTerminals.SelectedValue))
                    {
                        TableCell c = new TableCell();
                        DropDownList ddl_OUTTerminals = new DropDownList();
                        ddl_OUTTerminals.CssClass = "form-control select2";
                        ddl_OUTTerminals.ID = "ddl_OutTerminal" + count;
                        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("<lable>" + rm.GetString("EntranceMaster_Out_Terminal", ci) + " " + count + "</lable>"));
                        }
                        else
                        {
                            c.Controls.Add(new LiteralControl("<lable>" + 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";
                        count = count + 1;
                    }
                }
                Table2.Rows.Add(r);
            }
        }
        catch (Exception ex)
        {
            Message(ex.ToString());
        }
    }
    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"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["parentLevelId"]) != 0)
        {
            gintOrgLevel = Convert.ToInt32(Session["parentLevelId"]);
            Session["parentLevelId"] = null;
            strEntranceName = txt_EntranceName.Text.Replace("'", "''");
            strEntranceDesc = txt_EntranceDescription.Text.Replace("'", "''");
            int count = objentrance.CheckEntrance_Insert(strEntranceName, gintOrgLevel);
            if (count > 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("EntranceMaster_Entrance_Name_Duplicate", ci) + "');", true);
                return;
            }
            else
            {
                int intLoop;
                string strInTerminals = string.Empty;
                string strOutTerminals = string.Empty;
                try
                {
                    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('I', 0, strEntranceName, strEntranceDesc, gintOrgLevel, intNoOfInTerminals, intNoOfOutTerminals, strInTerminals, strOutTerminals, gintEmployeeID);
                    string strScript = "";
                    strScript = strScript + "<script language='javascript'>";
                    strScript = strScript + "alert('" + rm.GetString("Alert_Entrancesavedsuccessfully", ci) + "');";
                    strScript = strScript + "location.replace('TA_UI_Master_Entrance.aspx');";
                    strScript = strScript + "</script>";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                }
                catch
                { }
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_Selectallorganizationlevels", ci) + "');", true);
        }
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_Entrance.aspx");
    }
    #endregion
}