using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Text;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Reports_TA_UI_Report_PayRoll : Base.BasePage
{
    #region Variable Declaration
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    HeaderInformation hdrTable = new HeaderInformation();
    GetReports rptEmp = new GetReports();
    Rpt_Common rptcommon = new Rpt_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    ResourceManager rm;
    CultureInfo ci;
    int intIndividualUser = 0;
    cls_Common objClassCommon = new cls_Common();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {

        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language        
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objClassCommon.menu("ExportReport", "71");
        }
        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_SelectDuration", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        //Set UserRights
        UserRights();
        if (!Page.IsPostBack)
        {
            // Bindings the employee category and type.
            BindingEmployeeCategoryandType();
            filldurationDetails();
            FillDesignation();
            ddlDesignation.PreRender += LB_PreRender;
            if (Session["parentLevelId"] != null || Convert.ToInt16(Session["parentLevelId"]) != 0)
            {
                Session["parentLevelId"] = null;
            }
        }
        //Check whether the employee is Individual User
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        if (intIndividualUser >= 1)
        {
            PlaceHolderPayRoll.Controls.Clear();
            divEmployeeDetails.Visible = false;
            lblEmpSwipe.Visible = false;
            txtSwipeCard.Visible = false;
        }
    }
    #endregion
    #region User Defined function
    /// <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);
            }
        }
    }
    private void filldurationDetails()
    {
        DataSet ds_DurationDetails = new DataSet();
        ds_DurationDetails = rptEmp.SelectPayrollDurtaion();
        ddlDuration.DataSource = ds_DurationDetails;
        ddlDuration.DataValueField = "MonthClosure_Id";
        ddlDuration.DataTextField = "Duration";
        ddlDuration.DataBind();
        ddlDuration.Items.Insert(0, "-Select-");
        foreach (ListItem item in ddlDuration.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    /// <summary>
    /// Getting Information from resource file and display it in label
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        lblFilter.Text = rm.GetString("lblFilter", ci);
        btn_View.Text =  "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        lblPayrollReport.Text = rm.GetString("Report_Payroll_Header", 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);
        lblDuration.Text = rm.GetString("Report_Duration", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        ddlEmpCatagory.Items[0].Text = rm.GetString("Select", ci);
        ddlEmpType.Items[0].Text = rm.GetString("Select", 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);
        lbl_Mandatory.Text = rm.GetString("IndicatesMandatoryFields", ci);
    }
    /// <summary>
    /// To check rights for the logged in user
    /// </summary>
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(71, 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");
        }
    }
    /// <summary>
    /// To apply tooltip for the designation in the drop down
    /// </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);
        }
    }
    #endregion
    #region Button Event
    /// <summary>
    /// Validate the FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    //public void ValidateFromDate(object s, ServerValidateEventArgs e)
    //{
    //    try
    //    {
    //        string strFromDate = txtFromDate.Text;
    //        DateTime dtCurrent = DateTime.Now;
    //        DateTime dtFromDate = DateTime.Parse(strFromDate);
    //        TimeSpan diff = dtFromDate.Subtract(dtCurrent);
    //        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
    //    {
    //        DateTime dtToDate = DateTime.Parse(txtToDate.Text);
    //        DateTime dtCurrent = DateTime.Now;
    //        TimeSpan diff = dtToDate.Subtract(dtCurrent);
    //        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
    //    {
    //        DateTime dtToDate = DateTime.Parse(txtToDate.Text);
    //        DateTime dtFromDate = DateTime.Parse(txtFromDate.Text);
    //        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)
    {
        string MonthClosureId;
        int employeeCategory = 0, employeeType = 0;
        if (Convert.ToString(ddlDuration.SelectedValue) == "-Select-")
        {
            MonthClosureId = "";
        }
        else
        {
            MonthClosureId = Convert.ToString(ddlDuration.SelectedValue);
        }
        try
        {
            if (Page.IsValid == true)
            {
                //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"] = "Total Days Report";
                // 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 shft = 0;
                string Level = string.Empty;
                string employeeName = string.Empty;
                string empName = string.Empty;
                string empId = string.Empty;
                if (intIndividualUser >= 1)
                {
                    empName = string.Empty;
                    shft = 0;
                    Level = string.Empty;
                    empId = Convert.ToString(Session["EmployeeCodeId"]);
                }
                else
                {
                    empName = txtEmployeeName.Text.ToString();
                    empId = txtEmployeeId.Text.Trim().ToString();
                    //shft = Convert.ToInt16(this.ddlShift.SelectedValue);
                    employeeName = this.txtEmployeeName.Text.Replace("'", "''");
                    employeeName = employeeName.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"]));
                    //}
                }
                string strSwipeCard = "";
                strSwipeCard = txtSwipeCard.Text.ToString().Trim().Replace("'", "''");
                //Get the Report source from the Server and load this into the report table
                //DataSet dsPayRoll = new DataSet();
                //dsPayRoll = rptEmp.SelectPayrollReport(Level, employeeName, MonthClosureId, empId, employeeCategory, employeeType, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), strSwipeCard);
                ////Report Header
                //int orgID = Convert.ToInt16(Session["parentLevelId"]);
                //Session["parentLevelId"] = null;
                //if (orgID == 0)
                //{
                //    //orgID = 1;
                //    DataSet dsOrgid = new DataSet();
                //    dsOrgid = rptEmp.getMinLevel();
                //    if (dsOrgid.Tables[0].Rows.Count > 0)
                //    {
                //        if (dsOrgid.Tables[0].Rows[0]["minID"] != DBNull.Value)
                //        {
                //            orgID = Convert.ToInt16(dsOrgid.Tables[0].Rows[0]["minID"]);
                //        }
                //    }
                //}
                //int lid = 0;
                //org_levelsEventData getOrg = new org_levelsEventData();
                //getOrg.GetOrgParentLevels_FromID(orgID);
                //DataSet ds_getOrg = new DataSet();
                //ds_getOrg = getOrg.GetOrgParentLevels();
                //if (ds_getOrg.Tables[0].Rows.Count > 0)
                //{
                //    lid = Convert.ToInt16(ds_getOrg.Tables[0].Rows[0]["levelID"]);
                //}
                //DataSet dsHeader = new DataSet();
                //dsHeader = rptEmp.getHeader(lid);
                //if (dsHeader.Tables[0].Rows.Count > 0)
                //{
                //    DataTable dtRptHeader = new DataTable();
                //    dtRptHeader = dsHeader.Tables[0].Copy();
                //    //dtRptHeader.WriteXmlSchema(@"E:\reportHeader.xml");
                //    Session["reportHeader"] = dtRptHeader;
                //}
                ////End of Report Header
                //Session["parentLevelId"] = null;
                //dsEarlyOut = rptEmp.SelectEarlyOutReport(eId, cId, rId, bId, dId, sId, dtFrom, dtTo, empName);
                //DataTable dtPayRoll = new DataTable();
                ////Check the Empty table 
                //if (dsPayRoll.Tables["Table"] == null || dsPayRoll.Tables["Table"].Rows.Count == 0)
                //{
                //    string strMsg;
                //    strMsg = "<script language='javascript'>";
                //    strMsg = strMsg + "alert('There is no record ');";
                //    strMsg = strMsg + "</script>";
                //    Page.RegisterStartupScript("Display Message", strMsg);
                //    return;
                //}
                // dtPayRoll = dsPayRoll.Tables["Table"].Copy();
                // //Add the link column
                // DataColumn dcLink = new DataColumn("Link", Type.GetType("System.Int32"));
                // dtPayRoll.Columns.Add(dcLink);
                // for (int i = 0; i < dtPayRoll.Rows.Count; i++)
                // {
                //     dtPayRoll.Rows[i]["Link"] = 1;
                // }
                // //WRITE XML SCHEMA TO DISK - THIS IS ONLY REQUIRED DURING DEVELOPMENT TO BUILD THE REPORT
                // //dtPayRoll.WriteXmlSchema(@"E:\Payroll.xml");
                // //dtHdr.WriteXmlSchema(@"E:\PayrollHdr.xml");
                //// return;
                // //Set the session variable that holds the report table and report header table
                // Session["Hdr"] = dtHdr;
                // Session["Source"] = dtPayRoll;                
                // Session["Path"] = Server.MapPath(rm.GetString("PayRollrpt",ci));
                //Redirect the page to the report viewer
                StringBuilder strScript = new StringBuilder();
                //string strUrl = "TA_UI_Report_ReportViewer.aspx";
                strScript.Append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
                strScript.Append("<meta http-equiv='X-UA-Compatible' content='IE=edge' />");
                strScript.Append("<script language='javascript'>");
                strScript.Append("window.open('TA_UI_Report_ReportViewer.aspx?ReportName=MonthClosure&dtFrom=" + "" + "&dtTo=" + "" + "&empid=" + empId + "&shft=0&EmpName=" + employeeName + "&Level=" + Level + "&EmpCategory=" + employeeCategory + "&EmpType=" + employeeType + "&ParentLevelId=" + Convert.ToInt16(Session["parentLevelId"]).ToString() + "&UserGroupName=" + Convert.ToString(Session["UserGroupName"]) + "&GradeLevel=" + Convert.ToString(Session["GradeLevel"]) + "&MonthClosureId=" + MonthClosureId + "&SwipeCard=" + strSwipeCard + "&Designation=" + Convert.ToInt32(ddlDesignation.SelectedValue) + "&Gender=" + ddlGender.SelectedValue + "','','left=10px, top=60px, width=1000px, height=600px, scrollbars=yes, status=no, resizable=yes');");
                strScript.Append("</script>");
                Response.Write(strScript);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }



#endregion
}
