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_ReligionList : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    LeaveEventData objLeave = new LeaveEventData();
    ReligionMaster objReligion = new ReligionMaster();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Common objCommon = new cls_Common();
    DataSet ds_Checkuser = new DataSet();
    string gstrUserGroupName = string.Empty;
    int intEmployeeID = 0;
    string strScript = string.Empty;
    string strSource_Language;
    #endregion
    #region Page load
    /// <summary>
    /// To load page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        gvReligion.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            objCommon.menu("EmployeeMaster", "229");
        }
        //get the culture info to set the language
        strSource_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(strSource_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        // LoadStrings Function fills the Label from the Resource File
        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);
        intEmployeeID = Convert.ToInt32(Session["EmpAutoID"].ToString());
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        //Set UserRights
        UserRights();
        if (gstrUserGroupName != "SuperAdmin")
        {
            //gvReligion.Columns[4].Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        btn_add.Focus();
    }
    #endregion
    #region user defined
    //Getting Information from resource file and display it in label
    /// <summary>
    /// To load label names
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        lbl_LeaveType.Text = rm.GetString("Master_Religion", 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_Cancel.Text = 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);
        lblDataView.Text = rm.GetString("lblDataView", ci);
        gvReligion.Columns[2].Caption = rm.GetString("EmployeeMaster_Religion", ci);
        gvReligion.Columns[3].Caption = rm.GetString("Caption_ReligionName", ci);
        //gvReligion.Columns[4].Caption = rm.GetString("DeleteStatus", ci);
    }
    /// <summary>
    /// 
    /// </summary>
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(229, 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.gvReligion.Columns[0].Visible = true;
                this.gvReligion.Columns[6].Visible = true;
                this.btn_Restore.Visible = false;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.gvReligion.Columns[0].Visible = false;
                this.gvReligion.Columns[6].Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.gvReligion.Columns[5].Visible = true;
            }
            else
            {
                this.gvReligion.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.gvReligion.Columns[4].Visible = true;
            }
            else
            {
                this.gvReligion.Columns[4].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    /// <summary>
    /// To check delete
    /// </summary>
    /// <returns></returns>
    private string CheckDelete()
    {
        string chkBoxIDs = string.Empty;
        return (chkBoxIDs);
    }
    #endregion
    #region Button events
    /// <summary>
    /// To add religion
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_ReligionAdd.aspx");
    }
    /// <summary>
    /// To delete
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objReligion.Delete_ReligionTypes(hdnSelectRow.Value, intEmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_Religiondeletedsuccessfully", ci), "TA_UI_Master_ReligionList.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    /// <summary>
    /// To restore religion
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            string[] chkBoxIDs;
            chkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < chkBoxIDs.Length; i++)
            {
                int Count = objReligion.CheckRestoreReligionType(Convert.ToInt16(chkBoxIDs[i]));
                if (Count > 0)
                {
                    base.Message(rm.GetString("Message_Cannot_Restore", ci));
                    gvReligion.DataBind();
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objReligion.RestoreReligionTypes(hdnSelectRow.Value, intEmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_Religionrestoredsuccessfully", ci), "TA_UI_Master_ReligionList.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    /// <summary>
    /// To delete Records permanantly
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objReligion.PermanentDeletereligionTypes(hdnSelectRow.Value, intEmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_Religiondeletedsuccessfully", ci), "TA_UI_Master_ReligionList.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_Masters_Generic_List.aspx");
    }
    #endregion
    #region grid events
    protected void gvReligion_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            strSource_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(strSource_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            Label DeletedRecord = (Label)gvReligion.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)gvReligion.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)gvReligion.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("ID").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("ID").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("ID").ToString();
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    #endregion
    //Todo:need to add below two button events
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintReligionId = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_ReligionModify.aspx?Id=" + gintReligionId);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintReligionId = Convert.ToInt32(btn.CommandName);
        int intReferranceCount = objReligion.CheckReligionReferance(gintReligionId);
        // set enable status to delete check box
        if (intReferranceCount > 0)
        {
            base.Message(rm.GetString("Alert_Cannot_Delete_this_Record_It_is_Already_mapped", ci));
        }
        else if (intReferranceCount == 0)
        {
            objReligion.PermanentDeletereligionTypes(Convert.ToString(gintReligionId), intEmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_Religiondeletedsuccessfully", ci), "TA_UI_Master_ReligionList.aspx");
        }
    }
}
