Imports IntraSell_DLL

Public Class VorlageEditor

    Dim VORLAGE As String = VORLAGE_RECHNUNG
    Dim LOGO As String = "vorlagen\logo.png"
    Dim INTRASELL As String = ClassSettings.GetDataPath  ' "C:\intrasell_net\"

    Public Sub New(vorlageName As String)
        VORLAGE = vorlageName
        InitializeComponent()
    End Sub

    Private Sub VorlageEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            'Copy file not to lock it when saving 
            Try
                IO.File.Delete(LOGO + ".tmp")
                IO.File.Copy(LOGO, LOGO + ".tmp")
            Catch ex As Exception
            End Try
            Try
                PictureBox1.BackgroundImage = New Bitmap(LOGO + ".tmp")
            Catch ex As Exception
            End Try
            TextBox1.Text = IO.File.ReadAllText(VORLAGE)
            txtBARRECHNUNG_BEZEICHNUNG_LAENGE.Text = VarValue_Default(Variablen.BARRECHNUNG_BEZEICHNUNG_LAENGE, 20)
            cbPrintLogo.Checked = VarValue_Default(PRINT_LOGO, "true")
            cbSortiereNachWG.Checked = VarValue_Default(PRINT_SORTIERUNG_KATEGORIE, "false")

            TextBox_Kassenbon_Werbenachricht.Text = VarValue_Default("Kassenbon_Werbenachricht", "Vielen Dank für Ihren Einkauf!")
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonCancel.Click
        PictureBox1.BackgroundImage = Nothing
        Close()
    End Sub

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
        Try
            SetVarValue(Variablen.BARRECHNUNG_BEZEICHNUNG_LAENGE, txtBARRECHNUNG_BEZEICHNUNG_LAENGE.Text)
            IO.File.WriteAllText(VORLAGE, TextBox1.Text)
            IO.File.WriteAllText(INTRASELL + VORLAGE, TextBox1.Text)
            SetVarValue("Kassenbon_Werbenachricht", TextBox_Kassenbon_Werbenachricht.Text)
            Close()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub LogoLaden_Click(sender As Object, e As EventArgs) Handles ButtonLogoLaden.Click
        Try
            Dim ofd As New OpenFileDialog With {
                .Title = "Bitte Logo wählen"
            }
            ofd.ShowDialog()
            Dim photo = ofd.FileName.ToString
            Dim bmp As New Bitmap(ofd.FileName)
            PictureBox1.BackgroundImage = bmp
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonLogoSpeichern_Click(sender As Object, e As EventArgs) Handles ButtonLogoSpeichern.Click
        Try
            PictureBox1.BackgroundImage.Save(INTRASELL + LOGO, Imaging.ImageFormat.Png)
            PictureBox1.BackgroundImage.Save(LOGO, Imaging.ImageFormat.Png)
            IO.File.Delete("Logo.POS")
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.MouseClick
        Try
            TextBox1.Focus()
            TextBox1.SelectedText = ListBox1.SelectedItem
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub cbPrintLogo_MouseClick(sender As Object, e As MouseEventArgs) Handles cbPrintLogo.MouseClick
        Try
            SetVarValue("PRINT_LOGO", cbPrintLogo.Checked)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub cbSortiereNachWG_MouseClick(sender As Object, e As MouseEventArgs) Handles cbSortiereNachWG.MouseClick
        Try
            SetVarValue("PRINT_SORTIERUNG_KATEGORIE", cbSortiereNachWG.Checked)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
End Class