Imports DevExpress.XtraBars
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraTreeList
Imports DigitalData.GUIs.ZooFlow.ClassConstants
Public Class frmTreeViewNavi
    Dim RightAddActive As Boolean = False
    Dim FormShown As Boolean = False
    Dim SelectedNodeID As Integer = 0
    Dim selectedNodeCapt As String
    Private Sub frmTreeViewNavi_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim oSQL = "select SN_ID ChildID,SN_PARENT_ID ParentID,TERM_VALUE as CAPTION from  VWIDB_STRUCTURE_NODES_STRUCTURE"
            Dim oDT As DataTable = My.DatabaseIDB.GetDatatable(oSQL)

            TreeList2.Nodes.Clear()
            TreeList2.KeyFieldName = oDT.Columns(0).ColumnName
            TreeList2.ParentFieldName = oDT.Columns(1).ColumnName
            TreeList2.DataSource = oDT


            If My.Application.Modules.Item(MODULE_ZOOFLOW).IsAdmin Then
                RibbonPageGroup2.Visible = True
            Else
                RibbonPageGroup2.Visible = False
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub TreeList2_GetStateImage(sender As Object, e As DevExpress.XtraTreeList.GetStateImageEventArgs) Handles TreeList2.GetStateImage
        If e.Node.Expanded Then
            e.NodeImageIndex = 1
        Else
            e.NodeImageIndex = 0
        End If
    End Sub

    Private Sub TreeList2_SelectionChanged(sender As Object, e As EventArgs) Handles TreeList2.SelectionChanged

    End Sub
    Sub Refresh_all()
        Refresh_Relations()
        If XtraTabControlRelationScope.SelectedTabPageIndex = 0 Then
            Refresh_FreeUsers()
            GridViewFreeUser.FocusedRowHandle = GridControl.InvalidRowHandle
        Else
            Refresh_FreeGroups()
            GridViewFreeGroups.FocusedRowHandle = GridControl.InvalidRowHandle
        End If
        GridViewRelations.FocusedRowHandle = GridControl.InvalidRowHandle


    End Sub
    Private Sub BarCheckItemAdminFolderRights_CheckedChanged(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarCheckItemAdminFolderRights.CheckedChanged
        Dim oitm As BarCheckItem = e.Item
        RightAddActive = oitm.Checked
        SplitContainerControl1.Collapsed = Not RightAddActive
        If RightAddActive = True Then
            RibbonPageGroupRelationsChange.Visible = True
            If SelectedNodeID > 0 Then
                Refresh_all()
            End If
        Else
            RibbonPageGroupRelationsChange.Visible = False
        End If
    End Sub


    Private Sub Refresh_FreeUsers()
        Try
            Dim oSQL = $"SELECT GUID as USR_ID, EMAIL FROM DD_ECM.dbo.TBDD_USER USR WHERE GUID NOT IN (
            SELECT ScopeID FROM VWIDB_SN_RELATIONS WHERE Scope = 'USR' AND SN_ID = {SelectedNodeID}) AND USR.ACTIVE = 1 AND LEN(EMAIL) > 0"
            Dim oDT As DataTable = My.DatabaseIDB.GetDatatable(oSQL)
            If Not IsNothing(oDT) Then
                GridViewFreeUser.Columns.Clear()
                GridControlFreeUser.DataSource = oDT
                GridViewFreeUser.Columns("USR_ID").Visible = False
            End If

        Catch ex As Exception

        End Try
    End Sub
    Private Sub Refresh_FreeGroups()
        Try
            Dim oSQL = $"SELECT GUID as GroupID, NAME FROM DD_ECM.dbo.TBDD_GROUPS GRP WHERE GUID NOT IN (
            SELECT ScopeID FROM VWIDB_SN_RELATIONS WHERE Scope = 'GRP' AND SN_ID = {SelectedNodeID}) AND GRP.ACTIVE = 1 "
            Dim oDT As DataTable = My.DatabaseIDB.GetDatatable(oSQL)
            If Not IsNothing(oDT) Then
                GridViewFreeGroups.Columns.Clear()
                GridControlFreeGroups.DataSource = oDT
                GridViewFreeGroups.Columns("GroupID").Visible = False
            End If

        Catch ex As Exception

        End Try
    End Sub
    Private Sub Refresh_Relations()
        Try
            Dim oSQL = $"SELECT * FROM VWIDB_SN_RELATIONS WHERE SN_ID = {SelectedNodeID}"
            Dim oDT As DataTable = My.DatabaseIDB.GetDatatable(oSQL)
            If Not IsNothing(oDT) Then
                GridViewRelations.Columns.Clear()
                Dim columnStateIcon As New DataColumn()
                columnStateIcon.DataType = GetType(Image)
                columnStateIcon.ColumnName = "ICON"
                columnStateIcon.Caption = ""

                oDT.Columns.Add(columnStateIcon)
                GridControlRelations.DataSource = oDT

                GridViewRelations.Columns("REL_ID").Visible = False
                GridViewRelations.Columns("SN_ID").Visible = False
                GridViewRelations.Columns("Scope").Visible = False
                GridViewRelations.Columns("ScopeID").Visible = False
                GridViewRelations.Columns("NODE_CAPTION").Visible = False

                GridViewRelations.Columns.Item("ICON").MaxWidth = 25
                GridViewRelations.Columns.Item("ICON").MinWidth = 25
                GridViewRelations.Columns.Item("ICON").AppearanceCell.BackColor = Color.White
                GridViewRelations.Columns.Item("ICON").Fixed = FixedStyle.Left
                GridViewRelations.ViewCaption = $"Relationen für [{selectedNodeCapt}]"
                If oDT.Rows.Count > 0 Then
                    Dim oRowIndex As Integer = 0
                    For Each oRow As DataRow In oDT.Rows
                        Try
                            Dim oScope = oRow.Item("Scope")
                            If IsNothing(oScope) Then
                                Exit Sub
                            End If
                            Dim oImageIndex As Integer
                            oImageIndex = 1
                            If oScope.ToString = "GRP" Then
                                oImageIndex = 0
                            End If
                            GridViewRelations.SetRowCellValue(oRowIndex, "ICON", ImageCollection1.Images(oImageIndex))
                            oRowIndex += 1
                        Catch ex As Exception

                        End Try
                    Next
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmTreeViewNavi_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        FormShown = True
        SplitContainerControl1.Collapsed = True
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItemAddRelation.ItemClick
        Dim oInsert As String
        oInsert = $"INSERT INTO [dbo].[TBIDB_STRUCTURE_NODES_RELATIONS]
           ([SN_ID]"
        If XtraTabControlRelationScope.SelectedTabPageIndex = 0 Then
            oInsert &= ",[USR_ID]"

        Else
            oInsert &= ",[GRP_ID]"
        End If
        oInsert &= $",[ADDED_WHO]) VALUES ({SelectedNodeID}, {BarButtonItemRelationDel.Tag},'{My.Application.User.UserName}')"
        If My.DatabaseIDB.ExecuteNonQuery(oInsert) = True Then
            Refresh_all()
        End If
    End Sub

    Private Sub GridViewFreeUser_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridViewFreeUser.FocusedRowChanged
        Dim oFocusedUSR = GridViewFreeUser.GetFocusedRowCellValue(GridViewFreeUser.Columns("USR_ID"))
        If Not IsNothing(oFocusedUSR) Then
            RibbonPageGroupRelationsChange.Enabled = True
            BarButtonItemAddRelation.Enabled = True
            BarButtonItemRelationDel.Enabled = False
            BarButtonItemRelationDel.Tag = oFocusedUSR
            GridViewRelations.FocusedRowHandle = GridControl.InvalidRowHandle
        Else
            RibbonPageGroupRelationsChange.Enabled = False
        End If
    End Sub

    Private Sub GridViewRelations_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridViewRelations.FocusedRowChanged
        Dim oFocusedRELID = GridViewRelations.GetFocusedRowCellValue(GridViewRelations.Columns("REL_ID"))
        If Not IsNothing(oFocusedRELID) Then
            RibbonPageGroupRelationsChange.Enabled = True
            BarButtonItemAddRelation.Enabled = False
            BarButtonItemRelationDel.Enabled = True
            BarButtonItemRelationDel.Tag = oFocusedRELID

        Else
            RibbonPageGroupRelationsChange.Enabled = False
        End If
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItemRelationDel.ItemClick
        Dim oDel = $"DELETE FROM TBIDB_STRUCTURE_NODES_RELATIONS WHERE GUID = {BarButtonItemRelationDel.Tag}"
        If My.DatabaseIDB.ExecuteNonQuery(oDel) = True Then
            Refresh_all()
        End If
    End Sub

    Private Sub TreeList2_FocusedNodeChanged(sender As Object, e As FocusedNodeChangedEventArgs) Handles TreeList2.FocusedNodeChanged
        Try
            Dim KeyValue As Object
            KeyValue = TreeList2.FocusedNode(TreeList2.KeyFieldName)
            If IsNothing(KeyValue) Then
                Exit Sub
            End If
            Dim oNodeCaption = e.Node.GetDisplayText("CAPTION")

            selectedNodeCapt = oNodeCaption
            Console.WriteLine($"keyfield: {KeyValue.ToString}")
            If IsNumeric(KeyValue) Then
                SelectedNodeID = KeyValue
            Else
                Exit Sub
            End If
            If RightAddActive Then
                Refresh_all()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub GridViewFreeGroups_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridViewFreeGroups.FocusedRowChanged
        Dim oFocusedGRP = GridViewFreeGroups.GetFocusedRowCellValue(GridViewFreeGroups.Columns("GroupID"))
        If Not IsNothing(oFocusedGRP) Then
            RibbonPageGroupRelationsChange.Enabled = True
            BarButtonItemAddRelation.Enabled = True
            BarButtonItemRelationDel.Enabled = False
            BarButtonItemRelationDel.Tag = oFocusedGRP
        Else
            RibbonPageGroupRelationsChange.Enabled = False
        End If
    End Sub

    Private Sub XtraTabControlRelationScope_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControlRelationScope.SelectedPageChanged
        If XtraTabControlRelationScope.SelectedTabPageIndex = 1 Then
            Refresh_FreeGroups()
        Else
            Refresh_FreeUsers()
        End If
    End Sub
End Class