using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
using System.Web.UI;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;

public partial class Reports_GetGraphicalReportData : Base.BasePage
{
    #region Variable Declaration
    ResourceManager rm;
    CultureInfo ci;
    cls_Common objCommon = new cls_Common();
    ReportDocument rptDoc = new ReportDocument();
    GetReports rptEmp = new GetReports();
    HeaderInformation hdrTable = new HeaderInformation();
    Rpt_Common rptcommon = new Rpt_Common();
    EmailConfiguration objEmailConfiguration = new EmailConfiguration();
    int shft = 0, absentType = 0, EmployeeCategory = 0, EmployeeType = 0, NoOfDays, Designation, AllSwipesReportType, RawDataDashboard, ProcessType = 0;
    string rptFileName, Level = string.Empty, employeeName = string.Empty, empName = string.Empty, empId = string.Empty;
    int ParentLevelid = 0;
    string dtFrom, dtTo, UserGroupName, GradeLevel, ReportName, Normal, Status, LeaveType, MinWorkHours, EmpStatus, TableDescription, Operation, MonthClosureId, YearSettingsId;
    string strSwipOpt = "", strSwipeCard = "", strMaxDeviationHrs = string.Empty, strPunchType = string.Empty;
    string Gender = string.Empty, ReportType = string.Empty, TotalDays = string.Empty, OverTimeReportType = string.Empty, LeaveReportType = string.Empty, SanctionType = string.Empty, SummaryOrDetail = string.Empty; string strReportType = string.Empty; string dtIntime, dtOutTime;
    string strEmpAutoId = "0";
    int chkunplan = 0;
    DataSet dsEmp = new DataSet();
    int MonthlydataLoading = 0, ContAbsentDays = 0;
    string punchTypeValue = "All";
    Boolean blnCheckDataExits = true;
    DataSet dsLevelDetails = new DataSet();
    string Returnval = "", gstrDateFormat = "";
    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        string source_Language;
        GC.Collect();
        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;
        Level = Request.QueryString["Level"];
        employeeName = Request.QueryString["EmpName"];
        employeeName = employeeName.Replace("%", " ");
        employeeName = employeeName.Replace("''", "'");
        empId = Request.QueryString["empid"];
        dtFrom = Request.QueryString["dtFrom"];
        dtTo = Request.QueryString["dtTo"];
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        if (dtFrom != string.Empty)
            dtFrom = objCommon.DateConvertion(gstrDateFormat, dtFrom);
        if (dtTo != string.Empty)
            dtTo = objCommon.DateConvertion(gstrDateFormat, dtTo);
        EmployeeCategory = Convert.ToInt32(Request.QueryString["EmpCategory"]);
        EmployeeType = Convert.ToInt32(Request.QueryString["EmpType"]);
        ContAbsentDays = Convert.ToInt32(Request.QueryString["continueAbsentDays"]);
        string str = Convert.ToString(Request.QueryString["ParentLevelId"]);
        if (str == "" || str == string.Empty)
        {
            ParentLevelid = 0;
        }
        else
        {
            ParentLevelid = Convert.ToInt16(str);
        }
        UserGroupName = Request.QueryString["UserGroupName"];
        GradeLevel = Request.QueryString["GradeLevel"];
        ReportName = Request.QueryString["ReportName"];
        TotalDays = Request.QueryString["TotalDays"];
        strMaxDeviationHrs = Convert.ToString(Request.QueryString["MaxDeviationHrs"]);
        strPunchType = Convert.ToString(Request.QueryString["PunchType"]);
        if (Request.QueryString["absentType"] != null)
        {
            absentType = Convert.ToInt16(Request.QueryString["absentType"]);
        }
        if (Session["ReportsBasedOn"] != null)
        {
            if (Convert.ToString(Session["ReportsBasedOn"]) == "False")
            {
                strEmpAutoId = "0";
            }
            else
            {
                strEmpAutoId = Convert.ToString(Session["EmpAutoID"]);
            }
        }
        if (Request.QueryString["ProcessType"] != null)
        {
            ProcessType = Convert.ToInt16(Request.QueryString["ProcessType"]);
        }
        if (Request.QueryString["Planned"] != null)
        {
            chkunplan = Convert.ToInt32(Request.QueryString["Planned"]);
        }
        if (Request.QueryString["SanctionType"] != null)
        {
            SanctionType = Convert.ToString(Request.QueryString["SanctionType"]);
        }
        if (Request.QueryString["ReportType"] != null)
        {
            ReportType = Convert.ToString(Request.QueryString["ReportType"]);
        }
        if (Request.QueryString["AllSwipesReportType"] != null)
        {
            AllSwipesReportType = Convert.ToInt32(Request.QueryString["AllSwipesReportType"]);
        }
        if (Request.QueryString["OverTimeReportType"] != null)
        {
            OverTimeReportType = Convert.ToString(Request.QueryString["OverTimeReportType"]);
        }
        if (Request.QueryString["LeaveReportType"] != null)
        {
            LeaveReportType = Convert.ToString(Request.QueryString["LeaveReportType"]);
        }
        if (Request.QueryString["OutTime"] != null)
        {
            dtOutTime = Request.QueryString["OutTime"];
        }
        if (Request.QueryString["InTime"] != null)
        {
            dtIntime = Request.QueryString["InTime"];
        }
        Gender = Request.QueryString["Gender"];
        Designation = Convert.ToInt32(Request.QueryString["Designation"]);
        if (Request.QueryString["SwipOpt"] != null)
        {
            strSwipOpt = Convert.ToString(Request.QueryString["SwipOpt"]);
        }
        if (Request.QueryString["SwipeCard"] != null)
        {
            strSwipeCard = Convert.ToString(Request.QueryString["SwipeCard"]);
        }
        if (Request.QueryString["MonthlydataLoadingVer"] != null)
        {
            MonthlydataLoading = Convert.ToInt32(Request.QueryString["MonthlydataLoadingVer"]);
        }
        if (Request.QueryString["punchTypeValue"] != null)
        {
            punchTypeValue = Convert.ToString(Request.QueryString["punchTypeValue"]);
        }
        if (Request.QueryString["RawDataDashboard"] != null)
        {
            RawDataDashboard = Convert.ToInt16(Request.QueryString["RawDataDashboard"]);
        }
        Level = rptcommon.getNodes(Convert.ToInt16(ParentLevelid), Convert.ToString(Session["LevelRights"]));
        dsLevelDetails = rptEmp.SelectLevelDetails();
        switch (ReportName)
        {
            case "EmployeeShiftAllocation":
                if (!IsPostBack)
                {
                    LoadShiftNotAllocatedReport();
                }
                if (blnCheckDataExits == true)
                {

                }
                return;
            case "Absentees":
                if (Request.QueryString["ScheduleId"] == null)
                {
                    shft = Convert.ToInt32(Request.QueryString["shft"]);
                    //Normal = Convert.ToString(Request.QueryString["Normal"]);
                }
                if (!IsPostBack)
                {
                    LoadAbsentReport();
                    Response.Write(Returnval);
                    Response.End();
                }
                return;
            case "LateEntry":
            case "Report_Late_Entry":
                if (Request.QueryString["shft"] != null)
                {
                    shft = Convert.ToInt32(Request.QueryString["shft"]);
                }
                if (!IsPostBack)
                {
                    LoadLateEntryReport();
                    Response.Write(Returnval);
                    Response.End();
                }
                return;
            case "EarlyExit":
            case "Report_Early_Exit":
                if (Request.QueryString["shft"] != null)
                {
                    shft = Convert.ToInt32(Request.QueryString["shft"]);
                }
                if (!IsPostBack)
                {
                    LoadEarlyExitReport();
                    Response.Write(Returnval);
                    Response.End();
                }
                return;
            case "EarlyEntry":
            case "Report_EarltEntry":
                if (Request.QueryString["shft"] != null)
                {
                    shft = Convert.ToInt32(Request.QueryString["shft"]);
                }
                if (!IsPostBack)
                {
                    LoadEarlyEntryReport();
                    Response.Write(Returnval);
                    Response.End();
                }
                return;
            case "Discrepancy":
            case "Report_Discrepancy":
                if (Request.QueryString["shft"] != null)
                {
                    shft = Convert.ToInt32(Request.QueryString["shft"]);
                }
                if (!IsPostBack)
                {
                    LoadDiscrepancyReport();
                    Response.Write(Returnval);
                    Response.End();
                }
                return;
            case "SummaryReport":
            case "Report_SummaryReport":
                if (Request.QueryString["Shft"] != null)
                    shft = Convert.ToInt32(Request.QueryString["Shft"]);
                if (!IsPostBack)
                {
                    LoadSummaryReport();
                    Response.Write(Returnval);
                    Response.End();
                }
                return;
            case "Punctuality":
            case "Report_Punctuality":
                if (Request.QueryString["shft"] != null)
                    shft = Convert.ToInt32(Request.QueryString["shft"]);
                if (!IsPostBack)
                {
                    LoadPunctualityReport();
                    Response.Write(Returnval);
                    Response.End();
                }
                return;
        }

    }
    public void LoadShiftNotAllocatedReport()
    {
        DataSet dsHeader = new DataSet();
        DataTable dt = new DataTable();
        if (Convert.ToInt16(ParentLevelid) != 0)
        {
            dt = rptEmp.getHeader(Convert.ToInt16(ParentLevelid));
            Session["Ses_rptHeader_ShiftNotAllocated"] = dt;
        }
        else
        {
            Session["Ses_rptHeader_ShiftNotAllocated"] = dt;
        }
        DataSet dsShiftNotAllocated = new DataSet();

        dsShiftNotAllocated = rptEmp.SelectNotAllocationReport(dtFrom, dtTo, employeeName, Level, empId, EmployeeCategory, EmployeeType, Convert.ToString(Session["UserGroupName"]), GradeLevel, strSwipeCard, Convert.ToInt16(ParentLevelid), Convert.ToInt32(Designation), Gender, Convert.ToInt64(strEmpAutoId));
        if (dsShiftNotAllocated.Tables[0].Rows.Count <= 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
    }
    public void LoadAbsentReport()
    {
        dsEmp = rptEmp.SelectAbsent(dtFrom, dtTo, empId, shft, employeeName, Level, EmployeeCategory, EmployeeType, Convert.ToString(Session["UserGroupName"]), GradeLevel, strSwipeCard, Convert.ToInt16(ParentLevelid), Convert.ToInt32(Designation), Gender, absentType, Convert.ToInt64(strEmpAutoId), 2, ContAbsentDays);
        //Check the Empty table
        if (dsEmp.Tables["Table"] == null || dsEmp.Tables["Table"].Rows.Count == 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
        else
        {
            Returnval = dsEmp.Tables[0].Rows[0][0].ToString();

            if (Returnval == string.Empty)
            {
                blnCheckDataExits = false;
                string strMsg;
                strMsg = "<script language='javascript'>";
                strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
                strMsg = strMsg + "</script>";
                Page.RegisterStartupScript("DisplayMessage", strMsg);
                return;
            }
            else
            {
                Returnval = Returnval.Remove(Returnval.Length - 1);
                Returnval = Returnval + "~" + Convert.ToString(dsEmp.Tables[1].Rows[0][0].ToString());
            }

        }

    }

    public void LoadLateEntryReport()
    {
        DataSet dsLateIns = new DataSet();
        string MinExceptHours = "";
        if (Request.QueryString["MinExceptHours"] != null)
        {
            MinExceptHours = Convert.ToString(Request.QueryString["MinExceptHours"]);
        }
        dsLateIns = rptEmp.SelectLateInstanceReport(empId, Level, dtFrom, dtTo, employeeName, shft, EmployeeCategory, EmployeeType, Convert.ToString(Session["UserGroupName"]), GradeLevel, strSwipeCard, Convert.ToInt16(ParentLevelid), Convert.ToInt32(Designation), Gender, Convert.ToInt64(strEmpAutoId), 0, MinExceptHours);
        //Check the Empty table
        if (dsLateIns.Tables["Table"] == null || dsLateIns.Tables["Table"].Rows.Count == 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
        else
        {
            Returnval = "";
            Returnval = dsLateIns.Tables[0].Rows[0][0].ToString();
        }
    }

    public void LoadEarlyExitReport()
    {
        DataSet dsEarlyExit = new DataSet();
        string MinExceptHours = "";
        if (Request.QueryString["MinExceptHours"] != null)
        {
            MinExceptHours = Convert.ToString(Request.QueryString["MinExceptHours"]);
        }
        dsEarlyExit = rptEmp.SelectEarlyOutReportNew(dtFrom, dtTo, empId, shft, employeeName, Level, EmployeeCategory, EmployeeType, Convert.ToString(Session["UserGroupName"]), GradeLevel, strSwipeCard, Convert.ToInt16(ParentLevelid), Convert.ToInt32(Designation), Gender, 0, MinExceptHours, Convert.ToInt32(Session["EmpAutoID"]));
        //Check the Empty table
        if (dsEarlyExit.Tables["Table"] == null || dsEarlyExit.Tables["Table"].Rows.Count == 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
        else
        {
            Returnval = "";
            Returnval = dsEarlyExit.Tables[0].Rows[0][0].ToString();
        }

    }
    public void LoadEarlyEntryReport()
    {
        DataSet dsEarlyEntry = new DataSet();
        string MinExceptHours = "";
        if (Request.QueryString["MinExceptHours"] != null)
        {
            MinExceptHours = Convert.ToString(Request.QueryString["MinExceptHours"]);
        }
        dsEarlyEntry = rptEmp.SelectEarlyEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, EmployeeCategory, EmployeeType, Convert.ToString(Session["UserGroupName"]), GradeLevel, strSwipeCard, Convert.ToInt16(ParentLevelid), Convert.ToInt32(Designation), Gender, 0, MinExceptHours, Convert.ToInt32(Session["EmpAutoID"]));
        //Check the Empty table
        if (dsEarlyEntry.Tables["Table"] == null || dsEarlyEntry.Tables["Table"].Rows.Count == 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
        else
        {
            Returnval = "";
            Returnval = dsEarlyEntry.Tables[0].Rows[0][0].ToString();
        }

    }
    public void LoadDiscrepancyReport()
    {
        DataSet dsEarlyEntry = new DataSet();
        dsEarlyEntry = rptEmp.SelectDiscrepancy(dtFrom, dtTo, empId, shft, employeeName, Level, EmployeeCategory, EmployeeType, Convert.ToString(Session["UserGroupName"]), GradeLevel, strSwipeCard, Convert.ToInt16(ParentLevelid), Convert.ToInt32(Designation), Gender, 0, 0);
        //Check the Empty table
        if (dsEarlyEntry.Tables["Table"] == null || dsEarlyEntry.Tables["Table"].Rows.Count == 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
        else
        {
            Returnval = "";
            Returnval = dsEarlyEntry.Tables[0].Rows[0][0].ToString();
        }

    }

    public void LoadSummaryReport()
    {
        DataSet dsSummaryReport = new DataSet();
        dsSummaryReport = rptEmp.SelectSummaryReport(dtFrom, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(ParentLevelid), shft, 0, Convert.ToInt32(Session["EmpAutoID"]));
        //Check the Empty table
        if (dsSummaryReport.Tables["Table"] == null || dsSummaryReport.Tables["Table"].Rows.Count == 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
        else
        {
            Returnval = "";
            Returnval = dsSummaryReport.Tables[0].Rows[0][0].ToString();
        }
    }

    public void LoadPunctualityReport()
    {
        DataSet dsSummaryReport = new DataSet();
        dsSummaryReport = rptEmp.SelectPunctuality(dtFrom, dtTo, empId, shft, employeeName, Level, EmployeeCategory, EmployeeType, Convert.ToString(Session["UserGroupName"]), GradeLevel, strSwipeCard, Convert.ToInt16(ParentLevelid), Convert.ToInt32(Designation), Gender, Convert.ToInt64(strEmpAutoId), 0);
        //Check the Empty table
        if (dsSummaryReport.Tables["Table"] == null || dsSummaryReport.Tables["Table"].Rows.Count == 0)
        {
            blnCheckDataExits = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("DisplayMessage", strMsg);
            return;
        }
        else
        {
            Returnval = "";
            Returnval = dsSummaryReport.Tables[0].Rows[0][0].ToString();
        }
    }

}