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;
public partial class Masters_TA_UI_Master_EmployeeCatagory : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    LeaveEventData objLeave = new LeaveEventData();
    cls_Common objcommon = new cls_Common();
    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_Category.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            //base.CheckPages(94);
            objcommon.menu("EmployeeMaster", "94");
        }
        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);
        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);
        btn_add.Focus();
        //Set UserRights
        UserRights();
        string gstrUserGroupName = string.Empty;
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        if (gstrUserGroupName != "SuperAdmin")
        {
            //grv_Category.Columns[3].Visible = false;
            btn_Restore.Visible = false;
            //btn_PermanentDelete.Visible = false;
        }
    }
    #endregion
    #region UserDefinedFunctions
    private void UserRights()
    {
        Cls_CheckUser chck = new Cls_CheckUser();
        DataSet ds = chck.checkRights(94, Convert.ToInt32(Session["userGroup_Id"]), " ");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_add.Visible = true;
            }
            else
            {
                this.btn_add.Visible = false;
            }
            if (ds.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                this.btn_Delete.Visible = false;
                this.btn_Restore.Visible = false;
                this.btn_PermanentDelete.Visible = true;
                this.grv_Category.Columns[0].Visible = true;
                this.grv_Category.Columns[5].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.btn_Restore.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.grv_Category.Columns[0].Visible = false;
                this.grv_Category.Columns[5].Visible = false;
            }
            if (ds.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.grv_Category.Columns[4].Visible = true;
            }
            else
            {
                this.grv_Category.Columns[4].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_EmployeeCategory.Text = rm.GetString("Master_Category", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        grv_Category.Columns[2].Caption = rm.GetString("Emp_CategoryName", ci);
        grv_Category.Columns[3].Caption = rm.GetString("IsFlexibleTimings", ci);
        //grv_Category.Columns[4].Caption = rm.GetString("DeleteStatus", ci);
    }
    #endregion
    #region GridveiwEvents
    protected void grv_Category_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_Category.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_Category.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_Category.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("CategoryId").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("CategoryId").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("CategoryId").ToString();
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_EmployeeCategoryAdd.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        int CatID;
        CategoryEventData Cat = new CategoryEventData();
        string[] chkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            Cat.DeleteCategory(hdnSelectRow.Value, Convert.ToInt32(Session["EmpAutoID"]));
            string str = "alert('" + rm.GetString("Alert_EmployeeCategorydeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeCategorylist.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)
    {
        DataSet ds = new DataSet();
        string[] chkBoxIDs;
        CategoryEventData objCat = new CategoryEventData();
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            chkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < chkBoxIDs.Length; i++)
            {
                ds = objCat.CheckDuplicate("U", Convert.ToInt32(chkBoxIDs[i]));
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        base.Message(rm.GetString("Alert_EmployeeCategoryalreadyexists", ci));
                        grv_Category.DataBind();
                        hdnSelectRow.Value = "";
                        return;
                    }
                }
            }
            objCat.RestoreCategory(hdnSelectRow.Value, Convert.ToInt32(Session["EmpAutoID"]));
            string str = "alert('" + rm.GetString("Alert_EmployeeCategoryrestoredsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeCategorylist.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_PermanentDelete_Click(object sender, EventArgs e)
    {
        int CatID;
        CategoryEventData Cat = new CategoryEventData();
        string[] chkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            Cat.DeletePermanentCategory(hdnSelectRow.Value, Convert.ToInt32(Session["EmpAutoID"]));
            string str = "alert('" + rm.GetString("Alert_EmployeeCategorydeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeCategorylist.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
    //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_EmployeeCategoryModify.aspx?CatID=" + Id);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        //Todo: need to add a functionality
        LinkButton btn = sender as LinkButton;
        int Id = Convert.ToInt32(btn.CommandName);
        CategoryEventData Cat = new CategoryEventData();
        Cat.DeletePermanentCategory(Convert.ToString(Id), Convert.ToInt32(Session["EmpAutoID"]));
        string str = "alert('" + rm.GetString("Alert_EmployeeCategorydeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeCategorylist.aspx');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
    }
}
