Public Class frmAdmin_DoctypeBE
    Dim SELECTED_BEID As Integer
    Private SELECTED_FREE_DoctypeID As Integer
    Private SELECTED_ID2DELETE As Integer
    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Me.Close()
    End Sub

    Private Sub frmDoctypeBusinessEntities_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
        Catch ex As Exception
            ShowErrorMessage("Unexpected error in LoadingForm", 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
        LoadFreeDoctypes()
        LoadRelatedDoctypes()
    End Sub

    Sub LoadRelatedDoctypes()
        Dim oDT = GetRelatedDoctypesByBEID(SELECTED_BEID)
        If Not IsNothing(oDT) Then
            GridControlRelatedDoctypes.DataSource = oDT
        End If
    End Sub
    Sub LoadFreeDoctypes()
        Dim oDT = GetAvailableDoctypesByBEID(SELECTED_BEID)
        If Not IsNothing(oDT) Then
            GridControlDoctypesNonRelated.DataSource = oDT
        End If
    End Sub

    Private Function GetRelatedDoctypesByBEID(beID As Integer) As DataTable
        Try
            Dim dt As DataTable
            Dim oSQL = $"select ID,Doctype from VWIDB_DOCTYPE_BUSINESS_ENTITY 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 Function GetAvailableDoctypesByBEID(beID As Integer) As DataTable
        Try
            Dim dt As DataTable
            Dim oSQL = $"select Doctype_ID as DoctypeID,Doctype from VWIDB_DOCTYPE_LANGUAGE where LANG_CODE = '{My.Application.User.Language}' AND Doctype_ID NOT IN (SELECT DOCTYPE_ID FROM TBIDB_DOCTYPE_BE 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 Sub ShowErrorMessage(errorText As String, ex As Exception)
        MsgBox(errorText & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "BE Relations")
    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles btnAddDoctype.Click
        SELECTED_FREE_DoctypeID = 0
        Get_SelectedFree_DoctypeID()
        If SELECTED_FREE_DoctypeID <> 0 Then
            If AddDoctype2BE(SELECTED_FREE_DoctypeID, SELECTED_BEID) Then
                LoadFreeDoctypes()
                LoadRelatedDoctypes()
                Show_bsiInfo("Doctype Added")
            End If
        Else
            Show_bsiInfo("Error in Doctype Add - No DoctypeID")
        End If

    End Sub
    Sub Show_bsiInfo(pInfo As String)
        bsiInfo.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
        bsiInfo.Caption = pInfo & " - " & Now.ToString
    End Sub

    Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles btnRemoveDoctype.Click
        SELECTED_ID2DELETE = 0
        GET_SelectedID2Delete()
        If SELECTED_ID2DELETE <> 0 Then
            If DeleteDoctypefromBE(SELECTED_ID2DELETE) Then
                LoadFreeDoctypes()
                LoadRelatedDoctypes()
                SELECTED_ID2DELETE = 0
                Show_bsiInfo("Doctype Removed")
            End If
        Else

            Show_bsiInfo("Error in Remove Doctype - No ID")
        End If

    End Sub
    Public Function AddDoctype2BE(Doctype_ID As Integer, BeId As Integer) As Boolean
        Try
            Dim oSQL = $"INSERT INTO TBIDB_DOCTYPE_BE (BE_ID,DOCTYPE_ID,ADDED_WHO)
                VALUES ({BeId},{Doctype_ID},'{My.Application.User.UserName}')
            "
            Return My.DatabaseIDB.ExecuteNonQuery(oSQL)
        Catch ex As Exception
            ShowErrorMessage($"Error in AddAttr2BE", ex)
            Return False
        End Try
    End Function
    Public Function DeleteDoctypefromBE(ID As Integer) As Boolean
        Try
            Dim oSQL = $"DELETE FROM TBIDB_DOCTYPE_BE WHERE GUID = {ID}"
            Return My.DatabaseIDB.ExecuteNonQuery(oSQL)
        Catch ex As Exception
            ShowErrorMessage($"Error in DeleteAttrfromBE", ex)
            Return False
        End Try
    End Function

    Private Sub Get_SelectedFree_DoctypeID()
        Dim oID = GridViewDoctypesNonRelated.GetFocusedRowCellValue(GridViewDoctypesNonRelated.Columns("DoctypeID"))
        SELECTED_FREE_DoctypeID = oID
    End Sub

    Private Sub GET_SelectedID2Delete()
        Dim oID = GridViewRelatedDoctypes.GetFocusedRowCellValue(GridViewRelatedDoctypes.Columns("ID"))
        SELECTED_ID2DELETE = oID
    End Sub
End Class