Imports IntraSell_DLL

Public Class ucSuche

    Public Event GridDblClick(sender As Object, e As MouseEventArgs)

    Dim SQL = <a>
                   SELECT        'Artikel' AS Typ, 'Artikel' AS Typ1, ArtNr AS Id, CONCAT(EAN, '-', Bezeichnung) AS Bezeichnung
FROM            grartikel
WHERE        (Bezeichnung LIKE @Suche) OR
                         (EAN LIKE @Suche) OR
                         (Barcode LIKE @Suche) OR
                         (ArtNr LIKE @Suche)
UNION
SELECT        'Kunde' AS Typ, 'Kunde' AS Typ1,  IDNR, CONCAT(IFNULL(NAME,''), '-', IFNULL(Vorname,''), '-' , IFNULL(Firma,''), '-', IFNULL(Ort,'')) AS Bezeichnung
FROM            ofadressen
WHERE        (IDNR LIKE @Suche) OR
                         (Name LIKE @Suche) OR
                         (Vorname LIKE @Suche) OR
                         (Firma LIKE @Suche) OR
                         (Email LIKE @Suche) OR
                         (Tel LIKE @Suche) OR
                         (Mobil LIKE @Suche) OR
                         (SkypeAdresse LIKE @Suche) OR
                         (MessengerAdresse LIKE @Suche) OR
                         (UID LIKE @Suche)
UNION
SELECT        buchVorgangTyp.Bezeichnung AS Typ, 'Vorgang' AS Typ1, CONCAT(buchvorgang.Typ, '-', buchvorgang.Nummer) AS Nummer, CONCAT('Datum ', DATE_FORMAT( buchvorgang.Datum, '%d.%m.%Y'), ', Kunde ', IFNULL(ofadressen_1.Name, '')) AS Bezeichnung
FROM          buchvorgang ,  ofadressen ofadressen_1 , buchVorgangTyp
WHERE buchvorgang.Datum >  DATE_ADD(CURRENT_TIMESTAMP, INTERVAL  -365 DAY)  
    and buchVorgangTyp.Typ = buchvorgang.Typ 
    and buchvorgang.KundNr = ofadressen_1.IDNR              
    and (buchvorgang.Nummer LIKE @Suche
    or Exists(SELECT 1 FROM buchvorgaengeeigenschaften ve WHERE ve.VorgangTyp = buchvorgang.Typ AND ve.Nummer = buchvorgang.Nummer 
                        AND `VALUE` LIKE @Suche 
                        AND ErstelltAm >  DATE_ADD(CURRENT_TIMESTAMP, INTERVAL  -365 DAY))
         ) 

                   </a>.Value

    Dim clickedItems As New List(Of String)

    Public Sub SucheNow()
        Try
            Cursor = Cursors.IBeam
            DsSuche.Suche.Clear()
            Dim cmd = New MySql.Data.MySqlClient.MySqlCommand(SQL)
            cmd.Parameters.AddWithValue("Suche", "%" + TextBoxSuche.Text + "%")

            Dim dt = OpenRecordsetInMemory(cmd)
            Dim rowIndex As Integer = 0
            For Each r In dt.Rows
                Dim nR As dsSuche.SucheRow = DsSuche.Suche.NewRow()
                nR.Typ = r("Typ")
                nR.Typ1 = r("Typ1")
                nR.Id = extract(r("id"))
                nR.Bezeichnung = extract(r("Bezeichnung"))

                DsSuche.Suche.AddSucheRow(nR)
                rowIndex += 1
                If rowIndex > 100 Then
                    Cursor = Cursors.Default
                    Exit Sub
                End If
            Next
        Catch ex As Exception
            Label1.Text = (ex.Message)
        End Try
        Cursor = Cursors.Default
    End Sub

    Function Extract(feld As Object)
        If TypeOf feld Is String Then
            Return feld
        Else
            Return System.Text.Encoding.UTF8.GetString(feld)
        End If
    End Function

    Private Sub ButtonSuche_Click(sender As Object, e As EventArgs) Handles ButtonSuche.Click
        SucheNow()
    End Sub

    Private Sub DGSuche_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGSuche.CellClick
        DGSuche_RowClick()
    End Sub

    Private Sub DGSuche_RowClick()
        Try
            Dim row As dsSuche.SucheRow = SucheBindingSource.Current.row

            Dim clickedName = row.Id & "_" & row.Typ & "_" & row.Bezeichnung
            If clickedItems.Contains(clickedName) Then
                Label1.Text = "Maske bereits offen!"
                Exit Sub
            End If
            clickedItems.Add(clickedName)

            If row.Typ1 = "Artikel" Then
                OffneArtikelMaske(row.Id)
            ElseIf row.Typ1 = "Kunde" Then
                OeffneKundenMaske(row.Id, True)
            ElseIf row.Typ1 = "Vorgang" Then
                Dim Typ = Split(row.Id, "-")(0)
                Dim Nummer = Split(row.Id, "-")(1)

                If (IsBarVorgang(Typ, Nummer)) Then
                    OeffneBarVorgangByNummer(Typ, Nummer)
                Else
                    OeffneVorgangByNummer(Typ, Nummer)
                End If
            End If
        Catch ex As Exception
            Label1.Text = (ex.Message)
        End Try
    End Sub

    Private Sub TextBoxSuche_Validated(sender As Object, e As EventArgs) Handles TextBoxSuche.Validated
        ButtonSuche_Click(sender, e)
    End Sub

    Private Sub TextBoxSuche_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBoxSuche.KeyDown
        If e.KeyCode = Keys.Enter Then
            ButtonSuche_Click(sender, e)
        End If
    End Sub

    Private Sub DGSuche_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles DGSuche.MouseDoubleClick
        Try
            RaiseEvent GridDblClick(sender, e)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub DGSuche_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DGSuche.CellFormatting
        Try
            Dim r As dsSuche.SucheRow = DGSuche.Rows(e.RowIndex).DataBoundItem.row


            If r.Typ = "Artikel" Then
                e.CellStyle.BackColor = Color.AliceBlue
            ElseIf r.Typ = "Kunde" Then
                e.CellStyle.BackColor = Color.Aquamarine
            ElseIf r.Typ = "Rechnung" Then
                e.CellStyle.BackColor = Color.Beige
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class
