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;

public partial class Reports_TA_UI_Report_TotalHoursList : System.Web.UI.Page
{
    HeaderInformation hdrTable = new HeaderInformation();
    GetReports rptEmp = new GetReports();
    Rpt_Common rptcommon = new Rpt_Common();
    ResourceManager rm;
    cls_Common objCommon = new cls_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    CultureInfo ci;
    DataSet ds_Checkuser = new DataSet();

    string strFromdate, gstrDateFormat, strTodate;
    int intIndividualUser = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        //get the culture info to set the language

       

        if (Session["Session_ActiveLanguage"] == null)
        {
            Response.Redirect("../index.aspx");
        }
        else
        {
            string source_Language;
            source_Language = Session["Session_ActiveLanguage"].ToString();
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            LoadStrings(ci);
        }

        //Set UserRights
        UserRights();

        if (!IsPostBack)
        {

            Hidden1.Value = Session["IDateFormat"].ToString();
            // Bindings the employee category and type.
            BindingEmployeeCategoryandType();
            //Added and changed from date with session format : To avoiding datetime conversion error by Manjula.P on 12-Apr-2014
            txtFromDate.Text = (Convert.ToDateTime(Convert.ToString(Session["SearchFromDate"])).ToString(Session["MDateFormat"].ToString()));
            //Session["SearchFromDate"].ToString();
            txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
            //Added by Manjula.P on 06-May-2014
            FillDesignation();
            ddlDesignation.PreRender += LB_PreRender;
        }

        //Check whether the employee is Individual User

        intIndividualUser = objCheckUser.CheckIndividualUser(Session["UserGroupName"].ToString());

        if (intIndividualUser >= 1)
        {
            PlaceHolderEmployees.Controls.Clear();
            divEmployeeDetails.Visible = false;
        }

        //Convert date time to MM/dd/YYYY format
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strFromdate = txtFromDate.Text;
        strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        strTodate = txtToDate.Text;
        strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
    }
    /// <summary>
    /// Fills the designation in the drop down  :Added by Manjula.P on 06-May-2014
    /// </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>
    /// To apply tooltip for the designation in the drop down : Fills the designation in the drop down  :Added by Manjula.P on 06-May-2014
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void LB_PreRender(object sender, System.EventArgs e)
    {
        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();
        }

        // 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();
        }
    }

    /// <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 dtFromDate = DateTime.Parse(strFromdate);

            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
                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 dtFromDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
                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
                e.IsValid = false;
        }
        catch
        {
            //
        }
    }

    protected void btn_View_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid == true)
            {
                int employeeCategory = 0, employeeType = 0;

                //Create the datatable ,that holds the header information
                DataTable dtHdr = new DataTable();
                dtHdr = hdrTable.CreateHeader();
                dtHdr.TableName = "Header";

                //Add the Header information
                DataRow drHdr = dtHdr.NewRow();
                drHdr["Link"] = 1;
                drHdr["Hdr1"] = "Neuland Laboratories Limited";
                drHdr["Hdr2"] = "NEULAND";
                drHdr["Address1"] = "HeadOffice";
                drHdr["Address2"] = "Hyderabad";
                drHdr["ReportName"] = "Early Exit Report";

                if (txtFromDate.Text != "")
                {
                    DateTime tmpFrom = DateTime.Parse(strFromdate);
                    drHdr["FromDate"] = tmpFrom.ToString("dd-MMM-yyyy");
                }
                else
                    drHdr["FromDate"] = "";
                if (txtToDate.Text != "")
                {
                    DateTime tmpTo = DateTime.Parse(strTodate);
                    drHdr["ToDate"] = tmpTo.ToString("dd-MMM-yyyy");
                }
                else
                    drHdr["ToDate"] = "";

                dtHdr.Rows.Add(drHdr);


                string dtFrom = "", dtTo = "";

                if (txtFromDate.Text != "")
                    dtFrom = strFromdate;

                if (txtToDate.Text != "")
                    dtTo = strTodate;

                // 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;
                }




                //Get the Report source from the Server and load this into the report table
                DataSet dsTotHrsList = new DataSet();

                //this is new report

                //int shft = Convert.ToInt16(this.ddlShift.SelectedValue);


                string empName = "", empId = "";
                empName = txtEmployeeName.Text.ToString().Trim();
                empId = txtEmpId.Text.ToString().Trim();
                string employeeName = this.txtEmployeeName.Text.Trim().Replace("'", "''");

                //added now

                string Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));


                if (intIndividualUser >= 1)
                {
                    empName = string.Empty;
                    employeeName = string.Empty;
                    Level = string.Empty;
                    empId = Convert.ToString(Session["EmployeeCodeId"]);

                }

                //end of added now
                //Added and commented the strAutoEmId variable as 0 for passing this as int
                string strEmpAutoId = "0";
                //string strEmpAutoId = string.Empty;
                if (Session["ReportsBasedOn"] != null)
                {
                    if (Session["ReportsBasedOn"].ToString() == "False")
                    {
                        strEmpAutoId = "0";
                    }
                    else
                    {
                        strEmpAutoId = Convert.ToString(Session["EmpAutoID"]);
                    }
                }

                string MinWorkHours = txtMinWorkHours.Text.Trim();// == string.Empty ? "00:00" : txtMinWorkHours.Text.Trim();
                //string BreakLateHrs = txtMinLunchHours.Text.Trim() == string.Empty ? "00:00" : txtMinLunchHours.Text.Trim();                
                string strLateType = rbtnTotalLate.Checked ? "0" : "1";

                dsTotHrsList = rptEmp.SelectTotalHrsList_Angler(MinWorkHours, dtFrom, dtTo, empId, employeeName, Level, employeeCategory, employeeType, Session["UserGroupName"].ToString(), Convert.ToString(Session["GradeLevel"]), Convert.ToInt64(strEmpAutoId), strLateType, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), ddlGender.SelectedValue);

                //Check the Empty table
                if (dsTotHrsList.Tables["Table"] == null || dsTotHrsList.Tables["Table"].Rows.Count == 0)
                {
                    string strMsg;
                    strMsg = "<script language='javascript'>";
                    strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
                    strMsg = strMsg + "</script>";

                    Page.RegisterStartupScript("Display Message", strMsg);

                    return;
                }

                string strReportType = string.Empty;

                if (rbtnSummaryReport.Checked == true)
                {
                    strReportType = "SummaryReport";
                }
                else
                {
                    strReportType = "DetailedReport";
                }

                //Redirect the page to the report viewer
                string strReportName = "TotalLateHoursList";
                string strUrl = "TA_UI_Report_ReportViewer.aspx";
                //Cryptographys.Cryptography.Encrypt("TotalLateHoursList", "AnglerReports");
                string strScript = "";
                strScript = strScript + "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
                strScript = strScript + "<meta http-equiv='X-UA-Compatible' content='IE=document.documentMode;' />";
                strScript = strScript + "<script language='javascript'>";
                //Changed this reports by passing arguments without encrypted parameters to avoid input string was error by Manjula.P on 12-Apr-2014
                strScript = strScript + "window.open('TA_UI_Report_ReportViewer.aspx?ReportName=" + strReportName + "&dtFrom=" + dtFrom + "&dtTo=" + dtTo + "&empid=" + empId + "&EmpName=" + employeeName + "&ParentLevelId=" + Convert.ToInt16(Session["parentLevelId"]) + "&EmpCategory=" + Convert.ToString(employeeCategory) + "&EmpType=" + Convert.ToString(employeeType) + "&UserGroupName=" + Convert.ToString(Session["UserGroupName"]) + "&GradeLevel=" + Convert.ToString(Session["GradeLevel"]) + "&MinWorkHours=" + MinWorkHours + "&ReportType=" + strReportType + "&BreakLate=" + strLateType + "&Designation=" + Convert.ToInt32(ddlDesignation.SelectedValue) + "&Gender=" + ddlGender.SelectedValue + "','','left=10px, top=60px, width=1000px, height=600px,scrollbars=yes, status=no, resizable=yes');";
                //strScript = strScript + "window.open('TA_UI_Report_ReportViewer.aspx?ReportName=" + strReportName + "&dtFrom=" + Cryptographys.Cryptography.Encrypt(dtFrom, "AnglerReports") + "&dtTo=" + Cryptographys.Cryptography.Encrypt(dtTo, "AnglerReports") + "&empid=" + Cryptographys.Cryptography.Encrypt(empId, "AnglerReports") + "&EmpName=" + Cryptographys.Cryptography.Encrypt(employeeName, "AnglerReports") + "&ParentLevelId=" + Cryptographys.Cryptography.Encrypt(Convert.ToInt16(Session["parentLevelId"]).ToString(), "AnglerReports") + "&EmpCategory=" + Cryptographys.Cryptography.Encrypt(Convert.ToString(employeeCategory), "AnglerReports") + "&EmpType=" + Cryptographys.Cryptography.Encrypt(Convert.ToString(employeeType), "AnglerReports") + "&UserGroupName=" + Cryptographys.Cryptography.Encrypt(Session["UserGroupName"].ToString(), "AnglerReports") + "&GradeLevel=" + Cryptographys.Cryptography.Encrypt(Convert.ToString(Session["GradeLevel"]), "AnglerReports") + "&MinWorkHours=" + Cryptographys.Cryptography.Encrypt(MinWorkHours, "AnglerReports") + "&ReportType=" + Cryptographys.Cryptography.Encrypt(strReportType, "AnglerReports") + "&BreakLate=" + Cryptographys.Cryptography.Encrypt(strLateType, "AnglerReports") + "','','left=10px, top=60px, width=1000px, height=600px,scrollbars=yes, status=no, resizable=yes');";
                strScript = strScript + "</script>";
                Response.Write(strScript);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
            //
        }
    }

    //Getting Information from resource file and display it in label

    private void LoadStrings(CultureInfo ci)
    {
        btn_View.Text = rm.GetString("btn_View", ci);
        //Added by Manjula.P on 06-May-2014 for filtering option for designation and gender
        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);
    }

    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(74, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");

        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"]) == 1)
            {
                btn_View.Visible = true;
            }
            else
            {
                btn_View.Visible = false;
            }
        }
        else
        {
            Response.Redirect("ReportsLandingPage.aspx");
        }
    }
}
