using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;

public partial class Transactions_EmployeeNameForCompensationEntry : Base.BasePage
{
    string EmpCode;
    string EmpName;
    string LevelId;
    string level;
    Int32 employeeId;
    string EmployeeLevel;
    string ShiftDate;
    string InTime, OutTime, OTHrs, OTType;
    string gstrDateFormat, FromDate;
    string Process_Rec_ID, Employee_Id, LevelName, OverTimeStatus, TopLevelID,ExtraMinsHrs, ComStatus, LeavePolicyStatus;
    ResourceManager rm;
    CultureInfo ci;
    ManualEntryEventData obj = new ManualEntryEventData();
    cls_Common objClassCommon = new cls_Common();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    string Type = "";
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        if (Request.QueryString["EmpId"] != null && Request.QueryString["EmpId"] != "")
        {
            EmpCode = Convert.ToString(Request.QueryString["EmpId"]);
            LevelId = Convert.ToString(Request.QueryString["LevelId"]);
            ShiftDate = Convert.ToString(Request.QueryString["EmpDate"]);
            Type = Convert.ToString(Request.QueryString["Type"]);
            Rpt_Common rptcommon = new Rpt_Common();
            level = rptcommon.getNodes(Convert.ToInt32(LevelId), Convert.ToString(Session["LevelRights"]));
            if (Type == "empid")
            {
                GetEmployeeName(EmpCode, level);
            }
            else
            {
                gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
                ShiftDate = objClassCommon.DateConvertion(gstrDateFormat, ShiftDate);
                GetEmployeeShiftDateDetails(ShiftDate, EmpCode);
            }


        }
        else
        {
            Response.Clear();
            Response.End();
        }
    }
    private void GetEmployeeName(string EmpCode, string level)
    {
        DataSet ds_EmpName = new DataSet();
        ds_EmpName = obj.SelectEmpName(Convert.ToInt32(Session["EmpAutoID"]), EmpCode, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), level, 0, "");
        ds_EmpName.Tables[0].DefaultView.RowFilter = "CompOff_Eligible=True";
        DataTable dtEmployee = new DataTable();
        if (ds_EmpName != null)
        {
            if (ds_EmpName.Tables[0].Rows.Count > 0)
            {
                dtEmployee = ds_EmpName.Tables[0].DefaultView.ToTable();
                if (dtEmployee.Rows.Count > 0)
                {
                    EmpName = Convert.ToString(dtEmployee.Rows[0]["Employee Name"]);
                    employeeId = Convert.ToInt32(dtEmployee.Rows[0]["Employee_Id"]);
                    DataSet dsEmployeeDetails = new DataSet();
                    dsEmployeeDetails = objLeaveApplicationEventData.GetEmployeeDetails(Convert.ToInt32(employeeId));
                    if (dsEmployeeDetails.Tables.Count > 0)
                    {
                        if (dsEmployeeDetails.Tables[0].Rows.Count > 0)
                        {
                            EmployeeLevel = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["LevelId"]);
                            Response.Clear();
                            Response.Write(EmpName + "," + employeeId + "," + EmployeeLevel);
                            Response.End();
                        }
                    }
                }
                else
                {
                    Response.Clear();
                    Response.Write("Invalid EmployeeCode");
                    Response.End();
                }
                ds_EmpName.Dispose();
            }
        }
    }

    private void GetEmployeeShiftDateDetails(string ShiftDate, string EmpCode)
    {
        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;
        DataTable dtEmployee = new DataTable();
        dtEmployee = obj.SelectEmployeeShiftDateDetails(EmpCode, ShiftDate);
        if (dtEmployee != null)
        {
            if (dtEmployee.Rows.Count > 0)
            {
                EmpName = Convert.ToString(dtEmployee.Rows[0]["Employee Name"]);
                
                string NewTimeFormat = string.Empty;
                string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
                if (TimeFormat == "HH:mm")
                {
                    NewTimeFormat = "HH:mm";
                }
                else
                {
                    NewTimeFormat = "hh:mm tt";
                }
                InTime = Convert.ToString(dtEmployee.Rows[0]["InTime"]);
                if (InTime != "" && InTime != null && InTime != "00:00")
                {
                    InTime = (Convert.ToDateTime(InTime)).ToString(NewTimeFormat);
                }
                OutTime = Convert.ToString(dtEmployee.Rows[0]["OutTime"]);
                if (OutTime != "" && OutTime != null && OutTime != "00:00")
                {
                    OutTime = (Convert.ToDateTime(OutTime)).ToString(NewTimeFormat);
                }
                OTType = Convert.ToString(dtEmployee.Rows[0]["WeekofftypeNormOt"]);
                if (OTType == "Week Day")
                {
                    OTHrs = Convert.ToString(dtEmployee.Rows[0]["NormOt"]);
                }
                else if (OTType == "Weekly Off")
                {
                    OTHrs = Convert.ToString(dtEmployee.Rows[0]["OffOt"]);
                }
                else
                {
                    OTHrs = Convert.ToString(dtEmployee.Rows[0]["HolOt"]);
                }
                Process_Rec_ID = Convert.ToString(dtEmployee.Rows[0]["Process_Rec_ID"]);
                Employee_Id = Convert.ToString(dtEmployee.Rows[0]["Employee_Id"]);
                LevelName = Convert.ToString(dtEmployee.Rows[0]["LevelName"]);
                OverTimeStatus = Convert.ToString(dtEmployee.Rows[0]["OverTimeStatus"]);
                TopLevelID = Convert.ToString(dtEmployee.Rows[0]["TopLevelID"]);
                ExtraMinsHrs= Convert.ToString(dtEmployee.Rows[0]["ExtraMins"]);
                ComStatus = Convert.ToString(dtEmployee.Rows[0]["ComStatus"]);
                LeavePolicyStatus = Convert.ToString(dtEmployee.Rows[0]["LeavePolicyStatus"]);
                Response.Clear();
                Response.Write(OTType + "," + InTime + "," + OutTime + "," + OTHrs + "," + EmpName + "," + Process_Rec_ID + "," + Employee_Id + "," + LevelName + "," + OverTimeStatus + "," + TopLevelID + "," + ExtraMinsHrs + "," + ComStatus + "," + rm.GetString("Alert_Definedhalfdaycompoffhoursnotreached", ci) + "," + LeavePolicyStatus);
                Response.End();
            }
            else
            {
                Response.Clear();
                Response.Write("Invalid Date" + "," + EmpName);
                Response.End();
            }
            dtEmployee.Dispose();
        }
    }
}