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.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
public partial class Transactions_TA_UI_Transactions_OnDutySanctionView : Base.BasePage
{
    #region VariablDeclaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    YearSettingsEventData objYearSettingsEventData = new YearSettingsEventData();
    ManualEntryEventData objManualEntryEventData = new ManualEntryEventData();
    cls_Common objcls_Common = new cls_Common();
    Rpt_Common rptcommon = new Rpt_Common();
    DataTable dtLeaveTransactionView = new DataTable();
    DataSet dsYearSetting = new DataSet();
    int vmonthValue;
    string startDate;
    int EmployeeCategory, EmployeeType;
    string endDate, gstrDateFormat, LevelId, Status, From_Date, To_Date, StartMonth, EndMonth, StartYear, EndYear;
    string strSwipeCardNo = string.Empty;
    string strEmployeeName = string.Empty;
    string strEmployeeCodeId = string.Empty;
    string SelectedShift = string.Empty;
    string EmployeeID, EmployeeName;
    string strSwipeCard = "";
    DataTable dtManualEntrySanctionView = new DataTable(); int payrollConfig = 0;
    #endregion
    #region Page Load events
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        gdOndutySanctionView.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        int manualEntryExists = 0;
        //manualEntryExists = objcls_Common.CheckPageExistsInLicense(248);
        //if (manualEntryExists == 0)
        //{
        //    string strScripts = "";
        //    strScripts = strScripts + "<script language='javascript'>";
        //    strScripts = strScripts + "window.close();</script>";
        //    Page.RegisterStartupScript("AlertManualApproval", strScripts);
        //    return;
        //}
        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"]);
        payrollConfig = objcls_Common.CheckPageExistsInLicense(185);
        if (!IsPostBack)
        {
            switch (payrollConfig)
            {
                case 0:
                    objcls_Common.menu("Approvals", "248");
                    break;
                default:
                    objcls_Common.menu("Approvals", "174");
                    break;
            }
            LoadStrings(ci);
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertMessages_DeleteRestore('" + rm.GetString("Alert_ConfirmDelete") + "','" + rm.GetString("Alert_SelectItemsToDelete") + "','" + rm.GetString("Alert_ConfirmDeletePermanent") + "','" + rm.GetString("Alert_Areyousuretorestore") + "','" + rm.GetString("Alert_SelectItemsToRestore") + "','" + rm.GetString("Alert_SelectItemToForwarded") + "','" + rm.GetString("Alert_SelectItemToApproved") + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            UserRights();
        }

        if (Request.QueryString["Shift"] != null)
        {
            SelectedShift = Convert.ToString(Request.QueryString["Shift"]);
            Session["OndutyID"] = Convert.ToString(Request.QueryString["Shift"]);
        }
        else if (Convert.ToString(Session["OndutyID"]) != string.Empty)
        {
            SelectedShift = Convert.ToString(Session["OndutyID"]);
        }
        if (Request.QueryString["Status"] != null)
        {
            Status = Convert.ToString(Request.QueryString["Status"]);
            Session["OndutyStatus"] = Status;
        }
        else
        {
            Status = Convert.ToString(Session["OndutyStatus"]);
        }
        if (Request.QueryString["FromDate"] != null)
        {
            From_Date = Convert.ToString(Request.QueryString["FromDate"]);
            Session["ODFrmDate"] = Convert.ToString(Request.QueryString["FromDate"]);
        }
        else if (Convert.ToString(Session["ODFrmDate"]) != string.Empty)
        {
            From_Date = Convert.ToString(Session["ODFrmDate"]);
        }
        if (Request.QueryString["ToDate"] != null)
        {
            To_Date = Convert.ToString(Request.QueryString["ToDate"]);
            Session["ODToDate"] = Convert.ToString(Request.QueryString["ToDate"]);
        }
        else if (Convert.ToString(Session["ODToDate"]) != string.Empty)
        {
            To_Date = Convert.ToString(Session["ODToDate"]);
        }
        if (Request.QueryString["EmpId"] != null)
        {
            EmployeeID = Convert.ToString(Request.QueryString["EmpId"]);
            Session["ODEmpID"] = Convert.ToString(Request.QueryString["EmpId"]);
        }
        else if (Convert.ToString(Session["ODEmpID"]) != string.Empty)
        {
            EmployeeID = Convert.ToString(Session["ODEmpID"]);
        }
        if (Request.QueryString["EmpName"] != null)
        {
            EmployeeName = Convert.ToString(Request.QueryString["EmpName"]);
            Session["ODEmpName"] = Convert.ToString(Request.QueryString["EmpName"]);
        }
        else if (Convert.ToString(Session["ODEmpName"]) != string.Empty)
        {
            EmployeeName = Convert.ToString(Session["ODEmpName"]);
        }

        if (Request.QueryString["EmpCategory"] != null)
        {
            EmployeeCategory = Convert.ToInt32(Request.QueryString["EmpCategory"].ToString());
            Session["ODEmpCat"] = Convert.ToInt32(Request.QueryString["EmpCategory"].ToString());
        }
        else if (Convert.ToString(Session["ODEmpCat"]) != string.Empty)
        {
            EmployeeCategory = Convert.ToInt32(Session["ODEmpCat"]);
        }
        if (Request.QueryString["EmpType"] != null)
        {
            EmployeeType = Convert.ToInt32(Request.QueryString["EmpType"].ToString());
            Session["ODEmpType"] = Convert.ToInt32(Request.QueryString["EmpType"].ToString());
        }
        else if (Convert.ToString(Session["ODEmpType"]) != string.Empty)
        {
            EmployeeType = Convert.ToInt32(Session["ODEmpType"]);
        }

        if (Request.QueryString["SwipeCard"] != null)
        {
            strSwipeCard = Convert.ToString(Request.QueryString["SwipeCard"]);
            Session["ODSwipeCard"] = Convert.ToString(Request.QueryString["SwipeCard"].ToString());
        }
        else if (Convert.ToString(Session["ODSwipeCard"]) != string.Empty)
        {
            strSwipeCard = Convert.ToString(Session["ODSwipeCard"]);
        }

        LevelId = rptcommon.getNodes(Convert.ToInt32(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        if (From_Date != string.Empty && From_Date != null)
        {
            From_Date = objcls_Common.DateConvertion(gstrDateFormat, From_Date);
        }
        if (To_Date != string.Empty && To_Date != null)
        {
            To_Date = objcls_Common.DateConvertion(gstrDateFormat, To_Date);
        }
        if (Request.QueryString["isNotification"] != null)
        {
            if (Convert.ToString(Session["parentLevelId"]) == null)
            {
                Session["parentLevelId"] = "0";
            }
            LevelId = "0";
        }
        if (!IsPostBack)
        {
            int intCompanyId;
            string strStatus = string.Empty;
            string strModule = string.Empty;
            string strScript = "";
            if (Convert.ToString(Session["isDashBoard"]) == "No" || Session["isDashBoard"] == null)
            {
                if (Request.QueryString["isNotification"] == null)
                {
                    strScript = strScript + "<script language='javascript'>";
                    bool blnParentExists = true;
                    strModule = "Approval";
                    if (Session["parentLevelId"] == null)
                    {
                        blnParentExists = false;
                    }
                    else if (Convert.ToInt32(Session["parentLevelId"]) == 0)
                    {
                        blnParentExists = false;
                    }
                    if (blnParentExists == false)
                    {
                        strScript = strScript + "alert('" + rm.GetString("Alert_SelectCompany", ci) + "');window.close();";
                        strScript = strScript + "</script>";
                        Page.RegisterStartupScript("AlertManualApproval", strScript);
                        return;
                    }
                    intCompanyId = getParentCompany(Convert.ToInt32(Session["parentLevelId"]));
                    strStatus = GetApprovalStatus(intCompanyId, strModule);
                    if (strStatus == "ApprovalNotRequired")
                    {
                        strStatus = rm.GetString("Alert_ApprovalNotRequiredforthiscompany", ci);
                        strScript = strScript + "alert('" + strStatus + "');location.replace('TA_UI_Transactions_OnDutySanction.aspx');";
                        strScript = strScript + "</script>";
                        Page.RegisterStartupScript("AlertManualApproval", strScript);
                        return;
                    }
                    else if (strStatus == "ApprovalRequired")
                    {
                    }
                    else if (strStatus == "NotDefined")
                    {
                        strStatus = rm.GetString("Alert_ApprovalSettingsnotdefinedforthiscompany", ci);
                        strScript = strScript + "alert('" + strStatus + "');location.replace('../Organization%20Settings/Organization_ShiftSetting.aspx');";
                        strScript = strScript + "</script>";
                        Page.RegisterStartupScript("AlertManualApproval", strScript);
                        return;
                    }
                }
                else
                {
                    if (Convert.ToString(Session["parentLevelId"]) == null)
                    {
                        Session["parentLevelId"] = "0";
                    }
                    LevelId = "0";
                }
            }
            else
            {
                if (Convert.ToString(Session["parentLevelId"]) == null)
                {
                    Session["parentLevelId"] = "0";
                }
                LevelId = "0";
            }
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            dsYearSetting = objYearSettingsEventData.GetActiveYearSettings();
            if (dsYearSetting.Tables[0].Rows.Count <= 0)
            {
                return;
            }
            else
            {
                StartMonth = Convert.ToString(dsYearSetting.Tables[0].Rows[0]["StartMonth"]);
                StartYear = Convert.ToString(dsYearSetting.Tables[0].Rows[0]["StartYear"]);
                vmonthValue = MonthValue(StartMonth);
                startDate = vmonthValue.ToString() + "/1/" + StartYear;
                EndMonth = Convert.ToString(dsYearSetting.Tables[0].Rows[0]["EndMonth"]);
                EndYear = Convert.ToString(dsYearSetting.Tables[0].Rows[0]["EndYear"]);
                vmonthValue = MonthValue(EndMonth);
                endDate = vmonthValue.ToString() + "/31/" + EndYear;
            }
            if (LevelId == "0")
            {
                LevelId = null;
            }
            if (From_Date == string.Empty)
            {
                From_Date = startDate;
            }
            if (To_Date == string.Empty)
            {
                To_Date = endDate;
            }
        }
        if (Status == "Approved,Pending,Resend,Forwarded,Rejected,Reply")
        {
            Status = "All";
        }
        if (Status != null && Status != "")
        {
            Status = Status.Replace(",", "','");
        }
        if (!IsPostBack)
        {
            BindManualEntrySanctionDetails();
        }
    }
    #endregion
    #region User Defined Functions
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("OnDutyApproval_Header", ci);
        btnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        gdOndutySanctionView.Columns[1].Caption = rm.GetString("email_EmployeeId", ci);
        gdOndutySanctionView.Columns[2].Caption = rm.GetString("email_EmployeeName", ci);
        gdOndutySanctionView.Columns[3].Caption = rm.GetString("ManualTimeEntry_ManualTimeEntryDate", ci);
        gdOndutySanctionView.Columns[4].Caption = rm.GetString("OnDuty_InDate", ci);
        gdOndutySanctionView.Columns[5].Caption = rm.GetString("OnDuty_InTime", ci);
        gdOndutySanctionView.Columns[6].Caption = rm.GetString("OnDuty_OutDate", ci);
        gdOndutySanctionView.Columns[7].Caption = rm.GetString("OnDuty_OutTime", ci);
        gdOndutySanctionView.Columns[8].Caption = rm.GetString("email_Reason", ci);
        gdOndutySanctionView.Columns[9].Caption = rm.GetString("email_Status", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights((payrollConfig == 0) ? 248 : 174, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
    public void BindManualEntrySanctionDetails()
    {
        if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
        {
            dtManualEntrySanctionView = objManualEntryEventData.GetOnDutyEntrySanctionForSuperAdmin(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, EmployeeID, EmployeeName, EmployeeCategory, EmployeeType, From_Date, To_Date, strSwipeCard, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]));
            Session["TotalEmployees"] = dtManualEntrySanctionView.Rows.Count;
            Session["DataManualEntrySanction"] = dtManualEntrySanctionView;
        }
        else
        {
            dtManualEntrySanctionView = objManualEntryEventData.GetOnDutySanctionForUser(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, EmployeeID, EmployeeName, EmployeeCategory, EmployeeType, From_Date, To_Date, strSwipeCard, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]));
            Session["TotalEmployees"] = dtManualEntrySanctionView.Rows.Count;
            Session["DataManualEntrySanction"] = dtManualEntrySanctionView;
        }
        if (dtManualEntrySanctionView.Rows.Count > 0)
        {
            gdOndutySanctionView.DataSource = dtManualEntrySanctionView;
            gdOndutySanctionView.DataBind();
        }
        else
        {
            gdOndutySanctionView.DataSource = dtManualEntrySanctionView;
            gdOndutySanctionView.DataBind();
        }
    }
    public int getParentCompany(int iEmployeeLevelID)
    {
        org_levelsEventData getOrg = new org_levelsEventData();
        getOrg.GetOrgParentLevels_FromID(iEmployeeLevelID);
        DataSet ds_getOrg = new DataSet();
        ds_getOrg = getOrg.GetOrgParentLevels();
        int iCompanyID = 0;
        if (ds_getOrg != null)
        {
            if (ds_getOrg.Tables[0].Rows.Count > 0)
            {
                iCompanyID = Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"].ToString());
            }
        }
        return iCompanyID;
    }
    public string GetApprovalStatus(int intCompanyId, string strModule)
    {
        int approvalSettingsPageExists = 0;
        //Checking grade module is available or not and changed the id value from 110 to 228
        approvalSettingsPageExists = objcls_Common.CheckPageExistsInLicense(228);
        string strStatus = string.Empty;
        if (approvalSettingsPageExists == 1)
        {
            DataSet dsApprovalSettings = new DataSet();
            ShiftEvenData objShift = new ShiftEvenData();
            dsApprovalSettings = objShift.SelectApprovalSettings(intCompanyId);
            if (dsApprovalSettings != null)
            {
                if (dsApprovalSettings.Tables.Count > 0)
                {
                    if (dsApprovalSettings.Tables[0].Rows.Count > 0)
                    {
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0][strModule]) == false)
                            strStatus = "ApprovalNotRequired";
                        else
                            strStatus = "ApprovalRequired";
                    }
                    else
                        strStatus = "NotDefined";
                }
            }
        }
        else
        {
            strStatus = "ApprovalRequired";
        }
        return strStatus;
    }
    public int MonthValue(string monthName)
    {
        int monthNo = 1;
        switch (monthName)
        {
            case "January":
                monthNo = 1;
                break;
            case "February":
                monthNo = 2;
                break;
            case "March":
                monthNo = 3;
                break;
            case "April":
                monthNo = 4;
                break;
            case "May":
                monthNo = 5;
                break;
            case "June":
                monthNo = 6;
                break;
            case "July":
                monthNo = 7;
                break;
            case "August":
                monthNo = 8;
                break;
            case "September":
                monthNo = 9;
                break;
            case "October":
                monthNo = 10;
                break;
            case "November":
                monthNo = 11;
                break;
            case "December":
                monthNo = 12;
                break;
        }
        return monthNo;
    }
    #endregion
    #region Button events
    protected void btnBack_Click(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["isDashBoard"]) != "Yes")
        {
            int payrollConfig = 0;
            /*Added New parameter(Max Days/Month)for Angler. For this we are checking whether the payroll configuration page is exists or not*/
            payrollConfig = objcls_Common.CheckPageExistsInLicense(185);
            /*Below parameter is used for Angler*/
            if (payrollConfig == 0)
                Response.Redirect("../Transactions/TA_UI_Transaction_Sanctions.aspx?Type=Onduty");
            else
                Response.Redirect("../Transactions/TA_UI_Transactions_OnDutySanction.aspx");
        }
        else
        {
            Response.Redirect("../DashBoard.aspx?LevelId=" + LevelId + "&From_Date=" + From_Date + "&Shift=" + SelectedShift);
        }
    }
    #endregion
}
