Public Class frmHomepageWartung
    Private DB As New cDBHomepage
    Dim blocks As New List(Of cBlock)
    Private margin_top As Integer = 20
    Private margin_left As Integer = 20
    Private top_value As Integer = 0
    Private left_value As Integer = 0
    Private rowmaxheight As Integer = 0
    Private screenwidth As Integer = 1200

    Private Sub usrcntlHomepage_Load(sender As Object, e As EventArgs) Handles Me.Load
        top_value = margin_top
        left_value = margin_left
        Dim nav As List(Of cNavigation) = DB.qry_menu_tags("de")

        For Each n In nav
            Dim item As New ToolStripMenuItem(n.bezeichnung)
            item.Name = n.id

            For Each n2 In n.subnavigation
                Dim i As New ToolStripMenuItem(n2.bezeichnung)
                i.Name = n2.id

                For Each n3 In n2.subnavigation
                    Dim ii As New ToolStripMenuItem(n3.bezeichnung)
                    i.DropDownItems.Add(ii)
                    ii.Name = n3.id
                    AddHandler ii.Click, AddressOf itemclick
                Next
                AddHandler i.Click, AddressOf itemclick
                item.DropDownItems.Add(i)

                'Eigenes bearbeiten

                Dim edititem2 As New ToolStripMenuItem("--> """ & n2.bezeichnung & """ bearbeiten")
                i.DropDownItems.Add(edititem2)

                edititem2.Name = n2.id
                AddHandler edititem2.Click, AddressOf itemclick
                menueHomepage.Items.Add(item)


                'hinzufügen
                Dim additem2 As New ToolStripMenuItem("-->  +")
                i.DropDownItems.Add(additem2)
                additem2.Name = n2.id
                AddHandler additem2.Click, AddressOf itemclick
                menueHomepage.Items.Add(item)


            Next

            'Eigenes bearbeiten

            Dim edititem As New ToolStripMenuItem("--> """ & n.bezeichnung & """ bearbeiten")
            item.DropDownItems.Add(edititem)
            edititem.Name = n.id
            AddHandler edititem.Click, AddressOf itemclick
            menueHomepage.Items.Add(item)


            'hinzufügen
            Dim additem As New ToolStripMenuItem("--> +")
            item.DropDownItems.Add(additem)
            additem.Name = n.id
            AddHandler additem.Click, AddressOf itemclick
            menueHomepage.Items.Add(item)


        Next


        menueHomepage.Items.Add("--> +")


        '  initBlocks()
        
    End Sub

    Sub initBlocks(Optional block_id As Integer = 1)
        pnlHP.Controls.Clear()
        ' blocks = DB.qry_content(block_id)
        top_value = margin_top : left_value = margin_left
        For Each block In blocks
            For Each bf In block.blockfields
                addPanel(bf)
                'top_value += 100
                ' left_value += 100
            Next
        Next
    End Sub
    Private Sub itemclick(sender As Object, e As EventArgs)
        'If Not DirectCast(sender, ToolStripMenuItem).HasDropDownItems Then
        '  MsgBox(sender.name)
        initBlocks(sender.name)
        'End If
    End Sub
    Private Sub itemdoubleclick(sender As Object, e As EventArgs)
        'MsgBox(sender.name)
    End Sub
    Private Sub addBlockfield(top As Integer, left As Integer)


        Dim b As New Button
        b.Name = "b" & top
        b.Text = "Tewst"
        '  Dim l As New Label
        ' Dim t As New TextBox
        'p.Controls.Add(l)
        
        b.Top = top
        b.Left = left
        'p.Controls.Add(t)
        pnlHP.Controls.Add(b)




    End Sub

    Private Sub addPanel(bf As cBlockField)
        ' prüfen, ob zeilenumbruch:
        If left_value + (screenwidth * bf.bart_breiteProz / 100) > screenwidth Then
            left_value = margin_left
            top_value += rowmaxheight + 20
            rowmaxheight = 0
        Else

        End If

        Dim p As New FlowLayoutPanel
        p.BackColor = Color.Coral
        p.Height = 0 : p.Width = (screenwidth * (bf.bart_breiteProz / 100)) - 5
        p.Top = top_value
        p.Left = left_value
        'MsgBox(top_value & " - " & left_value)
        'p.Controls.Add(t)

       
        pnlHP.Controls.Add(p)

        For Each bfac In bf.blockfieldargumentcontents
            p.Height += 200
            Dim t As New TextBox
            t.Text = bfac.bfac_de
            t.Multiline = True
            t.Height = 150 : t.Width = (screenwidth * (bf.bart_breiteProz / 100)) - 5
            '  MsgBox("width: " & (screenwidth * bf.bart_breiteProz / 100) - 5)
            p.Controls.Add(t)

            Dim b_save As New Button
            b_save.Text = "Speichern"
            b_save.Name = "saveHp_" & bfac.bfac_ID
            b_save.Top = top_value + p.Height
            p.Controls.Add(b_save)
            AddHandler b_save.Click, AddressOf saveClick

            Dim b_add As New Button
            b_add.Text = "Hinzufügen"
            b_add.Name = "addHp_" & bfac.bfac_ID
            b_add.Top = top_value + p.Height
            p.Controls.Add(b_add)
            AddHandler b_add.Click, AddressOf addClick

            Dim b_del As New Button
            b_del.Text = "Löschen"
            b_del.Name = "delHp_" & bfac.bfac_ID
            b_del.Top = top_value + p.Height
            p.Controls.Add(b_del)
            AddHandler b_del.Click, AddressOf addClick
        Next

       




        left_value += (screenwidth * (bf.bart_breiteProz / 100)) - 5



        If rowmaxheight < p.Height Then rowmaxheight = p.Height
    End Sub

    Private Sub saveClick(sender As Object, e As EventArgs)
        If sender.name.ToString.Contains("saveHp_") Then
            Dim id As Integer = CInt(sender.name.ToString.Replace("saveHp_", ""))
            MsgBox("SAVE: " & id)
            '  DB.qry_content()

        End If
    End Sub
    Private Sub addClick(sender As Object, e As EventArgs)
        If sender.name.ToString.Contains("addHp_") Then
            Dim id As Integer = CInt(sender.name.ToString.Replace("addHp_", ""))
            MsgBox("ADD: " & id)
        End If
    End Sub
    Private Sub delClick(sender As Object, e As EventArgs)
        If sender.name.ToString.Contains("delHp_") Then
            Dim id As Integer = CInt(sender.name.ToString.Replace("delHp_", ""))
            MsgBox("DEL: " & id)
        End If
    End Sub


    Private Sub addBlockfield2(top As Integer, left As Integer)
        Dim p As New Panel
        Dim b As New Button
        b.Text = "Tewst2"
        '  Dim l As New Label
        ' Dim t As New TextBox
        'p.Controls.Add(l)
        'p.Controls.Add(t)
        p.Controls.Add(b)
        pnlHP.Controls.Add(p)
        p.Top = top
        p.Left = left
    End Sub


    Private Sub pnlHP_Paint(sender As Object, e As PaintEventArgs)

        'addBlockfield()
    End Sub

    Private Sub usrcntlHomepage_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub
End Class