Imports DigitalData.Modules.Logging

Public Class frmAdmin_GlobixRelations

    Private SELECTED_DTID As Integer
    Private Logger As Logger

    Private Function GetAvailableUsers(doctypeid As Integer) As DataTable
        Try
            Dim dt As DataTable
            Dim oSQL = $"SELECT GUID UserID,NAME as Name, PRENAME as Prename,USERNAME as Username, EMAIL as Email FROM TBDD_USER WHERE GUID NOT IN (SELECT USER_ID FROM TBDD_USER_DOKTYPE WHERE DOCTYPE_ID = {doctypeid})"
            dt = My.DatabaseECM.GetDatatable(oSQL)
            Return dt
        Catch ex As Exception
            ShowErrorMessage($"Error in GetAvailableUsers with ID {doctypeid}", ex)

            Return Nothing
        End Try
    End Function
    Private Function GetRelatedUsers(doctypeid As Integer) As DataTable
        Try
            Dim dt As DataTable
            Dim oSQL = $"SELECT A.GUID RelID,B.NAME as Name, B.PRENAME as Prename,B.USERNAME as Username, B.EMAIL as Email FROM TBDD_USER_DOKTYPE A 
INNER JOIN TBDD_USER B ON A.USER_ID = B.GUID WHERE A.DOCTYPE_ID = {doctypeid}"
            dt = My.DatabaseECM.GetDatatable(oSQL)
            Return dt
        Catch ex As Exception
            ShowErrorMessage($"Error in GetRelatedUsers with ID {doctypeid}", 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_GlobixRelations_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim oSQL = "SELECT DOCTYPE_ID As ID, DOCTYPE as Doctype FROM VWGI_DOCTYPE_IDB ORDER BY DOCTYPE"
            Dim oDT As DataTable = My.DatabaseECM.GetDatatable(oSQL)
            Logger = My.LogConfig.GetLogger()

            GridControlDoctypesUsers.DataSource = oDT
        Catch ex As Exception
            ShowErrorMessage($"Error in FormLoad", ex)
        End Try
    End Sub

    Private Sub GridViewDoctypesUser_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridViewDoctypesUser.FocusedRowChanged
        Dim oDTID = GridViewDoctypesUser.GetFocusedRowCellValue(GridViewDoctypesUser.Columns("ID"))
        SELECTED_DTID = oDTID
        LoadFreeUsers()
        LoadRelatedAttributes()
    End Sub

    Sub LoadRelatedAttributes()
        Dim oDT = GetRelatedUsers(SELECTED_DTID)
        If Not IsNothing(oDT) Then
            GridControlUsersRelated.DataSource = oDT
        End If
    End Sub
    Sub LoadFreeUsers()
        Dim oDT = GetAvailableUsers(SELECTED_DTID)
        If Not IsNothing(oDT) Then
            GridControlFreeUsers.DataSource = oDT
        End If
    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
        Dim selectedRows As Integer() = GridViewFreeUsers.GetSelectedRows()
        Dim i As Integer = 0
        For Each rowhandle As Integer In selectedRows
            Dim oSelectedUsrID = GridViewFreeUsers.GetRowCellValue(rowhandle, "UserID")
            AddUser(oSelectedUsrID)
        Next
        LoadFreeUsers()
        LoadRelatedAttributes()
    End Sub
    Private Function AddUser(UsrID As Integer) As Boolean
        Try
            Dim oSQL = $"
                INSERT INTO TBDD_USER_DOKTYPE (DOCTYPE_ID,USER_ID,ADDED_WHO)
                VALUES ({SELECTED_DTID},{UsrID},'{My.Application.User.UserName}')"
            Return My.DatabaseECM.ExecuteNonQuery(oSQL)
        Catch ex As Exception
            Logger.Error(ex)
            Return False
        End Try
    End Function

    Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles SimpleButton2.Click
        Dim selectedRows As Integer() = GridViewUsersRelated.GetSelectedRows()
        Dim i As Integer = 0
        For Each rowhandle As Integer In selectedRows
            Dim oSelectedUsrID = GridViewUsersRelated.GetRowCellValue(rowhandle, "RelID")
            DeleteUserRelation(oSelectedUsrID)
        Next
        LoadFreeUsers()
        LoadRelatedAttributes()
    End Sub
    Public Function DeleteUserRelation(ID As Integer) As Boolean
        Try
            Dim oSQL = $"DELETE FROM TBDD_USER_DOKTYPE WHERE GUID = {ID}"
            Return My.DatabaseECM.ExecuteNonQuery(oSQL)
        Catch ex As Exception
            Logger.Error(ex)
            Return False
        End Try
    End Function
End Class