Public Class frmViewsUser

    Private Sub frmViewsUser_Load(sender As Object, e As EventArgs) Handles Me.Load
        Load_Users_GS()
    End Sub
    Sub Load_Users_GS()
        Dim Sql = String.Format("SELECT * FROM TBDD_USER WHERE MODULE_RECORD_ORG = 1 AND GUID NOT IN (SELECT USER_ID FROM TBPMO_USER_GLOBAL_SEARCH) ORDER BY USERNAME")
        Dim DT_USER = ClassDatabase.Return_Datatable(Sql)
        Sql = String.Format("SELECT * FROM TBDD_USER WHERE GUID IN (SELECT USER_ID FROM TBPMO_USER_GLOBAL_SEARCH) ORDER BY USERNAME")
        Dim DT_USERS_CONFIGURED = ClassDatabase.Return_Datatable(Sql)
        Try
            DD_ECMAdmin.TBAD_Users.Clear()

            For Each row As DataRow In DT_USER.Rows
                Dim newUserRow As DD_ECMAdmin.TBAD_UsersRow
                newUserRow = DD_ECMAdmin.TBAD_Users.NewTBAD_UsersRow
                newUserRow.Username = row.Item("USERNAME")
                newUserRow.Surname = row.Item("NAME")
                newUserRow.Prename = row.Item("PRENAME")
                Try
                    newUserRow.Email = row.Item("EMAIL")
                Catch ex As Exception
                    newUserRow.Email = ""
                End Try

                newUserRow.ID = row.Item("GUID")
                DD_ECMAdmin.TBAD_Users.Rows.Add(newUserRow)
                ' chklbxUserForGroup.Items.Add(New MyListBoxItem() With {.Text = row.Item(1), .ExtraData = row.Item(0)})
            Next

            Try
                DD_ECMAdmin.TBWH_Users1.Clear()

                For Each row As DataRow In DT_USERS_CONFIGURED.Rows
                    Dim newUserRow As DD_ECMAdmin.TBWH_Users1Row
                    newUserRow = DD_ECMAdmin.TBWH_Users1.NewTBWH_Users1Row
                    newUserRow.Surname = row.Item("NAME")
                    newUserRow.Prename = row.Item("PRENAME")
                    newUserRow.Username = row.Item("USERNAME")
                    Try
                        newUserRow.Email = row.Item("EMAIL")
                    Catch ex As Exception
                        newUserRow.Email = ""
                    End Try

                    newUserRow.ID = row.Item("GUID")
                    DD_ECMAdmin.TBWH_Users1.Rows.Add(newUserRow)
                    ' chklbxUserForGroup.Items.Add(New MyListBoxItem() With {.Text = row.Item(1), .ExtraData = row.Item(0)})
                Next
            Catch ex As Exception
                MsgBox("Error loading Users for Global Search:" & vbNewLine & ex.Message)
            End Try

        Catch ex As Exception
            MsgBox("Error in Load_Users_GS:" & vbNewLine & ex.Message)
        End Try

    End Sub

    Private Sub btnAddUser2ConstructorREL_Click(sender As Object, e As EventArgs) Handles btnAddUser2ConstructorREL.Click
        Try
            For Each row As DataRow In DD_ECMAdmin.TBAD_Users.Rows
                If row.Item(0) = CBool(True) Then
                    Dim insert = "INSERT INTO TBPMO_USER_GLOBAL_SEARCH (USER_ID,ADDED_WHO) VALUES (" & row.Item(5) & ", '" & USER_USERNAME & "')"
                    If ClassDatabase.Execute_non_Query(insert, False) = False Then
                        MsgBox("Could not insert the User-Definition....Check the logfile!", MsgBoxStyle.Exclamation)
                    End If
                End If
            Next
            For Each row As DataRow In DD_ECMAdmin.TBAD_Users.Rows
                row.Item(0) = CBool(False)
            Next
            Load_Users_GS()
        Catch ex As Exception
            MsgBox("Error while adding user to Constructor-Relation: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btndeleteUserConstructorRel_Click(sender As Object, e As EventArgs) Handles btndeleteUserConstructorRel.Click
        Try
            Dim refresh As Boolean = False
            For Each row As DataRow In DD_ECMAdmin.TBWH_Users1.Rows
                If row.Item(0) = CBool(True) Then
                    Dim delete = "DELETE FROM TBPMO_USER_GLOBAL_SEARCH WHERE GUID = " & row.Item(5)
                    If ClassDatabase.Execute_non_Query(delete, True) Then
                        refresh = True
                    End If
                End If
            Next
            If refresh = True Then Load_Users_GS()
        Catch ex As Exception
            MsgBox("Error while deleting user to Constructor-Relation: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
End Class