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

        base.Page_Load(sender, e);
        grv_EmpDetails.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!IsPostBack)
        {
            base.CheckPages(232);
            objCommon.menu("Shift", "232");
            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());
        }
        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;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string 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);        
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        //grv_EmpDetails.Styles.AlternatingRow.Enabled= true;  
        //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);
        btnExportToResponse.Enabled = false;
        btnExportToExcel.Enabled = false;
        if (ViewState["EmployeeSearch"] != null)
        {
            grv_EmpDetails.DataSource = (DataTable)ViewState["EmployeeSearch"];
            grv_EmpDetails.DataBind();
            btnExportToResponse.Enabled = true;
            btnExportToExcel.Enabled = true;
        }
    }
    private void UserRights()
    {
        DataSet ds_Checkuser = new DataSet();
        ds_Checkuser = objCheckUser.checkRights(232, 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");
        }
    }
    /// <summary>
    /// Getting Information from resource file and display it in label
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        lblEmpSwipeCard.Text = rm.GetString("TerminalDataType_Card_No", ci);
        lblEmployeeDetailsReport.Text = rm.GetString("Report_Employee_Details", ci);
        lblStatus.Text = rm.GetString("EmployeeMaster_EmployeeStatus", ci);
        ddlStatus.Items[0].Text = rm.GetString("ExportData_All", ci);
        ddlStatus.Items[1].Text = rm.GetString("rbtn_EmployeeMaster_Active", ci);
        ddlStatus.Items[2].Text = rm.GetString("rbtn_EmployeeMaster_InActive", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        ddlSwipeOpt.Items[0].Text = rm.GetString("Select", ci);
        ddlSwipeOpt.Items[1].Text = rm.GetString("ExportData_All", ci);
        ddlSwipeOpt.Items[2].Text = rm.GetString("Report_Exists", ci);
        ddlSwipeOpt.Items[3].Text = rm.GetString("Report_NotExists", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        lblTimePeriod.Text = rm.GetString("Reports_TimePeriod", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        lblFrom.Text = rm.GetString("email_FromDate", ci);
        lblTo.Text = rm.GetString("email_ToDate", 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);
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        grv_EmpDetails.Columns[0].Caption = rm.GetString("email_EmployeeId", ci);
        grv_EmpDetails.Columns[1].Caption = rm.GetString("email_EmployeeName", ci);
        grv_EmpDetails.Columns[2].Caption = rm.GetString("email_Status", ci);
        grv_EmpDetails.Columns[3].Caption = rm.GetString("EmployeeMaster_DateOfBirth", ci);
        grv_EmpDetails.Columns[4].Caption = rm.GetString("EmployeeMaster_Nationality", ci);
        grv_EmpDetails.Columns[5].Caption = rm.GetString("Master_Designation", ci);
        grv_EmpDetails.Columns[6].Caption = rm.GetString("UserGroups_OrganizationLevel", ci);
        grv_EmpDetails.Columns[7].Caption = rm.GetString("EmployeeMaster_DateOfJoining", ci);
        grv_EmpDetails.Columns[8].Caption = rm.GetString("EmployeeMaster_Gender", ci);
        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["EmployeeSearch"] as DataTable;
        if (grv_EmpDetails.Columns.Count <= 12)
        {
            ASPxGridViewExporter1.Styles.Header.Font.Size = 5;
            ASPxGridViewExporter1.Styles.Cell.Font.Size = 5;
        }
        if (grv_EmpDetails.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["EmployeeSearch"] as DataTable;
        if (grv_EmpDetails.Columns.Count <= 12)
        {
            ASPxGridViewExporter1.Styles.Header.Font.Size = 5;
            ASPxGridViewExporter1.Styles.Cell.Font.Size = 5;
        }
        if (grv_EmpDetails.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();
    }
    protected void butBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_XtraGridMain.aspx");
    }


    /// <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 GridBinding()
    {
        int shft = 0;
        string Level = string.Empty;
        int employeeCategory;
        int employeeType;
        if (intIndividualUser >= 1)
        {
            shft = 0;
            strEmployeeName = string.Empty;
            Level = string.Empty;
            strEmployeeId = Convert.ToString(Session["EmployeeCodeId"]);
        }
        else
        {
            strEmployeeName = txtEmployeeName.Text.ToString();
            strEmployeeId = txtEmployeeId.Text.Trim().ToString();
            //shft = Convert.ToInt16(this.ddlShift.SelectedValue);
            strEmployeeName = this.txtEmployeeName.Text.Replace("'", "''");
            strEmployeeName = strEmployeeName.Replace(' ', '%');
            //if (Convert.ToInt16(Session["parentLevelId"]) != 0)
            //{
            //Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]));
            Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
        }
        // set employee category and type
        if (Convert.ToInt32(ddlEmpCatagory.SelectedValue) != 0)
        {
            employeeCategory = Convert.ToInt32(ddlEmpCatagory.SelectedValue);
        }
        else
        {
            employeeCategory = 0;
        }
        if (Convert.ToInt32(ddlEmpType.SelectedValue) != 0)
        {
            employeeType = Convert.ToInt32(ddlEmpType.SelectedValue);
        }
        else
        {
            employeeType = 0;
        }
        int ParentLevelId = 0;
        if (Session["parentLevelId"] != null)
        {
            ParentLevelId = Convert.ToInt16(Session["parentLevelId"].ToString());
        }
        string UserGroupName = Convert.ToString(Session["UserGroupName"]);
        string GradeLevel = Convert.ToString(Session["GradeLevel"]);
        string EmployeeStatus = ddlStatus.SelectedValue.ToString();
        string strSwipOpt = "";
        if (ddlSwipeOpt.SelectedIndex != 0)
        {
            strSwipOpt = ddlSwipeOpt.SelectedValue;
        }
        string strSwipeCard = "";
        strSwipeCard = txtSwipeCard.Text.ToString().Trim().Replace("'", "''");
        int Designation = Convert.ToInt32(ddlDesignation.SelectedValue);
        string Gender = ddlGender.SelectedValue;
        dsEmpDetails = objGetReport.SelectEmployee(strFromdate, strTodate, strEmployeeId, shft, strEmployeeName, Level, employeeCategory, employeeType, EmployeeStatus, UserGroupName, GradeLevel, strSwipOpt, strSwipeCard, ParentLevelId, Designation, Gender, Convert.ToInt32(Session["EmpAutoID"]));
        //dsAttendance = objGetReport.SelectEmployeeDetailXtraGrid( strFromdate,strTodate, Convert.ToString(Session["UserGroupName"]),Convert.ToString(Session["GradeLevel"]),Convert.ToString(Session["LevelRights"]),Convert.ToInt16(Session["parentLevelId"]));
        btnExportToResponse.Enabled = false;
        btnExportToExcel.Enabled = false;
        grv_EmpDetails.DataSource = null;
        ViewState["EmployeeSearch"] = null;
        grv_EmpDetails.DataBind();
        if (dsEmpDetails != null && dsEmpDetails.Tables.Count > 0)
        {
            if (dsEmpDetails.Tables[0].Rows.Count > 0)
            {
                ViewState["EmployeeSearch"] = dsEmpDetails.Tables[0];
                grv_EmpDetails.DataSource = dsEmpDetails.Tables[0];
                grv_EmpDetails.DataBind();
                btnExportToResponse.Enabled = true;
                btnExportToExcel.Enabled = true;
            }
        }
    }
}
