using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DevExpress.Web;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                         Started Date  : 12-Aug-2007
* Module          : Holiday Master                                    Last Modified : 20-Aug-2007
* Programmer      : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_Holiday : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    HolidayEventData objHoliday = new HolidayEventData();
    cls_Common objCommon = new cls_Common();
    string gstrUserGroupName = string.Empty;
    int auEmpId; int gintTempWeekdayCount = 0;
    DataSet dsYear = new DataSet();
    cls_Common objClassCommon = new cls_Common();
    string source_Language;
    LeaveEventData objLeave = new LeaveEventData();
    string strScript = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);        
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        if (!Page.IsPostBack)
        {
            base.CheckPages(16);
            objCommon.menu("Configuration", "16");
            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) + "','" + rm.GetString("Alert_AreyousuretoDelete", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("RestoreAlert", strSetAlert);
            UserRights();
            btn_Add.Focus();
        }
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        auEmpId = Convert.ToInt32(Session["EmpAutoID"]);
        grv_Holiday.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (ViewState["DsYearSetting"] != null)
        {
            dsYear = (DataSet)ViewState["DsYearSetting"];
        }
        else
        {
            dsYear = objCommon.GetYearsetting();
        }
        if (dsYear.Tables.Count > 0)
        {
            if (dsYear.Tables[0].Rows.Count > 0)
            {
                Session["YearsettingsId"] = Convert.ToInt32(dsYear.Tables[0].Rows[0]["YearSettings_Id"]);
            }
        }
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName == "SuperAdmin")
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = true;
        }
        else
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
    }
    #endregion

    protected void btnExport_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        grv_Holiday.Columns[0].Visible = false;
        grv_Holiday.Columns[5].Visible = false;
        grv_Holiday.Columns[7].Visible = false;
        grv_Holiday.Columns[8].Visible = false;

        ASPxGridViewExporter1.GridViewID = "grv_Holiday";
        ASPxGridViewExporter1.FileName = "List of Holidays";
        ASPxGridViewExporter1.Landscape = true;
        ASPxGridViewExporter1.LeftMargin = 30;
        ASPxGridViewExporter1.RightMargin = 30;
        ASPxGridViewExporter1.MaxColumnWidth = 200;
        ASPxGridViewExporter1.PageHeader.Center = "List Of Holidays";
        switch (btn.CommandName)
        {
            case "excel":
                ASPxGridViewExporter1.WriteXlsxToResponse();
                break;
            case "pdf":
                ASPxGridViewExporter1.WritePdfToResponse();
                break;
            default:
                break;
        }
    }
    #region ButtonEvents
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_HolidayAdd.aspx");
    }
    protected void btn_Delete_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 Count = objHoliday.GetRestrictedHolidayCount(Convert.ToInt32(ChkBoxIDs[i]));
                if (Count > 0)
                {
                    hdnSelectRow.Value = "";
                    base.Message(rm.GetString("Master_Holiday_Cannot_Delete", ci));
                    return;
                }
            }
            objHoliday.DeleteHolidayDetails(hdnSelectRow.Value, "TemporaryDelete", auEmpId);
            base.MessageWithRedirect(rm.GetString("Alert_Holidaydeletedsuccessfully", ci), "TA_UI_Master_Holiday.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    //Getting Information from resource file and display it in label   
    protected void btn_PermanentDelete_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 Count = objHoliday.GetRestrictedHolidayCount(Convert.ToInt32(ChkBoxIDs[i]));
                if (Count > 0)
                {
                    hdnSelectRow.Value = "";
                    base.Message(rm.GetString("Master_Holiday_Cannot_Delete", ci));
                    return;
                }
            }
            objHoliday.DeleteHolidayDetails(hdnSelectRow.Value, "PermanentDelete", auEmpId);
            base.MessageWithRedirect(rm.GetString("Alert_Holidaydeletedsuccessfully", ci), "TA_UI_Master_Holiday.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)
    {
        string gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        int GlobalMax = 0, holidaycount;
        DataSet ds = new DataSet();
        string SelectedChkBoxIDs = "";
        ds = objHoliday.MaxHoliday();
        string[] ChkBoxIDs;
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count <= 0)
            {
                base.Message(rm.GetString("Alert_DefineHolidaysettingsinOrganisationSettings", ci));
                return;
            }
            else
            {
                GlobalMax = Convert.ToInt16(ds.Tables[0].Rows[0]["Global_Maxdays"]);
            }
        }
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            holidaycount = objHoliday.HoildayCount("G");
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                int Holiday_Id = Convert.ToInt32(ChkBoxIDs[i]);
                DataSet dsHoliday = new DataSet();
                dsHoliday = objHoliday.GetHolidayDetailsByHolidayID(Holiday_Id);
                if (dsHoliday.Tables.Count > 0)
                {
                    if (dsHoliday.Tables[0].Rows.Count > 0)
                    {
                        string strHolidaytype = Convert.ToString(dsHoliday.Tables[0].Rows[0]["Holiday_Type"]);
                        string strHolidayDescription = Convert.ToString(dsHoliday.Tables[0].Rows[0]["Holiday_Description"]);
                        string strFromdate = Convert.ToString(dsHoliday.Tables[0].Rows[0]["Holiday_FromDate"]);
                        string strTodate = Convert.ToString(dsHoliday.Tables[0].Rows[0]["Holiday_ToDate"]);
                        if (strHolidaytype == "Global Holiday")
                        {
                            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
                            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
                            gintTempWeekdayCount = objHoliday.checkHolidayTempWeekDay(strFromdate, strTodate);
                            if (gintTempWeekdayCount > 0)
                            {
                                base.Message(rm.GetString("Alert_CannotrestoreGlobalholidayonTemporaryWeekday", ci));
                                hdnSelectRow.Value = "";
                                return;
                            }
                            DateTime dtFromDate = DateTime.Parse(strFromdate);
                            DateTime dtToDate = DateTime.Parse(strTodate);
                            TimeSpan diff = dtToDate.Subtract(dtFromDate);
                            int day = diff.Days;
                            holidaycount = holidaycount + day;
                            if (holidaycount > GlobalMax)
                            {
                                base.Message(rm.GetString("Master_Holiday_Global_Reaches_Max_Nos", ci));
                                hdnSelectRow.Value = "";
                                return;
                            }
                        }
                        int count = objHoliday.checkHolidayRestore(Holiday_Id, strHolidaytype, strFromdate, strTodate, strHolidayDescription);
                        if (count > 0)
                        {
                            base.Message(rm.GetString("Message_Cannot_Restore", ci));
                            grv_Holiday.DataBind();
                            hdnSelectRow.Value = "";
                            return;
                        }
                        if (SelectedChkBoxIDs == "")
                        {
                            SelectedChkBoxIDs = ChkBoxIDs[i];
                        }
                        else
                        {
                            SelectedChkBoxIDs = SelectedChkBoxIDs + "," + ChkBoxIDs[i];
                        }
                    }
                }
            }
            objHoliday.DeleteHolidayDetails(SelectedChkBoxIDs, "Restore", auEmpId);
            base.MessageWithRedirect(rm.GetString("Alert_Holidayrestoredsuccessfully", ci), "TA_UI_Master_Holiday.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    #endregion
    #region UserDefinedFunctions
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderHolidays.Text = rm.GetString("Masters_Holidays_Holidays", ci);
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Delete.Text = rm.GetString("btn_Delete", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = rm.GetString("btn_Restore", ci);
        grv_Holiday.Columns[3].Caption = rm.GetString("Holidays_Date", ci);
        grv_Holiday.Columns[2].Caption = rm.GetString("Holidays_Desc", ci);
        grv_Holiday.Columns[4].Caption = rm.GetString("Holidays_Holiday_Type", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(16, 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.btn_Restore.Visible = false;
                this.grv_Holiday.Columns[0].Visible = true;
                this.grv_Holiday.Columns[8].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                this.grv_Holiday.Columns[0].Visible = false;
                this.grv_Holiday.Columns[8].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.grv_Holiday.Columns[7].Visible = true;
            }
            else
            {
                this.grv_Holiday.Columns[7].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    protected void grv_Holiday_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == 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_Holiday.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True" || DeletedRecord.Text == rm.GetString("Grid_True", ci))
                {
                    DeletedRecord.ForeColor = System.Drawing.Color.Red;
                }
            }
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_Holiday.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_Holiday.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");
            if (deleteChkBxItem != null)
            {
                if (gstrUserGroupName != "SuperAdmin")
                {
                    int Holiday_Id = Convert.ToInt32(e.GetValue("Holiday_Id"));
                    int Count = objHoliday.GetRestrictedHolidayCount(Holiday_Id);
                    if (Count > 0)
                    {
                        deleteChkBxItem.Disabled = true;
                        LnkbtnDelete.Enabled = false;
                        LnkbtnDelete.OnClientClick = null;
                    }
                    else
                    {
                        deleteChkBxItem.Disabled = false;
                        LnkbtnDelete.Enabled = true;
                    }
                }
            }
            if (DeletedRecord != null)
            {
                switch (DeletedRecord.Text)
                {
                    case "True":
                        DeletedRecord.Text = rm.GetString("Grid_True", ci);
                        if (hdnDeletedData.Value == string.Empty)
                            hdnDeletedData.Value = Convert.ToString(e.GetValue("Holiday_Id"));
                        else
                            if (hdnDeletedData.Value.Contains(Convert.ToString(e.GetValue("Holiday_Id"))) == false)
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + Convert.ToString(e.GetValue("Holiday_Id"));
                        break;
                    case "False":
                        DeletedRecord.Text = rm.GetString("Grid_False", ci);
                        break;
                    default:
                        break;
                }
            }
            if (gstrUserGroupName == "SuperAdmin")
            {
                Label lblHolidayType = (Label)grv_Holiday.FindRowCellTemplateControl(e.VisibleIndex, null, "lblHType1");
                if (lblHolidayType != null)
                    switch (lblHolidayType.Text)
                    {
                        case "Global Holiday":
                            lblHolidayType.Text = rm.GetString("Holiday_GlobalHoliday", ci);
                            break;
                        case "Restricted Holiday":
                            lblHolidayType.Text = rm.GetString("Holiday_RestrictedHoliday", ci);
                            break;
                        default:
                            break;
                    }
            }
        }
    }
    #endregion
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int Id = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_HolidayModify.aspx?Holiday_Id=" + Id);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int HID = Convert.ToInt32(btn.CommandName);
        int Count = objHoliday.GetRestrictedHolidayCount(Convert.ToInt32(HID));
        if (Count > 0)
        {
            hdnSelectRow.Value = "";
            base.Message(rm.GetString("Master_Holiday_Cannot_Delete", ci));
            return;
        }
        objHoliday.DeleteHolidayDetails(Convert.ToString(HID), "PermanentDelete", auEmpId);
        base.MessageWithRedirect(rm.GetString("Alert_Holidaydeletedsuccessfully", ci), "TA_UI_Master_Holiday.aspx");
    }
}
