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 Transactions_TA_UI_Transaction_EncashmentModify : Base.BasePage
{
    ResourceManager rm;
    DataSet ds_RemainigNoLeaves = new DataSet();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    //LeavePolicyTypeEventData objLeavePolicy = new LeavePolicyTypeEventData();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    EncashmentEventData ObjEncashmentEventData = new EncashmentEventData();
    int MonthType;
    int intEmpId, intLeaveTypeId, intEncashmentId;
    string strLeaveTypeName, strEmployeeCodeId, strEmployeeName;
    double dNumberOfDays, dRemainingLeaves;
    CultureInfo ci;
    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;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_DayEmpty", ci) + "','" + rm.GetString("Alert_ReasonShouldNotbeEmpty", ci) + "','" + rm.GetString("Alert_NumberOfDaysAppliedShouldbelesserthanRemainingnumberofDays", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_NumberOfDaysenteredisnotvalid", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        if (Request.QueryString["EmployeeId"].ToString() == "" || Request.QueryString["LeaveTypeId"].ToString() == "" || Request.QueryString["LeaveTypeId"].ToString() == "-Select-")
        {
            lbl_Error.Text = "";
            return;
        }
         intEmpId = Convert.ToInt32(Request.QueryString["EmployeeId"]);
         intLeaveTypeId = Convert.ToInt32(Request.QueryString["LeaveTypeId"]);
         strLeaveTypeName = Request.QueryString["LeaveTypeName"];
         strEmployeeCodeId = Request.QueryString["EmployeeCodeId"];
         strEmployeeName = Request.QueryString["EmployeeName"];
         dNumberOfDays = Convert.ToDouble(Request.QueryString["NoOfDays"]);
         intEncashmentId = Convert.ToInt32(Request.QueryString["EncashmentId"]);
         dRemainingLeaves=0.0;
        //MonthType = objLeavePolicy.SelectLeavePolicyType();
        //Checking the Monthly Policy Type
        //if (MonthType == 1)
        //{
        //    ds_RemainigNoLeaves = objLeavePolicy.GetRemainingLeaves(intEmpId, intLeaveTypeId);
        //}
        //else
        //{
            ds_RemainigNoLeaves = objLeaveApplicationEventData.GetRemainingNumberOfLeave(Convert.ToString(intEmpId), intLeaveTypeId);
        //}
        if(ds_RemainigNoLeaves.Tables[0].Rows.Count>0)
        {
            dRemainingLeaves = Convert.ToDouble(ds_RemainigNoLeaves.Tables[0].Rows[0]["RemainingNoOfLeaves"]) + dNumberOfDays;
        }
        if (!IsPostBack)
        {
            lbl_EmpCode.Text = strEmployeeCodeId;
            lbl_EmpName.Text = strEmployeeName;
            lblLeaveTypeData.Text = strLeaveTypeName;
            lblRemainingNoOfDaysData.Text = dRemainingLeaves.ToString();
            hdnRemainingNoOfDays.Value = dRemainingLeaves.ToString();
            txt_Numberofdays.Text = dNumberOfDays.ToString();
            hdnNumberOfDays.Value = dNumberOfDays.ToString();
            txtReason.Text = Request.QueryString["Reason"];
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("UserGroups_Encashment", ci);
        lbl_Code.Text = rm.GetString("Ecashmnent_Empcode", ci);
        lbl_LeaveType.Text = rm.GetString("lbl_leavetype", ci);
        lbl_Name.Text = rm.GetString("email_EmployeeName", ci);
        lbl_Nodays.Text = rm.GetString("Encashment_Remainingdays", ci);
        lbl_Numberofdaysapp.Text = rm.GetString("Encashment_Nodaysapplied", ci);
        //lbl_EmployeeNames.Text = rm.GetString("email_EmployeeName", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Encashment_Nodaysapplied", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lbl_Reason.Text = rm.GetString("email_Reason", ci);
        btn_Save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Back.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
       // btnSearch.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Search", ci);
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            double RemainingDays = Convert.ToDouble(hdnRemainingNoOfDays.Value);
            double NumberOfDaysApplied = Convert.ToDouble(txt_Numberofdays.Text);
            if (NumberOfDaysApplied > RemainingDays)
            {
                Message(rm.GetString ("Alert_NumberofDaysappliedshouldlesserthanremainingleaves",ci));
                //lbl_Error.Text = "Number of Days applied should lesser than remaining leaves";
                return;
            }
            if (NumberOfDaysApplied <= 0)
            {
                Message(rm.GetString ("Msg_NumberofDaysappliedshouldbegreaterthanzero",ci));
                //lbl_Error.Text = "Number of Days applied should be greater than zero";
                return;
            }
            double NumberOfDaysAppliedForClosure = 0.0;
            if ((Convert.ToDouble(hdnNumberOfDays.Value) > NumberOfDaysApplied) || (Convert.ToDouble(hdnNumberOfDays.Value) < NumberOfDaysApplied))
            {
                NumberOfDaysAppliedForClosure = NumberOfDaysApplied - Convert.ToDouble(hdnNumberOfDays.Value);
            }
            //else if (Convert.ToDouble(hdnNumberOfDays.Value) < NumberOfDaysApplied)
            //{
            //    NumberOfDaysAppliedForClosure = Convert.ToDouble(hdnNumberOfDays.Value) - NumberOfDaysApplied;
            //}
            else if (Convert.ToDouble(hdnNumberOfDays.Value) == NumberOfDaysApplied)
            {
                NumberOfDaysAppliedForClosure = 0.0;
            }
            try
            {
                //if (MonthType == 1)
                //{
                //    objLeavePolicy.updateMonthlyLeaveclosureEncashment(intEmpId.ToString(), NumberOfDaysAppliedForClosure, intLeaveTypeId);
                //}
                //else
                //{
                    objLeaveApplicationEventData.updateYearclosureEncashment(intEmpId.ToString(), NumberOfDaysAppliedForClosure, intLeaveTypeId);
                //}
                //Update Encashment Details in Encashment Table
                ObjEncashmentEventData.UpdateEncashmentDetails(intEncashmentId,NumberOfDaysApplied, txtReason.Text);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            lbl_EmpCode.Text = "";
            lbl_EmpName.Text = "";
            //lbl_Numberofdays.Text = "";
            hdnRemainingNoOfDays.Value = "";
            txt_Numberofdays.Text = "";
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EncashmentApplicationUpdatedSuccessfully", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Transaction_Encashment.aspx');";
            strScript = strScript + "</script>";
            //Dispaly alert messsage.
            Page.RegisterStartupScript("AlertEncashMod99623866", strScript);
            //Response.Redirect("TA_UI_Transaction_Encashment.aspx");
        }
        catch
        {
            //
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Transaction_Encashment.aspx");
    }
    #region Message.

    /// <summary>
    /// Messages the specified message content.
    /// </summary>
    /// <param name="messageContent">Content of the message.</param>
    private void Message(string messageContent)
    {
        string strScript = "<script language='javascript' type='text/javascript'>";
        strScript += "alert('" + messageContent + "');";
        strScript += " </script>";
        this.RegisterStartupScript("setScript", strScript);

    }

    #endregion
}
