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_EmployeeSchedule : Base.BasePage
{
    HeaderInformation hdrTable = new HeaderInformation();
    GetReports rptEmp = new GetReports();
    ResourceManager rm;
    cls_Common objCommon = new cls_Common();
    CultureInfo ci;
    string strFromdate, gstrDateFormat, strTodate;

    protected override void Page_Load(object sender, EventArgs e)
    {

        // session checking
        base.Page_Load(sender, e);        

        //get the culture info to set the language

    

       
            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);
        

        if (!IsPostBack)
        {
            txtFromDate.Text = (Convert.ToDateTime(Session["SearchFromDate"].ToString())).ToString(Session["MDateFormat"].ToString());
            //txtFromDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
            txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
        }

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


    /// <summary>
    /// Load the Punchality report
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_View_Click(object sender, EventArgs e)
    {
        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"] = "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 cId, rId, bId, dId, sId, eId;




                string dtFrom = "", dtTo = "";

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

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

                string empName = "";
                empName = txtEmployeeName.Text.ToString();

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

                //this is new report

                int shft = 1;
                string employeeName = this.txtEmployeeName.Text.Replace("'", "''");
                dsEmpSchedule = rptEmp.SelectEmpSchedule(dtFrom, dtTo, "", shft, employeeName, Convert.ToInt16(Session["parentLevelId"]));
                //Report Header
                int orgID = Convert.ToInt16(Session["parentLevelId"]);
                Session["parentLevelId"] = null;
                if (orgID == 0)
                {
                    orgID = 1;
                }
                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();
                DataTable dt = new DataTable();
                if (Convert.ToInt16(Session["parentLevelId"]) != 0)
                {
                    dt = 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"] = dt;
                    //}
                }
                else
                {
                    Session["reportHeader"] = dt;
                }

                //End of Report Header
                Session["parentLevelId"] = null;

                DataTable dtEmpSchedule = new DataTable();

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

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

                    return;
                }

                dtEmpSchedule = dsEmpSchedule.Tables["Table"].Copy();

                //Add the link column
                DataColumn dcLink = new DataColumn("Link", Type.GetType("System.Int32"));
                dtEmpSchedule.Columns.Add(dcLink);

                for (int i = 0; i < dtEmpSchedule.Rows.Count; i++)
                {
                    dtEmpSchedule.Rows[i]["Link"] = 1;
                }

                //WRITE XML SCHEMA TO DISK - THIS IS ONLY REQUIRED DURING DEVELOPMENT TO BUILD THE REPORT
                // dtEmpSchedule.WriteXmlSchema(@"E:\employeeSchedule.xml");
                 //dtHdr.WriteXmlSchema(@"E:\employeeScheduleHdr.xml");
                //return;

                //Set the session variable that holds the report table and report header table
                Session["Hdr"] = dtHdr;
                Session["Source"] = dtEmpSchedule;
                Session["Path"] = Server.MapPath("employeeSchedule.rpt");

                //Redirect the page to the report viewer
                string strScript = "";
                string strUrl = "TA_UI_Report_ReportViewer.aspx";
                strScript = "<script language='javascript'>";
                strScript = strScript + "window.open('" + strUrl + "');";
                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);
        reqToDate.ErrorMessage = rm.GetString("Alert_PleaseselectToDate", ci);
        reqFromDate.ErrorMessage = rm.GetString("Alert_PleaseselectFromDate", ci);

        

    }
}
