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;
/****************************************************************************************************************************
* Project Name    : TimeCheck                              Started Date  : 12-Aug-2007
* Module          : Permission Sanction                    Last Modified : 14-Aug-2007
* Programmer      : Prabakaran.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_TA_UI_Transactions_PermissionSanction : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    cls_Common objClassCommon = new cls_Common();
    DateTime CurrentDate;
    DataTable dtPermissionSanctionView = new DataTable();
    PermissionEventData objPermissionEventData = new PermissionEventData();
    int month;
    int year;
    int date;
    int TotalDays;
    DataSet dsEmployeeGroup = new DataSet();
    string gstrDateFormat, strFromDateTime, strToDateTime;
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            //Todo:need to add this line into new file.
            objClassCommon.menu("PermissionTransaction", "25");
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
        }
        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);
        UserRights();
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EnterDate", ci) + "','" + rm.GetString("Alert_EnterToDate", ci) + "','" + rm.GetString("Alert_SelectStatus", 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") + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        if (!IsPostBack)
        {
            txtFromDate.Text = (Convert.ToDateTime(CurrentMonthStartDate().ToString())).ToString(Session["MDateFormat"].ToString());
            txtToDate.Text = (Convert.ToDateTime(NextMonthEndDate())).ToString(Session["MDateFormat"].ToString());
        }
    }
    public DateTime CurrentMonthStartDate()
    {
        CurrentDate = (DateTime.Now);
        CurrentDate = CurrentDate.AddDays(-(CurrentDate.AddDays(-1).Day));
        CurrentDate = CurrentDate.AddMonths(-1);
        return CurrentDate;
    }
    public DateTime NextMonthEndDate()
    {
        CurrentDate = (DateTime.Now);
        CurrentDate = CurrentDate.AddMonths(1);
        month = CurrentDate.Month;
        year = CurrentDate.Year;
        TotalDays = System.DateTime.DaysInMonth(year, month);
        date = CurrentDate.Day;
        date = TotalDays - date;
        CurrentDate = CurrentDate.AddDays(date);
        return CurrentDate;
    }
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            strToDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtToDate.Text, "D");
            DateTime dtToDate = DateTime.Parse(strToDateTime);
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
            {
                e.IsValid = true;
            }
            else
            {
                base.Message(rm.GetString("Msg_GrearterThanFrmDate", ci));
                e.IsValid = false;
            }
        }
        catch
        {
        }
    }
    public void CompareCurrentdate(object s, ServerValidateEventArgs e)
    {
    }
    public void CompareTodate(object s, ServerValidateEventArgs e)
    {
    }
    protected void btn_View_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            int LevelId = Convert.ToInt32(Session["parentLevelId"]);
            string strScript = string.Empty;
            int intCompanyId;
            string strStatus = string.Empty;
            string strModule = string.Empty;
            string strSwipeCardNo = string.Empty;
            string strEmployeeName = string.Empty;
            string strEmployeeCodeId = string.Empty;
            Session["PermissionSanctionStatus"] = ddlStatus.SelectedItem.Text.ToString();
            strSwipeCardNo = txtSwipeCard.Text;
            strEmployeeName = txt_EmpName.Text.Trim().Replace("'", "''");
            strEmployeeCodeId = txtEmployeeId.Text.Trim().Replace("'", "''");
            strScript = "";
            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) + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            intCompanyId = getParentCompany(Convert.ToInt32(Session["parentLevelId"]));
            strStatus = GetApprovalStatus(intCompanyId, strModule);
            if (strStatus == "ApprovalNotRequired")
            {
                strScript = strScript + "alert('" + rm.GetString("Alert_ApprovalNotRequiredforthiscompany", ci) + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            else if (strStatus == "ApprovalRequired")
            {
            }
            else if (strStatus == "NotDefined")
            {
                strScript = strScript + "alert('" + rm.GetString("Alert_ApprovalSettingsnotdefinedforthiscompany", ci) + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            Session["LevelId"] = LevelId;
            Session["PermissionSanctionStatus"] = Convert.ToString(ddlStatus.SelectedValue);
            Session["strSwipeCardNo"] = strSwipeCardNo;
            Session["strEmployeeName"] = strEmployeeName;
            Session["strEmployeeCodeId"] = strEmployeeCodeId;
            Session["PermissionFrom_Date"] = txtFromDate.Text;
            Session["PermissionTo_Date"] = txtToDate.Text;

            int intCount = 0;
            intCount = GetPermissionCount(Convert.ToString(LevelId), ddlStatus.SelectedItem.Text.ToString(), objClassCommon.DateConvertion(gstrDateFormat, txtFromDate.Text), objClassCommon.DateConvertion(gstrDateFormat, txtToDate.Text), strSwipeCardNo, strEmployeeName, strEmployeeCodeId);
            if (intCount == 0)
            {
                strStatus = rm.GetString("Alert_Thereisnorecord", ci);
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            else
            {
                if (ddlStatus.SelectedItem.Text == rm.GetString("btn_Forwarded", ci) || ddlStatus.SelectedItem.Text == rm.GetString("btn_Pending", ci))
                {

                    Response.Redirect("../Transactions/TA_UI_Transactions_PermissionBulkSanction.aspx?LevelId=" + LevelId + "&Status=" + Convert.ToString(ddlStatus.SelectedValue) + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                }
                else
                {
                    Response.Redirect("../Transactions/TA_UI_Transactions_PermissionSanctionView.aspx?LevelId=" + LevelId + "&Status=" + Convert.ToString(ddlStatus.SelectedValue) + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                }
            }
        }
    }
    public int GetPermissionCount(string LevelId, string Status, string From_Date, string To_Date, string strSwipeCardNo, string strEmployeeName, string strEmployeeCodeId)
    {
        int intCount = 0;
        if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
        {
            dtPermissionSanctionView = objPermissionEventData.GetPermissionSanctionDetailsForSuperAdmin(Convert.ToInt32(Session["EmpAutoID"]), LevelId, Status, From_Date, To_Date, strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), 0, 0);
            intCount = dtPermissionSanctionView.Rows.Count;
        }
        else
        {
            dtPermissionSanctionView = objPermissionEventData.GetPermissionSanctionDetails(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, Convert.ToString(From_Date), Convert.ToString(To_Date), strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), 0, 0);
            intCount = dtPermissionSanctionView.Rows.Count;
        }
        return intCount;
    }
    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 = objClassCommon.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;
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("Transaction_PermissionSanction_PermissionSanction", ci);
        lbl_From.Text = rm.GetString("email_FromDate", ci);
        lbl_Status.Text = rm.GetString("email_Status", ci);
        lbl_To.Text = rm.GetString("email_ToDate", ci);
        cus_ComDate.ErrorMessage = rm.GetString("Msg_GrearterThanFrmDate", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        lbl_EmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lbl_EmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        ddlStatus.Items[0].Text = rm.GetString("ExportData_All", ci);
        ddlStatus.Items[1].Text = rm.GetString("btn_Approved", ci);
        ddlStatus.Items[2].Text = rm.GetString("btn_Forwarded", ci);
        ddlStatus.Items[3].Text = rm.GetString("btn_Pending", ci);
        ddlStatus.Items[4].Text = rm.GetString("btn_Reject", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);

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