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          : Leave 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_LeaveSanction_Angler : Base.BasePage
{
    ResourceManager rm;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    DateTime CurrentDate;
    //Cls_Connect cl = new Cls_Connect();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    cls_Common objClassCommon = new cls_Common();
    DataSet dsEmployeeGroup = new DataSet();
    string gstrDateFormat, strFromDateTime, strToDateTime;
    int payrollConfig = 0;
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //if Standar Edition set the visibility to false
        if (Session["Edition"].ToString() == "1")
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
        payrollConfig = objClassCommon.CheckPageExistsInLicense(185);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            //Todo:need to add this line into new file.
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            if (payrollConfig == 0)
            {
                objClassCommon.menu("LeaveTransaction", "248");
            }
            else
            {
                objClassCommon.menu("LeaveTransaction", "260");
            }
        }
        //get the culture info to set the language
        CultureInfo ci;
        string source_Language;
        source_Language = Session["Session_ActiveLanguage"].ToString();
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        //Set UserRights
        UserRights();
        if (!IsPostBack)
        {
            ddlStatus.Items.Clear();
            ddlStatus.Items.Insert(0, "-Select Status-");
            DataSet ds_RecordsCountLeaveReportingTo = new DataSet();
            ds_RecordsCountLeaveReportingTo = objLeaveApplicationEventData.GetRecordsCountLeaveReportingTo(Convert.ToInt32(Session["EmpAutoId"]));
            if (Convert.ToInt32(ds_RecordsCountLeaveReportingTo.Tables[0].Rows[0]["RecordsCount"]) > 0)
            {
                //ddlStatus.Items.Clear();
                //ddlStatus.Items.Insert(0, "-Select Status-");
                //ddlStatus.Items.Insert(1, "Pending");
                //ddlStatus.Items.Insert(2, "Rejected");
                //ddlStatus.Items.Insert(3, "Resend");
                //ddlStatus.Items.Insert(4, "Reply");
                ddlStatus.Items.Clear();
                ddlStatus.Items.Insert(0, "-Select Status-");
                ddlStatus.Items.Insert(1, "All");
                ddlStatus.Items.Insert(2, "Approved");
                ddlStatus.Items.Insert(3, "Forwarded");
                ddlStatus.Items.Insert(4, "Pending");
                ddlStatus.Items.Insert(5, "Rejected");
                ddlStatus.Items.Insert(6, "Resend");
                ddlStatus.Items.Insert(7, "Reply");
                CBPending.Checked = true;
            }
            DataSet dsRecordsCount = new DataSet();
            dsRecordsCount = objLeaveApplicationEventData.GetRecordsCountLeaveSanction(Convert.ToInt32(Session["EmpAutoId"]));
            if (Convert.ToInt32(dsRecordsCount.Tables[0].Rows[0]["RecordsCount"]) > 0)
            {
                //ddlStatus.Items.Clear();
                //ddlStatus.Items.Insert(0, "-Select Status-");
                //ddlStatus.Items.Insert(1, "Approved");
                //ddlStatus.Items.Insert(2, "Forwarded");
                //ddlStatus.Items.Insert(3, "Rejected");
                //ddlStatus.Items.Insert(4, "Resend");
                //ddlStatus.Items.Insert(5, "Reply");
                ddlStatus.Items.Clear();
                ddlStatus.Items.Insert(0, "-Select Status-");
                ddlStatus.Items.Insert(1, "All");
                ddlStatus.Items.Insert(2, "Approved");
                ddlStatus.Items.Insert(3, "Forwarded");
                ddlStatus.Items.Insert(4, "Pending");
                ddlStatus.Items.Insert(5, "Rejected");
                ddlStatus.Items.Insert(6, "Resend");
                ddlStatus.Items.Insert(7, "Reply");
                CBForwarded.Checked = true;
            }
            if (Session["UserGroupName"].ToString() == "SuperAdmin" || Session["HasSpecialRights"].ToString() == "True")
            {
                ddlStatus.Items.Clear();
                ddlStatus.Items.Insert(0, "-Select Status-");
                ddlStatus.Items.Insert(1, "All");
                ddlStatus.Items.Insert(2, "Approved");
                ddlStatus.Items.Insert(3, "Forwarded");
                ddlStatus.Items.Insert(4, "Pending");
                ddlStatus.Items.Insert(5, "Rejected");
                ddlStatus.Items.Insert(6, "Resend");
                ddlStatus.Items.Insert(7, "Reply");
            }
            txtFromDate.Text = (Convert.ToDateTime(CurrentMonthStartDate().ToString())).ToString(Session["MDateFormat"].ToString());
            //txtFromDate.Text = (Convert.ToDateTime(System.DateTime.Now.AddMonths(-1).ToShortDateString())).ToString(Session["MDateFormat"].ToString().Trim());
            txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.AddMonths(1).ToShortDateString())).ToString(Session["MDateFormat"].ToString().Trim());
        }
    }
    /// <summary>
    /// Compare FromDate and ToDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            //Convert Date Format to MM/dd/yyyy Format
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            // strApplicationDate = objClassCommon.DateConvertion(gstrDateFormat, TxtPermissionDate.Text);
            //Convert Date Format to MM/dd/yyyy Format
            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("To Date should be greater than From Date");
                e.IsValid = false;
            }
        }
        catch
        {
            //
        }
    }
    /// <summary>
    /// Compare FromMonth With Current Month
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareCurrentdate(object s, ServerValidateEventArgs e)
    {
        try
        {
            //Convert Date Format to MM/dd/yyyy Format
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            // strApplicationDate = objClassCommon.DateConvertion(gstrDateFormat, TxtPermissionDate.Text);
            //Convert Date Format to MM/dd/yyyy Format
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            DateTime dtCurrentDate = DateTime.Now;
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
            //
        }
    }
    /// <summary>
    /// Compare Tomonth with Current Month
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareTodate(object s, ServerValidateEventArgs e)
    {
        try
        {
            //Convert Date Format to MM/dd/yyyy Format
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strToDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtToDate.Text, "D");
            DateTime dtCurrentDate = DateTime.Now;
            DateTime dtFromDate = DateTime.Parse(strToDateTime);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
            //
        }
    }
    protected void btn_View_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            //if (ddlStatus.SelectedItem.Text == "-Select Status-")
            // {
            string strScript = string.Empty;
            int LevelId = Convert.ToInt32(Session["parentLevelId"]);
            int intCompanyId;
            string strStatus = string.Empty;
            string strModule = string.Empty;
            string strSwipeCardNo = string.Empty;
            string strEmployeeName = string.Empty;
            string strEmployeeCodeId = string.Empty;
            strSwipeCardNo = txtSwipeCard.Text;
            strEmployeeName = txt_EmpName.Text.Trim().Replace("'", "''");
            strEmployeeCodeId = txtEmployeeId.Text.Trim().Replace("'", "''");
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            bool blnParentExists = true;
            strModule = "LeaveSanction";
            if (Session["parentLevelId"] == null)
            {
                blnParentExists = false;
            }
            else if (Convert.ToInt32(Session["parentLevelId"]) == 0)
            {
                blnParentExists = false;
            }
            if (blnParentExists == false)
            {
                strStatus = "Select a company";
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            intCompanyId = getParentCompany(Convert.ToInt32(Session["parentLevelId"]));
            strStatus = GetApprovalStatus(intCompanyId, strModule);
            if (strStatus == "ApprovalNotRequired")
            {
                //lblApprovalStatus.Text = "Approval Not Required for this company";
                strStatus = "Approval Not Required for this company";
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            else if (strStatus == "ApprovalRequired")
            {
                //continue
            }
            else if (strStatus == "NotDefined")
            {
                //lblApprovalStatus.Text = "Approval Settings not defined for this company";
                strStatus = "Approval Settings not defined for this company";
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            string strStatus1 = "";
            strStatus1 = CbApproved.Checked == true ? "Approved," : strStatus1;
            strStatus1 = CBPending.Checked == true ? strStatus1 + "Pending," : strStatus1;
            strStatus1 = CBResend.Checked == true ? strStatus1 + "Resend," : strStatus1;
            strStatus1 = CBForwarded.Checked == true ? strStatus1 + "Forwarded," : strStatus1;
            strStatus1 = CBRejected.Checked == true ? strStatus1 + "Rejected," : strStatus1;
            strStatus1 = CBReply.Checked == true ? strStatus1 + "Reply," : strStatus1;
            strStatus1 = strStatus1.Length > 0 ? strStatus1.Substring(0, strStatus1.Length - 1) : strStatus1;
            strStatus1 = strStatus1 == "" ? "Approved,Pending,Resend,Forwarded,Rejected,Reply" : strStatus1;
            //Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionView.aspx?LevelId=" + LevelId + "&Status=" + ddlStatus.SelectedValue + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
            //Added by Manjula.P on 25-Apr-2014 for Bulk approval on Leave
            if (payrollConfig == 0)
            {
                if (strStatus1 == "Forwarded" || strStatus1 == "Pending")
                {
                    //Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionView.aspx?LevelId=" + LevelId + "&Status=" + strStatus1 + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                    Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionViewForBulkApproval.aspx?LevelId=" + LevelId + "&Status=" + strStatus1 + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                }
                else
                {
                    Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionView.aspx?LevelId=" + LevelId + "&Status=" + strStatus1 + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                }
            }
            else
            {
                if (strStatus1 == "Forwarded" || strStatus1 == "Pending")
                {
                    //Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionView.aspx?LevelId=" + LevelId + "&Status=" + strStatus1 + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                    Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionViewForBulkApproval_Angler.aspx?LevelId=" + LevelId + "&Status=" + strStatus1 + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                }
                else
                {
                    Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionView_Angler.aspx?LevelId=" + LevelId + "&Status=" + strStatus1 + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);
                }
            }
            // }
        }
    }
    public int getParentCompany(int iEmployeeLevelID)
    {
        //Select Company
        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)
    {
        //Get Approval Settings Based on Company Id
        DataSet dsApprovalSettings = new DataSet();
        ShiftEvenData objShift = new ShiftEvenData();
        string strStatus = string.Empty;
        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";
                }
            }
        }
        return strStatus;
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo 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);
        //CustomValidator1.ErrorMessage = rm.GetString("Alert_SelectvalidFromDate", ci);
        cus_ComDate.ErrorMessage = rm.GetString("Msg_GrearterThanFrmDate", ci);
        //CustomValidator2.ErrorMessage = rm.GetString("Alert_SelectvalidToDate", ci);
        lbl_Header.Text = rm.GetString("Menu_Transactions_LeaveSanction", 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);
        //lblTimePeriod.Text = "Date Range";
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(260, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
        else
        {
            //   
        }
    }
    public DateTime CurrentMonthStartDate()
    {
        CurrentDate = (DateTime.Now);
        CurrentDate = CurrentDate.AddDays(-(CurrentDate.AddDays(-1).Day));
        CurrentDate = CurrentDate.AddMonths(-1);
        return CurrentDate;
    }
}
