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.Data.SqlClient;
//using System.Web.Mail;
using System.Net.Mail;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources; 
public partial class Transactions_TA_UI_Transactions_ManualTimeEntrySanctionModify : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    string strSetAlert;
    ManualEntryEventData objManualEntryEventData = new ManualEntryEventData();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    DataSet dsRecordsCount = new DataSet();
    DataSet dsManualTimeEntrySanctionView = new DataSet();
    DataTable dtManualEntrySanctionView = new DataTable();
    int gintManualTimeEntry_Id, gintEmployee_Id;
    string gstrStatus = string.Empty;
    string strEmployeeName = string.Empty, strEmployeeCodeId = string.Empty, strManualTimeEntryDate = string.Empty;
    string strInEntry = string.Empty, strOutEntry = string.Empty, strStatus = string.Empty;
    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);
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("MaxLengthh", strSetAlert);
        if (!IsPostBack)
        {
            gintManualTimeEntry_Id = Convert.ToInt32(Request.QueryString["ManualTimeEntry_Id"]);
            DataSet ds_RecordsCountLeaveReportingTo = new DataSet();
            ds_RecordsCountLeaveReportingTo = objLeaveApplicationEventData.GetRecordsCountLeaveReportingTo(Convert.ToInt32(Session["EmpAutoId"]));
            if (Convert.ToInt32(ds_RecordsCountLeaveReportingTo.Tables[0].Rows[0]["RecordsCount"]) > 0)
            {
                rblStatus.Items.Clear();
                rblStatus.Items.Insert(0, "Forwarded");
                rblStatus.Items.Insert(1, "Pending");
                rblStatus.Items.Insert(2, "Rejected");
            }
            dsRecordsCount = objLeaveApplicationEventData.GetRecordsCountLeaveSanction(Convert.ToInt32(Session["EmpAutoId"]));
            if (Convert.ToInt32(dsRecordsCount.Tables[0].Rows[0]["RecordsCount"]) > 0)
            {
                rblStatus.Items.Clear();
                rblStatus.Items.Insert(0, "Approved");
                rblStatus.Items.Insert(1, "Rejected");
            }
            ManualEntrySanctionViewDetails();
        }        
    }
    private void ManualEntrySanctionViewDetails()
    {
        // For SuperAdmin Login 
        DataSet dsEmployeeGroup = new DataSet();
        dsEmployeeGroup = objLeaveApplicationEventData.GetEmployeeGroupName(Convert.ToInt32(Session["EmpAutoId"]));
        if (dsEmployeeGroup.Tables[0].Rows.Count > 0)
        {
            if (dsEmployeeGroup.Tables[0].Rows[0]["GroupName"].ToString() == "SuperAdmin")
            {
                rblStatus.Items.Clear();
                rblStatus.Items.Insert(0, "Approved");
                rblStatus.Items.Insert(1, "Forwarded");
                rblStatus.Items.Insert(2, "Pending");
                rblStatus.Items.Insert(3, "Rejected");
                rblStatus.Enabled = true;
                txtRemarks.ReadOnly = false;
            }
            else
            {
            }
        }
        else
        {
        }
        dsManualTimeEntrySanctionView = objManualEntryEventData.SelectManualEntryById(gintManualTimeEntry_Id);
        dtManualEntrySanctionView = dsManualTimeEntrySanctionView.Tables[0];
        ViewState["Employee_code_Id"] = dtManualEntrySanctionView.Rows[0]["Employee_Code_Id"].ToString();
        lbl_EmployeeName.Text = dtManualEntrySanctionView.Rows[0]["Employee_Name"].ToString();
        lblManualTimeEntry_Date.Text = dtManualEntrySanctionView.Rows[0]["MDate"].ToString();
        if (dtManualEntrySanctionView.Rows[0]["InDate"] != DBNull.Value && dtManualEntrySanctionView.Rows[0]["InDate"].ToString() != string.Empty)
        {
            lblInDateTime.Text = DateTime.Parse(dtManualEntrySanctionView.Rows[0]["InDate"].ToString()).ToShortDateString() + " " + dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_InTime"].ToString();
        }
        if (dtManualEntrySanctionView.Rows[0]["OutDate"] != DBNull.Value && dtManualEntrySanctionView.Rows[0]["OutDate"].ToString() != string.Empty)
        {
            lblOutDateTime.Text = DateTime.Parse(dtManualEntrySanctionView.Rows[0]["OutDate"].ToString()).ToShortDateString() + " " + dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_OutTime"].ToString();
        }
        gstrStatus = dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Status"].ToString();
        lblReason.Text = dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Reason"].ToString();
        txtRemarks.Text = dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Remarks"].ToString();
        ViewState["StatusOnLoad"] = dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Status"].ToString();
        if (dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Status"].ToString() == "Pending")
        {
            rblStatus.SelectedValue = "Pending";
        }
        else if (dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Status"].ToString() == "Forwarded")
        {
            if (Convert.ToInt32(dsRecordsCount.Tables[0].Rows[0]["RecordsCount"]) > 0)
            {
            }
            else
            {
                rblStatus.SelectedValue = "Forwarded";
                rblStatus.Enabled = false;
                txtRemarks.ReadOnly = true;
            }
        }
        else if (dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Status"].ToString() == "Rejected")
        {
            rblStatus.SelectedValue = "Rejected";
            rblStatus.Enabled = false;
            txtRemarks.ReadOnly = true;
        }
        else if (dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Status"].ToString() == "Approved")
        {
            rblStatus.SelectedValue = "Approved";
            rblStatus.Enabled = false;
            txtRemarks.ReadOnly = true;
        }
        if (dsEmployeeGroup.Tables[0].Rows.Count > 0)
        {
            if (dsEmployeeGroup.Tables[0].Rows[0]["GroupName"].ToString() == "SuperAdmin")
            {
                rblStatus.Enabled = true;
                txtRemarks.ReadOnly = false;
            }
        }
    }
    // Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("UserGroups_ManualTimeEntrySanction", ci);
        lbl_EmpName.Text = rm.GetString("email_EmployeeName", ci);
        lbl_ManualTimeEntryDate.Text = rm.GetString("ManualTimeEntry_ManualTimeEntryDate", ci);
        lbl_InEntry.Text = rm.GetString("ManualTimeEntrySanction_InEntry", ci);
        lbl_OutEntry.Text = rm.GetString("ManualTimeEntrySanction_OutEntry", ci);
        lbl_Reason.Text = rm.GetString("email_Reason", ci);
        lbl_Remarks.Text = rm.GetString("email_Remarks", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_Save.Text = rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            gintManualTimeEntry_Id = Convert.ToInt32(Request.QueryString["ManualTimeEntry_Id"]);
            dsManualTimeEntrySanctionView = objManualEntryEventData.SelectManualEntryById(gintManualTimeEntry_Id);
            dtManualEntrySanctionView = dsManualTimeEntrySanctionView.Tables[0];
            strEmployeeName = dtManualEntrySanctionView.Rows[0]["Employee_Name"].ToString();
            strManualTimeEntryDate = dtManualEntrySanctionView.Rows[0]["MDate"].ToString();
            gstrStatus = dtManualEntrySanctionView.Rows[0]["ManualTimeEntry_Status"].ToString();
            if (rblStatus.SelectedValue != string.Empty)
            {
                strStatus = rblStatus.SelectedValue;
            }
            else
            {
                strStatus = string.Empty;
            }
            if (strStatus == string.Empty)
            {
                lbl_Error.Text = rm.GetString("Msg_ChooseOption", ci);
                return;
            }
            // Check whether the record has been Processed. If Processed then the record cannot be modified 
            int intCount = 0;
            if (gstrStatus == "Approved" && strStatus != "Approved")
            {
                //intCount = objManualEntryEventData.CheckProcessedManualEntry(gintManualTimeEntry_Id);
                intCount = objManualEntryEventData.UpdateManualTimeEntrySanction(gintManualTimeEntry_Id, strStatus, txtRemarks.Text, Convert.ToInt32(Session["EmpAutoId"]));
                if (intCount > 0)
                {
                    lbl_Error.Text = rm.GetString("lbl_Error", ci);
                    return;
                }
            }
            //objManualEntryEventData.UpdateManualTimeEntrySanction(gintManualTimeEntry_Id, strStatus, txtRemarks.Text, Convert.ToInt32(Session["EmpAutoId"]));
            string strScript = string.Empty;
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_ManualTimeEntrySanctionApplicationSaved", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Transactions_ManualTimeEntrySanction.aspx');";
            strScript = strScript + "</script>";
            // Display Alert Messsage.
            Page.RegisterStartupScript("AlertMTAdd9975466", strScript);
        }
        catch (Exception ex)
        {
            throw ex;
        }   
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Transactions/TA_UI_Transactions_ManualTimeEntrySanction.aspx");
    }
}
