using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Reports_TA_UI_Report_InOut_Grid : Base.BasePage
{
    #region Variable Declaration
    GetReports rptEmp = new GetReports();
    ResourceManager rm;
    CultureInfo ci;
    string EmpID = string.Empty;
    int shiftID = 0;
    string fromDate = string.Empty;
    string toDate = string.Empty;
    string Empname = string.Empty;
    int employeeCategory = 0;
    int employeeType = 0;
    string userGroupName = string.Empty;
    string gradeLevel = string.Empty;
    string strReportType = string.Empty;
    string ParentLevelid = string.Empty;
    string strSwipeCard = string.Empty;
    string Gender = string.Empty;
    string Designation = string.Empty;
    #endregion
    #region Page Load Events
    /// <summary>
    /// On page loading
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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 = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        EmpID = Convert.ToString(Request.QueryString["empid"]);
        shiftID = Convert.ToInt32(Request.QueryString["shft"]);
        fromDate = Convert.ToString(Request.QueryString["dtFrom"]);
        toDate = Convert.ToString(Request.QueryString["dtTo"]);
        Empname = Convert.ToString(Request.QueryString["EmpName"]);
        ParentLevelid = Request.QueryString["ParentLevelId"].ToString();
        employeeCategory = Convert.ToInt32(Request.QueryString["EmpCategory"]);
        employeeType = Convert.ToInt32(Request.QueryString["EmpType"]);
        userGroupName = Convert.ToString(Request.QueryString["UserGroupName"]);
        gradeLevel = Convert.ToString(Request.QueryString["GradeLevel"]);
        strSwipeCard = Convert.ToString(Request.QueryString["SwipeCard"]);
        Gender = Convert.ToString(Request.QueryString["Gender"]);
        Designation = Convert.ToString(Request.QueryString["Designation"]);
        if (!IsPostBack)
        {
            DataSet dsEmployeeInOut = new DataSet();
            dsEmployeeInOut = rptEmp.EmployeeInOutDetails(EmpID, fromDate, toDate, Empname, shiftID, employeeCategory, employeeType, userGroupName, gradeLevel, strSwipeCard, Convert.ToInt32(ParentLevelid), Convert.ToInt32(Designation), Gender, Convert.ToInt32(Session["EmpAutoID"]));
            Session["EmployeeInOutData"] = dsEmployeeInOut.Tables[0];
            grvEmployeeInOut.DataSource = dsEmployeeInOut;
            grvEmployeeInOut.DataBind();
        }
    }
    #endregion
    #region Button Events
    /// <summary>
    /// To view the grid data in a crystal report.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_View_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid == true)
            {
                //Redirect the page to the report viewer
                string strScript = "";
                //strScript = "TA_UI_Report_ReportViewer.aspx";
                strScript = "<script language='javascript'>";
                strScript = strScript + "window.open('TA_UI_Report_ReportViewer.aspx?ReportName=InOutReport&dtFrom=" + fromDate + "&dtTo=" + toDate + "&empid=" + EmpID + "&shft=" + shiftID + "&EmpName=" + Empname +
                    "&EmpCategory=" + employeeCategory + "&EmpType=" + employeeType + "&ParentLevelId=" + ParentLevelid + "&UserGroupName=" + userGroupName + "&GradeLevel=" + gradeLevel +
                    "&SwipeCard=" + strSwipeCard + "','','left=10px, top=60px, width=1000px, height=600px, scrollbars=yes, status=no, resizable=yes');";
                strScript = strScript + "</script>";
                Response.Write(strScript);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion
    #region User Defined Functions
    /// <summary>
    /// Getting Information from resource file and display it in label
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        btnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblEmployeeInOut.Text = rm.GetString("Report_EmployeeInOut", ci);
        grvEmployeeInOut.Columns[0].HeaderText = rm.GetString("email_EmployeeId", ci);
        grvEmployeeInOut.Columns[1].HeaderText = rm.GetString("email_EmployeeName", ci);
        grvEmployeeInOut.Columns[2].HeaderText = rm.GetString("EmployeeMaster_Designation", ci);
        grvEmployeeInOut.Columns[3].HeaderText = rm.GetString("LevelDetails_LevelName", ci);
        grvEmployeeInOut.Columns[4].HeaderText = rm.GetString("Reports_Date", ci);
        grvEmployeeInOut.Columns[5].HeaderText = rm.GetString("ManualTimeEntry_In_Time", ci);
        grvEmployeeInOut.Columns[6].HeaderText = rm.GetString("ManualTimeEntry_Out_Time", ci);
        grvEmployeeInOut.Columns[7].HeaderText = rm.GetString("EmployeeMaster_Gender", ci);
    }
    /// <summary>
    /// To load the employee in out grid based on the sorting.
    /// </summary>
    protected void LoadEmployeeInOutGrid()
    {
        DataTable dtEmployeeInOut = new DataTable();
        dtEmployeeInOut = (DataTable)Session["EmployeeInOutData"];
        DataView dvEmployeeInOut = new DataView(dtEmployeeInOut);
        if (ViewState["Sort_Field"] != null)
        {
            dvEmployeeInOut.Sort = ViewState["Sort_Field"].ToString() + " " + ViewState["Sort_Order"].ToString();
        }
        grvEmployeeInOut.DataSource = dvEmployeeInOut;
        grvEmployeeInOut.DataBind();
        Session["EmployeeInOutData"] = dvEmployeeInOut.ToTable();
    }
    /// <summary>
    /// To get the label name to show in the empty grid.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void GetEmptyText(object sender, EventArgs e)
    {
        Label lblNoRecords = (Label)sender;
        lblNoRecords.Text = rm.GetString("Alert_Norecordsfound", ci);
    }
    #endregion
    #region Grid Events
    /// <summary>
    /// To load the grid based on sorting
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployeeInOut_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["Sort_Field"] = e.SortExpression;
        if (ViewState["Sort_Order"] == null)
            ViewState["Sort_Order"] = "Asc";
        else if (ViewState["Sort_Order"].ToString() == "Asc")
            ViewState["Sort_Order"] = "Desc";
        else
            ViewState["Sort_Order"] = "Asc";
        if (grvEmployeeInOut.PageCount > 0)
            grvEmployeeInOut.PageIndex = 0;
        LoadEmployeeInOutGrid();
    }
    /// <summary>
    /// To load the grid based on the paging
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployeeInOut_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadEmployeeInOutGrid();
        grvEmployeeInOut.PageIndex = e.NewPageIndex;
        grvEmployeeInOut.DataBind();
    }
    /// <summary>
    /// To change the gender label according to the language selected
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployeeInOut_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblGender = (Label)e.Row.FindControl("lblGender");
            if (lblGender.Text.Trim() == "Male" || lblGender.Text.Trim() == "MALE")
            {
                lblGender.Text = rm.GetString("rbtn_EmployeeMaster_Male", ci);
            }
            else if (lblGender.Text.Trim() == "Female" || lblGender.Text.Trim() == "FEMALE")
            {
                lblGender.Text = rm.GetString("rbtn_EmployeeMaster_Female", ci);
            }
            Label lbl = (Label)e.Row.FindControl("lblDate");
            if (lbl.Text != "")
            {
                lbl.Text = (Convert.ToDateTime(lbl.Text.ToString())).ToString(Session["MDateFormat"].ToString());
            }
        }
    }
    #endregion
}
