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_XGReportMovementsEntry : Base.BasePage
{
    Rpt_Common rptcommon = new Rpt_Common();
    cls_Common objCommon = new cls_Common();
    GetReports objGetReport = new GetReports();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    string strFromdate, gstrDateFormat, strTodate, strInTime, strOutTime;
    ResourceManager rm;
    CultureInfo ci;
    EmployeeEventData objEmployee = new EmployeeEventData();
    string strEmployeeId, strEmployeeName;
    int levelId;
    int intIndividualUser;
    string UserGroupName;
    string strSetAlert = "";
    DataSet dsMovementsEntry = new DataSet();
    protected override void Page_Load(object sender, EventArgs e)
    {

        // session checking
        base.Page_Load(sender, e);
        movementsentryASPxGridView1.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (!IsPostBack)
        {
            base.CheckPages(234);
            objCommon.menu("Shift", "234");
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            Hidden2.Value = Convert.ToString(Session["ITimeFormat"]);
            //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());
        }
        //Set UserRights
        UserRights();
        strSetAlert = strSetAlert + "<script language='javascript'>";
        //strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_Holiday_SelectHolidayType",ci) + "','" + rm.GetString("Alert_Fromdate",ci) + "','" + rm.GetString("Alert_ToDate",ci) + "','" + rm.GetString("Alert_Holiday_FillDescription",ci) + "','" + rm.GetString("Alert_EnterToDate",ci) + "','" + rm.GetString("Alert_ConfirmSave",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) + "','" + rm.GetString("Alert_EnterToTime", ci) + "','" + rm.GetString("Alert_EnterFromTime", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        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) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        //Check whether the employee is Individual User
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        FillDesignation();
        BindingEmployeeCategoryandType();
        fillShift();
        //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);
        GetTimes();
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        if (ViewState["AllSwipes"] != null)
        {
            movementsentryASPxGridView1.DataSource = ViewState["AllSwipes"] as DataTable;
            btnExportToResponse.Enabled = true;
            btnExportToExcel.Enabled = true;
        }
        else
        {
            btnExportToResponse.Enabled = false;
            btnExportToExcel.Enabled = false;
        }
    }
    private void UserRights()
    {
        DataSet ds_Checkuser = new DataSet();
        ds_Checkuser = objCheckUser.checkRights(234, 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");
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        //lblEmployeeDetailsReport.Text = rm.GetString("Reports_TimePeriod", ci);
        lblEmployeeDetailsReport.Text = rm.GetString("Report_XtraGrid_AllSwipes", 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);
        lblFromTime.Text = rm.GetString("AllSwipes_FromTime", ci);
        lblToTime.Text = rm.GetString("AllSwipes_ToTime", 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);
    }
    protected void btnExportToResponse_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = ViewState["AllSwipes"] 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 (rbtnPortrait.Checked == true)
        {
            ASPxGridViewExporter1.Landscape = false;
        }
        else
        {
            ASPxGridViewExporter1.Landscape = true;
        }
        ASPxGridViewExporter1.WritePdfToResponse();
    }
    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = ViewState["AllSwipes"] 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 (rbtnPortrait.Checked == true)
        {
            ASPxGridViewExporter1.Landscape = false;
        }
        else
        {
            ASPxGridViewExporter1.Landscape = true;
        }
        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>
    /// 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 GetTimes()
    {
        if (txtFromTime.Text != string.Empty)
        {
            strFromdate = txtFromDate.Text;
            strInTime = txtFromTime.Text;
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        }
        else
        {
            strFromdate = txtFromDate.Text;
            strInTime = "12:00:00 AM";
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        }
        if (txtToTime.Text != string.Empty)
        {
            strTodate = txtToDate.Text;
            strOutTime = txtToTime.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
        }
        else
        {
            strTodate = txtToDate.Text;
            strOutTime = "11:59:59 PM";
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
        }
    }
    private void GridBinding()
    {
        if (txtFromTime.Text != "" && txtToTime.Text != "")
        {
            if (Convert.ToDateTime(txtFromTime.Text) > Convert.ToDateTime(txtToTime.Text))
            {
                base.Message(rm.GetString("Alert_FromTimeGreater", ci));
                return;
            }
        }
        string dtFrom = strFromdate;
        string dtTo = strTodate;
        string dtIntime = strInTime;
        string dtOutTime = strOutTime;
        int shft = 0;
        string Level = string.Empty;
        string employeeName = string.Empty;
        string empName = string.Empty;
        string empId = string.Empty;
        if (intIndividualUser >= 1)
        {
            empName = string.Empty;
            Level = string.Empty;
            empId = Convert.ToString(Session["EmployeeCodeId"]);
        }
        else
        {
            empName = txtEmployeeName.Text.ToString();
            empId = txtEmployeeId.Text.Trim().ToString();
            employeeName = this.txtEmployeeName.Text.Replace("'", "''");
            employeeName = employeeName.Replace(' ', '%');
            Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
        }
        int EmployeeCategory = Convert.ToInt32(ddlEmpCatagory.SelectedValue.ToString());
        int EmployeeType = Convert.ToInt32(ddlEmpType.SelectedValue.ToString());
        UserGroupName = Convert.ToString(Session["UserGroupName"]);
        string GradeLevel = Convert.ToString(Session["GradeLevel"]);
        string strSwipeCard = txtSwipeCard.Text.ToString().Trim().Replace("'", "''");
        strEmployeeId = txtEmployeeId.Text.Trim();
        strEmployeeName = txtEmployeeName.Text.Trim();
        int ParentLevelid;
        if (Session["parentLevelId"] != null)
        {
            ParentLevelid = Convert.ToInt16(Session["parentLevelId"].ToString());
        }
        else
        {
            ParentLevelid = 0;
        }
        string Designation = ddlDesignation.SelectedValue;
        string Gender = ddlGender.SelectedValue;
        dsMovementsEntry = objGetReport.SelectMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, EmployeeCategory, EmployeeType, UserGroupName, GradeLevel, strSwipeCard, ParentLevelid, Convert.ToInt32(Designation), Gender, dtIntime, dtOutTime, 0, "0", 0, 0);
        // dsMovementsEntry = objGetReport.SelectMovementsEntryXtraGrid(strFromdate, strTodate, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
        btnExportToResponse.Enabled = false;
        btnExportToExcel.Enabled = false;
        movementsentryASPxGridView1.DataSource = null;
        movementsentryASPxGridView1.DataBind();
        if (dsMovementsEntry != null && dsMovementsEntry.Tables.Count > 0)
        {
            if (dsMovementsEntry.Tables[0].Rows.Count > 0)
            {
                movementsentryASPxGridView1.DataSource = dsMovementsEntry.Tables[0];
                ViewState["AllSwipes"] = dsMovementsEntry.Tables[0];
                movementsentryASPxGridView1.DataBind();
                btnExportToResponse.Enabled = true;
                btnExportToExcel.Enabled = true;
            }
        }
    }
    protected void butBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_XtraGridMain.aspx");
    }
    /// <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>
    /// Bindings the employee category and type.
    /// </summary>
    private void BindingEmployeeCategoryandType()
    {
        // employee category binding
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
            foreach (ListItem item in ddlEmpCatagory.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        // employee type binding
        DataSet dsType = new DataSet();
        dsType = objEmployee.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
            foreach (ListItem item in ddlEmpType.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    /// <summary>
    /// To fill shift in the drop down
    /// </summary>
    public void fillShift()
    {
        ShiftEvenData getShift = new ShiftEvenData();
        DataSet ds = new DataSet();
        string strusergroup = string.Empty;
        //ds = getShift.SelectShift(strusergroup, Convert.ToString(Session["LevelRights"]));
        ds = getShift.SelectShift(Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["LevelRights"]), 0);
        foreach (DataRow drRow in ds.Tables[0].Rows)
        {
            switch (drRow["Shift_Name"].ToString())
            {
                case "No Shift":
                    drRow["Shift_Name"] = rm.GetString("Caption_NoShift", ci);
                    break;
            }
        }
        if (ds.Tables[0].Rows.Count > 0)
        {
            this.ddlShift.DataSource = ds;
            this.ddlShift.DataTextField = "Shift_Name";
            this.ddlShift.DataValueField = "Shift_Id";
            this.ddlShift.DataBind();
        }
        ListItem lstItem = new ListItem();
        lstItem.Value = "0";
        //lstItem.Text = "All";
        lstItem.Text = rm.GetString("ExportData_All", ci);
        ddlShift.Items.Insert(0, lstItem);
        foreach (ListItem item in ddlShift.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    protected void movementsentryASPxGridView1_CustomColumnSort(object sender, DevExpress.Web.CustomColumnSortEventArgs e)
    {
    }
}
