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 : 14-July-2007
* Programmer      : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_EntranceView : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    EntranceEventData objEntrance = new EntranceEventData();
    cls_Common objcommon = new cls_Common();
    DataSet ds_Checkuser = new DataSet();
    int intEntranceID;
    string gstrUserGroupName = string.Empty;
    int EmployeeID = 0;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        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);
        EmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
        UserRights();
        loadcss();
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        if (gstrUserGroupName != "SuperAdmin")
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        intEntranceID = Convert.ToInt32(Request.QueryString["Entrance_Id"]);
        FillEntranceDetails();
        btn_modify.Focus();
        if (!IsPostBack)
        {
            btn_Restore.OnClientClick = "return confirm('" + rm.GetString("Alert_Areyousuretorestore", ci) + "')";
            btn_delete.Attributes.Add("onclick", "return confirm('" + rm.GetString("Alert_ConfirmDelete", ci) + "')");
            btn_PermanentDelete.OnClientClick = "return confirm('" + rm.GetString("Alert_AreyouwantDeleteRecordPermanently", ci) + "')";
            objcommon.menu("Device", "15");
        }
        int count = objEntrance.CheckEntrance_Delete(intEntranceID);
        if (count > 0)
        {
            btn_delete.Enabled = false;
            btn_PermanentDelete.Enabled = false;
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_EntranceModify.aspx?Entrance_Id=" + intEntranceID);
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_Entrance.aspx");
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        int count = objEntrance.CheckEntrance_Delete(intEntranceID);
        if (count > 0)
        {
            base.Message(rm.GetString("Masters_Entrance_Cannot_Delete", ci));
            return;
        }
        objEntrance.deleteEntrances("TemporaryDelete", Convert.ToString(intEntranceID), EmployeeID);
        base.MessageWithRedirect(rm.GetString("Alert_Entrancedeletedsuccessfully", ci), "TA_UI_Master_Entrance.aspx");
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        string EntranceName;
        EntranceName = lbl_EntranceName.Text;
        int count = objEntrance.CheckRestoreEntrance(intEntranceID);
        if (count > 0)
        {
            base.Message(rm.GetString("Message_Cannot_Restore", ci));
            return;
        }
        objEntrance.deleteEntrances("Restore", Convert.ToString(intEntranceID), EmployeeID);
        base.MessageWithRedirect(rm.GetString("Alert_Entrancerestoredsuccessfully", ci), "TA_UI_Master_Entrance.aspx");
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        int count = objEntrance.CheckEntrance_Delete(intEntranceID);
        if (count > 0)
        {
            base.Message(rm.GetString("Masters_Entrance_Cannot_Delete", ci));
            return;
        }
        objEntrance.deleteEntrances("PermanentDelete", Convert.ToString(intEntranceID), EmployeeID);
        base.MessageWithRedirect(rm.GetString("Alert_Entrancedeletedsuccessfully", ci), "TA_UI_Master_Entrance.aspx");
    }
    #endregion
    #region UserDefinedFunctions
    private void FillEntranceDetails()
    {
        DataSet ds_Entrance = new DataSet();
        ds_Entrance = objEntrance.SelectEntranceDetailsByIDview(intEntranceID, gstrUserGroupName);
        if (ds_Entrance.Tables[0].Rows.Count > 0)
        {
            lbl_OrgLevel.Text = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["Name"]);
            lbl_EntranceName.Text = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["Entrance_Name"]);
            lbl_EntranceDescription.Text = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["Entrance_Description"]);
            lbl_NoOfINTerminals.Text = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["NoOfInTerminals"]);
            GenerateHtmlTableForInTerminals(Convert.ToInt16(lbl_NoOfINTerminals.Text));
            FillInTerminalValues(Convert.ToInt16(lbl_NoOfINTerminals.Text));
            lbl_NoOfOUTTerminals.Text = Convert.ToString(ds_Entrance.Tables[0].Rows[0]["NoOfOutTerminals"]);
            GenerateHtmlTableForOutTerminals(Convert.ToInt16(lbl_NoOfOUTTerminals.Text));
            FillOutTerminalValues(Convert.ToInt16(lbl_NoOfOUTTerminals.Text));
        }
        ds_Entrance.Dispose();
    }
    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();
                        Label lbl_Value = new Label();
                        lbl_Value.ID = "lbl_InTerminal" + count;
                        lbl_Value.Text = Convert.ToString(count);
                        lbl_Value.CssClass = "form-control";
                        c.CssClass = "col-md-4";
                        if (count < 10)
                        {
                            c.Controls.Add(new LiteralControl(rm.GetString("EntranceMaster_In_Terminal", ci) + " " + count + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                        }
                        else
                        {
                            c.Controls.Add(new LiteralControl(rm.GetString("EntranceMaster_In_Terminal", ci) + " " + count + "&nbsp;&nbsp;&nbsp;"));
                        }
                        c.Controls.Add(lbl_Value);
                        r.Cells.Add(c);
                        r.CssClass = "row";
                        count = count + 1;
                    }
                    Table1.Rows.Add(r);
                }
            }
        }
        catch
        { }
    }
    private void FillInTerminalValues(int inNoOfINTerminals)
    {
        try
        {
            DataSet ds_InTerminals = new DataSet();
            ds_InTerminals = objEntrance.SelectEntranceINOUTDetailsByIDview(intEntranceID, "In");
            int i = 0; for (i = 0; i < inNoOfINTerminals; i++)
            {
                if (ds_InTerminals.Tables.Count > 0)
                {
                    if (ds_InTerminals.Tables[0].Rows.Count > 0)
                    {
                        Label lblInTerminalId = new Label();
                        string LabelId = "lbl_InTerminal" + (i + 1);
                        lblInTerminalId = (Label)Table1.FindControl(LabelId);
                        if (lblInTerminalId != null)
                        {
                            lblInTerminalId.Text = "<b>" + Convert.ToString(ds_InTerminals.Tables[0].Rows[i]["Terminal_ReaderID"]) + "</b>";
                        }
                    }
                }
            }
        }
        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();
                        Label lbl_Value = new Label();
                        lbl_Value.ID = "lbl_OutTerminal" + count;
                        lbl_Value.Text = Convert.ToString(count); ;
                        lbl_Value.CssClass = "form-control";
                        c.CssClass = "col-md-4";
                        if (count < 10)
                        {
                            c.Controls.Add(new LiteralControl(rm.GetString("EntranceMaster_Out_Terminal", ci) + " " + count + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                        }
                        else
                        {
                            c.Controls.Add(new LiteralControl(rm.GetString("EntranceMaster_Out_Terminal", ci) + " " + count + "&nbsp;&nbsp;&nbsp;"));
                        }
                        c.Controls.Add(lbl_Value);
                        r.Cells.Add(c);
                        r.CssClass = "row";
                        count = count + 1;
                    }
                    Table2.Rows.Add(r);
                }
            }
        }
        catch
        { }
    }
    private void FillOutTerminalValues(int inNoOfOUTTerminals)
    {
        try
        {
            DataSet ds_OutTerminals = new DataSet();
            ds_OutTerminals = objEntrance.SelectEntranceINOUTDetailsByIDview(intEntranceID, "Out");
            int i = 0; for (i = 0; i < inNoOfOUTTerminals; i++)
            {
                if (ds_OutTerminals.Tables.Count > 0)
                {
                    if (ds_OutTerminals.Tables[0].Rows.Count > 0)
                    {
                        Label lblOutTerminalId = new Label();
                        string LabelId = "lbl_OutTerminal" + (i + 1);
                        lblOutTerminalId = (Label)Table1.FindControl(LabelId);
                        if (lblOutTerminalId != null)
                            lblOutTerminalId.Text = "<b>" + Convert.ToString(ds_OutTerminals.Tables[0].Rows[i]["Terminal_ReaderID"]) + "</b>";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_EntranceName1.Text = rm.GetString("EntranceMaster_Entrance_Name", ci);
        lbl_EntranceDescription1.Text = rm.GetString("EntranceMaster_Entrance_Desc", ci); ;
        lbl_NoOfINTerminals1.Text = rm.GetString("EntranceMaster_NoOfINTerminals", ci);
        lbl_NoOfOUTTerminals1.Text = rm.GetString("EntranceMaster_NoOfOUTTerminals", ci);
        lbl_OrganizationLevel.Text = rm.GetString("UserGroups_OrganizationLevel", ci);
        lbl_EntranceMaster.Text = rm.GetString("Masters_Entrance_EntranceMaster", ci);
        btn_modify.Text = rm.GetString("btn_Modify", ci);
        btn_delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_PermanentDelete", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(15, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) != 1)
            {
                Response.Redirect("TA_UI_Master_Entrance.aspx");
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.btn_modify.Visible = true;
            }
            else
            {
                this.btn_modify.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                this.btn_delete.Visible = true;
                this.btn_PermanentDelete.Visible = true;
                this.btn_Restore.Visible = true;
            }
            else
            {
                this.btn_delete.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void loadcss()
    {
        CultureInfo ci;
        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;
        htmlEntrance.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
            stylebuild.Href = "../bootstrap/font-awesome/css/build-rtl.css";
        }
        else
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
            stylebuild.Href = "../bootstrap/font-awesome/css/build.css";
        }
    }
    #endregion
}