
Public Class frmControl_Detail
    Private Sub TBPM_CONTROL_TABLEBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TBPM_CONTROL_TABLEBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DD_DMSLiteDataSet)

    End Sub

    Public Function FillData(ID As Integer)
        Try
            TBPM_CONTROL_TABLETableAdapter.Connection.ConnectionString = CONNECTION_STRING
            TBPM_CONTROL_TABLETableAdapter.FillByGUID(DD_DMSLiteDataSet.TBPM_CONTROL_TABLE, ID)
            Return True
        Catch ex As System.Exception
            MsgBox(TBPM_CONTROL_TABLETableAdapter.Connection.ConnectionString & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Fehler beim Laden der Daten zu SpaltenID: " & ID)
        End Try

    End Function

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Save_Data

    End Sub
    Sub Save_Data()
        Try
            TBPM_CONTROL_TABLEBindingSource.EndEdit()
            If DD_DMSLiteDataSet.TBPM_CONTROL_TABLE.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox.Text = USER_USERNAME
                TBPM_CONTROL_TABLEBindingSource.EndEdit()
                TBPM_CONTROL_TABLETableAdapter.cmdUpdate(SPALTENNAMETextBox.Text, SPALTEN_HEADERTextBox.Text, SPALTENBREITETextBox.Text, True, READ_ONLYCheckBox.Checked, LOAD_IDX_VALUECheckBox.Checked, CHANGED_WHOTextBox.Text, REGEX_MATCHTextBox.Text, REGEX_MESSAGE_DETextBox.Text, REGEX_MESSAGE_DETextBox.Text, GUIDTextBox.Text)
                tslblAenderungen.Visible = True
                tslblAenderungen.Text = "Änderungen gespeichert - " & Now
                FillData(GUIDTextBox.Text)
            Else
                tslblAenderungen.Visible = False
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Speichern:")
        End Try
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            If MessageBox.Show("Wollen Sie diese Spalte wirklich löschen?", "Spalte löschen", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                TBPM_CONTROL_TABLETableAdapter.Delete(GUIDTextBox.Text)
                Close()
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Löschen:")
        End Try
    End Sub

    Private Sub frmControl_Detail_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.frmTableColumns_Position = Me.Location
    End Sub

    Private Sub frmControl_Detail_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Me.TBPM_CONTROL_TABLETableAdapter.Connection.ConnectionString = CONNECTION_STRING
            If My.Settings.frmTableColumns_Position.IsEmpty = False Then
                If My.Settings.frmTableColumns_Position.X > 0 And My.Settings.frmTableColumns_Position.Y > 0 Then
                    Me.Location = My.Settings.frmTableColumns_Position
                End If
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Laden der Formulardaten:")
        End Try
       
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oForm As New frmRegexEditor()
        oForm.RegexString = REGEX_MATCHTextBox.Text

        Dim oResult = oForm.ShowDialog()
        If oResult = DialogResult.OK Then
            REGEX_MATCHTextBox.Text = oForm.RegexString
        End If
    End Sub

    Private Sub btnSQLEdit_Click(sender As Object, e As EventArgs) Handles btnSQLEdit.Click
        Dim oldSQL = SQL_COMMANDTextBox.Text
        Dim oForm As New frmSQL_DESIGNER() With {.SQLCommand = oldSQL}
        Dim oResult = oForm.ShowDialog()

        If oResult = DialogResult.OK Then
            If oldSQL <> oForm.SQLCommand Then
                Dim oUpdate As String = $"Update TBPM_CONTROL_TABLE SET CHANGED_WHO = '{USER_USERNAME}', SQL_COMMAND = '{oForm.SQLCommand.Replace("'", "''")}' 
                , CONNECTION_ID = {oForm.ConnectionID} WHERE GUID = {GUIDTextBox.Text}"
                If ClassDatabase.Execute_non_Query(oUpdate) = True Then
                    tslblAenderungen.Visible = True
                    tslblAenderungen.Text = "Changes saved - " & Now
                End If

            End If
        End If
    End Sub
End Class