Imports System.ComponentModel

Public Class frmSearch

    Dim locZeilen As Integer
    Dim Grid_aktiv As Boolean
    Dim hSQL As String
    Private UID As cOptionenDAL


    Private Sub frmSearch_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' If frmLogin.USRBER > 0 Then btnMitarb.Visible = False
        '  If frmLogin.USRBER > 5 Then btnNew.Enabled = False : btnDel.Enabled = False : btnChange.Enabled = False
        UID = New cOptionenDAL
        Grid_aktiv = True
        Tabelle_anzeigen()
    End Sub

    Public Sub Tabelle_anzeigen()
        Dim start As Long = (DateTime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds
        'lblMs.Image = My.Resources.sanduhr
        'Paint wieder reaktivieren
        locZeilen = -1
        If Not Grid_aktiv Then Exit Sub
        Dim top As String = "" ' "top 100"

        'je nach Auswahl SQL anpassen
        'je nach Auswahl SQL anpassen
        hSQL = "SELECT " & top & " kd_id, kd_firma, (kd_str + ' ' + kd_strnr) as strasse, (kd_plz + ' - ' + kd_ort) as ort, kd_land,kd_firma, kd_str, kd_strnr, kd_land, kd_plz, kd_ort,  (kd_anspr1_vn + ' ' + kd_anspr1_nn) as anspr FROM tblKunden "
        Dim search As String = txtSuche.Text
        Dim where As String = ""
        If txtSuche.Text <> "" Then
            'where += " kd_firma LIKE '%" & search & "%'"



            Dim arr() As String = Split(txtSuche.Text.Replace(" ", ","), ",")

            Dim cnt As Integer = 1
            If arr.Count() > 0 Then cnt = arr.Count
            Dim or_txt As String = ""
            For i As Integer = 0 To cnt - 1 Step 1
                If arr(i) <> "" Then search = arr(i).Trim
                If i > 0 Then where += " AND "
                where += " ( kd_firma LIKE '%" & search & "%' OR  kd_str LIKE '%" & search & "%' OR  kd_strnr LIKE '%" & search & "%' OR  kd_plz LIKE '%" & search & "%' OR  kd_ort LIKE '%" & search & "%' OR  kd_land LIKE '%" & search & "%' OR  kd_anspr1_vn LIKE '%" & search & "%' OR  kd_anspr1_nn LIKE '%" & search & "%' OR  kd_anspr2_vn LIKE '%" & search & "%' OR  kd_anspr2_nn LIKE '%" & search & "%' OR  kd_tel LIKE '%" & search & "%' OR  kd_fax LIKE '%" & search & "%' OR  kd_email LIKE '%" & search & "%' OR  kd_homepage LIKE '%" & search & "%' ) "
            Next


            where = " WHERE " & where
        End If

        ' MsgBox(hSQL + where)
        dgvUID.DataSource = UID.AnzeigeTabelle(hSQL + where)


        'Tabelle Kommt/Geht bereinigen
        If dgvUID.RowCount = 0 Then
            dgvUID.DataSource = Nothing
        Else
            Spalten_festlegen()
            '  Eintraege_festlegen()
            'AL: Sortierung nach Ausfahrt (Zeit)
            dgvUID.Sort(dgvUID.Columns(3), ListSortDirection.Ascending)
        End If

        'Anzeige der Anzahl gewählter/aller Datensätze
        If (dgvUID.RowCount = 100) Then
            'lblAnzahl.Text = Format(dgvUID.RowCount, "#,##0") & " ausgewählt (Die Suche wurde auf 100 Ergebnisse beschränkt.)"
        Else
            ' lblAnzahl.Text = Format(dgvUID.RowCount, "#,##0") & " ausgewählt"
        End If
        ' lblAnzahl.Refresh()
        Dim ende As Long = (DateTime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds
        ' lblMs.Text = "Ladezeit: " & (ende - start) & " ms"
        'lblMs.Image = Nothing
    End Sub

    Private Sub Spalten_festlegen()
        With dgvUID
            .RowTemplate.Height = 20
            .RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = True
            .AllowUserToResizeRows = False
            .RowTemplate.ReadOnly = True
            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            .Columns(0).Visible = False

            .Columns(1).Width = 365
            .Columns(1).HeaderText = "Firma"
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

            .Columns(2).Width = 250
            .Columns(2).HeaderText = "Straße"

            .Columns(3).Width = 250
            .Columns(3).HeaderText = "Ort"

            .Columns(4).Width = 200
            .Columns(4).HeaderText = "Land"



        End With
    End Sub

    Private Sub dgvUID_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvUID.CellDoubleClick
        Button3.PerformClick()
    End Sub
    Private Sub SearchChanged(sender As Object, e As EventArgs) Handles txtSuche.TextChanged
        Tabelle_anzeigen()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        With dgvUID
            'frmAuftrag.getDataFromSearch(.CurrentRow.Cells(5).Value, .CurrentRow.Cells(6).Value, .CurrentRow.Cells(7).Value, .CurrentRow.Cells(8).Value, .CurrentRow.Cells(9).Value, .CurrentRow.Cells(10).Value, "", "", .CurrentRow.Cells(11).Value, .CurrentRow.Cells(0).Value)
            Me.Close()
        End With
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        ' frmDetails.rowId = -1 : frmDetails.mainForm = "frmSearch" : frmDetails.Show()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        '  frmDetails.rowId = dgvUID.CurrentRow.Cells(0).Value : frmDetails.mainForm = "frmSearch" : frmDetails.Show()
    End Sub
End Class