using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
using System.Web.UI.HtmlControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                   Started Date  : 12-Aug-2007
* Module          : Leave Master                                Last Modified : 14-Aug-2007
* Programmer      : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Master_TA_UI_MasterLeaveTypeList : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    LeaveEventData objLeave = new LeaveEventData();

    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    string gstrUserGroupName = string.Empty;
    int EmployeeID = 0;
    string source_Language;
    string strScript = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        Session["CountFactor_LeaveTypeId"] = "0";
        // session checking
        base.Page_Load(sender, e);
        GridView1.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;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        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);
        // LoadStrings Function fills the Label from the Resource File
        LoadStrings(ci);
        EmployeeID = Convert.ToInt32(Convert.ToString(Session["EmpAutoID"]));
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        //Set UserRights
        UserRights();
        if (gstrUserGroupName != "SuperAdmin")
        {
            //GridView1.Columns[4].Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        btn_add.Focus();
        if (!IsPostBack)
        {
            objCommon.menu("LeaveMasters", "195");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_MasterLeaveTypeAdd.aspx");
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_MasterLeaveMain.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objLeave.Delete_LeaveTypes(hdnSelectRow.Value, EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_LeaveTypedeletedsuccessfully", ci), "TA_UI_MasterLeaveTypeList.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)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            string[] LeaveTypeIDs;
            LeaveTypeIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < LeaveTypeIDs.Length; i++)
            {
                int Count = objLeave.CheckRestoreLeaveType(Convert.ToInt32(LeaveTypeIDs[i]));
                if (Count > 0)
                {
                    base.Message(rm.GetString("Message_Cannot_Restore", ci));
                    GridView1.DataBind();
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objLeave.RestoreLeaveTypes(hdnSelectRow.Value, EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_LeaveTyperestoredsuccessfully", ci), "TA_UI_MasterLeaveTypeList.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)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            int Count = objLeave.CheckPermanentDeleteLeaveTypes(hdnSelectRow.Value);
            if (Count > 0)
            {
                base.Message(rm.GetString("Message_Cannot_Delete", ci));
                GridView1.DataBind();
                hdnSelectRow.Value = "";
                return;
            }
            objLeave.PermanentDeleteLeaveTypes(hdnSelectRow.Value, EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_LeaveTypedeletedsuccessfully", ci), "TA_UI_MasterLeaveTypeList.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 EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintLeaveTypeId = Convert.ToInt32(btn.CommandName);
        int Count = objLeave.CheckLeaveType(gintLeaveTypeId);
        int Count1 = objLeave.CheckLeaveType_LeaveApp(gintLeaveTypeId);
        if (Count > 0 || Count1 > 0)
        {
            base.Message(rm.GetString("Master_Leave_Type_Cannot_Modify", ci));
        }
        else
        {
            Response.Redirect("TA_UI_MasterLeaveTypeModify.aspx?LeaveId=" + gintLeaveTypeId);
        }
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int gintLeaveTypeId = Convert.ToInt32(btn.CommandName);
        int Count = objLeave.CheckLeaveType(gintLeaveTypeId);
        int Count1 = objLeave.CheckLeaveType_LeaveApp(gintLeaveTypeId);
        if (Count > 0 || Count1 > 0)
        {
            base.Message(rm.GetString("Alert_Cannot_Delete_this_Record_It_is_Already_mapped", ci));
        }
        else
        {
            objLeave.PermanentDeleteLeaveTypes(Convert.ToString(gintLeaveTypeId), EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_LeaveTypedeletedsuccessfully", ci), "TA_UI_MasterLeaveTypeList.aspx");
        }

    }
    //Getting Information from resource file and display it in label
    #endregion
    #region UserDefinedFunctions
    private void LoadStrings(CultureInfo ci)
    {
        lbl_LeaveType.Text = rm.GetString("lbl_leavetype", 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);
        GridView1.Columns[2].Caption = rm.GetString("lbl_leavetype", ci);
        GridView1.Columns[3].Caption = rm.GetString("LeaveType_LeaveDesc", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);

    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(195, 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 = true;
                //this.btn_PermanentDelete.Visible = true;
                //this.GridView1.Columns[0].Visible = true;
                //this.btn_Restore.Visible = true;
                //Karthikeyan
                this.btn_Delete.Visible = false;
                this.btn_PermanentDelete.Visible = true;
                this.GridView1.Columns[0].Visible = true;
                this.GridView1.Columns[6].Visible = true;
                this.btn_Restore.Visible = false;

            }
            else
            {
                this.btn_Delete.Visible = false;
                this.GridView1.Columns[0].Visible = false;
                this.GridView1.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.GridView1.Columns[5].Visible = true;
            }
            else
            {
                this.GridView1.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.GridView1.Columns[4].Visible = true;
            }
            else
            {
                this.GridView1.Columns[4].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    protected void GridView1_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        base.Page_Load(sender, 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)GridView1.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)GridView1.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)GridView1.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 = Convert.ToString(e.GetValue("LeaveType_Id"));
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(Convert.ToString(e.GetValue("LeaveType_Id"))) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + Convert.ToString(e.GetValue("LeaveType_Id"));
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
                if (Convert.ToBoolean(e.GetValue("CountFactor")) == true)
                    Session["CountFactor_LeaveTypeId"] = Convert.ToString(e.GetValue("LeaveType_Id"));
            }
        }
    }
    #endregion
}