using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Masters_TA_UI_Master_LoanApplicationAdd : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    DataSet ds_Checkuser = new DataSet();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    ShiftAllocationEvendata objShiftAllocation = new ShiftAllocationEvendata();
    EmployeeEventData objEmployee = new EmployeeEventData();
    PayrollConfigurationEventData objPayroll = new PayrollConfigurationEventData();
    DataSet ds = new DataSet();
    int gintUserId;
    string gstrUserGroupName;
    string gstrDateFormat = string.Empty;
    cls_Common objCommon = new cls_Common();
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        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"]);
        gintUserId = Int32.Parse(Convert.ToString(Session["EmpAutoID"]));
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        DataSet dsLoanId = new DataSet();

        if (!IsPostBack)
        {
            UserRights();
            LoadStrings(ci);
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "','" + rm.GetString("Select", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validmonthdatefourdigit", ci) + "','" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_InValidTime", ci) + "','" + rm.GetString("Alert_InValidTimeFormat", ci) + "','" + rm.GetString("Alert_InvalidTimeMoreThan24", ci) + "','" + rm.GetString("Alert_InvalidTimeMoreThan59", ci) + "','" + rm.GetString("Alert_SelectInTerminal", ci) + "','" + rm.GetString("Alert_SelectPunchType", ci) + "','" + rm.GetString("Alert_FillInDate", ci) + "','" + rm.GetString("Alert_FillInTime", ci) + "','" + rm.GetString("Alert_SelectOutTerminal", ci) + "','" + rm.GetString("Alert_RouteName", ci) + "','" + rm.GetString("Alert_InTimeCorrectFormat", ci) + "','" + rm.GetString("Alert_InTimeCorrectFormat", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_OutDate", ci) + "','" + rm.GetString("Alert_OutTime", ci) + "','" + rm.GetString("Alert_SelectInDate", ci) + "','" + rm.GetString("Alert_SelecttheRouteName", ci) + "','" + rm.GetString("Alert_Norecordsfound", ci) + "','" + rm.GetString("Alert_Errorinretrivingdetails", ci) + "','" + rm.GetString("Alert_SelectEntranceID", ci) + "','" + rm.GetString("Alert_SelectOutPunchType", ci) + "','" + rm.GetString("Alert_EntertheEmployeeID", ci) + "','" + rm.GetString("Validator_ManualTimeEntry_EntranceName", ci) + "','" + rm.GetString("Validator_ManualTimeEntry_Reason", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        }

    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        int validateCount = 0;
        int loanDurationMonth;
        string loanPayoff;
        Int64 loanAmount=0;
        string loanDescription = string.Empty;
        string EmployeeId;
        DateTime startmonth;

        EmployeeId = txt_EmployeeId.Text;
        if (EmployeeId == "" || Convert.ToString(EmployeeId) == String.Empty)
        {
            string scrt = "<script>Validation()</script>";
            this.Page.RegisterStartupScript("a", scrt);
        }

        DataSet dsEmpDOJ = new DataSet();
        Hashtable empDoj = new Hashtable();
        empDoj.Add("@tag", "DOJ");
        empDoj.Add("@employeeID", EmployeeId);
        dsEmpDOJ = objPayroll.InsertLoanDetails(empDoj);
        
        if(dsEmpDOJ.Tables[0].Rows.Count==0)
        {

            hdnEmployeeId.Value = "0";
            string scrt = "<script>Validation()</script>";
            this.Page.RegisterStartupScript("a", scrt);
        }
        else
        {
            hdnEmployeeDOJ.Value = Convert.ToString(dsEmpDOJ.Tables[0].Rows[0]["DateOfJoining"]);
            hdnEmployeeId.Value= Convert.ToString(dsEmpDOJ.Tables[0].Rows[0]["Employee_Code_Id"]);
            loanDurationMonth = Convert.ToInt16(txt_LoanDurationMonth.Text);
            if (loanDurationMonth == 0)
            {
                string scrt = "<script>Validation()</script>";
                this.Page.RegisterStartupScript("a", scrt);
            }
            loanAmount = Convert.ToInt64(txt_LoanAmount.Text);
            if (loanAmount == 0)
            {
                string scrt = "<script>Validation()</script>";
                this.Page.RegisterStartupScript("a", scrt);
            }
            loanDescription = txt_loanDescription.Text;
            loanPayoff = Convert.ToString(hdnMonthPayoff.Value);

            startmonth = Convert.ToDateTime(txt_DeductionstartMonth.Text);
            String _MonthYear = startmonth.ToString("MMM/yyyy");

            if (startmonth >= Convert.ToDateTime(hdnEmployeeDOJ.Value) && hdnEmployeeDOJ.Value != "")
            {
                Hashtable hash_LoanDetails = new Hashtable();
                hash_LoanDetails.Add("@employeeID", EmployeeId);
                hash_LoanDetails.Add("@loanAmount", loanAmount);
                hash_LoanDetails.Add("@totalDeductionMonth", loanDurationMonth);
                hash_LoanDetails.Add("@deductionStartMonth", _MonthYear);
                hash_LoanDetails.Add("@monthlyDedcutAmount", loanPayoff);
                hash_LoanDetails.Add("@loandescription", loanDescription);
                hash_LoanDetails.Add("@tag", "I");
                objPayroll.InsertLoanDetails(hash_LoanDetails);
               // ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Sucessfull_LoanEntry", ci) + "');", true);
                base.MessageWithRedirect(rm.GetString("Sucessfull_LoanEntry", ci), "TA_UI_Master_LoanEntryList.aspx");

            }
            else
            {
                base.MessageWithRedirect("Please check Deduction Starting Month", "TA_UI_Master_LoanApplicationAdd.aspx");
            }
        }       
    }

    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Masters/TA_UI_Master_LoanEntryList.aspx");
    }
    public void CheckInDate(object s, ServerValidateEventArgs e)
    {

        string strInDate = "";
        if (txt_DeductionstartMonth.Text != "")
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            // strInDate = objCommon.DateConvertion(gstrDateFormat, txt_DeductionstartMonth.Text);
            DateTime dtCurrent = DateTime.Now;
            // DateTime dtInDate = DateTime.Parse(strInDate);
            // int Days = dtCurrent.CompareTo(dtInDate);
            //DateTime Current_Month = DateTime.ParseExact (txt_LoanDurarionMonth.Text,"dd/mmm/yyyy",null);
            //string Prev_Month= DateTime.Now.AddMonths(-1).ToString("MMM");


            //if (Days > 0)
            //{
            //    e.IsValid = true;
            //}
            //else
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Validator_ManualTimeEntry_InDate", ci) + "');", true);
            //    txt_DeductionstartMonth.Focus();
            //    e.IsValid = false;
            //}

            //if (Prev_Month!=(Current_Month.ToString("MMM")))
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Sucessfull');", true);
            //}
        }
        else
        {
            e.IsValid = true;
        }

    }

    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("LoanHeaderMasterAdd", ci);
        //lbl_GroupEntryType.Text = rm.GetString("ManualTimeEntry_GroupEntryType", ci);
        lbl_EmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lbl_EmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        // lbl_EntranceName.Text = rm.GetString("EntranceMaster_Entrance_Name", ci);
        lbl_LoanDescription.Text = rm.GetString("loan_Description", ci);
        lbl_LoanDeductStartingMonth.Text = rm.GetString("loan_DeductStart_Month", ci);
        lbl_LoanDurantion.Text = rm.GetString("Loan_DurationMonths", ci);

        lbl_LoanAmount.Text = rm.GetString("LoanAmount", ci);
        //lbl_LoanAccountNumber.Text = rm.GetString("Loan_AccountNumber", ci);
        lbl_LoanPayoff.Text = rm.GetString("Loan_MonthlyPayoff", ci);
        // lbl_Reason.Text = rm.GetString("email_Reason", ci);
        // lbl_RouteName.Text = rm.GetString("Transportation_Route_RouteName", ci);
        lbl_TypeOfManualEntry.Text = rm.GetString("ManualTimeEntry_TypeOfManualEntry", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        CustomValidator1.ErrorMessage = rm.GetString("Validator_ManualTimeEntry_InDate", ci);
        rbtSingle.Text = rm.GetString("rbtn_EmployeeMaster_Single", ci);
        rbtGroup.Text = rm.GetString("rbtn_ManualTimeEntry_Group", ci);
        //rbtBusLate.Text = rm.GetString("rbtn_ManualTimeEntry_BusLate", ci);
        //rbtOnDuty.Text = rm.GetString("Dash_OnDuty", ci);
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        //  btn_View.Text = rm.GetString("btn_View", ci);
        //btn_Search.Text =  rm.GetString("btn_Search", ci);
        // lbl_Employees.Text = rm.GetString("ManualTimeEntry_EmployeesAdd", ci);
        //btn_ViewEmployees.Text = rm.GetString("btn_Add", ci);
        //ddlRouteName.Items[0].Text = rm.GetString("Select", ci);
        //ddlEntranceName.Items[0].Text = rm.GetString("Select", ci);
        //grvEmployees.Columns[2].HeaderText = rm.GetString("email_EmployeeId", ci);
        //grvEmployees.Columns[3].HeaderText = rm.GetString("email_EmployeeName", ci);
        lblLoading.Text = rm.GetString("ProgressBarText", ci);
        //rbExcludingWeeklyOff.Text = rm.GetString("ShiftSetting_No", ci);
        //btnGroupEmployees.Text = rm.GetString("btn_Add", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        lblSelectEmployee.Text = rm.GetString("lblSelectEmployee", ci);
    }

    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(316, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }

}