Imports DevExpress.XtraLayout
Imports DigitalData.GUIs.Common
Imports DigitalData.GUIs.Common.Base
Imports DigitalData.Modules.Logging

Public Class frmAdmin_User
    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



    Private Property Pages As ClassDetailPageManager
    Private Property Logger As Logger
    Private Property FormHelper As FormHelper

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

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        PrimaryKey = pPrimaryKey
        IsInsert = pIsInsert
        Logger = My.LogConfig.GetLogger()
    End Sub

    Private Sub frmAdmin_User_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            TBDD_USERTableAdapter.Connection.ConnectionString = My.DatabaseECM.CurrentSQLConnectionString

            If IsInsert Then
                DSDD_Stammdaten.TBDD_USER.ADDED_WHOColumn.DefaultValue = My.Application.User.UserName
                TBDD_USERBindingSource.AddNew()
            Else
                TBDD_USERTableAdapter.Fill(DSDD_Stammdaten.TBDD_USER, PrimaryKey)
            End If

            ' Add Focus Handler to all controls in all LayoutControls
            Pages = New ClassDetailPageManager(My.LogConfig, Me, New List(Of LayoutControl) From {LayoutControl1})
            Pages.Add(New ClassDetailPageManager.PrimaryPage(IsInsert) With {
                .Name = "Benutzer",
                .BindingSource = TBDD_USERBindingSource,
                .DataTable = DSDD_Stammdaten.TBDD_USER,
                .AddedWhoEdit = txtAddedWho,
                .ChangedWhoEdit = txtChangedWho
            })

            Pages.PrepareLoad()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#Region "DELETE ----------------------------------------------"
    Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        If DeleteData() Then
            Close()
        Else
            txtStatus.Caption = $"Error while deleting!"
        End If
    End Sub

    Private Function DeleteData() As Boolean Implements IAdminForm.DeleteData
        Try
            TBDD_USERTableAdapter.Delete(PrimaryKey)
            Return True
        Catch ex As Exception
            FormHelper.ShowErrorMessage(ex, "DeleteData")
            Return False
        End Try
    End Function
#End Region

#Region "SAVE ----------------------------------------------"
    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        SaveData()
    End Sub

    Private Sub frmAdmin_User_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
        If e.KeyCode = Keys.F5 Then
            SaveData()
        End If
    End Sub

    Private Sub SaveData()
        If Pages.PrepareSave() = True Then
            Try
                Dim oPage = Pages.Current
                TBDD_USERTableAdapter.Update(oPage.DataTable)
                oPage.IsInsert = False
                txtStatus.Caption = $"{oPage.Name} gespeichert!"
            Catch ex As Exception
                FormHelper.ShowErrorMessage(ex, "ItemClick")
            End Try
        Else
            txtStatus.Caption = $"Keine Änderungen"
        End If
    End Sub
#End Region
End Class