Imports DigitalData.GUIs.Common

Public Class frmIDBCommonSQL
    Private Sub TBIDB_COMMON_SQLBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TBIDB_COMMON_SQLBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DSIDB_Stammdaten)

    End Sub

    Private Sub frmIDBCommonSQL_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.TBIDB_COMMON_SQLTableAdapter.Connection.ConnectionString = My.DatabaseIDB.CurrentSQLConnectionString
            Me.TBIDB_COMMON_SQLTableAdapter.Fill(Me.DSIDB_Stammdaten.TBIDB_COMMON_SQL)
        Catch ex As Exception

        End Try


    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.Close()
    End Sub
    Private Function SaveSQL() As Boolean
        Try
            TBIDB_COMMON_SQLBindingSource.EndEdit()
            If DSIDB_Stammdaten.TBIDB_COMMON_SQL.GetChanges() IsNot Nothing Then
                CHANGED_WHOTextBox.Text = My.Application.User.UserName
                TBIDB_COMMON_SQLBindingSource.EndEdit()
                TBIDB_COMMON_SQLTableAdapter.Update(DSIDB_Stammdaten.TBIDB_COMMON_SQL)
            End If

            ShowStatus("SQL-Command saved", Color.DodgerBlue)
            Return True
        Catch ex As Exception
            ShowStatus($"Error saving SQL-Command {ex.Message} - {Now.ToString}", Color.Red)
            Return False
        End Try
    End Function
    Private Sub ShowStatus(Message As String, ocolor As Color)
        bsiStatus.Caption = $"{Message} - {Now.ToString}"
        bsiStatus.ItemAppearance.Normal.BackColor = ocolor
        bsiStatus.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
    End Sub

    Private Sub TBIDB_COMMON_SQLBindingSource_PositionChanged(sender As Object, e As EventArgs) Handles TBIDB_COMMON_SQLBindingSource.PositionChanged
        bsiStatus.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        SaveSQL()
    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
        Dim oSQLbefore = SQL_COMMANDTextBox.Text
        Dim oForm2 As New frmSQLEditor(My.LogConfig, My.DatabaseECM) With {
            .SQLCommand = SQL_COMMANDTextBox.Text,
            .SQLConnection = 2
         }
        oForm2.ShowDialog()

        If oForm2.DialogResult = DialogResult.OK And oSQLbefore <> oForm2.SQLCommand Then
            SQL_COMMANDTextBox.Text = oForm2.SQLCommand
            'TBDD_INDEX_MANTableAdapter.Adapter.UpdateCommand.Parameters("@SQL_RESULT").Value = oForm.SQLCommand
            'TBDD_INDEX_MANTableAdapter.Adapter.UpdateCommand.Parameters("@CONNECTION_ID").Value = oForm.ConnectionID
            CHANGED_WHOTextBox.Text = My.Application.User.UserName
        End If
    End Sub
End Class