using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                         Started Date  : 12-Aug-2007
* Module          : Generic Master                                    Last Modified : 21-Aug-2007
* Programmer      : Arun Vinoth.T
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_GradeList : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    GenericEventdata objGeneric = new GenericEventdata();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    string gstrUserGroupName = string.Empty;
    int EmpId;
    LeaveEventData objLeave = new LeaveEventData();
    string strScript = string.Empty;
    string source_Language;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        grv_grade.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        //get the culture info to set the 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"]);
        EmpId = Convert.ToInt32(Session["EmpAutoID"].ToString());
        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);
        //Set UserRights
        UserRights();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName == "SuperAdmin")
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = true;
        }
        else
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
            //grv_grade.Columns[3].Visible = false;
        }
        btn_add.Focus();
        if (!IsPostBack)
        {
            objcommon.menu("EmployeeMaster", "228");
        }
        ////if Standar Edition set the visibility to false
        //    if (Session["Edition"].ToString() == "1")
        //    {
        //        Response.Redirect("TA_UI_Masters_Generic_List.aspx");
        //    }        
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_GradeAdd.aspx");
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Masters_Generic_List.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++)
            {
                int Count = objGeneric.CountGrade(Convert.ToInt32(ChkBoxIDs[i]));
                if (Count > 0)
                {
                    base.Message(rm.GetString("Message_Grade_Cannot_Deleted", ci));
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objGeneric.TemporaryDeleteGrade(hdnSelectRow.Value, EmpId);
            string str = "alert('" + rm.GetString("Alert_Gradedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_GradeList.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        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 Count = objGeneric.CheckGradeName_Update(Convert.ToInt32(ChkBoxIDs[i]));
                if (Count > 0)
                {
                    base.Message(rm.GetString("Message_Cannot_Restore", ci));
                    GridBind();
                    hdnSelectRow.Value = "";
                    return;
                }
                objGeneric.RestoreGrade(ChkBoxIDs[i], EmpId);
            }
            base.MessageWithRedirect(rm.GetString("Alert_Graderestoredsuccessfully", ci), "TA_UI_Master_GradeList.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 GridBind()
    {
        grv_grade.DataBind();
    }
    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++)
            {
                int Count = objGeneric.CountGrade(Convert.ToInt16(ChkBoxIDs[i]));
                if (Count > 0)
                {
                    base.Message(rm.GetString("Message_Grade_Cannot_Deleted", ci));
                    grv_grade.DataBind();
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            //permanent delete
            objGeneric.PermanentDeleteGrade(hdnSelectRow.Value, EmpId);
            string str = "alert('" + rm.GetString("Alert_Gradedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_GradeList.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    #endregion
    #region UserDefinedFunctions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderGrade.Text = rm.GetString("Masters_Generic_Grade", 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_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        grv_grade.Columns[1].Caption = rm.GetString("Grade_GradeName", ci);
        grv_grade.Columns[5].Caption = rm.GetString("Grade_GradeLevel", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
        //grv_grade.Columns[3].Caption = rm.GetString("DeleteStatus", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(228, 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.grv_grade.Columns[0].Visible = true;
                this.grv_grade.Columns[4].Visible = true;
                this.btn_PermanentDelete.Visible = true;
                this.btn_Restore.Visible = false;
            }
            else
            {
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                this.btn_Delete.Visible = false;
                this.grv_grade.Columns[0].Visible = false;
                this.grv_grade.Columns[4].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.grv_grade.Columns[3].Visible = true;
            }
            else
            {
                this.grv_grade.Columns[3].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                this.grv_grade.Columns[2].Visible = true;
            }
            else
            {
                this.grv_grade.Columns[2].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    protected void grv_grade_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.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_grade.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_grade.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_grade.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");
            if (deleteChkBxItem != null)
            {
                if (deleteChkBxItem.Disabled == false)
                {
                    LnkbtnDelete.Enabled = true;
                }
                else
                {
                    LnkbtnDelete.Enabled = false;
                    LnkbtnDelete.OnClientClick = null;
                }
            }
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = e.GetValue("Grade_ID").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("Grade_ID").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("Grade_ID").ToString();
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    //Todo:need to add below two button events
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int Id = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_GradeModify.aspx?Id=" + Id);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int Id = Convert.ToInt32(btn.CommandName);
        int Count = objGeneric.CountGrade(Convert.ToInt32(Id));
        if (Count > 0)
        {
            base.Message(rm.GetString("Message_Grade_Cannot_Deleted", ci));
            return;
        }
        objGeneric.PermanentDeleteGrade(Convert.ToString(Id), EmpId);
        string str = "alert('" + rm.GetString("Alert_Gradedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_GradeList.aspx');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
    }
    #endregion
}
