Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

Public Class frmAdmin_IDBBERelations
    Private GridCursorLocation As Point
    Private SELECTED_BEID As Integer
    Private SELECTED_FREE_ATTRID As Integer
    Private SELECTED_ID2DELETE As Integer
    Private DraggedAttributeID
    Private DragDropManager As ClassDragDrop = Nothing
    Private downHitInfo As GridHitInfo = Nothing
    Private Function GetAvailableAttributesByBEID(beID As Integer) As DataTable
        Try
            Dim dt As DataTable
            Dim oSQL = $"SELECT AttributeID,Attribute,AttributeType FROM VWIDB_ATTRIBUTE_LANG WHERE LANG_CODE = '{My.Application.User.Language}' AND AttributeID NOT IN (SELECT ATTR_ID FROM TBIDB_BE_ATTRIBUTE WHERE BE_ID = {beID})"
            dt = My.DatabaseIDB.GetDatatable(oSQL)
            Return dt
        Catch ex As Exception
            ShowErrorMessage($"Error in GetAvailableAttributesByBEID with groupId {beID}", ex)

            Return Nothing
        End Try
    End Function
    Private Function GetRelatedAttributesByBEID(beID As Integer) As DataTable
        Try
            Dim dt As DataTable
            Dim oSQL = $"select GUID as RelID, ATTR_TITLE as Attribute, [TYPE_NAME] as [AttributeType]  FROM VWIDB_BE_ATTRIBUTE WHERE BE_ID = {beID} and LANG_CODE = '{My.Application.User.Language}'"
            dt = My.DatabaseIDB.GetDatatable(oSQL)
            Return dt
        Catch ex As Exception
            ShowErrorMessage($"Error in GetAvailableAttributesByBEID with groupId {beID}", ex)

            Return Nothing
        End Try
    End Function
    Private Sub ShowErrorMessage(errorText As String, ex As Exception)
        MsgBox(errorText & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "BE Relations")
    End Sub

    Private Sub frmAdmin_IDBBERelations_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim oSQL = "Select Guid As ID,TITLE As BusinessEntity from TBIDB_BUSINESS_ENTITY"
            Dim oDT As DataTable = My.DatabaseIDB.GetDatatable(oSQL)
            GridControlBusinessEntities.DataSource = oDT
            DragDropManager = New ClassDragDrop(My.LogConfig)
            DragDropManager.AddGridView(GridViewFreeAttributes)
            DragDropManager.AddGridView(GridViewRelatedAttributes)
            LoadFreeAttributes()
            LoadRelatedAttributes()
        Catch ex As Exception
            ShowErrorMessage($"Error in FormLoad", ex)
        End Try
    End Sub

    Private Sub GridViewBusinessEntities_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridViewBusinessEntities.FocusedRowChanged
        Dim oBEID = GridViewBusinessEntities.GetFocusedRowCellValue(GridViewBusinessEntities.Columns("ID"))
        SELECTED_BEID = oBEID
        LoadFreeAttributes()
        LoadRelatedAttributes()
    End Sub

    Sub LoadRelatedAttributes()
        Dim oDT = GetRelatedAttributesByBEID(SELECTED_BEID)
        If Not IsNothing(oDT) Then
            GridControlAttributesRelated.DataSource = oDT
        End If
    End Sub
    Sub LoadFreeAttributes()
        Dim oDT = GetAvailableAttributesByBEID(SELECTED_BEID)
        If Not IsNothing(oDT) Then
            GridControlFreeAttributes.DataSource = oDT
        End If
    End Sub

    Private Sub GridControlFreeAttributes_DragDrop(sender As Object, e As DragEventArgs) Handles GridControlFreeAttributes.DragDrop
        Try
            Dim data As String = e.Data.GetData(DataFormats.Text)
            Dim AttrID As Integer = data.Split("|")(0)


            If AddAttr2BE(AttrID, SELECTED_BEID) Then
                Dim oDT = GetRelatedAttributesByBEID(SELECTED_BEID)
                If Not IsNothing(oDT) Then
                    GridControlAttributesRelated.DataSource = Nothing
                    GridControlAttributesRelated.DataSource = oDT
                End If
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Adding AttrID:")
        End Try
    End Sub

    Public Shared Function AddAttr2BE(AttrId As Integer, BeId As Integer) As Boolean
        Try
            Dim oSQL = $"
                INSERT INTO TBIDB_BE_ATTRIBUTE (BE_ID,ATTR_ID,ADDED_WHO)
                VALUES ({BeId},{AttrId},'{My.Application.User.UserName}')
            "
            Return My.DatabaseIDB.ExecuteNonQuery(oSQL)
        Catch ex As Exception
            LOGGER.Error(ex)
            Return False
        End Try
    End Function
    Public Shared Function DeleteAttrfromBE(ID As Integer) As Boolean
        Try
            Dim oSQL = $"DELETE FROM TBIDB_BE_ATTRIBUTE WHERE GUID = {ID}"
            Return My.DatabaseIDB.ExecuteNonQuery(oSQL)
        Catch ex As Exception
            LOGGER.Error(ex)
            Return False
        End Try
    End Function

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
        If AddAttr2BE(SELECTED_FREE_ATTRID, SELECTED_BEID) Then
            LoadFreeAttributes()
            LoadRelatedAttributes()
        End If

    End Sub

    Private Sub GridViewFreeAttributes_FocusedRowChanged(sender As Object, e As FocusedRowChangedEventArgs) Handles GridViewFreeAttributes.FocusedRowChanged
        Dim oID = GridViewFreeAttributes.GetFocusedRowCellValue(GridViewFreeAttributes.Columns("AttributeID"))
        SELECTED_FREE_ATTRID = oID

    End Sub

    Private Sub GridViewRelatedAttributes_FocusedRowChanged(sender As Object, e As FocusedRowChangedEventArgs) Handles GridViewRelatedAttributes.FocusedRowChanged
        Dim oID = GridViewRelatedAttributes.GetFocusedRowCellValue(GridViewRelatedAttributes.Columns("RelID"))
        SELECTED_ID2DELETE = oID
    End Sub

    Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles SimpleButton2.Click
        If DeleteAttrfromBE(SELECTED_ID2DELETE) Then
            LoadFreeAttributes()
            LoadRelatedAttributes()
            SELECTED_ID2DELETE = 0
        End If
    End Sub
End Class