using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                   Started Date  : 12-Aug-2007
* Module          : Temporary WeekDay Settings                  Last Modified : 14-Aug-2007
* Programmer      : Janakaraj.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
****************************************************************************************************************************/
public partial class Organization_Settings_Organization_Tempweekdaylist : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    LeaveEventData objLeave = new LeaveEventData();
    DataSet ds_Checkuser = new DataSet();
    TempWeekdayEvendata obj = new TempWeekdayEvendata();
    //Added by MAnjula.P on 09-Apr-2014
    string gstrDateFormat = string.Empty;
    string strStartDate = string.Empty;
    string strEndDate = string.Empty;
    cls_Common objClassCommon = new cls_Common();
    string gstrUserGroupName = string.Empty;
    int gintHolidayCount = 0;
    int UserId;
    string source_Language;
    #endregion
    #region Page Load Event
    protected override void Page_Load(object sender, EventArgs e)
    {
        this.ucOrganizationLevel.ehClick += new EventHandler(this.btnPageButton_Click);
        base.Page_Load(sender, e);
        grv_TempWeekDay.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            Session["strEndDate"] = null;
            Session["strStartDate"] = null;
            objClassCommon.menu("Others", "48");
        }
        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);
        UserId = Convert.ToInt32(Session["EmpAutoID"]);
        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 + "SetAlertCheckDate('" + rm.GetString("Alert_Validmonthdatefourdigit", ci) + "','" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("RestoreAlert", strSetAlert);
        UserRights();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName != "SuperAdmin")
        {
            grv_TempWeekDay.Columns[5].Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        btn_add.Focus();
    }
    #endregion
    #region User Defined Functions
    private void LoadStrings(CultureInfo ci)
    {
        lblFilter.Text = rm.GetString("lblFilter", ci);
        lbl_Header.Text = rm.GetString("OrganizationSettings_TemporaryWeekDay_TemporaryWeekDaySetting", 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_PermanentDelete", ci);
        grv_TempWeekDay.Columns[2].Caption = rm.GetString("Reports_Date", ci);
        grv_TempWeekDay.Columns[3].Caption = rm.GetString("OnDuty_OutDate", ci);
        grv_TempWeekDay.Columns[4].Caption = rm.GetString("TemporaryWeekDaySetting_ClosingTime", ci);
        grv_TempWeekDay.Columns[5].Caption = rm.GetString("email_Reason", ci);
        grv_TempWeekDay.Columns[6].Caption = rm.GetString("DeleteStatus", ci);
        btn_Search.Text = "&nbsp;&nbsp" + rm.GetString("btn_Search", ci);
        btn_Clear.Text = "&nbsp;&nbsp" + rm.GetString("btn_Clear", ci);
        lblStartDate.Text = rm.GetString("email_FromDate", ci);
        lblEndDate.Text = rm.GetString("email_ToDate", ci);
    }
    private string CheckDelete()
    {
        string ChkBoxIDs = "";
        try
        {
        }
        catch
        {
        }
        return ChkBoxIDs;
    }
    public void UserRights()
    {
        try
        {
            ds_Checkuser = objCheckUser.checkRights(48, Convert.ToInt32(Session["userGroup_Id"]), "");
            if (ds_Checkuser.Tables[0].Rows.Count > 0)
            {
                if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 0)
                {
                    Response.Redirect("OrganizationLandingPage.aspx");
                }
                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)
                {
                    //ToDo : This line change to karthikeyan.R
                    this.btn_Delete.Visible = true;
                    this.btn_PermanentDelete.Visible = false;
                    this.btn_Restore.Visible = false;
                    this.grv_TempWeekDay.Columns[1].Visible = true;
                }
                else
                {
                    this.btn_Delete.Visible = false;
                    this.btn_PermanentDelete.Visible = false;
                    this.btn_Restore.Visible = false;
                    this.grv_TempWeekDay.Columns[1].Visible = false;
                }                
            }
            else
            {
                Response.Redirect("OrganizationLandingPage.aspx");
            }
        }
        catch
        {
        }
    }
    #endregion
    #region Button Click Events
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty)
        {
            string[] ChkBoxIDs;
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            obj.TemporaryDeleteTempWeekday(hdnSelectRow.Value, UserId);
            string str = "alert('" + rm.GetString("Alert_TemporaryWeekdayDeletedSuccessfully", ci) + "');location.replace('Organization_Tempweekdaylist.aspx?');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        Response.Redirect("Organization_TemporaryWeekday.aspx");
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty)
        {
            string[] ChkBoxIDs;
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            obj.PermanentDeleteTempWeekday(hdnSelectRow.Value, UserId);
            string str = "alert('Temporary Weekday deleted successfully');location.replace('Organization_Tempweekdaylist.aspx?');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty)
        {
            string[] ChkBoxIDs;
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                gintHolidayCount = obj.CheckTempWeekDayHoliday_Insert(Convert.ToInt32(ChkBoxIDs[i]), "", "", "R", "");
                if (gintHolidayCount > 0)
                {
                    string strTemp = rm.GetString("TemporaryWeekDaySettingRestore_HolidayExists", ci);
                    string str = "alert('" + strTemp + "');";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                    hdnSelectRow.Value = string.Empty;
                    return;
                }
                int count = obj.CheckRestoreWeekdate(Convert.ToInt32(ChkBoxIDs[i]));
                if (count > 0)
                {
                    string strTemp = rm.GetString("Message_Cannot_Restore", ci);
                    string str = "alert('" + strTemp + "');";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                    hdnSelectRow.Value = string.Empty;
                    grv_TempWeekDay.DataBind();
                    return;
                }
                obj.RestoreTempWeekday(Convert.ToString(ChkBoxIDs[i]), UserId);
            }
            string strTempRestore = rm.GetString("Alert_TemporaryWeekdayRestoredSuccessfully", ci);
            string strTempRestorealert = "alert('" + strTempRestore + "');location.replace('Organization_Tempweekdaylist.aspx?');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", strTempRestorealert, true);
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            strStartDate = objClassCommon.DateConvertion(gstrDateFormat, txtStartDate.Text.Trim());
            strEndDate = objClassCommon.DateConvertion(gstrDateFormat, txtEndDate.Text.Trim());
            if (strEndDate == string.Empty)
                Session["strEndDate"] = null;
            else
                Session["strEndDate"] = strEndDate;
            if (strStartDate == string.Empty)
                Session["strStartDate"] = null;
            else
                Session["strStartDate"] = strStartDate;
            DataSet dsTEmpraryWeekday = new DataSet();
            dsTEmpraryWeekday = obj.SelectTempweekday(gstrUserGroupName, Convert.ToInt16(Session["parentLevelId"]), strStartDate, strEndDate);
            Session["WeeklyDayData"] = dsTEmpraryWeekday;
            grv_TempWeekDay.DataSourceID = ObjectDataSource1.ID;
        }
        catch (Exception ex)
        {
            base.Message(ex.Message);
        }
    }
    protected void btn_Clear_Click(object sender, EventArgs e)
    {
        try
        {
            Session["parentLevelId"] = 0;
            txtStartDate.Text = string.Empty;
            txtEndDate.Text = string.Empty;
            Session["strEndDate"] = null;
            Session["strStartDate"] = null;
            Response.Redirect("Organization_Tempweekdaylist.aspx");
        }
        catch (Exception ex)
        {
            base.Message(ex.Message);
        }
    }
    protected void btnPageButton_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "Loaddropdown();", true);
    }
    #endregion
    #region GridView events
    protected void grv_TempWeekDay_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            Label DeletedRecord = (Label)grv_TempWeekDay.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;
                }
            }
            Label lblDate = (Label)grv_TempWeekDay.FindRowCellTemplateControl(e.VisibleIndex, null, "lblTemporaryFromDate");
            Label lblTime = (Label)grv_TempWeekDay.FindRowCellTemplateControl(e.VisibleIndex, null, "lblclosingtime");
            string date = ((Label)grv_TempWeekDay.FindRowCellTemplateControl(e.VisibleIndex, null, "lblTemporaryFromDate")).Text;
            string time = ((Label)grv_TempWeekDay.FindRowCellTemplateControl(e.VisibleIndex, null, "lblclosingtime")).Text;
            //DateTime tempTime = Convert.ToDateTime(time);
            //lblTime.Text = tempTime.ToString(Convert.ToString(Session["MTimeFormat"]));
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_TempWeekDay.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            if (deleteChkBxItem != null)
            {
                int TempWeekday_Id = Convert.ToInt32(e.GetValue("tempWeekday_Id"));
                int Count = obj.GetTempWeeklyOffProcessedDays(TempWeekday_Id);
                if (Count > 0)
                {
                    deleteChkBxItem.Disabled = true;
                }
                else
                {
                    deleteChkBxItem.Disabled = false;
                }
            }
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = e.GetValue("tempWeekday_Id").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("tempWeekday_Id").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("tempWeekday_Id").ToString();
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    #endregion
}
