Public Class frmConfiguration_Basic
    Dim loaded As Boolean = False
    Dim changes_ModuleKonfig As Boolean = False
    Dim changes_Konfig As Boolean = False
    Private Sub frmConfiguration_Basic_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim sql As String = "select * from TBDD_MODULES where NAME = 'Record-Organizer'"
            Dim dt As DataTable = ClassDatabase.Return_Datatable(sql)
            If Not dt Is Nothing Then
                If dt.Rows.Count = 1 Then
                    Me.txtDelimiterVersion.Text = dt.Rows(0).Item("VERSION_DELIMITER")
                    Me.txtDelimiterFile.Text = dt.Rows(0).Item("FILE_DELIMITER")
                    Me.txtwddriveletter.Text = dt.Rows(0).Item("STRING1")
                    Me.txtProductName.Text = dt.Rows(0).Item("PRODUCT_NAME1")

                    ' Hintergrundbild unwandeln und anzeigen
                    If Not IsDBNull(dt.Rows(0).Item("BACKGROUND_IMAGE")) Then
                        Dim bimage() As Byte = dt.Rows(0).Item("BACKGROUND_IMAGE")
                        Dim image = ByteArrayToBitmap(bimage)
                        Me.pbBackground.Image = image
                    End If
                End If
            End If
            sql = "select * from TBPMO_KONFIGURATION where GUID = 1"
            dt = ClassDatabase.Return_Datatable(sql)
            If Not dt Is Nothing Then
                If dt.Rows.Count = 1 Then
                    Me.chkUnicode.Checked = dt.Rows(0).Item("WD_UNICODE")
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Form Load: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        loaded = True
    End Sub

    Private Sub TextChanged_ModuleKonfig(sender As Object, e As EventArgs) Handles txtDelimiterFile.TextChanged, txtDelimiterVersion.TextChanged, txtwddriveletter.TextChanged, txtProductName.TextChanged
        If loaded = True Then
            changes_ModuleKonfig = True
            btnsave.Enabled = True
        End If
    End Sub
   
    Private Sub chkUnicode_CheckedChanged(sender As Object, e As EventArgs) Handles chkUnicode.CheckedChanged
        If loaded = True Then
            changes_Konfig = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click
        If changes_ModuleKonfig = True Then
            Try
                ' Hintergrund speichern
                Dim image As Bitmap = Nothing
                Dim bimage() As Byte = Nothing

                Dim sql As String = "UPDATE TBDD_MODULES SET VERSION_DELIMITER = @VERSION_DELIMITER, FILE_DELIMITER = @FILE_DELIMITER, STRING1 = @STRING1, BACKGROUND_IMAGE = @BACKGROUND_IMAGE, PRODUCT_NAME1 = @PRODUCT_NAME1, CHANGED_WHO = @CHANGED_WHO WHERE NAME = 'Record-Organizer'"
                Dim conn As New SqlClient.SqlConnection(MyConnectionString)
                Dim cmd As New SqlClient.SqlCommand(sql, conn)

                If pbBackground.ImageLocation IsNot Nothing Then
                    image = CType(Drawing.Image.FromFile(pbBackground.ImageLocation, True), Bitmap)
                    bimage = BitmapToByteArray(image)
                    cmd.Parameters.Add("@BACKGROUND_IMAGE", SqlDbType.VarBinary).Value = bimage
                Else
                    cmd.Parameters.Add("@BACKGROUND_IMAGE", SqlDbType.VarBinary).Value = DBNull.Value
                End If

                cmd.Parameters.Add("@VERSION_DELIMITER", SqlDbType.Text).Value = txtDelimiterVersion.Text
                cmd.Parameters.Add("@FILE_DELIMITER", SqlDbType.Text).Value = txtDelimiterFile.Text
                cmd.Parameters.Add("@STRING1", SqlDbType.Text).Value = txtwddriveletter.Text
                cmd.Parameters.Add("@CHANGED_WHO", SqlDbType.Text).Value = Environment.UserName
                cmd.Parameters.Add("@PRODUCT_NAME1", SqlDbType.Text).Value = txtProductName.Text

                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()

                lblSaveresult.Visible = True
            Catch ex As Exception
                MsgBox("Error in Save ModuleConfig: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
        If changes_Konfig = True Then
            Try
                Dim wduni As Integer = 0
                If chkUnicode.Checked Then
                    wduni = 1
                End If
                Dim sql As String = "UPDATE TBPMO_KONFIGURATION SET WD_UNICODE = " & wduni & ",CHANGED_WHO = '" & Environment.UserName & "' where GUID = 1"
                If ClassDatabase.Execute_non_Query(sql, True) = True Then
                    lblSaveresult.Visible = True
                Else
                    lblSaveresult.Visible = False
                End If
            Catch ex As Exception
                MsgBox("Error in Save Config: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If

    End Sub

    Private Sub chkChildWindow_CheckedChanged(sender As Object, e As EventArgs) Handles chkChildWindow.CheckedChanged
        My.Settings.Save()
    End Sub

    Private Sub btnSelectBackground_Click(sender As Object, e As EventArgs) Handles btnSelectBackground.Click
        OpenFileDialog1.Filter = "PNG Bilder|*.png"

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim path As String = OpenFileDialog1.FileName
            pbBackground.ImageLocation = path
            btnsave.Enabled = True
            changes_ModuleKonfig = True
        End If
    End Sub

    Private Sub btnResetBackground_Click(sender As Object, e As EventArgs) Handles btnResetBackground.Click
        If pbBackground.Image IsNot Nothing Then
            btnsave.Enabled = True
            changes_ModuleKonfig = True
        End If

        pbBackground.ImageLocation = Nothing
        pbBackground.Image = Nothing
    End Sub
End Class