Imports DevExpress.Web
Imports STP.EL
Public Class ucMenu
    Inherits System.Web.UI.UserControl

    Public Property Orientacion() As String
        Set(ByVal value As String)
            If value.Equals("V") Then
                HeaderMenu.Orientation = Orientation.Vertical
            Else
                HeaderMenu.Orientation = Orientation.Horizontal
            End If
        End Set
        Get
            Return HeaderMenu.Orientation
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then


            If HttpContext.Current.User.Identity.IsAuthenticated AndAlso IsNumeric(Page.User.Identity.Name) Then
                HeaderMenu.Items(2).Visible = False
                HeaderMenu.Items(3).Visible = False
                HeaderMenu.Items(4).Visible = False
                Dim ListMenu As EntityList(Of SEG.Menu) = LoginBusinessObject.GetListMenu(CInt(Page.User.Identity.Name), ConfigurationManager.AppSettings("idSistema"))
                CargarMenu(HeaderMenu, ListMenu)
            End If
        End If
    End Sub



    Private Shared Sub CargarMenu(ByRef menu As DevExpress.Web.ASPxMenu, ByRef ListaOpcionesMenu As EntityList(Of SEG.Menu))
        Dim menuItems As Dictionary(Of String, DevExpress.Web.MenuItem) = New Dictionary(Of String, DevExpress.Web.MenuItem)()

        Try
            For Each opcionMenu As SEG.Menu In ListaOpcionesMenu
                Dim item As MenuItem = CreateMenuItem(opcionMenu)
                Dim idPadre As Integer = If(opcionMenu.idPadre Is Nothing, 0, opcionMenu.idPadre)
                Dim idMenu As Integer = If(opcionMenu.idMenu Is Nothing, 0, opcionMenu.idMenu)

                If menuItems.ContainsKey(idPadre) Then
                    menuItems(idPadre).Items.Add(item)
                Else
                    If idPadre = 0 Then ' It's Root Item
                        menu.Items.Add(item)
                    End If
                End If
                menuItems.Add(idMenu, item)
            Next

        Catch ex As Exception
            Throw
        End Try
    End Sub

    Private Shared Function CreateMenuItem(ByVal opcionMenu As SEG.Menu) As MenuItem
        Dim MenuItem As New MenuItem
        If Not String.IsNullOrEmpty(opcionMenu.URLimagen) Then
            MenuItem.Image.Url = String.Format("~/Content/Images/{0}", opcionMenu.URLimagen)
            MenuItem.Image.AlternateText = "Imagen " & opcionMenu.URLimagen
        End If
        MenuItem.Text = opcionMenu.nombreMenu
        MenuItem.NavigateUrl = opcionMenu.URLOpcion
        Return MenuItem
    End Function
End Class