using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Threading;
using System.Resources;
public partial class Reports_TA_UI_Report_WeeklyWorkHours_Angler : 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();
    cls_Common objCommon = new cls_Common();
    ResourceManager rm;
    CultureInfo ci;
    string dtFrom = "", dtTo = "", EmployeeId = "", gstrDateFormat, strFromdate, strTodate;
    int intIndividualUser = 0;
    string gStrLevel = string.Empty, ParenetLevelid = string.Empty;
    #endregion
    #region Page Load
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            objCommon.menu("Shift", "160");
            Hidden1.Value = Session["IDateFormat"].ToString();
            base.CheckPages(0);
        }
        //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;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        //Set UserRights
        UserRights();
        if (!IsPostBack)
        {
            // Bindings the employee category and type.
            BindingEmployeeCategoryandType();
            string strDayName = Convert.ToString(DateTime.Now.Date.DayOfWeek).ToLower();
            double iFromDate = strDayName.Contains("monday") ? 0 : strDayName.Contains("tuesday") ? -1 : strDayName.Contains("wednesday") ? -2 : strDayName.Contains("thursday") ? -3 : strDayName.Contains("friday") ? -4 : -5;
            txtFromDate.Text = System.DateTime.Now.Date.AddDays(iFromDate).ToString(Convert.ToString(Session["MDateFormat"]));
            txtToDate.Text = System.DateTime.Now.Date.AddDays(iFromDate + 5).ToString(Convert.ToString(Session["MDateFormat"]));
            fillShift();
        }
        //Check whether the employee is Individual User
        intIndividualUser = objCheckUser.CheckIndividualUser(Session["UserGroupName"].ToString());
        if (intIndividualUser >= 1)
        {
            PlaceHolderLateEntry.Controls.Clear();
            divEmployeeDetails.Visible = false;
            lblEmpSwipe.Visible = false;
            txtSwipeCard.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);
    }
    #endregion
    #region Button Event
    /// <summary>
    /// Load Late Entry report
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_View_Click(object sender, EventArgs e)
    {
        int EmpCategory = 0, EmpType = 0;
        string Level = string.Empty, EmpName = string.Empty, EmpId = string.Empty, SwipeCard = string.Empty;
        try
        {
            if (Page.IsValid == true)
            {
                // set employee category and type
                if (Convert.ToInt32(ddlEmpCatagory.SelectedValue) != 0)
                    EmpCategory = Convert.ToInt32(ddlEmpCatagory.SelectedValue);
                if (Convert.ToInt32(ddlEmpType.SelectedValue) != 0)
                    EmpType = Convert.ToInt32(ddlEmpType.SelectedValue);
                if (txtFromDate.Text != "")
                    dtFrom = strFromdate;
                if (txtToDate.Text != "")
                    dtTo = strTodate;
                //Get the Report source from the Server and load this into the report table\
                DataSet dsWeeklyHrs = new DataSet();
                //shft = Convert.ToInt16(this.ddlShift.SelectedValue);
                if (intIndividualUser >= 1)
                {
                    EmpId = Convert.ToString(Session["EmployeeCodeId"]);
                    EmployeeId = Convert.ToString(Session["EmployeeCodeId"]);
                }
                else
                {
                    EmpId = txtEmployeeId.Text.Trim().ToString();
                    EmployeeId = txtEmployeeId.Text.Trim().ToString();
                    EmpName = txtEmployeeName.Text;
                    SwipeCard = txtSwipeCard.Text.Trim().ToString().Replace("'", "''");
                    Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
                    gStrLevel = Level;
                }
                int shft = 0;
                shft = Convert.ToInt16(this.ddlShift.SelectedValue);
                Session["LateInsLevel"] = Level;
                string strEmpAutoId = string.Empty;
                if (Session["ReportsBasedOn"] != null)
                {
                    if (Session["ReportsBasedOn"].ToString() == "False")
                    {
                        strEmpAutoId = "0";
                    }
                    else
                    {
                        strEmpAutoId = Session["EmpAutoID"].ToString();
                    }
                }
                string strReportType = rbtnSummaryReport.Checked ? "SummaryReport" : "DetailedReport";
                //Redirect the page to the report viewer
                //string strReportName = Cryptographys.Cryptography.Encrypt("WeeklyWorkHours", "AnglerReports");
                //string strEmpCategory = Cryptographys.Cryptography.Encrypt(EmpCategory.ToString(), "AnglerReports");
                //string strEmpType = Cryptographys.Cryptography.Encrypt(EmpType.ToString(), "AnglerReports");
                string strReportName = "WeeklyWorkHours_Angler";
                string strEmpCategory = EmpCategory.ToString();
                string strEmpType = EmpType.ToString();
                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'>";
                //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(EmpName, "AnglerReports") + "&Level=" + Cryptographys.Cryptography.Encrypt(Level, "AnglerReports") + "&EmpCategory=" + strEmpCategory + "&EmpType=" + strEmpType + "&ParentLevelId=" + Cryptographys.Cryptography.Encrypt(Convert.ToInt16(Session["parentLevelId"]).ToString(), "AnglerReports") + "&SwipeCard=" + Cryptographys.Cryptography.Encrypt(SwipeCard, "AnglerReports") + "&ReportType=" + Cryptographys.Cryptography.Encrypt(strReportType, "AnglerReports") + "','','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=" + dtFrom + "&dtTo=" + dtTo + "&empid=" + EmpId + "&EmpName=" + EmpName + "&Level=" + Level + "&EmpCategory=" + strEmpCategory + "&EmpType=" + strEmpType + "&ParentLevelId=" + Convert.ToInt16(Session["parentLevelId"]) + "&SwipeCard=" + SwipeCard + "&shft=" + shft + "&ReportType=" + strReportType + "','','left=10px, top=60px, width=1000px, height=600px, scrollbars=yes, status=no, resizable=yes');";
                //     strScript = strScript + "window.open('TA_UI_Report_ReportViewer.aspx?ReportName=EmployeeShiftAllocation&dtFrom=" + dtFrom + "&dtTo=" + dtTo + "&empid=" + empId + "&shft=" + shft + "&EmpName=" + employeeName + "&Level=" + Level + "&EmpCategory=" + Convert.ToInt32(ddlEmpCatagory.SelectedValue) + "&EmpType=" + Convert.ToInt32(ddlEmpType.SelectedValue) + "&ParentLevelId=" + Convert.ToInt16(Session["parentLevelId"]).ToString() + "&UserGroupName=" + Session["UserGroupName"].ToString() + "&GradeLevel=" + Convert.ToString(Session["GradeLevel"]) + "&SwipeCard=" + txtSwipeCard.Text.ToString().Trim().Replace("'", "''") + "&Designation=" + Convert.ToInt32(ddlDesignation.SelectedValue) + "&Gender=" + ddlGender.SelectedValue + "','','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.ToString());
        }
    }
    #endregion
    #region User Define Function
    /// <summary>
    /// Validate the FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateFromDate(object s, ServerValidateEventArgs e)
    {
        e.IsValid = false;
        try
        {
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            e.IsValid = (day <= 0) && (dtFromDate.ToLongDateString().Split(',')[0].Equals("Monday"));
            if (!e.IsValid)
                base.Message((day > 0) ? "Enter Valid From Date" : "Select Monday in From Date");
        }
        catch
        {
            base.Message("Enter Valid From Date");
        }
    }
    /// <summary>
    /// Validate the ToDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateToDate(object s, ServerValidateEventArgs e)
    {
        e.IsValid = false;
        try
        {
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtToDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtToDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            //e.IsValid = (day <= 0) && (dtToDate.ToLongDateString().Split(',')[0].Equals("Saturday"));
            e.IsValid = (dtToDate.ToLongDateString().Split(',')[0].Equals("Saturday"));
            if (!e.IsValid)
                base.Message((day > 0) ? "Enter Valid To Date" : "Select Saturday in To Date");
        }
        catch
        {
            base.Message("Enter Valid To Date");
        }
    }
    /// <summary>
    /// Compare ToDate with FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        e.IsValid = false;
        try
        {
            DateTime dtToDate = DateTime.Parse(strTodate);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            e.IsValid = (day == 5);
            if (!e.IsValid)
                base.Message("Select saturday in the same week");
        }
        catch
        {
            base.Message("Select saturday in the same week");
        }
    }
    //Getting Information from resource file and display it in label
    /// <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();
        }
    }
    public void fillShift()
    {
        ShiftEvenData getShift = new ShiftEvenData();
        DataSet ds = new DataSet();
        string strusergroup = string.Empty;
        //ds = getShift.SelectShift(strusergroup, Convert.ToString(Session["LevelRights"]), 0);
        ds = getShift.SelectShift(Session["UserGroupName"].ToString(), Convert.ToString(Session["LevelRights"]), 0);
        if (ds.Tables[0].Rows.Count > 0)
        {
            this.ddlShift.DataSource = ds;
            this.ddlShift.DataTextField = "Shift_Name";
            this.ddlShift.DataValueField = "Shift_Id";
            this.ddlShift.DataBind();
        }
        ListItem lstItem = new ListItem();
        lstItem.Value = "0";
        lstItem.Text = "All";
        ddlShift.Items.Insert(0, lstItem);
    }
    private void LoadStrings(CultureInfo ci)
    {
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        lblWeeklyWorkHours.Text = rm.GetString("Menu_Reports_WeeklyWorkHoursLabel", ci);
        lblShift.Text = rm.GetString("Menu_Reports_Shift", 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);
        lblFrom.Text = rm.GetString("email_FromDate", ci);
        lblTo.Text = rm.GetString("email_ToDate", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(160, 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");
        }
    }
    #endregion
}