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          : Title Master                                  Last Modified : 09-Feb-2008
* Programmer      : Arun vinoth.T
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Master_TA_UI_Master_TitleFirstList : Base.BasePage
{
    protected void btnExport_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        GridView1.Columns[0].Visible = false;
        GridView1.Columns[5].Visible = false;
        GridView1.Columns[7].Visible = false;
        ASPxGridViewExporter1.GridViewID = "GridView1";
        ASPxGridViewExporter1.FileName = "List of Designation";
        ASPxGridViewExporter1.Landscape = true;
        ASPxGridViewExporter1.LeftMargin = 30;
        ASPxGridViewExporter1.RightMargin = 30;
        ASPxGridViewExporter1.MaxColumnWidth = 200;
        switch (btn.CommandName)
        {
            case "excel":
                ASPxGridViewExporter1.WriteXlsxToResponse();
                break;
            case "pdf":
                ASPxGridViewExporter1.WritePdfToResponse();
                break;
            default:
                break;
        }
    }
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Master_Title objTitle = new cls_Master_Title();
    string strSetAlert;
    DataSet ds_Checkuser = new DataSet();
    LeaveEventData objLeave = new LeaveEventData();
    cls_Common objCommon = new cls_Common();
    string gstrUserGroupName = string.Empty;
    int auEmpId;
    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);
        GridView1.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        //get the culture info to set the language       
        if (!Page.IsPostBack)
        {
            objCommon.menu("EmployeeMaster", "17");
        }
        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"]);
        
        if (!IsPostBack)
        {
            //Set UserRights
            UserRights();
            LoadStrings(ci);
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            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("MaxLengthh", strSetAlert);
        }// LoadStrings Function fills the Label from the Resource File

        auEmpId = Convert.ToInt32(Session["EmpAutoID"].ToString());

        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        if (gstrUserGroupName != "SuperAdmin")
        {
            //GridView1.Columns[4].Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        btn_add.Focus();
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_TitleFirstAdd.aspx");
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_TitleMain.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        string ChkBoxIDs = string.Empty;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objTitle.DeleteTitles(hdnSelectRow.Value, auEmpId);
            string str = "alert('" + rm.GetString("Alert_DesignationDeletedSuccessfully", ci) + "');location.replace('TA_UI_Master_TitleFirstList.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 intCount = objTitle.CheckTitleName_Update(Convert.ToInt32(ChkBoxIDs[i]));
                if (intCount > 0)
                {
                    base.Message(rm.GetString("Message_Cannot_Restore", ci));
                    GridBind();
                    hdnSelectRow.Value = "";
                    return;
                }
            }
            objTitle.RestoreTitles(hdnSelectRow.Value, auEmpId);
            Response.Redirect("TA_UI_Master_TitleFirstList.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()
    {
        GridView1.DataBind();
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        string ChkBoxIDs = string.Empty;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            objTitle.PermanentDeleteTitles(hdnSelectRow.Value, auEmpId);
            string str = "alert('" + rm.GetString("Alert_DesignationDeletedSuccessfully", ci) + "');location.replace('TA_UI_Master_TitleFirstList.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_Clear_Click(object sender, EventArgs e)
    {
        txt_Designation.Text = string.Empty;
        GridView1.DataSourceID = ObjectDataSource1.ID;
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        FillGrid_Search();
    }
    #endregion
    #region UserDefinedFunctions
    protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["Designation"] = txt_Designation.Text.Replace("'", "''").Trim();
    }
    private void FillGrid_Search()
    {
        string strDesignation = txt_Designation.Text.Trim();
        txt_Designation.Text = strDesignation;
        if (strDesignation == string.Empty)
        {
            base.Message(rm.GetString("Alert_Pleasetypethedesignationnametosearch", ci));
            GridView1.Columns.Clear();
            txt_Designation.Focus();
        }
        else
        {
            GridView1.DataSourceID = ObjectDataSource1.ID;
        }
    }
    public void GetEmptyText(object sender, EventArgs e)
    {
        Label lblNoRecords = (Label)sender;
        lblNoRecords.Text = rm.GetString("Alert_Norecordsfound", ci);
    }
    private string CheckDelete()
    {
        string ChkBoxIDs = string.Empty;
        return (ChkBoxIDs);
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lbl_Designation.Text = rm.GetString("Designation_DesignationName", ci);
        btn_Search.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Search", ci);
        btn_Clear.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Clear", 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);
        GridView1.Columns[2].Caption = rm.GetString("Designation_DesignationName", ci);
        GridView1.Columns[3].Caption = rm.GetString("Designation_DesignationDesc", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
        //GridView1.Columns[4].Caption = rm.GetString("DeleteStatus", ci);
        GridView1.Columns[4].Caption = rm.GetString("EntranceMaster_Company", ci);

        GridView1.Columns[8].Caption = rm.GetString("Grade_GradeName", ci);
        GridView1.Columns[9].Caption = rm.GetString("Grade_GradeLevel", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(17, 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_Restore.Visible = false;
                this.btn_PermanentDelete.Visible = true;
                this.GridView1.Columns[0].Visible = true;
                this.GridView1.Columns[7].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.btn_Restore.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.GridView1.Columns[0].Visible = false;
                this.GridView1.Columns[7].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[6].Visible = true;
            }
            else
            {
                this.GridView1.Columns[6].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[5].Visible = true;
            }
            else
            {
                this.GridView1.Columns[5].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    protected void GridView1_HtmlRowCreated(object sender, DevExpress.Web.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 = e.GetValue("TitleId").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("TitleId").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("TitleId").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 TitleId = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_TitleFirstModify.aspx?TitleId=" + TitleId);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int TitleId = Convert.ToInt32(btn.CommandName);
        int count = objTitle.CountTitle(TitleId);
        if (count == 0)
        {
            objTitle.PermanentDeleteTitles(Convert.ToString(TitleId), auEmpId);
            base.MessageWithRedirect(rm.GetString("Alert_DesignationDeletedSuccessfully", ci), "TA_UI_Master_TitleFirstList.aspx");
        }
        else
        {
            base.Message(rm.GetString("Alert_DesignationCannotbeDeleted", ci));
            return;
        }
    }
}