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_EmployeeTypeList : Base.BasePage
{
    #region VariableDeclaration
    LeaveEventData objLeave = new LeaveEventData();
    cls_Common objcommon = new cls_Common();
    ResourceManager rm;
    CultureInfo ci;
    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_Type.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            //base.CheckPages(95);
            objcommon.menu("EmployeeMaster", "95");
        }
        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_Type.Columns[3].Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_EmployeeTypeAdd.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        TypeEventData Cat = new TypeEventData();
        string[] chkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            Cat.DeleteType(hdnSelectRow.Value, Convert.ToInt32(Session["EmpAutoID"]));
            string str = "alert('" + rm.GetString("Alert_EmployeeTypedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeTypelist.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;
        TypeEventData Cat = new TypeEventData();
        string[] chkBoxIDs;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            Cat.DeletePermanentType(hdnSelectRow.Value, Convert.ToInt32(Session["EmpAutoID"]));
            string str = "alert('" + rm.GetString("Alert_EmployeeTypedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeTypelist.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)
    {
        int CatID;
        TypeEventData Cat = new TypeEventData();
        string[] chkBoxIDs;
        DataSet ds = new DataSet();
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            chkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < chkBoxIDs.Length; i++)
            {
                ds = Cat.CheckDuplicate("U", Convert.ToInt32(chkBoxIDs[i]));
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        base.Message(rm.GetString("Alert_EmployeeTypealreadyexists", ci));
                        grv_Type.DataBind();
                        hdnSelectRow.Value = "";
                        return;
                    }
                }
            }
            Cat.RestoreType(hdnSelectRow.Value, Convert.ToInt32(Session["EmpAutoID"]));
            string str = "alert('" + rm.GetString("Alert_EmployeeTypeRestoredsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeTypelist.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 GridviewEvents
    protected void grv_Type_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_Type.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_Type.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_Type.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("TypeId").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("TypeId").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("TypeId").ToString();
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    #endregion
    #region UserDefinedFunction
    private void LoadStrings(CultureInfo ci)
    {
        lbl_EmployeeType.Text = rm.GetString("Master_Type", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Delete.Text = rm.GetString("btn_Delete", ci);
        btn_Restore.Text = rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        grv_Type.Columns[2].Caption = rm.GetString("Emp_TypeName", ci);
        grv_Type.Columns[3].Caption = rm.GetString("IsFlexibleTimings", ci);
        //grv_Type.Columns[4].Caption = rm.GetString("DeleteStatus", ci);
    }
    private void UserRights()
    {
        Cls_CheckUser chck = new Cls_CheckUser();
        DataSet ds = chck.checkRights(95, 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_PermanentDelete.Visible = true;
                this.btn_Restore.Visible = false;
                this.grv_Type.Columns[0].Visible = true;
                this.grv_Type.Columns[5].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                this.grv_Type.Columns[0].Visible = false;
                this.grv_Type.Columns[5].Visible = false;
            }
            if (ds.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.grv_Type.Columns[4].Visible = true;
            }
            else
            {
                this.grv_Type.Columns[4].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        //ToDo: 24-11-2015
        LinkButton btn = sender as LinkButton;
        int intEntranceID = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_EmployeeTypeModify.aspx?TypeID=" + intEntranceID);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        //ToDo: 24-11-2015
        LinkButton btn = sender as LinkButton;
        int intTypeID = Convert.ToInt32(btn.CommandName);
        TypeEventData Cat = new TypeEventData();
        Cat.DeletePermanentType(Convert.ToString(intTypeID), Convert.ToInt32(Session["EmpAutoID"]));
        string str = "alert('" + rm.GetString("Alert_EmployeeTypedeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeTypelist.aspx');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
    }
    #endregion
}
