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.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
/*
 * Modified By      :   Manjula.P
 * Modified Date    :   19-Jul-2013
 * Descriptions     :   for getting first year leave quota
 * */
public partial class Transactions_PopUp_EmployeeLeaveQuota : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    cls_Common objClassCommon = new cls_Common();
    ShiftAllocationEvendata objShiftAllocation = new ShiftAllocationEvendata();
    int EmployeeId;
    int intYearSettingId = 0;
    string strToDate = string.Empty;
    string TimeFormat = string.Empty;

    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        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);
        loadcss();
        if (!IsPostBack)
        {
            EmployeeId = Convert.ToInt32(Request.QueryString["EmpId"]);
            string gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strToDate = objClassCommon.DateConvertion(gstrDateFormat, Convert.ToString(Request.QueryString["ToDate"]));

            if (strToDate != string.Empty)
                strToDate = DateTime.Parse(strToDate).ToShortDateString();
            LoadLeaves();
            //Based on the organization settings, the LOP and Other Board in leave application will be displayed to users.
            DataSet dsEmp = new DataSet();
            dsEmp = objShiftAllocation.GetCompanyDetailsUsingEmpCodeForShiftSettings(Convert.ToString(EmployeeId));
           
            if (dsEmp != null)
            {
                if (dsEmp.Tables.Count > 0)
                {
                    if (dsEmp.Tables[0].Rows.Count > 0)
                    {
                        if (Convert.ToInt64(dsEmp.Tables[0].Rows[0]["isLOPDisplayed"]) == 1)
                        {
                            divLOP.Style.Add("display", "");
                            divOthers.Style.Add("display", "");
                            LoadLOPLeaves();
                            LoadRestrictedHolidays();
                        }
                        else
                        {
                            divLOP.Style.Add("display", "none");
                            divOthers.Style.Add("display", "none");
                        }
                    }
                    else
                    {
                        divLOP.Style.Add("display", "none");
                        divOthers.Style.Add("display", "none");
                    }
                }
                else
                {
                    divLOP.Style.Add("display", "none");
                    divOthers.Style.Add("display", "none");
                }
            }
            else
            {
                divLOP.Style.Add("display", "none");
                divOthers.Style.Add("display", "none");
            }
            LoadCompOffDays();
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lblLeave.Text = rm.GetString("MonthClosure_LeaveDays", ci);
        grvLeaveQuota.Columns[0].HeaderText = rm.GetString("lbl_leavetype", ci);
        grvLeaveQuota.Columns[1].HeaderText = rm.GetString("LeaveSanction_BalanceUpto", ci);
        lblLOPLeave.Text = rm.GetString("Caption_LOPLeaveDays", ci);
        grvLOPLeave.Columns[0].HeaderText = rm.GetString("lbl_leavetype", ci);
        grvLOPLeave.Columns[1].HeaderText = rm.GetString("LeaveSanction_BalanceUpto", ci);
        lblRestrictedHoliday.Text = rm.GetString("Holiday_RestrictedHoliday", ci);
        lblCompOff.Text = rm.GetString("Caption_CompOffDays", ci);
        lblbalance.Text = rm.GetString("lbl_BalanceLeaveDetails", ci);
        lblOtherDetails.Text = rm.GetString("lbl_Otherdetails", ci);
    }

    public void LoadLeaves()
    {
        DataSet dsRemainingNoOfLeaves = new DataSet();
        if (Request.QueryString["ApplicationYearsetting"] != string.Empty && Request.QueryString["ApplicationYearsetting"] != null)
        {
            intYearSettingId = Convert.ToInt32(Request.QueryString["ApplicationYearsetting"].ToString());
        }
        if (intYearSettingId != 0)
        {
            dsRemainingNoOfLeaves = objLeaveApplicationEventData.GetRemainingNumberOfLeavesForLeaveApplication(EmployeeId, -1, intYearSettingId);
            if (dsRemainingNoOfLeaves.Tables.Count > 0)
            {
                if (dsRemainingNoOfLeaves.Tables[0].Rows.Count > 0)
                {
                    if (Convert.ToString(Session["packageID"]) != "")
                    {
                        if (Convert.ToInt32(Session["packageID"]) == 169)  //MTTS
                        {
                            ///Implemented this newly
                            ///As per the marital status in employee master, we need to show PL and ML, WL(wedding leave)
                            ///i.e single means, we need to load wl instead of ml and pl
                            ///at that time married means, we need to display pl and ml leave type instead of wl.
                            DataSet ds_Empdetails = new DataSet();
                            EmployeeEventData objEmployee = new EmployeeEventData();
                            ds_Empdetails = objEmployee.Select_EmployeeDetailsbyId(EmployeeId);
                            if (ds_Empdetails.Tables.Count > 0)
                            {
                                if (ds_Empdetails.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_Empdetails.Tables[0].Rows[0]["Marital_status"] != DBNull.Value && Convert.ToString(ds_Empdetails.Tables[0].Rows[0]["Marital_status"]) != "")
                                    {
                                        if (Convert.ToString(ds_Empdetails.Tables[0].Rows[0]["Marital_status"]) == "Single")
                                        {
                                            RemoveParticularLeaveType(dsRemainingNoOfLeaves, "PL", "ML");
                                        }
                                        else if (Convert.ToString(ds_Empdetails.Tables[0].Rows[0]["Marital_status"]) == "Married")
                                        {
                                            RemoveParticularLeaveType(dsRemainingNoOfLeaves, "WL");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    grvLeaveQuota.DataSource = dsRemainingNoOfLeaves.Tables[0];
                    Session["grvLeaveQuota"] = dsRemainingNoOfLeaves.Tables[0];
                    grvLeaveQuota.DataBind();
                }
            }
            //Adding by MAnjula.P on 19-Jul-2013 for getting first year leave quota.
            //DataSet dsFirstYearLeaveQuota = objLeaveApplicationEventData.LoadFirstYearLeaveQuota(intYearSettingId, EmployeeId);
            //if (dsFirstYearLeaveQuota != null)
            //{
            //    if (dsFirstYearLeaveQuota.Tables[0].Rows.Count > 0)
            //    {
            //        if (Convert.ToInt32(dsFirstYearLeaveQuota.Tables[0].Rows[0]["Id"]) > 0)
            //        {
            //            grvLeaveQuota.DataSource = dsFirstYearLeaveQuota.Tables[0];
            //            Session["grvLeaveQuota"] = dsFirstYearLeaveQuota.Tables[0];
            //            grvLeaveQuota.DataBind();
            //        }
            //    }
            //}
        }
    }

    private static void RemoveParticularLeaveType(DataSet dsRemainingNoOfLeaves, params string[] leavetype)
    {
        for (int i = dsRemainingNoOfLeaves.Tables[0].Rows.Count - 1; i >= 0; i--)
        {
            DataRow dr = dsRemainingNoOfLeaves.Tables[0].Rows[i];
            for (int j = 0; j < leavetype.Length; j++)
            {
                if (Convert.ToString(dr["Leave Type"]) == leavetype[j])
                {
                    dr.Delete();
                    dsRemainingNoOfLeaves.AcceptChanges();
                    break;
                }
            }
        }
    }

    public void LoadLOPLeaves()
    {
        DataSet dsRemainingNoOfLOPLeaves = new DataSet();
        dsRemainingNoOfLOPLeaves = objLeaveApplicationEventData.GetRemainingNoOfLOP(EmployeeId, -1, DateTime.Now.ToString(), DateTime.Now.ToString(), intYearSettingId);
        if (dsRemainingNoOfLOPLeaves.Tables.Count > 0)
        {
            if (dsRemainingNoOfLOPLeaves.Tables[0].Rows.Count > 0)
            {
                grvLOPLeave.DataSource = dsRemainingNoOfLOPLeaves.Tables[0];
                Session["grvLOPLeave"] = dsRemainingNoOfLOPLeaves.Tables[0];
                grvLOPLeave.DataBind();
            }
        }
    }
    public void LoadRestrictedHolidays()
    {
        DataSet dsRemainingNoOfRestrictedHolidays = new DataSet();
        dsRemainingNoOfRestrictedHolidays = objLeaveApplicationEventData.GetNumberOfHolidaysTaken(EmployeeId, -1, intYearSettingId);
        if (dsRemainingNoOfRestrictedHolidays.Tables.Count > 0)
        {
            if (dsRemainingNoOfRestrictedHolidays.Tables[0].Rows.Count > 0)
            {
                lblRestrictedHolidayData.Text = dsRemainingNoOfRestrictedHolidays.Tables[0].Rows[0]["NumberOfDays"].ToString();
            }
        }
    }
    public void LoadCompOffDays()
    {
        DataSet dsRemainingNoOfCompOffDays = new DataSet();
        dsRemainingNoOfCompOffDays = objLeaveApplicationEventData.GetCompOffDays(EmployeeId, intYearSettingId, strToDate);
        if (dsRemainingNoOfCompOffDays.Tables.Count > 0)
        {
            if (dsRemainingNoOfCompOffDays.Tables[0].Rows.Count > 0)
            {
                lblCompensationOffData.Text = dsRemainingNoOfCompOffDays.Tables[0].Rows[0]["CompOffDays"].ToString();
            }
        }
    }
    protected void grvLeaveQuota_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["grvLeaveQuota"];
        grvLeaveQuota.DataSource = dt;
        grvLeaveQuota.PageIndex = e.NewPageIndex;
        grvLeaveQuota.DataBind();
    }
    protected void grvLeaveQuota_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
            if (Session["grvLeaveQuota"] != null)
            {
                string sort = e.SortExpression;
                if (gridviewsortdirection == SortDirection.Ascending)
                {
                    gridviewsortdirection = SortDirection.Descending;
                    sortgridview(sort, "DESC");
                }
                else
                {
                    gridviewsortdirection = SortDirection.Ascending;
                    sortgridview(sort, "ASC");
                }
            }
        }
        catch (Exception ex)
        {
            // scriptmanager.scriptmanagermessage(this.Page, ex.Message);
        }
    }
    protected void grvLOPLeave_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["grvLOPLeave"];
        grvLOPLeave.DataSource = dt;
        grvLOPLeave.PageIndex = e.NewPageIndex;
        grvLOPLeave.DataBind();
    }
    protected void grvLOPLeave_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
            if (Session["grvLOPLeave"] != null)
            {
                string sort = e.SortExpression;
                if (gridviewsortdirection == SortDirection.Ascending)
                {
                    gridviewsortdirection = SortDirection.Descending;
                    sortgridview1(sort, "DESC");
                }
                else
                {
                    gridviewsortdirection = SortDirection.Ascending;
                    sortgridview1(sort, "ASC");
                }
            }
        }
        catch (Exception ex)
        {
            //scriptmanager.scriptmanagermessage(this.Page, ex.Message);
        }
    }
    public SortDirection gridviewsortdirection
    {
        get
        {
            if (ViewState["sortdirection"] == null)
            {
                ViewState["sortdirection"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["sortdirection"];
        }
        set
        {
            ViewState["sortdirection"] = value;
        }
    }
    public void sortgridview(string sortexpression, string direction)
    {
        DataTable dt = new DataTable();
        dt = Session["grvLeaveQuota"] as DataTable;
        DataView dv = new DataView(dt);
        dv.Sort = sortexpression + " " + direction;
        Session["grvLeaveQuota"] = dv.ToTable();
        grvLeaveQuota.DataSource = dv;
        grvLeaveQuota.DataBind();
    }
    public void sortgridview1(string sortexpression, string direction)
    {
        DataTable dt = new DataTable();
        dt = Session["grvLOPLeave"] as DataTable;
        DataView dv = new DataView(dt);
        dv.Sort = sortexpression + " " + direction;
        Session["grvLOPLeave"] = dv.ToTable();
        grvLOPLeave.DataSource = dv;
        grvLOPLeave.DataBind();
    }

    private void loadcss()
    {
        CultureInfo ci;
        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;
        //if Standard Demo
        htmlEmpLeavePop.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
        else
        {
            //htmlEmpLeavePop.Attributes.Add("dir", "rtl");
            //stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            //stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            //styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            //stylecustom.Href = "../dist/css/custom-rtl.css";
            //stylefonts.Href = "../dist/css/fonts.css";
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
    }

    //protected void OnDataBound(object sender, EventArgs e)
    //{
    //    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    //    TableHeaderCell cell = new TableHeaderCell();
    //    Label lbl1 = new Label();
    //    cell = new TableHeaderCell();
    //    cell.ColumnSpan = 2;
    //    cell.Style.Add("border-style", "none");
    //    //cell.CssClass = "h3";
    //    cell.Text = "<h3>Balance Leave Details</h3>";
    //    row.Controls.Add(cell);
    //    grvLeaveQuota.HeaderRow.Parent.Controls.AddAt(0, row);
    //}
}
