Imports IntraSell_DLL

Public Class ArtikelControl

    Dim DEFAULT_TEXT As String = "Barcode Scan ..."

    Public Const SQL = "SELECT ArtNr, trim(concat(EAN, ' ', Bezeichnung)) as Art FROM grArtikel WHERE ArtNr>0 and (ProduktAktiv<>0 or ProduktAktivOnline<>0) ORDER BY trim(concat(EAN, ' ', Bezeichnung))"

    Private _ArtNr As Integer = -1
    Private _EAN As String
    Private _Bezeichnung As String

    Public Event ArtNrChanged(ArtNr As Integer) 'wenn der User einen neuen Artikel gewählt hat 
    Public Event ArtNrAdded(ArtNr As Integer) 'wenn mit dem '+' der user einen neuen Artikel eingefügt hat 
    Public Event SerialNrErkannt(SerialNr As Object) 'wenn barcode scan einen artikel erkennt 

    Dim loading As Boolean = False
    Dim barcodeScannerSendsReturn As Boolean = True

    Public Property ArtNr() As Integer
        Get
            Return _ArtNr
        End Get
        Set(value As Integer)
            _ArtNr = value
            loading = True
            ArtikelComboBox.SelectedValue = _ArtNr
            loading = False
        End Set
    End Property

    Public ReadOnly Property EAN() As String
        Get
            Return _EAN
        End Get
    End Property

    Public ReadOnly Property Bezeichnung() As String
        Get
            Return _Bezeichnung
        End Get
    End Property

    Public Property ShowArtikelSelectorButton() As Boolean
        Get
            Return SelectorButton.Visible
        End Get
        Set(value As Boolean)
            SelectorButton.Visible = value
        End Set
    End Property

    Public Property ShowAddNew() As Boolean
        Get
            Return AddNewButton.Visible
        End Get
        Set(value As Boolean)
            AddNewButton.Visible = value
            If value Then
                TableLayoutPanel1.ColumnStyles(2).Width = 10
            Else
                TableLayoutPanel1.ColumnStyles(2).Width = 0
            End If
        End Set
    End Property

    Public Property ShowDetailsButton() As Boolean
        Get
            Return DetailsButton.Visible
        End Get
        Set(value As Boolean)
            DetailsButton.Visible = value
        End Set
    End Property

    Sub New()
        InitializeComponent()

        Try
            ReloadData()
            barcodeScannerSendsReturn = CBool(VarValue_Default(BARCODE_SCANNER_SENDS_RUTURN, "True"))
        Catch
            'Ignore
        End Try
    End Sub

    Private Sub ArtikelControl_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load

        If _ArtNr = 0 Then
            ArtikelComboBox.Text = DEFAULT_TEXT
            ArtikelComboBox.ForeColor = Color.Gray
        End If

    End Sub

    Private Sub ArtikelComboBox_DropDown(sender As Object, e As EventArgs) Handles ArtikelComboBox.DropDown
        ReloadData()
    End Sub

    Private Sub ReloadData()
        Try
            loading = True
            FillComboBox(ArtikelComboBox, SQL, "Art", "ArtNr")
            'Me.ArtikelComboBox.Text = ""
            If _ArtNr > 0 Then
                ArtikelComboBox.SelectedValue = _ArtNr
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        Finally
            loading = False
        End Try
    End Sub

    Private Sub ArtikelSelectorButton_Click(sender As Object, e As System.EventArgs) Handles SelectorButton.Click
        Try
            ArtikelSelector.Show()
            ArtikelSelector.BringToFront()
            ArtikelSelector.Focus()
            ArtikelSelector.ArtikelComboBox = ArtikelComboBox
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub ArtikelComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ArtikelComboBox.SelectedIndexChanged
        Try
            If loading Then Exit Sub
            Dim changed As Boolean = ArtikelComboBox.SelectedValue <> ArtNr
            If IsNumeric(ArtikelComboBox.SelectedValue) Then
                ArtNr = ArtikelComboBox.SelectedValue
                Try
                    _EAN = FirstRow("select ean from grArtikel where artnr = " & ArtNr)
                    _Bezeichnung = FirstRow("select Bezeichnung from grArtikel where artnr = " & ArtNr)
                Catch ex As Exception
                    'Ignore
                End Try
                If changed Then
                    RaiseEvent ArtNrChanged(ArtNr)
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    ''' <summary>
    ''' Remove selected ArtNr (for Filters etc.)
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ArtikelComboBox_TextUpdate(sender As Object, e As System.EventArgs) Handles ArtikelComboBox.TextUpdate
        Try
            If ArtikelComboBox.Text = "" Then
                ArtNr = Nothing
                If Not loading Then
                    RaiseEvent ArtNrChanged(ArtNr)
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub StammdatenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles StammdatenToolStripMenuItem.Click
        Try
            Dim k As New Artikel With {.MdiParent = Main}
            k.Show()
            k.FilterBy("ArtNr=" & _ArtNr)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' Add new Artikel
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub AddNewButton_Click(sender As Object, e As System.EventArgs) Handles AddNewButton.Click
        Try
            Dim a As New ArtikelQuick2 With {.BeginNewFlag = True}
            a.ShowDialog()
            'hier wartet bis dialog fenster zu ist
            'If k.DialogResult = Windows.Forms.DialogResult.OK Then
            loading = True
            FillComboBox(ArtikelComboBox, SQL, "Art", "ArtNr")
            ArtNr = a.ArtNrTextBox.Text
            loading = False
            RaiseEvent ArtNrAdded(ArtNr)
            'end if

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ArtikelComboBox_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ArtikelComboBox.MouseDoubleClick
        ShowArtikelDetails()
    End Sub

    Private Sub ArtikelDetailsButton_Click(sender As Object, e As EventArgs) Handles DetailsButton.Click
        ShowArtikelDetails()
    End Sub

    ''' <summary>
    ''' Artikelverwaltung öffnen
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ShowArtikelDetails()
        Try
            Artikel.FilterBy("ArtNr=" & ArtNr)
            Artikel.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' Barcode erkennung
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ArtikelComboBox_KeyUp(sender As Object, e As KeyEventArgs) Handles ArtikelComboBox.KeyUp
        Try

            If Not barcodeScannerSendsReturn Or e.KeyValue = Keys.Enter Then
                'Debug.Print("k:" & e.KeyValue)
                Dim neuerText As String = sender.Text ' + e.KeyChar
                If neuerText.Length >= 2 Then
                    Dim artNrTxt = GetArtNrNachSeriennummer(neuerText)
                    If artNrTxt <> "" Then 'erkannt serien nummer
                        Dim ArtNr = CType(artNrTxt, Integer)
                        ArtikelComboBox.Text = ArtikelBezeichnung(ArtNr)
                        ArtikelComboBox.Focus()
                        RaiseEvent ArtNrChanged(ArtNr)
                        RaiseEvent SerialNrErkannt(neuerText)
                    End If
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ArtikelComboBox_Enter(sender As Object, e As EventArgs) Handles ArtikelComboBox.Enter
        If ArtikelComboBox.Text = DEFAULT_TEXT Then
            ArtikelComboBox.Text = ""
        End If
        ArtikelComboBox.ForeColor = Color.Black
    End Sub
End Class
