Imports DevExpress.XtraEditors.DXErrorProvider
Imports DevExpress.XtraLayout

Public Class frmAdmin_SourceSQL
    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 Pages As ClassDetailPages

    Public Sub New(PrimaryKey As Integer)
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

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

    Private Sub frmAdmin_SourceSQL_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Pages = New ClassDetailPages(My.LogConfig, Me, New List(Of LayoutControl) From {LayoutControl1})
            Pages.Add(New ClassDetailPages.PrimaryPage(IsInsert) With {
                .Name = "Source SQL",
                .AddedWhoEdit = txtAddedWho,
                .ChangedWhoEdit = txtChangedWho,
                .BindingSource = TBZF_ADMIN_SOURCE_SQLBindingSource,
                .DataTable = DSIDB_Stammdaten.TBZF_ADMIN_SOURCE_SQL,
                .IsInsert = IsInsert,
                .TabPage = Nothing
            })

            TBZF_ADMIN_SOURCE_SQLTableAdapter.Connection.ConnectionString = My.DatabaseECM.CurrentSQLConnectionString

            If IsInsert Then
                TBZF_ADMIN_SOURCE_SQLBindingSource.AddNew()
            Else

                TBZF_ADMIN_SOURCE_SQLTableAdapter.Fill(DSIDB_Stammdaten.TBZF_ADMIN_SOURCE_SQL, PrimaryKey)
            End If

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

    Public Sub ValidationHelper()
        Dim oTable As DataTable = DSIDB_Stammdaten.TBZF_ADMIN_SOURCE_SQL
        Dim oProvider As DXValidationProvider = DxValidationProvider1
        Dim oRootControl As Control = LayoutControl1
        Dim oControls As List(Of Control) = oRootControl.Controls.
            OfType(Of Control).
            ToList()

        Dim oBoundControls = ListBoundControls(oControls)

        Console.WriteLine(oBoundControls.Count)

        For Each oControl As Control In oBoundControls
            Dim oBinding As Binding = oControl.DataBindings.Item(0)
        Next
    End Sub


    Public Function ListBoundControls(Controls As List(Of Control)) As List(Of Control)
        Dim oControlList As New List(Of Control)

        For Each oControl As Control In Controls
            If oControl.DataBindings IsNot Nothing AndAlso oControl.DataBindings.Count > 0 Then
                oControlList.Add(oControl)
            End If

            If oControl.Controls IsNot Nothing AndAlso oControl.Controls.Count > 0 Then
                Dim oControls As New List(Of Control)
                oControls.AddRange(oControl.Controls.OfType(Of Control))

                oControlList.AddRange(ListBoundControls(oControls))
            End If
        Next

        Return oControlList
    End Function

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

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

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

                TBZF_ADMIN_SOURCE_SQLBindingSource.EndEdit()
                TBZF_ADMIN_SOURCE_SQLTableAdapter.Update(DSIDB_Stammdaten.TBZF_ADMIN_SOURCE_SQL)
            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
            TBZF_ADMIN_SOURCE_SQLTableAdapter.Delete(PrimaryKey)
            Return True
        Catch ex As Exception
            ShowErrorMessage(ex)
            Return False
        End Try
    End Function



    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 BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        ResetMessages()

        If SaveData() And HasChanges Then
            ShowStatus("Attribute gespeichert!")
        End If
    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 BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        If MsgBox($"Wollen Sie den SourceSQL [{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