Public Class frmStructureNodeConfig
    Dim DT_ENTITIES As DataTable
    Private Shared _Instance As frmStructureNodeConfig = Nothing
    Private setimageNull As Boolean = False
    Public Shared Function Instance() As frmStructureNodeConfig
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New frmStructureNodeConfig
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function
    Private Sub TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingNavigatorSaveItem.Click
        Save_Config()
    End Sub

    Sub Load_TreeView()
        Try
            Try
                TreeView1.Nodes.Clear()
            Catch ex As Exception

            End Try
            Dim DTNODES As DataTable = DD_DMSDataSet.TBPMO_STRUCTURE_NODES_CONFIGURATION

            Dim TREEVIEW_IMAGELIST As ImageList
            TREEVIEW_IMAGELIST = New ImageList
            For Each row As DataRow In DTNODES.Rows
                Try
                    Dim bimage = row.Item("NODE_IMAGE")
                    If Not IsDBNull(bimage) Then
                        Dim Node_image = ByteArrayToBitmap(bimage)
                        TREEVIEW_IMAGELIST.Images.Add(row.Item("GUID").ToString, Node_image)
                    End If
                Catch ex As Exception

                End Try

            Next
            TreeView1.ImageList = TREEVIEW_IMAGELIST

            ' Presuming the DataTable has a column named ENTITY_ID and TYPE_NODE.
            Dim expression As String
            expression = "PARENT_NODE = 0"
            Dim foundRowsLevel0() As DataRow
            ' Use the Select method to find all rows matching the filter.
            foundRowsLevel0 = DTNODES.Select(expression)
            Dim i As Integer
            Dim NODE_CONFIG_ID
            Dim LEVEL0_NODE As TreeNode

            ' For each row create a Node
            For i = 0 To foundRowsLevel0.GetUpperBound(0)
                Dim ID = foundRowsLevel0(i)("GUID")

                Dim nodetext = foundRowsLevel0(i)("NAME")
                Dim Type_node = foundRowsLevel0(i)("TYPE_NODE")
                LEVEL0_NODE = New TreeNode(nodetext)

                LEVEL0_NODE.Tag = ID

                Dim index As Integer = 0
                For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                    If img = ID Then
                        Exit For
                    End If
                    index += 1
                Next
                LEVEL0_NODE.ImageIndex = index
                LEVEL0_NODE.SelectedImageIndex = index

                '### LEVEL 1 laden #######
                Dim expressionLevel1 As String
                expressionLevel1 = "PARENT_NODE = " & ID 'TYPE_NODE = 1 AND  Einschränkung auf Type 2 und PARENT_NODE
                Dim foundRowsLevel1() As DataRow
                foundRowsLevel1 = DTNODES.Select(expressionLevel1, "TYPE_NODE")
                Dim j As Integer
                For j = 0 To foundRowsLevel1.GetUpperBound(0)
                    Dim ID1 = foundRowsLevel1(j)("GUID")
                    Dim nodetext1 = foundRowsLevel1(j)("NAME")
                    Dim Type_node1 = foundRowsLevel1(j)("TYPE_NODE")

                    ' Node erstellen..
                    Dim LEVEL1_NODE As TreeNode
                    LEVEL1_NODE = New TreeNode(nodetext1)
                    LEVEL1_NODE.Tag = ID1
                    index = 0
                    For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                        If img = ID1 Then
                            Exit For
                        End If
                        index += 1
                    Next
                    LEVEL1_NODE.ImageIndex = index
                    LEVEL1_NODE.SelectedImageIndex = index
                    ' ..und einfügen
                    LEVEL0_NODE.Nodes.Add(LEVEL1_NODE)
                    '### LEVEL 2 laden #######
                    Dim expressionLevel2 As String
                    expressionLevel2 = "PARENT_NODE = " & ID1 'TYPE_NODE = 2 AND Einschränkung auf Type  und PARENT_NODE
                    Dim foundRowsLevel2() As DataRow
                    foundRowsLevel2 = DTNODES.Select(expressionLevel2, "TYPE_NODE")
                    Dim k As Integer
                    For k = 0 To foundRowsLevel2.GetUpperBound(0)
                        Dim ID2 = foundRowsLevel2(k)("GUID")
                        Dim nodetext2 = foundRowsLevel2(k)("NAME")
                        ' Node erstellen..
                        Dim LEVEL2_NODE As TreeNode
                        LEVEL2_NODE = New TreeNode(nodetext2)
                        LEVEL2_NODE.Tag = ID2

                        index = 0
                        For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                            If img = ID2 Then
                                Exit For
                            End If
                            index += 1
                        Next
                        LEVEL2_NODE.ImageIndex = index
                        LEVEL2_NODE.SelectedImageIndex = index
                        ' ..und einfügen
                        LEVEL1_NODE.Nodes.Add(LEVEL2_NODE)
                        '### LEVEL 3 laden #######
                        Dim expressionLevel3 As String
                        expressionLevel3 = "PARENT_NODE = " & ID2
                        Dim foundRowsLevel3() As DataRow
                        foundRowsLevel3 = DTNODES.Select(expressionLevel3, "TYPE_NODE")
                        Dim l As Integer
                        For l = 0 To foundRowsLevel3.GetUpperBound(0)
                            Dim ID3 = foundRowsLevel3(l)("GUID")
                            Dim nodetext3 = foundRowsLevel3(l)("NAME")
                            ' Node erstellen..
                            Dim LEVEL3_NODE As TreeNode
                            LEVEL3_NODE = New TreeNode(nodetext3)
                            LEVEL3_NODE.Tag = ID3

                            index = 0
                            For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                If img = ID3 Then
                                    Exit For
                                End If
                                index += 1
                            Next
                            LEVEL3_NODE.ImageIndex = index
                            LEVEL3_NODE.SelectedImageIndex = index
                            ' ..und einfügen
                            LEVEL2_NODE.Nodes.Add(LEVEL3_NODE)
                            '### LEVEL 4 laden #######
                            Dim expressionLevel4 As String
                            expressionLevel4 = "PARENT_NODE = " & ID3
                            Dim foundRowsLevel4() As DataRow
                            foundRowsLevel4 = DTNODES.Select(expressionLevel4, "TYPE_NODE")
                            Dim m As Integer
                            For m = 0 To foundRowsLevel4.GetUpperBound(0)
                                Dim ID5 = foundRowsLevel4(m)("GUID")
                                Dim nodetext5 = foundRowsLevel4(m)("NAME")

                                ' Node erstellen..
                                Dim LEVEL4_NODE As TreeNode
                                LEVEL4_NODE = New TreeNode(nodetext5)
                                LEVEL4_NODE.Tag = ID5

                                index = 0
                                For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                    If img = ID5 Then
                                        Exit For
                                    End If
                                    index += 1
                                Next
                                LEVEL4_NODE.ImageIndex = index
                                LEVEL4_NODE.SelectedImageIndex = index
                                ' ..und einfügen
                                LEVEL3_NODE.Nodes.Add(LEVEL4_NODE)
                                '### LEVEL 5 laden #######
                                Dim expressionLevel5 As String
                                Dim sortExp As String = "TYPE_NODE"
                                expressionLevel5 = "PARENT_NODE = " & ID5 'TYPE_NODE = 5 AND  Einschränkung auf Type  und PARENT_NODE
                                Dim foundRowsLevel5() As DataRow
                                foundRowsLevel5 = DTNODES.Select(expressionLevel5, sortExp)
                                Dim n As Integer
                                If ID5 = 1146 Then
                                    Console.WriteLine("Aha")
                                End If
                                For n = 0 To foundRowsLevel5.GetUpperBound(0)
                                    Dim ID6 = foundRowsLevel5(n)("GUID")
                                    Dim nodetext6 = foundRowsLevel5(n)("NAME")

                                    ' Node erstellen..
                                    Dim LEVEL5_NODE As TreeNode
                                    LEVEL5_NODE = New TreeNode(nodetext6)

                                    LEVEL5_NODE.Tag = ID6

                                    index = 0
                                    For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                        If img = ID6 Then
                                            Exit For
                                        End If
                                        index += 1
                                    Next
                                    LEVEL5_NODE.ImageIndex = index
                                    LEVEL5_NODE.SelectedImageIndex = index
                                    ' ..und einfügen
                                    LEVEL4_NODE.Nodes.Add(LEVEL5_NODE)
                                    '### LEVEL 5 laden #######
                                    Dim expressionLevel6 As String
                                    expressionLevel6 = "PARENT_NODE = " & ID6 'TYPE_NODE = 6 AND  Einschränkung auf Type  und PARENT_NODE
                                    Dim foundRowsLevel6() As DataRow
                                    foundRowsLevel6 = DTNODES.Select(expressionLevel6, sortExp)
                                    If ID6 = 1146 Then
                                        Console.WriteLine("Aha")
                                    End If
                                    Dim o As Integer
                                    For o = 0 To foundRowsLevel6.GetUpperBound(0)
                                        Dim ID7 = foundRowsLevel6(o)("GUID")
                                        Dim nodetext7 = foundRowsLevel6(o)("NAME")

                                        ' Node erstellen..
                                        Dim LEVEL6_NODE As TreeNode
                                        LEVEL6_NODE = New TreeNode(nodetext7)
                                        LEVEL6_NODE.Tag = ID7

                                        index = 0
                                        For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                            If img = ID7 Then
                                                Exit For
                                            End If
                                            index += 1
                                        Next
                                        LEVEL6_NODE.ImageIndex = index
                                        LEVEL6_NODE.SelectedImageIndex = index
                                        ' ..und einfügen
                                        LEVEL5_NODE.Nodes.Add(LEVEL6_NODE)
                                        '### LEVEL 7 laden #######
                                        Dim expressionLevel7 As String
                                        expressionLevel7 = "PARENT_NODE = " & ID7 'TYPE_NODE = 7 AND Einschränkung auf Type  und PARENT_NODE
                                        Dim foundRowsLevel7() As DataRow
                                        foundRowsLevel7 = DTNODES.Select(expressionLevel7, sortExp)
                                        Dim p As Integer
                                        For p = 0 To foundRowsLevel7.GetUpperBound(0)
                                            Dim ID8 = foundRowsLevel7(p)("GUID")
                                            Dim nodetext8 = foundRowsLevel7(p)("NAME")
                                            ' Node erstellen..
                                            Dim LEVEL7_NODE As TreeNode
                                            LEVEL7_NODE = New TreeNode(nodetext8)
                                            LEVEL7_NODE.Tag = ID8

                                            index = 0
                                            For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                                If img = ID8 Then
                                                    Exit For
                                                End If
                                                index += 1
                                            Next
                                            LEVEL7_NODE.ImageIndex = index
                                            LEVEL7_NODE.SelectedImageIndex = index
                                            ' ..und einfügen
                                            LEVEL6_NODE.Nodes.Add(LEVEL7_NODE)
                                            '### LEVEL 7 laden #######
                                            Dim expressionLevel8 As String
                                            expressionLevel8 = "PARENT_NODE = " & ID8 'TYPE_NODE = 7 AND  Einschränkung auf Type  und PARENT_NODE
                                            Dim foundRowsLevel8() As DataRow
                                            foundRowsLevel8 = DTNODES.Select(expressionLevel8, sortExp)
                                            Dim q As Integer
                                            For q = 0 To foundRowsLevel8.GetUpperBound(0)
                                                Dim ID9 = foundRowsLevel8(q)("GUID")
                                                Dim nodetext9 = foundRowsLevel8(q)("NAME")
                                                ' Node erstellen..
                                                Dim LEVEL8_NODE As TreeNode
                                                LEVEL8_NODE = New TreeNode(nodetext9)
                                                LEVEL8_NODE.Tag = ID9


                                                index = 0
                                                For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                                    If img = ID9 Then
                                                        Exit For
                                                    End If
                                                    index += 1
                                                Next
                                                LEVEL8_NODE.ImageIndex = index
                                                LEVEL8_NODE.SelectedImageIndex = index
                                                ' ..und einfügen
                                                LEVEL7_NODE.Nodes.Add(LEVEL8_NODE)
                                                '### LEVEL 8 laden #######
                                            Next ' Level 7 Ende
                                        Next ' Level 7 Ende
                                    Next ' Level 6 Ende
                                Next ' Level 5 Ende
                            Next 'Level 4 Ende
                        Next 'Level 3 Ende
                    Next ' Level 2 Ende
                Next 'Level 1 Ende
                TreeView1.Nodes.Add(LEVEL0_NODE)
            Next

            TreeView1.ExpandAll()

        Catch ex As Exception
            MsgBox("Unexpected Error in Load treeView: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Function Save_Config()
        Try
            Me.TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingSource.EndEdit()
            If DD_DMSDataSet.TBPMO_STRUCTURE_NODES_CONFIGURATION.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox.Text = USER_USERNAME
                TBPMO_STRUCTURE_NODES_CONFIGURATIONTableAdapter.Update(DD_DMSDataSet.TBPMO_STRUCTURE_NODES_CONFIGURATION)
                lblSave.Text = "Successfully saved - " & Now
                lblSave.Visible = True
                GroupBoxNodeIcon.Enabled = True
            Else
                lblSave.Visible = False
            End If
            Return True
        Catch ex As Exception
            MsgBox("Error in Saving Configuration:" & vbNewLine & ex.Message)
            Return False
        End Try
    End Function
    Private Sub frmStructureNodeConfig_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.TBWH_ENTITYTableAdapter.Connection.ConnectionString = MYDB_ECM.CurrentConnectionString
            Me.TBWH_ENTITYTableAdapter.Fill(Me.DD_DMSDataSet.TBWH_ENTITY, USER_LANGUAGE)
            Me.TBPMO_STRUCTURE_NODES_CONFIGURATIONTableAdapter.Connection.ConnectionString = MYDB_ECM.CurrentConnectionString
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Connection.ConnectionString = MYDB_ECM.CurrentConnectionString
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Fill(Me.DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR, USER_LANGUAGE, CURRENT_SCREEN_ID)
        Catch ex As Exception
            MsgBox("Error in Loading Configuration:" & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingSource.AddingNew
        DD_DMSDataSet.TBPMO_STRUCTURE_NODES_CONFIGURATION.ADDED_WHOColumn.DefaultValue = USER_USERNAME
    End Sub

    Private Sub btnSelectBackground_Click(sender As Object, e As EventArgs) Handles btnSelectBackground.Click
        Try
            OpenFileDialog1.Filter = "PNG Bilder|*.png"
            If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim path As String = OpenFileDialog1.FileName
                pbBackground.ImageLocation = path
                If Save_Config() = True Then
                    ' Hintergrund speichern
                    Dim image As Bitmap = Nothing
                    Dim bimage() As Byte = Nothing

                    If pbBackground.ImageLocation IsNot Nothing Then
                        image = CType(Drawing.Image.FromFile(pbBackground.ImageLocation, True), Bitmap)
                        bimage = BitmapToByteArray(image)
                        Dim sql As String = String.Format("UPDATE TBPMO_STRUCTURE_NODES_CONFIGURATION SET NODE_IMAGE = @Image WHERE GUID = {0}", Me.GUIDTextBox.Text)
                        Dim conn As New SqlClient.SqlConnection(MyConnectionString)
                        Dim cmd As New SqlClient.SqlCommand(sql, conn)
                        cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = bimage
                        conn.Open()
                        cmd.ExecuteNonQuery()
                        conn.Close()
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Setting NodeImage:" & vbNewLine & ex.Message)
        End Try

    End Sub

    Private Sub btnResetBackground_Click(sender As Object, e As EventArgs) Handles btnResetBackground.Click
        Try
            Me.setimageNull = True
            pbBackground.ImageLocation = Nothing
            pbBackground.Image = Nothing
            Dim sql As String = String.Format("UPDATE TBPMO_STRUCTURE_NODES_CONFIGURATION SET NODE_IMAGE IS NULL WHERE GUID = {0}", Me.GUIDTextBox.Text)
            If MYDB_ECM.ExecuteNonQuery(SQL) = True Then
                setimageNull = False
            End If
        Catch ex As Exception
            MsgBox("Error in Deleting NodeImage:" & vbNewLine & ex.Message)
        End Try

    End Sub

    Private Sub TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingSource.CurrentChanged
        Load_image()
    End Sub
    Sub Load_Image()
        If GUIDTextBox.Text <> "" Then
            Try
                pbBackground.ImageLocation = Nothing
                pbBackground.Image = Nothing
                Dim sql = "SELECT NODE_IMAGE FROM TBPMO_STRUCTURE_NODES_CONFIGURATION WHERE GUID = " & GUIDTextBox.Text
                Dim img = MYDB_ECM.GetScalarValue(SQL)
                ' Hintergrundbild unwandeln und anzeigen
                If Not IsDBNull(img) Then
                    Dim bimage() As Byte = img
                    Dim image = ByteArrayToBitmap(bimage)
                    Me.pbBackground.Image = image
                End If
            Catch ex As Exception
                MsgBox("Error in Getting NodeImage:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub

    Private Sub frmStructureNodeConfig_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Load_Image()

        If cmbEntity.Items.Count > 0 Then
            cmbEntity.SelectedIndex = 0
            LoadData()
        End If
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        Try
            Dim SelectedNode As TreeNode = TryCast(TreeView1.SelectedNode, TreeNode)
            If SelectedNode IsNot Nothing Then
                Dim ID = SelectedNode.Tag
                If IsNumeric(ID) Then
                    TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingSource.Filter = "GUID = " & ID
                End If
            End If
            Me.lblSave.Visible = False
        Catch ex As Exception
            MsgBox("Error in TreeView.AfterSelect:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub cmbEntity_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbEntity.SelectedIndexChanged
        LoadData()
    End Sub

    Private Sub LoadData()
        If cmbEntity.SelectedIndex <> -1 Then
            Me.TBPMO_STRUCTURE_NODES_CONFIGURATIONTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_STRUCTURE_NODES_CONFIGURATION, cmbEntity.SelectedValue)
            If DD_DMSDataSet.TBPMO_STRUCTURE_NODES_CONFIGURATION.Rows.Count > 0 Then
                Load_TreeView()
            End If
        End If
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            TBPMO_STRUCTURE_NODES_CONFIGURATIONBindingSource.AddNew()
            GroupBoxNodeIcon.Enabled = False
        Catch ex As Exception
            MsgBox("Error in AddNode:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
End Class