Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class ArtikelListeControl

    Public Event ArtikelUebernehmen(ArtNr As Integer, EAN As String, Bezeichnung As String)

    Public ReadOnly Property ArtNrListe As List(Of Integer)
        Get
            Dim Liste As New List(Of Integer)
            For Each row In dgArtikel.SelectedRows
                Liste.Add(row.Cells(0).Value)
            Next
            Return Liste
        End Get
    End Property


    Private Sub ArtikelSelector_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            ArtikelKategorieControl1.ArtKatNr = -1
            DsArtikel.EnforceConstraints = False
            GrartikelTableAdapter.Fill(DsArtikel.grartikel)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub btnAktualisieren_Click(sender As Object, e As EventArgs) Handles btnAktualisieren.Click
        Try
            DatenNeuLaden()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Sub DatenNeuLaden()
        DsArtikel.EnforceConstraints = False
        GrartikelTableAdapter.Fill(DsArtikel.grartikel)
    End Sub

    Private Sub txtSuche_TextChanged(sender As Object, e As System.EventArgs) Handles txtSuche.TextChanged
        FilterNow()
    End Sub

    Private Sub ArtikelKategorieControl1_ArtKatNrChanged(ArtKatNr As Integer) Handles ArtikelKategorieControl1.ArtKatNrChanged2
        FilterNow()
    End Sub

    Sub FilterNow()
        Try
            Dim flt As String = "ProduktAktiv<>0 "
            'filter dataset 
            If txtSuche.Text.Length > 0 Then
                flt += " and (EAN like '%" & txtSuche.Text & "%' or Bezeichnung Like '%" & txtSuche.Text & "%' or Barcode Like '%" & txtSuche.Text & "%')"
                If ArtikelKategorieControl1.ArtKatNr > 0 Then
                    flt += " and (ArtKatNr = " & ArtikelKategorieControl1.ArtKatNr & ")"
                End If
            Else
                If ArtikelKategorieControl1.ArtKatNr > 0 Then
                    flt += " and (ArtKatNr = " & ArtikelKategorieControl1.ArtKatNr & ")"
                Else
                    flt = Nothing
                End If
            End If
            GrartikelBindingSource.Filter = flt
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub btnDetails_Click(sender As Object, e As System.EventArgs) Handles btnDetails.Click
        Try
            Dim row = Nothing
            If dgArtikel.SelectedRows.Count > 0 Then
                row = dgArtikel.SelectedRows(0)
            ElseIf dgArtikel.SelectedCells.Count > 0 Then
                Dim cell = dgArtikel.SelectedCells(0)
                row = dgArtikel.Rows(cell.RowIndex)
            End If
            If row IsNot Nothing Then
                Dim ArtNr = row.Cells(0).Value
                OffneArtikelMaske(artnr)
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Public Sub Uebernehmen()
        btnUebernehmen_Click(Nothing, Nothing)
    End Sub


    Private Sub btnUebernehmen_Click(sender As Object, e As EventArgs) Handles btnUebernehmen.Click
        Try
            Dim row = Nothing

            'If Me.dgArtikel.SelectedRows.Count > 0 Then
            '    row = Me.dgArtikel.SelectedRows(0)
            'ElseIf Me.dgArtikel.SelectedCells.Count > 0 Then
            '    Dim cell = Me.dgArtikel.SelectedCells(0)
            '    row = dgArtikel.Rows(cell.RowIndex)
            'End If
            For Each row In dgArtikel.SelectedRows
                If row IsNot Nothing Then
                    Dim ArtNr = row.Cells(0).Value
                    Dim EAN = row.Cells(1).Value
                    Dim Bezeichnung = row.Cells(2).Value
                    RaiseEvent ArtikelUebernehmen(ArtNr, EAN, Bezeichnung)
                End If
            Next
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub dgArtikel_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgArtikel.CellDoubleClick
        btnUebernehmen_Click(sender, e)
    End Sub

    Private Sub dgArtikel_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgArtikel.RowHeaderMouseClick
        Dim art = GetArtNr()
        If art > 0 Then
            Artikeloeffnen.ShowAuswahl(art)
        End If
    End Sub

    Private Function GetArtNr() As Integer
        If GrartikelBindingSource.Current IsNot Nothing Then
            Dim ds = GrartikelBindingSource.Current.row
            Return ds.ArtNr()
        End If
        Return -1
    End Function

    Private Sub ButtonNeuenArtikel_Click(sender As Object, e As EventArgs) Handles ButtonNeuenArtikel.Click
        Try
            FormManager.BeginNewArtikel()
            AddHandler ArtikelQuick2.FormClosed, AddressOf ArtikelQuick2_Closed
        Catch ex As Exception
            HandleAppError(ex, Silent:=False)
        End Try
    End Sub

    Private Sub ArtikelQuick2_Closed(sender As Object, e As FormClosedEventArgs)
        DatenNeuLaden()
    End Sub
End Class
