using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Reports_TA_UI_XGReportAttendance : Base.BasePage
{
    cls_Common objCommon = new cls_Common();
    GetReports objGetReport = new GetReports();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    string strFromdate, gstrDateFormat, strTodate;
    ResourceManager rm;
    CultureInfo ci;
    string source_Language;
    Rpt_Common rptcommon = new Rpt_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    int intIndividualUser;
    string strSetAlert = "";
    DataSet dsAttendance = new DataSet();
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        attendanceASPxGridView.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!IsPostBack)
        {
            base.CheckPages(233);
            objCommon.menu("Shift", "233");
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            //txtFromDate.Text = (Convert.ToDateTime(Session["SearchFromDate"].ToString())).ToString(Session["MDateFormat"].ToString());
            //txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
            txtFromDate.Text = (Convert.ToDateTime(objCommon.CurrentMonthStartDate())).ToString(Session["MDateFormat"].ToString());
            txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
            FillDesignation();
        }
        //Set UserRights
        UserRights();
        //Convert date time format
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strFromdate = txtFromDate.Text;
        strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        strTodate = txtToDate.Text;
        strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
        GridBinding();
        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);
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EnterDate", ci) + "','" + rm.GetString("Alert_EnterToDate", 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) + "','" + rm.GetString("Alert_EnterToDate", ci) + "','" + rm.GetString("Alert_EnterDate", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertCheckDate('" + 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("SetAlertMessages", strSetAlert);
    }
    private void LoadStrings(CultureInfo ci)
    {
        //lblEmployeeDetailsReport.Text = rm.GetString("Reports_TimePeriod", ci);
        lblEmployeeDetailsReport.Text = rm.GetString("Caption_EmployeeAttendanceDetails", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lblGender.Text = rm.GetString("EmployeeMaster_Gender", ci);
        ddlGender.Items[0].Text = rm.GetString("Caption_Both", ci);
        ddlGender.Items[1].Text = rm.GetString("rbtn_EmployeeMaster_Male", ci);
        ddlGender.Items[2].Text = rm.GetString("rbtn_EmployeeMaster_Female", ci);
        ddlDesignation.Items[0].Text = rm.GetString("ExportData_All", ci);
        lblTimePeriod.Text = rm.GetString("Reports_TimePeriod", ci);
        lblFrom.Text = rm.GetString("email_FromDate", ci);
        lblTo.Text = rm.GetString("email_ToDate", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        //Commented by MAnjula.P on 22-Jul-2013 because of error in index.
        // attendanceASPxGridView.Columns[0].Caption = "Name_AR";
        lblExportPrinting.Text = rm.GetString("Caption_ExportPrintingMode", ci);
        rbtnPortrait.Text = rm.GetString("Caption_Portrait", ci);
        rbtnLandscape.Text = rm.GetString("Caption_Landscape", ci);
        btnCustWindow.Text = "&nbsp;&nbsp;" + rm.GetString("Caption_ShowCustomizationWindow", ci);
        btnExportToResponse.Text = "&nbsp;&nbsp;" + rm.GetString("Export_To_PDF", ci);
        btnExportToExcel.Text = "&nbsp;&nbsp;" + rm.GetString("Caption_ExportToXLS", ci);
        butBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblDateFilter.Text = rm.GetString("lbl_DateFilter", ci);
        rbCustom.Text = rm.GetString("rb_Custom", ci);
        rbpreviousMonth.Text = rm.GetString("rb_PreviousMonth", ci);
        rbpreviousweek.Text = rm.GetString("rb_PreviousWeek", ci);
        rbThisMonth.Text = rm.GetString("rb_ThisMonth", ci);
        rbThisweek.Text = rm.GetString("rb_ThisWeek", ci);
    }
    private void UserRights()
    {
        DataSet ds_Checkuser = new DataSet();
        ds_Checkuser = objCheckUser.checkRights(233, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser != null && ds_Checkuser.Tables.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows.Count == 0)
            {
                Response.Redirect("ReportsLandingPage.aspx");
            }
            else
            {
                //   
            }
        }
        else
        {
            Response.Redirect("ReportsLandingPage.aspx");
        }
    }
    protected void btnExportToResponse_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = Session["Attendance"] as DataTable;
        if (dt.Columns.Count <= 12)
        {
            ASPxGridViewExporter1.Styles.Header.Font.Size = 5;
            ASPxGridViewExporter1.Styles.Cell.Font.Size = 5;
        }
        if (dt.Columns.Count > 12)
        {
            ASPxGridViewExporter1.Styles.Header.Font.Size = 4;
            ASPxGridViewExporter1.Styles.Cell.Font.Size = 4;
        }
        if (rbtnLandscape.Checked == true)
        {
            ASPxGridViewExporter1.Landscape = true;
        }
        else
        {
            ASPxGridViewExporter1.Landscape = false;
        }
        ASPxGridViewExporter1.WritePdfToResponse();
    }
    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = Session["Attendance"] as DataTable;
        if (dt.Columns.Count <= 12)
        {
            ASPxGridViewExporter1.Styles.Header.Font.Size = 5;
            ASPxGridViewExporter1.Styles.Cell.Font.Size = 5;
        }
        if (dt.Columns.Count > 12)
        {
            ASPxGridViewExporter1.Styles.Header.Font.Size = 3;
            ASPxGridViewExporter1.Styles.Cell.Font.Size = 3;
        }
        if (rbtnLandscape.Checked == true)
        {
            ASPxGridViewExporter1.Landscape = true;
        }
        else
        {
            ASPxGridViewExporter1.Landscape = false;
        }
        ASPxGridViewExporter1.WriteXlsToResponse();
    }
    /// <summary>
    /// Validate the FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateFromDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            //strCurrentDate = objCommon.DateConvertion(gstrDateFormat,DateTime.Today.Date.ToString());
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            //strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            //DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date.ToString("MM/dd/yyyy")); ;
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
            {
                // base.Message("Select valid From Date");  
                base.Message(rm.GetString("Alert_SelectvalidFromDate", ci));
                e.IsValid = false;
            }
        }
        catch
        {
            //
        }
    }
    /// <summary>
    /// Fills the designation in the drop down
    /// </summary>
    private void FillDesignation()
    {
        DataSet ds_designation = new DataSet();
        ds_designation = objEmployee.select_CommonDesignation();
        if (ds_designation != null)
        {
            if (ds_designation.Tables[0].Rows.Count > 0)
            {
                ddlDesignation.DataSource = ds_designation;
                ddlDesignation.DataTextField = "TitleName";
                ddlDesignation.DataValueField = "TitleId";
                ddlDesignation.DataBind();
            }
            ds_designation.Dispose();
            foreach (ListItem item in ddlDesignation.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    /// <summary>
    /// Validate the ToDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateToDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            //strCurrentDate = objCommon.DateConvertion(gstrDateFormat, DateTime.Today.Date.ToString());
            strTodate = txtToDate.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            //DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date.ToString("MM/dd/yyyy"));
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
            {
                // base.Message("Select valid To Date");
                base.Message(rm.GetString("Alert_SelectvalidToDate", ci));
                e.IsValid = false;
            }
        }
        catch
        {
            //
        }
    }
    /// <summary>
    /// Compare ToDate with FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            strTodate = txtToDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtToDate = DateTime.Parse(strTodate);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
            {
                // base.Message("Select valid date compare with Fromdate");
                base.Message(rm.GetString("Msg_SelectvaliddatecomparewithFromdate", ci));
                e.IsValid = false;
            }
        }
        catch
        {
            //
        }
    }
    protected void btn_View_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            GridBinding();
        }
    }
    private void GridBinding()
    {
        string strSwipeCard = "";
        string dtFrom = "", dtTo = "";
        dtFrom = (Convert.ToDateTime(Convert.ToString(Session["SearchFromDate"]))).ToString(Convert.ToString(Session["MDateFormat"]));
        dtTo = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Convert.ToString(Session["MDateFormat"]));
        if (txtFromDate.Text != "")
            dtFrom = strFromdate;
        if (txtToDate.Text != "")
            dtTo = strTodate;
        string empName = "", empId = "";
        string employeeName = string.Empty;
        string Level = string.Empty;
        if (intIndividualUser >= 1)
        {
            empName = string.Empty;
            Level = string.Empty;
            empId = Convert.ToString(Session["EmployeeCodeId"]);
        }
        else
        {
            empName = txtEmployeeName.Text.ToString();
            empId = txtEmpId.Text.ToString().Trim();
            employeeName = this.txtEmployeeName.Text.Replace("'", "''");
            Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
        }
        String Gender = ddlGender.SelectedValue;
        dsAttendance = objGetReport.SelectDetailAttend_Old(dtFrom, dtTo, empId, employeeName, Level, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), strSwipeCard, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), Gender, 0, 0);
        //dsAttendance = objGetReport.SelectAttendanceXtraGrid(strFromdate, strTodate, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
        btnExportToResponse.Enabled = false;
        btnExportToExcel.Enabled = false;
        attendanceASPxGridView.DataSource = null;
        attendanceASPxGridView.DataBind();
        if (dsAttendance != null && dsAttendance.Tables.Count > 0)
        {
            if (dsAttendance.Tables[0].Rows.Count > 0)
            {
                Session["Attendance"] = dsAttendance.Tables[0];
                attendanceASPxGridView.DataSource = dsAttendance;
                attendanceASPxGridView.DataBind();
                btnExportToResponse.Enabled = true;
                btnExportToExcel.Enabled = true;
            }
        }
    }
    protected void butBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_XtraGridMain.aspx");
    }

}
