Imports DigitalData.Modules.Logging

Public Class frmAdmin_IDBEntity
    Implements IAdminForm
    Public Property PrimaryKey As Integer Implements IAdminForm.PrimaryKey
    Public Property HasChanges As Boolean Implements IAdminForm.HasChanges
    Public Property IsInsert As Boolean Implements IAdminForm.IsInsert

    Public Sub New(PrimaryKey As Integer, Optional IsInsert As Boolean = False)
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        InitializeBaseForm(My.LogConfig)
        Me.PrimaryKey = PrimaryKey
        Me.IsInsert = IsInsert
    End Sub

    Private Sub frmAdmin_IDBEntity_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            TBIDB_BUSINESS_ENTITYTableAdapter.Connection.ConnectionString = My.DatabaseIDB.CurrentSQLConnectionString
            If IsInsert Then
                DSIDB_Stammdaten.TBIDB_BUSINESS_ENTITY.ADDED_WHOColumn.DefaultValue = My.Application.User.UserName
                TBIDB_BUSINESS_ENTITYBindingSource.AddNew()
            Else
                TBIDB_BUSINESS_ENTITYTableAdapter.Fill(DSIDB_Stammdaten.TBIDB_BUSINESS_ENTITY, PrimaryKey)
            End If

        Catch ex As Exception
            ShowErrorMessage(ex)
        End Try
    End Sub

    Private Sub ResetMessages()
        labelStatus.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
    End Sub
    Private Sub ShowStatus(Message As String)
        labelStatus.Caption = Message
        labelStatus.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        ResetMessages()

        If SaveData() Then
            Close()
        End If
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        ResetMessages()

        If SaveData() And HasChanges Then
            ShowStatus("Business Entity saved!")
        End If
    End Sub

    Public Function SaveData() As Boolean
        Try
            TBIDB_BUSINESS_ENTITYBindingSource.EndEdit()

            If DSIDB_Stammdaten.TBIDB_BUSINESS_ENTITY.GetChanges() IsNot Nothing Then
                HasChanges = True

                If IsInsert Then
                    txtAddedWho.EditValue = My.Application.User.UserName
                Else
                    txtChangedWho.EditValue = My.Application.User.UserName
                End If

                TBIDB_BUSINESS_ENTITYBindingSource.EndEdit()
                TBIDB_BUSINESS_ENTITYTableAdapter.Update(DSIDB_Stammdaten.TBIDB_BUSINESS_ENTITY)
            End If

            Return True
        Catch ex As Exception
            ShowErrorMessage(ex)
            Return False
        End Try
    End Function

    Public Function DeleteData() As Boolean Implements IAdminForm.DeleteData
        Try
            TBIDB_BUSINESS_ENTITYTableAdapter.Delete(PrimaryKey)
            Return True
        Catch ex As Exception
            ShowErrorMessage(ex)
            Return False
        End Try
    End Function

    Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        If MsgBox($"Wollen Sie die Entität [{PrimaryKey}] wirklich löschen?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, Text) = MsgBoxResult.Yes Then
            If DeleteData() Then
                Close()
            End If
        End If
    End Sub
End Class