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.Resources;
using System.Globalization;
using System.Threading;
using System.IO;

public partial class Transactions_TA_UI_Transactions_ManualCompensationEntry_Edit : Base.BasePage
{
    #region Variable Declaration
    Email_Triggering ObjEmailTrigger = new Email_Triggering();
    LeaveApplicationEventData objLeaveApplication = new LeaveApplicationEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    OTEventData objOT = new OTEventData();
    cls_Common objClassCommon = new cls_Common();
    DataSet ds_Checkuser = new DataSet();
    DataSet dsCompensation = new DataSet();
    DateTime gdtmBackDate;
    DataSet dsEmployeeDetails = new DataSet();
    DateTime dtCurrentDate, dtApplicationDate, dtFromDate, dtExpiryDate;
    ResourceManager rm;
    CultureInfo ci;
    bool blnCheckEmail;
    int intIndividualUser = 0;
    string strEmailIdSanctioningAuthority, strEmailIdReportingTo, strEmployeeCodeId, strEmployeeName, strlevelName, strEmailID;
    string strEmpFirstName, strEmpLastName;
    string strCompoffID = string.Empty;
    string gstrDateFormat = string.Empty;
    string strFromDateTime, strApplicationDate, strApplyDate;
    #endregion
    #region Page Load Event
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            objClassCommon.menu("OverTime", "254");
        }
        UserRights();
        string strSourceLanguage;
        strSourceLanguage = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(strSourceLanguage, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EntertheEmployeeID", ci) + "','" + rm.GetString("Alert_SelectDutyDate", ci) + "','" + rm.GetString("Alert_WorkedHoursShouldnotbeempty", ci) + "','" + rm.GetString("Alert_WorkedHoursShouldnotbe0hrs", ci) + "','" + rm.GetString("Validator_ManualTimeEntry_Reason", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        dtCurrentDate = Convert.ToDateTime(System.DateTime.Today.Date.ToShortDateString());
        lblLeaveApplicationDateData.Text = dtCurrentDate.ToString(Session["MDateFormat"].ToString());
        hdnDateFormat.Value = Convert.ToString(Session["IDateFormat"]);
        DataSet dsEmployeeGroup = new DataSet();
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        Session["intIndividualUser"] = intIndividualUser;
        hdnEmployeeId.Value = "0";
        if (Request.QueryString["CoffId"] != null)
        {
            strCompoffID = Request.QueryString["CoffId"].ToString();
        }
        if (!Page.IsPostBack)
        {
            GetEmployeeName();
        }
        //if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || intIndividualUser == 0)
        //{
        //    divEmployees.Visible = false;
        //    divAdmin.Visible = true;
        //    lblManCount.Text = "3";
        //}
        //else
        //{
        //    divEmployees.Visible = true;
        //    divAdmin.Visible = false;
        //    hdnEmployeeId.Value = Convert.ToString(Session["EmpAutoId"]);
        //    lblManCount.Text = "2";
        //    GetEmployeeName();
        //}
    }
    #endregion
    #region User Defined Function
    public void CompareCurrentdate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
            TimeSpan diff;
            if (Convert.ToString(Session["HasSpecialRights"]) == "True" || Convert.ToString(Session["UserGroupName"]) == "IndividualUsers")
            {
                diff = dtFromDate.Subtract(gdtmBackDate);
            }
            else
            {
                DateTime dtCurrentDate = DateTime.Now;
                diff = dtFromDate.Subtract(dtCurrentDate);
            }
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
            {
                e.IsValid = false;
            }
            if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin")
            {
                e.IsValid = true;
            }
            if (e.IsValid == false)
            {
                
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "alert('"+ rm.GetString("Alert_Pastentriesarenotallowed", ci) + "');";
                strScript += " </script>";
                this.RegisterStartupScript("setScript", strScript);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        //lblEmployeeIDLabel.Text = rm.GetString("email_EmployeeId", ci);
        lblEmployeeIDLabel1.Text = rm.GetString("email_EmployeeId", ci);
        //lblEmployeeNameLabel.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeNameLabel1.Text = rm.GetString("email_EmployeeName", ci);
        lblLeaveApplicationDate.Text = rm.GetString("LeaveApplication_LeaveApplicationDate", ci);
        lblFromDate.Text = rm.GetString("lbl_DutyDate", ci);
        lbl_InTime.Text = rm.GetString("ManualTimeEntry_In_Time", ci);
        lbl_outTime.Text = rm.GetString("ManualTimeEntry_Out_Time", ci);
        lbl_DayWorked.Text = rm.GetString("lbl_Dayworked", ci);
        lbl_WorkedHrs.Text = rm.GetString("lbl_ExtraWorkedHrs", ci);
        btnAdd.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btnCancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblReason.Text= rm.GetString("email_Reason", ci);
        lbl_Status.Text = rm.GetString("email_Status", ci);
        lblHeaderLeaveApplication.Text= rm.GetString("Transaction_CompensationEntry", ci);
    }
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    #endregion
    #region Button Events
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (hdnDayCompStatus.Value == "Not Reached")
            {
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_Definedhalfdaycompoffhoursnotreached", ci) + "');";
                strScript = strScript + "EmpName('CompOff');";
                strScript = strScript + "EmpNameForCompensationEntry('date', '" + hdnDateFormat.Value + "');";
                strScript = strScript + "</script>";
                //Dispaly alert messsage.
                Page.RegisterStartupScript("Alertalreadyapplied", strScript);
                return;
            }
            //Proc_TA_GetShiftSettingsByComIdForCompOff
            int isRequestCompoff = 0;
            isRequestCompoff = objOT.GetShiftSettingsByComIdForCompOff(Convert.ToInt32(hdfTopLevelID.Value));
            if (isRequestCompoff == 1)
            {
                gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
                strApplicationDate = objClassCommon.DateConvertion(gstrDateFormat, txtFromDate.Text);
                strApplyDate = objClassCommon.DateConvertion(gstrDateFormat, lblLeaveApplicationDateData.Text);
                dsCompensation = objOT.InsertCompoff(Convert.ToInt32(hdfProcess_Rec_ID.Value), Convert.ToInt32(hdfEmployee_Id.Value), strApplyDate, strApplicationDate, Convert.ToString(hdfTotalWorkedHrs.Value), Convert.ToInt32(hdfExtraMins.Value), Convert.ToString(hdfOverTimeStatus.Value), Convert.ToInt32(hdfTopLevelID.Value), Convert.ToString(hdfDayWorked.Value), Convert.ToString(hdfInPunchTime.Value), Convert.ToString(hdfoutPunchTime.Value), Convert.ToString(txtReason.Text), Convert.ToInt32(hdfCompOffID.Value), "D");
                if (dsCompensation.Tables[0].Rows[0][0].ToString() == "1")
                {
                    dsEmployeeDetails = objLeaveApplication.GetEmployeeDetails(Convert.ToInt32(hdfEmployee_Id.Value));
                    if (dsEmployeeDetails.Tables.Count > 0)
                    {
                        if (dsEmployeeDetails.Tables[0].Rows.Count > 0)
                        {
                            if (dsEmployeeDetails.Tables[0].Rows[0]["ReportingToEmailID"] != DBNull.Value)
                            {
                                strEmailIdReportingTo = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["ReportingToEmailID"]);
                            }
                            if (dsEmployeeDetails.Tables[0].Rows[0]["ApprovalAuthorityEmailID"] != DBNull.Value)
                            {
                                strEmailIdSanctioningAuthority = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["ApprovalAuthorityEmailID"]);
                            }
                            strEmpFirstName = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["Employee_FirstName"]);
                            strEmpLastName = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["Employee_LastName"]);
                            strEmployeeCodeId = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["Employee_Code_Id"]);
                            strEmployeeName = strEmpFirstName + " " + strEmpLastName;
                            strlevelName = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["levelName"]);
                            strEmailID = Convert.ToString(dsEmployeeDetails.Tables[0].Rows[0]["EmailId"]);
                            blnCheckEmail = ObjEmailTrigger.SendMailCompOff(strEmailID, strEmailIdReportingTo, strEmailIdSanctioningAuthority, strEmployeeCodeId, strEmployeeName, Convert.ToString(lblLeaveApplicationDateData.Text), Convert.ToString(txtFromDate.Text), Convert.ToString(hdfInPunchTime.Value), Convert.ToString(hdfoutPunchTime.Value), Convert.ToString(hdfDayWorked.Value), Convert.ToString(hdfTotalWorkedHrs.Value), Convert.ToString(txtReason.Text), Convert.ToString(hdfOverTimeStatus.Value), strlevelName, "Modify");
                        }
                    }

                    if (blnCheckEmail == true)
                    {
                        string strScript = "";
                        strScript = strScript + "<script language='javascript'>";
                        strScript = strScript + "alert('" + rm.GetString("Alert_COffCreditApplicationSavedSuccessfully", ci) + "');";
                        strScript = strScript + "location.replace('TA_UI_Transactions_ManualCompensationEntry.aspx');";
                        strScript = strScript + "</script>";
                        //Dispaly alert messsage.
                        Page.RegisterStartupScript("AlertPAdd65507866", strScript);
                    }
                    else
                    {
                        string strScript = "";
                        strScript = strScript + "<script language='javascript'>";
                        strScript = strScript + "alert('" + rm.GetString("Alert_COffCreditApplicationSavedSuccessfullyEmailNot", ci) + "');";
                        strScript = strScript + "location.replace('TA_UI_Transactions_ManualCompensationEntry.aspx');";
                        strScript = strScript + "</script>";
                        //Dispaly alert messsage.
                        Page.RegisterStartupScript("AlertPAdd65507866", strScript);
                    }
                }
                else if (dsCompensation.Tables[0].Rows[0][0].ToString() == "0")
                {
                    lblEmployeeNameData.Text = Convert.ToString(hdnEmployeeName.Value);
                    string strScript = "";
                    strScript = strScript + "<script language='javascript'>";
                    strScript = strScript + "alert('" + rm.GetString("Alert_CompensationEntryisalreadyapplied", ci) + "');";
                    strScript = strScript + "EmpName('CompOff');";
                    strScript = strScript + "EmpNameForCompensationEntry('date', '" + hdnDateFormat.Value + "');";
                    strScript = strScript + "</script>";
                    //Dispaly alert messsage.
                    Page.RegisterStartupScript("Alertalreadyapplied", strScript);
                    return;
                }
            }
            else
            {
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_CompensationEntryRequestisnotallowed", ci) + "');";
                strScript = strScript + "EmpName('CompOff');";
                strScript = strScript + "EmpNameForCompensationEntry('date', '" + hdnDateFormat.Value + "');";
                strScript = strScript + "</script>";
                //Dispaly alert messsage.
                Page.RegisterStartupScript("AlertRequestisnot", strScript);
                return;
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Transactions/TA_UI_Transactions_ManualCompensationEntry.aspx");
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(254, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
    #endregion
    #region Employee Name.
    public void GetEmployeeName()
    {
        if (Session["user"] == null || Session["user"].ToString() == "")
        {
            Response.Redirect("../Index.aspx");
        }
        else
        {
            dsCompensation = objLeaveApplication.SelectCompensationDatabyid(Convert.ToInt32(strCompoffID));
            intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
            if (dsCompensation.Tables[0].Rows.Count > 0)
            {
                lblEmployeeIdData.Text = dsCompensation.Tables[0].Rows[0]["Employee_Id"].ToString();
                lblEmployeeNameData.Text = dsCompensation.Tables[0].Rows[0]["Employee_FirstName"].ToString();
                lblLeaveApplicationDateData.Text = dsCompensation.Tables[0].Rows[0]["ApplicationDate"].ToString();
                txtFromDate.Text = dsCompensation.Tables[0].Rows[0]["FromDate"].ToString();
                Txt_OTType.Text = dsCompensation.Tables[0].Rows[0]["DayWorked"].ToString();
                lbl_InPunchTime.Text = dsCompensation.Tables[0].Rows[0]["InTime"].ToString();
                lbl_outPunchTime.Text = dsCompensation.Tables[0].Rows[0]["OutTime"].ToString();
                lbl_TotalWorkedHrs.Text = dsCompensation.Tables[0].Rows[0]["WorkedHours"].ToString();
                hdfTotalWorkedHrs.Value = dsCompensation.Tables[0].Rows[0]["WorkedHours"].ToString();
                lblStatus.Text = dsCompensation.Tables[0].Rows[0]["Comp_Status"].ToString();
                hdfCompOffID.Value = dsCompensation.Tables[0].Rows[0]["Compensation_Id"].ToString();
                txtReason.Text = dsCompensation.Tables[0].Rows[0]["Reason"].ToString();
                hdfEmployee_Id.Value= dsCompensation.Tables[0].Rows[0]["E_Employee_ID"].ToString();
                hdfProcess_Rec_ID.Value = dsCompensation.Tables[0].Rows[0]["Process_RecID"].ToString();
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "EmpNameForCompensationEntry('date', '" + hdnDateFormat.Value + "');";
                strScript = strScript + "</script>";
                //Dispaly alert messsage.
                Page.RegisterStartupScript("AlertRequestisnot", strScript);
                return;
            }
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        }
    }
    #endregion
}
