using DevExpress.Web;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                        Started Date  : 30-July-2007
* Module          : Terminal Datatype Master                         Last Modified : 08-Aug-2007
* Programmer      : Kumuthini.L
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          : \\windev\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_TerminalDatatype : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    TerminalDatatypeEventData objTerminalDatatype = new TerminalDatatypeEventData();
    TerminalEventData objTerminalEventData = new TerminalEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Common objCommon = new cls_Common();
    string source_Language;
    DataSet ds_Checkuser = new DataSet();
    LeaveEventData objLeave = new LeaveEventData();
    string gstrUserGroupName = string.Empty;
    int gintUserId;
    string strScript = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        grv_TerminalDatatype.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        gintUserId = Convert.ToInt32(Session["EmpAutoID"]);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "RestoreAlert('" + rm.GetString("Alert_Norecordsfound", ci) + "','" + rm.GetString("Alert_Areyousuretorestore", ci) + "','" + rm.GetString("Alert_Selecteddataisnotindeletedstatus", ci) + "','" + rm.GetString("Alert_Selectdatatorestore", ci) + "','" + rm.GetString("Alert_Nodeletedrecordsavailable", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("RestoreAlert", strSetAlert);
        UserRights();
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        if (gstrUserGroupName != "SuperAdmin")
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        btn_add.Focus();
        if (!IsPostBack)
        {
            base.CheckPages(223);
            objCommon.menu("Terminal", "223");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_TerminalDatatypeAdd.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                string UserGroupName = string.Empty;
                int Count = objTerminalEventData.GetCountTerminalForPermanentDelete(Convert.ToInt32(ChkBoxIDs[i]), UserGroupName);
                if (Count > 0)
                {
                    base.Message(rm.GetString("Master_Terminal_DataType_Delete", ci));
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objTerminalDatatype.PermanentDeleteTerminalDataTypes(hdnSelectRow.Value, gintUserId, "TemporaryDelete");
            base.MessageWithRedirect(rm.GetString("Alert_TerminalDataTypedeletedsuccessfully", ci), "TA_UI_Master_TerminalDatatype.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                int intTerminalDatatypeId = Convert.ToInt16(ChkBoxIDs[i]);
                int Count = objTerminalDatatype.CheckRestoreTerminalDataType(intTerminalDatatypeId);
                if (Count > 0)
                {
                    base.Message(rm.GetString("Message_Cannot_Restore", ci));
                    grv_TerminalDatatype.DataBind();
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objTerminalDatatype.PermanentDeleteTerminalDataTypes(hdnSelectRow.Value, gintUserId, "Restore");
            base.MessageWithRedirect(rm.GetString("Alert_TerminalDataTyperestoredsuccessfully", ci), "TA_UI_Master_TerminalDatatype.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                string UserGroupName = string.Empty;
                int Count = objTerminalEventData.GetCountTerminalForPermanentDelete(Convert.ToInt32(ChkBoxIDs[i]), UserGroupName);
                if (Count > 0)
                {
                    base.Message(rm.GetString("Master_Terminal_DataType_Delete", ci));
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objTerminalDatatype.PermanentDeleteTerminalDataTypes(hdnSelectRow.Value, gintUserId, "PermanentDelete");
            base.MessageWithRedirect(rm.GetString("Terminal_PermanentDelete"), "TA_UI_Master_TerminalDatatype.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_TerminalList.aspx");
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintTerminalDatatypeID = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_TerminalDatatypeModify.aspx?TerminalDatatype_ID=" + Cryptographys.Cryptography.Encrypt(Convert.ToString(gintTerminalDatatypeID), "TimecheckPhaseI"));
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintTerminalDatatypeID = Convert.ToInt32(btn.CommandName);
        string userGroupName = string.Empty;
        int Count = objTerminalEventData.GetCountTerminal(gintTerminalDatatypeID, userGroupName);
        if (Count > 0)
        {
            base.Message(rm.GetString("Master_Terminal_DataType_Delete", ci));
            return;
        }
        objTerminalDatatype.PermanentDeleteTerminalDataTypes(Convert.ToString(gintTerminalDatatypeID), gintUserId, "PermanentDelete");
        base.MessageWithRedirect(rm.GetString("Alert_TerminalDataTypedeletedsuccessfully", ci), "TA_UI_Master_TerminalDatatype.aspx");
    }
    #endregion
    #region UserDefinedFunctions
    // **********************************************************************************************************************
    // Function Name : LoadStrings
    // Purpose :
    //     LoadStrings Function is used to get Information from resource file and display it in label
    // **********************************************************************************************************************
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderTerminalDatatype1.Text = rm.GetString("Masters_Terminal_TerminalDataType", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", 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_Delete", ci);
        grv_TerminalDatatype.Columns[2].Caption = rm.GetString("TerminalDataType_RawDataFormat", ci);
        grv_TerminalDatatype.Columns[3].Caption = rm.GetString("TerminalDataType_FileType", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
    }
    // **********************************************************************************************************************
    // Function Name : UserRights
    // Purpose :
    //     The UserRights Method is used to get the User Rights for the currently Logged IN Employee and based on the Rights,
    //     Add, Modify, Delete and View Button will be either visible or not visible. 
    // **********************************************************************************************************************
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(10, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_add.Visible = true;
            }
            else
            {
                this.btn_add.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 = false;
                this.btn_PermanentDelete.Visible = true;
                this.btn_Restore.Visible = false;
                this.grv_TerminalDatatype.Columns[1].Visible = true;
                this.grv_TerminalDatatype.Columns[6].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                this.grv_TerminalDatatype.Columns[1].Visible = false;
                this.grv_TerminalDatatype.Columns[6].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.grv_TerminalDatatype.Columns[5].Visible = true;
            }
            else
            {
                this.grv_TerminalDatatype.Columns[5].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                this.grv_TerminalDatatype.Columns[4].Visible = true;
            }
            else
            {
                this.grv_TerminalDatatype.Columns[4].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    /// <summary>
    /// Handles the RowDataBound event of the grv_TerminalDatatype control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
    protected void grv_TerminalDatatype_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            Label DeletedRecord = (Label)grv_TerminalDatatype.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True" || DeletedRecord.Text == rm.GetString("Grid_True", ci))
                {
                    DeletedRecord.ForeColor = System.Drawing.Color.Red;
                }
            }
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_TerminalDatatype.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_TerminalDatatype.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");
            if (deleteChkBxItem != null)
            {
                int TerminalId = Convert.ToInt16(e.GetValue("TerminalDatatype_ID"));
                string UserGroupName = string.Empty;
                int Count = objTerminalEventData.GetCountTerminal(TerminalId, UserGroupName);
                if (Count > 0)
                {
                    deleteChkBxItem.Disabled = true;
                    LnkbtnDelete.Enabled = false;
                    LnkbtnDelete.OnClientClick = null;
                }
                else
                {
                    deleteChkBxItem.Disabled = false;
                    LnkbtnDelete.Enabled = true;
                }
            }
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = Convert.ToString(e.GetValue("TerminalDatatype_ID"));
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(Convert.ToString(e.GetValue("TerminalDatatype_ID"))) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + Convert.ToString(e.GetValue("TerminalDatatype_ID"));
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    #endregion
}