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_View : 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();
    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;
    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 source_Language;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        gdLeaveSanctionView.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        //get the culture info to set the 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);
        if (!Page.IsPostBack)
        {
            //Todo:need to add this line into new file.
            //objcls_Common.menu("LeaveTransaction", "24");
            objcls_Common.menu("Approvals", "248");
        }
        //Set UserRights
        UserRights();
        if (Session["LevelId"] == string.Empty || Session["LevelId"] == null)
        {
            LevelId = Request.QueryString["LevelId"].ToString();
            LevelId = rptcommon.getNodes(Convert.ToInt16(LevelId), Convert.ToString(Session["LevelRights"]));
            Session["LevelId"] = LevelId;
        }
        else
        {
            LevelId = Convert.ToString(Session["LevelId"]);
        }

        if (Session["LeaveSanctionStatus"] == string.Empty || Session["LeaveSanctionStatus"] == null)
        {
            Status = Request.QueryString["Status"].ToString();
            Session["LeaveSanctionStatus"] = Status;
        }
        else
        {
            Status = Convert.ToString(Session["LeaveSanctionStatus"]);
        }
        if (Convert.ToString(Request.QueryString["isNotification"]) != null)
        {
            Session["LevelId"] = Convert.ToString(Request.QueryString["LevelId"]);
            Status = Request.QueryString["Status"].ToString();
            Session["LeaveSanctionStatus"] = Status;
        }
        if (Request.QueryString["Shift"] != null)
        {
            SelectedShift = Convert.ToString(Request.QueryString["Shift"]);
        }
        if (Session["strSwipeCardNo"] == string.Empty || Session["strSwipeCardNo"] == null)
        {
            if (Request.QueryString["strSwipeCardNo"] != null)
            {
                strSwipeCardNo = Request.QueryString["strSwipeCardNo"].ToString();
            }
            else
            {
                strSwipeCardNo = "";
            }
            Session["strSwipeCardNo"] = strSwipeCardNo;
        }
        else
        {
            strSwipeCardNo = Convert.ToString(Session["strSwipeCardNo"]);
        }
        if (Session["strEmployeeName"] == string.Empty || Session["strEmployeeName"] == null)
        {
            if (Request.QueryString["strEmployeeName"] != null)
            {
                strEmployeeName = Request.QueryString["strEmployeeName"].ToString();
            }
            else
            {
                strEmployeeName = "";
            }
            Session["strEmployeeName"] = strEmployeeName;
        }
        else
        {
            strEmployeeName = Convert.ToString(Session["strEmployeeName"]);
        }
        if (Session["strEmployeeCodeId"] == string.Empty || Session["strEmployeeCodeId"] == null)
        {
            if (Request.QueryString["strEmployeeName"] != null)
            {
                strEmployeeCodeId = Request.QueryString["strEmployeeCodeId"].ToString();
            }
            else
            {
                strEmployeeCodeId = "";
            }
            Session["strEmployeeCodeId"] = strEmployeeCodeId;
        }
        else
        {
            strEmployeeCodeId = Convert.ToString(Session["strEmployeeCodeId"]);
        }
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        if (Session["LeaveFrom_Date"] == null || Session["LeaveFrom_Date"] == string.Empty)
        {
            if (Request.QueryString["From_Date"] != null)
            {
                From_Date = Request.QueryString["From_Date"].ToString();
            }
            else
            {
                From_Date = "";
            }
            Session["LeaveFrom_Date"] = From_Date;
        }
        else
        {
            From_Date = Convert.ToString(Session["LeaveFrom_Date"]);
        }
        if (Session["LeaveTo_Date"] == null || Session["LeaveTo_Date"] == string.Empty)
        {
            if (Request.QueryString["To_Date"] != null)
            {
                To_Date = Request.QueryString["To_Date"].ToString();
            }
            else
            {
                To_Date = "";
            }
            Session["LeaveTo_Date"] = To_Date;
        }
        else
        {
            To_Date = Convert.ToString(Session["LeaveTo_Date"]);
        }
        if (From_Date != "")
        {
            From_Date = objcls_Common.DateConvertion(gstrDateFormat, From_Date);
        }
        if (To_Date != "")
        {
            To_Date = objcls_Common.DateConvertion(gstrDateFormat, To_Date);
        }
        dsYearSetting = objYearSettingsEventData.GetActiveYearSettings();
        if (dsYearSetting.Tables[0].Rows.Count <= 0)
        {
            return;
        }
        else
        {
            StartMonth = dsYearSetting.Tables[0].Rows[0]["StartMonth"].ToString();
            StartYear = dsYearSetting.Tables[0].Rows[0]["StartYear"].ToString();
            vmonthValue = MonthValue(StartMonth);
            startDate = vmonthValue.ToString() + "/1/" + StartYear;
            EndMonth = dsYearSetting.Tables[0].Rows[0]["EndMonth"].ToString();
            EndYear = dsYearSetting.Tables[0].Rows[0]["EndYear"].ToString();
            vmonthValue = MonthValue(EndMonth);
            endDate = vmonthValue.ToString() + "/31/" + EndYear;
        }
        if (LevelId == "0")
        {
            LevelId = null;
        }
        if (Status == "-Select Status-")
        {
            Status = null;
        }
        else if (Status == "All")
        {
            Status = "All";
        }
        if (From_Date == "")
        {
            From_Date = startDate;
        }
        if (To_Date == "")
        {
            To_Date = endDate;
        }
        bindLeaveSanctionDetails();
    }
    #endregion
    #region User Defined Functions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("Menu_Transactions_LeaveSanction", ci);
        btnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        gdLeaveSanctionView.Columns[0].Caption = rm.GetString("email_EmployeeId", ci);
        gdLeaveSanctionView.Columns[1].Caption = rm.GetString("email_EmployeeName", ci);
        gdLeaveSanctionView.Columns[2].Caption = rm.GetString("LeaveApplication_LeaveApplicationDate", ci);
        gdLeaveSanctionView.Columns[3].Caption = rm.GetString("Report_Duration", ci);
        gdLeaveSanctionView.Columns[4].Caption = rm.GetString("email_Status", ci);
    }
    public int MonthValue(string monthName)
    {
        if (monthName == "January")
            return 1;
        else if (monthName == "February")
            return 2;
        else if (monthName == "March")
            return 3;
        else if (monthName == "April")
            return 4;
        else if (monthName == "May")
            return 5;
        else if (monthName == "June")
            return 6;
        else if (monthName == "July")
            return 7;
        else if (monthName == "August")
            return 8;
        else if (monthName == "September")
            return 9;
        else if (monthName == "October")
            return 10;
        else if (monthName == "November")
            return 11;
        else if (monthName == "December")
            return 12;
        else
            return 0;
    }
    public void SortGrid(string SortColumn, string SortOrder)
    {
        DataTable dtSrch = new DataTable();
        DataView dvSearch = new DataView();
        dtSrch = (DataTable)ViewState["DataLeaveSanction"];
        dvSearch.Table = dtSrch;
        dvSearch.Sort = SortColumn + " " + SortOrder;
        gdLeaveSanctionView.DataSource = dvSearch;
        gdLeaveSanctionView.DataBind();
    }
    public void GetEmptyText(object sender, EventArgs e)
    {
        Label lblNoRecords = (Label)sender;
        lblNoRecords.Text = rm.GetString("Alert_Norecordsfound", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(248, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
        else
        {
            //   
        }
    }
    public void bindLeaveSanctionDetails()
    {
        //To check the user is in SuperAdmin Group
        if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
        {
            dtLeaveTransactionView = objLeaveApplicationEventData.GetLeaveSanctionDetailsForSuperAdmin(Convert.ToInt32(Session["EmpAutoID"]), LevelId, Status, From_Date, To_Date, strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), 0, 0);
            gdLeaveSanctionView.DataSource = dtLeaveTransactionView;
            ViewState["DataLeaveSanction"] = dtLeaveTransactionView;
            gdLeaveSanctionView.DataBind();
        }
        else
        {
            dtLeaveTransactionView = objLeaveApplicationEventData.GetLeaveSanctionDetails(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, Convert.ToDateTime(From_Date), Convert.ToDateTime(To_Date), strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), 0, 0);
            gdLeaveSanctionView.DataSource = dtLeaveTransactionView;
            ViewState["DataLeaveSanction"] = dtLeaveTransactionView;
            gdLeaveSanctionView.DataBind();
        }
    }
    public void clearAllSession()
    {
        Session["LevelId"] = string.Empty;
        Session["LeaveSanctionStatus"] = string.Empty;
        Session["strSwipeCardNo"] = string.Empty;
        Session["strEmployeeName"] = string.Empty;
        Session["strEmployeeCodeId"] = string.Empty;
        Session["LeaveFrom_Date"] = string.Empty;
        Session["LeaveTo_Date"] = string.Empty;
    }
    #endregion
    #region GridviewEvents
    protected void gdLeaveSanctionView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = (Label)e.Row.Cells[1].FindControl("Label1");
            string date1 = (Convert.ToDateTime(lbl.Text)).ToString(Session["MDateFormat"].ToString());
            lbl.Text = date1;
            e.Row.Cells[1].Controls.Add(lbl);
            Label lbldur = (Label)e.Row.Cells[1].FindControl("Label3");
            string[] duration = lbldur.Text.Split(" - ".ToCharArray());
            string datedur = (Convert.ToDateTime(duration[0])).ToString(Session["MDateFormat"].ToString());
            string datedur1 = (Convert.ToDateTime(duration[3])).ToString(Session["MDateFormat"].ToString());
            lbldur.Text = datedur + " - " + datedur1;
            e.Row.Cells[2].Controls.Add(lbldur);
            Label lblStatus = (Label)e.Row.Cells[1].FindControl("Label1");
            if (lblStatus.Text == "Approved")
            {
                lblStatus.Text = rm.GetString("btn_Approved", ci);
            }
            else if (lblStatus.Text == "Forwarded")
            {
                lblStatus.Text = rm.GetString("btn_Forwarded", ci);
            }
            else if (lblStatus.Text == "Pending")
            {
                lblStatus.Text = rm.GetString("btn_Pending", ci);
            }
            else if (lblStatus.Text == "Rejected")
            {
                lblStatus.Text = rm.GetString("btn_Reject", ci);
            }
        }
    }
    protected void gdLeaveSanctionView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdLeaveSanctionView.PageIndex = e.NewPageIndex;
        bindLeaveSanctionDetails();
    }
    protected void gdLeaveSanctionView_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["SortExpression"] = e.SortExpression;
        if (Convert.ToString(Session["SortOrderLeaveSanction"]) == string.Empty)
        {
            Session["SortOrderLeaveSanction"] = "Asc";
        }
        else if (Convert.ToString(Session["SortOrderLeaveSanction"]) == "Asc")
        {
            Session["SortOrderLeaveSanction"] = "Desc";
        }
        else
        {
            Session["SortOrderLeaveSanction"] = "Asc";
        }
        SortGrid(e.SortExpression, Convert.ToString(Session["SortOrderLeaveSanction"]));
    }
    protected void gdLeaveSanctionView_PageIndexChanged(object sender, EventArgs e)
    {
        try
        {
            // To display the sorted records in the new page if the Vendor has Sorted the Gridview by clicking any header
            if ((ViewState["SortExpression"]) != null)
            {
                string SortColumn = Convert.ToString(ViewState["SortExpression"]);
                string SortOrder = Convert.ToString(Session["SortOrderLeaveSanction"]);
                SortGrid(SortColumn, SortOrder);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void gdLeaveSanctionView_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            LoadStrings(ci);
            Label lbl = (Label)gdLeaveSanctionView.FindRowCellTemplateControl(e.VisibleIndex, null, "Label1");
            string date1 = (Convert.ToDateTime(lbl.Text)).ToString(Session["MDateFormat"].ToString());
            lbl.Text = date1;
            e.Row.Cells[1].Controls.Add(lbl);
            Label lbldur = (Label)gdLeaveSanctionView.FindRowCellTemplateControl(e.VisibleIndex, null, "Label3");
            string[] duration = lbldur.Text.Split(" - ".ToCharArray());
            string datedur = (Convert.ToDateTime(duration[0])).ToString(Session["MDateFormat"].ToString());
            string datedur1 = (Convert.ToDateTime(duration[3])).ToString(Session["MDateFormat"].ToString());
            lbldur.Text = datedur + " - " + datedur1;
            e.Row.Cells[2].Controls.Add(lbldur);
            Label lblStatus = (Label)e.Row.Cells[1].FindControl("Label1");
            if (lblStatus.Text == "Approved")
            {
                lblStatus.Text = rm.GetString("btn_Approved", ci);
            }
            else if (lblStatus.Text == "Forwarded")
            {
                lblStatus.Text = rm.GetString("btn_Forwarded", ci);
            }
            else if (lblStatus.Text == "Pending")
            {
                lblStatus.Text = rm.GetString("btn_Pending", ci);
            }
            else if (lblStatus.Text == "Rejected")
            {
                lblStatus.Text = rm.GetString("btn_Reject", ci);
            }
        }
    }
    #endregion
    #region ButtonEvents
    protected void btnBack_Click(object sender, EventArgs e)
    {
        clearAllSession();
        if (Convert.ToString(Session["isDashBoard"]) == "Yes")
        {
            if (Convert.ToString(Session["packageID"]) != "")
            {
                if (Convert.ToInt32(Session["packageID"]) == 168)  //Samsung
                    Response.Redirect("../Dashboard_Samsung.aspx?LevelId=" + LevelId + "&From_Date=" + From_Date + "&Shift=" + SelectedShift, true);
            }
            Response.Redirect("../DashBoard.aspx?LevelId=" + LevelId + "&From_Date=" + From_Date + "&Shift=" + SelectedShift, true);
        }
        else
        {
            Session["SanctionType"] = null;
            Response.Redirect("TA_UI_Transaction_Sanctions.aspx?Type=Leave");
        }
    }
    #endregion
}
