
Imports System.Data
Imports Telerik.Web.UI

Partial Class DefaultVB
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' Bind on every request to ensure the server-side tree exists before drill-down handling
        Dim employees = BuildEmployees()
        RadOrgChart1.DataSource = employees
        RadOrgChart1.DataFieldID = "TeamID"
        RadOrgChart1.DataFieldParentID = "ReportsTo"
        RadOrgChart1.DataTextField = "Team"
        RadOrgChart1.DataBind()
    End Sub

    Private Function BuildEmployees() As DataTable
        Dim employees = New DataTable()
        employees.Columns.Add("TeamID", GetType(String))
        employees.Columns.Add("ReportsTo", GetType(String))
        employees.Columns.Add("Team", GetType(String))

        employees.Rows.Add("1", DBNull.Value, "Company Name")
        employees.Rows.Add("ANTON", "1", "Antonio Moreno")
        employees.Rows.Add("BLONP", "ANTON", "Frédérique Citeaux")
        employees.Rows.Add("AROUT", "ANTON", "Thomas Hardy")
        employees.Rows.Add("BLAUS", "ANTON", "Hanna Moos")
        employees.Rows.Add("COMMI", "AROUT", "Pedro Afonso")
        employees.Rows.Add("CACTU", "AROUT", "Patricio Simpson")

        Return employees
    End Function

    Private Sub RadOrgChart1_DrillDown(sender As Object, e As OrgChartDrillDownEventArguments) Handles RadOrgChart1.DrillDown
        ' Guard for safety; if the node could not be resolved, exit quietly
        If e Is Nothing OrElse e.SourceNode Is Nothing Then Return

        ' Replace the clicked node content with our "details" template (except the root with ID "1")
        If e.SourceNode.ID <> "1" Then
            Dim item = New OrgChartGroupItem() With {.Template = New EmployeeInfo(e.SourceNode.ID)}
            e.SourceNode.Renderer.Controls.Remove(e.SourceNode.Nodes.Renderer)
            e.SourceNode.GroupItems.Clear()
            e.SourceNode.Nodes.Clear()
            e.SourceNode.GroupItems.Add(item)
        End If
    End Sub
End Class

' Simple DTO for fake details
Friend Class Contact
    Public Property ContactName As String
    Public Property ContactTitle As String
    Public Property City As String
    Public Property Address As String
End Class

' Drill-down template using in-memory dummy data (no database)
Public Class EmployeeInfo
    Implements ITemplate

    Private ReadOnly _id As String

    ' Dummy data keyed by TeamID
    Private Shared ReadOnly Contacts As New Dictionary(Of String, Contact)(StringComparer.OrdinalIgnoreCase) From {
        {"ANTON", New Contact With {.ContactName = "Antonio Moreno", .ContactTitle = "Sales Manager", .City = "México D.F.", .Address = "Mataderos 2312"}},
        {"BLONP", New Contact With {.ContactName = "Frédérique Citeaux", .ContactTitle = "Marketing Manager", .City = "Strasbourg", .Address = "24, place Kléber"}},
        {"AROUT", New Contact With {.ContactName = "Thomas Hardy", .ContactTitle = "Sales Representative", .City = "London", .Address = "120 Hanover Sq."}},
        {"BLAUS", New Contact With {.ContactName = "Hanna Moos", .ContactTitle = "Sales Representative", .City = "Mannheim", .Address = "Forsterstr. 57"}},
        {"COMMI", New Contact With {.ContactName = "Pedro Afonso", .ContactTitle = "Sales Associate", .City = "Sao Paulo", .Address = "Av. dos Lusíadas, 23"}},
        {"CACTU", New Contact With {.ContactName = "Patricio Simpson", .ContactTitle = "Owner", .City = "Buenos Aires", .Address = "Cerrito 333"}}
    }

    Public Sub New(id As String)
        _id = id
    End Sub

    Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
        Dim info As Contact = Nothing
        Contacts.TryGetValue(_id, info)

        ' Left image (placeholder)
        Dim imageWrap = New Panel() With {.CssClass = "imageWrap"}
        container.Controls.Add(imageWrap)




        Dim img = New Image() With {
            .ImageUrl = "https://placehold.co/65x80/EEE/333?text=" & System.Web.HttpUtility.UrlEncode(_id),
            .Height = Unit.Pixel(80),
            .Width = Unit.Pixel(65)
        }
        imageWrap.Controls.Add(img)
        imageWrap.Controls.Add(img)

        ' Right panel with details (UL/LI as in the Telerik sample)
        Dim panelWrap = New Panel() With {.CssClass = "rocPanelWrap"}
        container.Controls.Add(panelWrap)

        Dim ul = New HtmlGenericControl("UL")
        ul.Attributes("class") = "rocPanel"
        panelWrap.Controls.Add(ul)

        Dim name = New Label() With {.Text = If(info IsNot Nothing, info.ContactName, ("ID: " & _id))}
        Dim nameLi = New HtmlGenericControl("LI")
        nameLi.Attributes("class") = "contactName"
        nameLi.Controls.Add(name)
        ul.Controls.Add(nameLi)

        Dim title = New Label() With {.Text = If(info IsNot Nothing, info.ContactTitle, "N/A")}
        Dim titleLi = New HtmlGenericControl("LI")
        titleLi.Attributes("class") = "title"
        titleLi.Controls.Add(title)
        ul.Controls.Add(titleLi)

        Dim city = New Label() With {.Text = If(info IsNot Nothing, info.City, "N/A")}
        Dim cityLi = New HtmlGenericControl("LI")
        cityLi.Attributes("class") = "city"
        cityLi.Controls.Add(city)
        ul.Controls.Add(cityLi)

        Dim address = New Label() With {.Text = If(info IsNot Nothing, info.Address, "N/A")}
        Dim addressLi = New HtmlGenericControl("LI")
        addressLi.Attributes("class") = "address"
        addressLi.Controls.Add(address)
        ul.Controls.Add(addressLi)
    End Sub

End Class
