using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserControls_TA_uc_ReportFilter : System.Web.UI.UserControl
{
    #region Variable Declaration
    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_master_levels class1 = new cls_master_levels();
    DataSet dsLevels = new DataSet();
    ResourceManager rm;
    CultureInfo ci;
    int TotalLevelCount = 0;
    EmployeeEventData objEmployee = new EmployeeEventData();
    string strFromdate, gstrDateFormat, strTodate;
    int intIndividualUser = 0;
    #endregion
    #region Page Load
    protected void Page_Load(object sender, EventArgs e)
    {
        //  if (!IsPostBack)
        //  {
        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);
        if (Session["UserGroupName"] != null)
        {
            dsLevels = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
        }
        int MaxLevelID = 0;
        //if (Session["orgLevelid"] != null)
        //{
        //    MaxLevelID = Convert.ToInt32(Session["orgLevelid"]);
        //}
        //else
        //{
        MaxLevelID = class1.getMaxLevel();
        //}
        DataSet ds_ParentLevels = new DataSet();
        objCommon.GetParentLevels_FromLevelID(MaxLevelID);
        ds_ParentLevels = objCommon.GetParentLevels();
        if (ds_ParentLevels.Tables[0].Rows.Count > 0)
        {
            //if (Session["orgLevelid"] != null)
            //{
            //    TotalLevelCount = (ds_ParentLevels.Tables[0].Rows.Count) - 1;
            //}
            //else
            //{
            TotalLevelCount = ds_ParentLevels.Tables[0].Rows.Count;
            //}
        }
        else
        {
        }
        int i;
        pnlLevel.Controls.Clear();
        for (i = 1; i <= TotalLevelCount; i++)
        {
            Literal ltSpaceText = new Literal();
            if (i % 2 == 1)
            {
                ltSpaceText.Text = "<tr>";
            }
            else
            {
                ltSpaceText.Text = "";
            }
            pnlLevel.Controls.Add(ltSpaceText);
            Label lbl = new Label();
            lbl.Text = "<td class='td'>" + ds_ParentLevels.Tables[0].Rows[i - 1]["LevelName"].ToString() + " : </td><td class='td'>";
            lbl.Width = 129;
            //if (i % 2 == 0)
            //{
            //    lbl.Width = 105;
            //    lbl.Style.Add("padding-left", "16px");
            //}
            pnlLevel.Controls.Add(lbl);
            CreateDropDownList(Convert.ToString(i), i);
            if (i % 2 == 0)
            {
                Literal ltText = new Literal();
                ltText.Text = "</td></tr>";
                pnlLevel.Controls.Add(ltText);
            }
            else
            {
                Literal ltText = new Literal();
                ltText.Text = "</td>";
                pnlLevel.Controls.Add(ltText);
            }
        }
        if (TotalLevelCount % 2 == 1)
        {
            Literal ltSpaceTextEnd = new Literal();
            ltSpaceTextEnd.Text = "<td class='td'></td><td class='td'></td></tr>";
            pnlLevel.Controls.Add(ltSpaceTextEnd);
        }
        if (!IsPostBack)
        {
            intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
            if (intIndividualUser >= 1)
            {
                trEmployeeCategoryType.Visible = false;
                trEmployeeDesGender.Visible = false;
                trEmployeenameId.Visible = false;
            }
            BindingEmployeeCategoryandType();
            FillDesignation();
            ddlDesignation.PreRender += LB_PreRender;
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            txtFromDate.Text = (Convert.ToDateTime(objCommon.CurrentMonthStartDate())).ToString(Session["MDateFormat"].ToString());
            txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
        }
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strFromdate = txtFromDate.Text;
        strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        strTodate = txtToDate.Text;
        strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
        //   }
    }
    #endregion
    #region User Defined functions
    private void LoadStrings(CultureInfo ci)
    {
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        // lblTimePeriod.Text = rm.GetString("Reports_TimePeriod", ci);
        lblFrom.Text = rm.GetString("email_FromDate", ci);
        lblTo.Text = rm.GetString("email_ToDate", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        reqFromDate.ErrorMessage = rm.GetString("Alert_PleaseselectFromDate", ci);
        reqToDate.ErrorMessage = rm.GetString("Alert_PleaseselectToDate", ci);
        lblDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lblGender.Text = rm.GetString("EmployeeMaster_Gender", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        ddlEmpCatagory.Items[0].Text = rm.GetString("Select", ci);
        ddlEmpType.Items[0].Text = rm.GetString("Select", ci);
    }
    private void BindingEmployeeCategoryandType()
    {
        // employee category binding
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
            foreach (ListItem item in ddlEmpCatagory.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        // employee type binding
        DataSet dsType = new DataSet();
        dsType = objEmployee.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
            foreach (ListItem item in ddlEmpType.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    private void FillDesignation()
    {
        DataSet ds_designation = new DataSet();
        ds_designation = objEmployee.select_CommonDesignation();
        if (ds_designation != null)
        {
            if (ds_designation.Tables[0].Rows.Count > 0)
            {
                ddlDesignation.DataSource = ds_designation;
                ddlDesignation.DataTextField = "TitleName";
                ddlDesignation.DataValueField = "TitleId";
                ddlDesignation.DataBind();
            }
            ds_designation.Dispose();
            foreach (ListItem item in ddlDesignation.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    private void LB_PreRender(object sender, System.EventArgs e)
    {
        foreach (ListItem item in ddlDesignation.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    /// <summary>
    /// Validate the FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateFromDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            //strCurrentDate = objCommon.DateConvertion(gstrDateFormat,DateTime.Today.Date.ToString());
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            //strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            //DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date.ToString("MM/dd/yyyy")); ;
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
            {
                //  base.Message(rm.GetString("Alert_EnterValidFromDate", ci));
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_EnterValidFromDate", ci) + "');", true);
                e.IsValid = false;
            }
        }
        catch
        {
            //
        }
    }
    /// <summary>
    /// Validate the ToDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateToDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            //strCurrentDate = objCommon.DateConvertion(gstrDateFormat, DateTime.Today.Date.ToString());
            strTodate = txtToDate.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            //DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date.ToString("MM/dd/yyyy"));
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
            {
                //  base.Message(rm.GetString("Alert_EnterValidToDate", ci));
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_EnterValidToDate", ci) + "');", true);
                e.IsValid = false;
            }
        }
        catch
        {
            //
        }
    }
    /// <summary>
    /// Compare ToDate with FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            strTodate = txtToDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtToDate = DateTime.Parse(strTodate);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
            {
                //    base.Message(rm.GetString("Msg_SelectvaliddatecomparewithFromdate", ci));
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_SelectvaliddatecomparewithFromdate", ci) + "');", true);
                e.IsValid = false;
            }
        }
        catch
        {
            //
        }
    }
    /// <summary>
    /// To create the drop down dynamically
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Count"></param>
    private void CreateDropDownList(string ID, int Count)
    {
        DropDownList ddl = new DropDownList();
        ddl.ID = ID;
        if (Count == 1)
        {
            DataTable dtFirstLevel = new DataTable();
            dsLevels.Tables[0].DefaultView.RowFilter = "ParentId = 0";
            dtFirstLevel = dsLevels.Tables[0].DefaultView.ToTable();
            ddl.DataSource = dtFirstLevel;
            ddl.DataValueField = "id";
            ddl.DataTextField = "name";
            ddl.DataBind();
        }
        ddl.Items.Insert(0, rm.GetString("Select", ci));
        ddl.CssClass = "dlist";
        ddl.Width = 185;
        ddl.AutoPostBack = true;
        ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        pnlLevel.Controls.Add(ddl);
        Literal ltText = new Literal();
        ltText.Text = "&nbsp;&nbsp;";
        pnlLevel.Controls.Add(ltText);
        foreach (ListItem item in ddl.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    /// <summary>
    /// Event handler on drop down selected index changed.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        string DropDownID = ddl.ID;
        if (ddl.SelectedIndex != 0)
        {
            int SelectedValue = Convert.ToInt32(ddl.SelectedValue);
            Session["parentLevelId"] = SelectedValue;
            LoadDropDown(Convert.ToInt32(DropDownID), SelectedValue);
        }
        else
        {
            LoadDropDown(Convert.ToInt32(DropDownID), -1);
        }
        //Page.RegisterClientScriptBlock("Alert", "<script type = 'text/javascript'>alert('" + ID +" fired SelectedIndexChanged event');</script>");
    }
    /// <summary>
    /// To load the drop down values.
    /// </summary>
    /// <param name="DropDownId"></param>
    /// <param name="SelectedLevel"></param>
    private void LoadDropDown(int DropDownId, int SelectedLevel)
    {
        if (SelectedLevel != -1)
        {
            DropDownList ddl = (DropDownList)pnlLevel.FindControl(Convert.ToString(DropDownId + 1));
            if (ddl != null)
            {
                ddl.Items.Clear();
                DataTable dtLevel = new DataTable();
                dsLevels.Tables[0].DefaultView.RowFilter = "ParentId = " + SelectedLevel;
                dtLevel = dsLevels.Tables[0].DefaultView.ToTable();
                ddl.DataSource = dtLevel;
                ddl.DataTextField = "name";
                ddl.DataValueField = "id";
                ddl.DataBind();
                ddl.Items.Insert(0, rm.GetString("Select", ci));
                foreach (ListItem item in ddl.Items)
                {
                    item.Attributes.Add("title", item.Text);
                }
            }
            int i = 0;
            for (i = DropDownId + 2; i <= TotalLevelCount; i++)
            {
                DropDownList ddlChilds = (DropDownList)pnlLevel.FindControl(Convert.ToString(i));
                if (ddlChilds != null)
                {
                    ddlChilds.Items.Clear();
                    ddlChilds.Items.Insert(0, rm.GetString("Select", ci));
                }
            }
        }
        else
        {
            int i = 0;
            for (i = DropDownId + 1; i <= TotalLevelCount; i++)
            {
                DropDownList ddl = (DropDownList)pnlLevel.FindControl(Convert.ToString(i));
                if (ddl != null)
                {
                    ddl.Items.Clear();
                    ddl.Items.Insert(0, rm.GetString("Select", ci));
                }
            }
            DropDownList ddlBeforeLevel = (DropDownList)pnlLevel.FindControl(Convert.ToString(DropDownId - 1));
            if (ddlBeforeLevel != null)
            {
                Session["parentLevelId"] = ddlBeforeLevel.SelectedValue;
            }
            else
            {
                Session["parentLevelId"] = 0;
            }
        }
    }
    #endregion
}
