Public Class frmKundenSuche
    Public erweitern As Boolean = False
    Dim sql As New SQL
    Public WithEvents kundenNrSearch As New TextBox

    Sub New()
        InitializeComponent()
    End Sub
    Sub New(searchtxt As String)
        InitializeComponent()
        Me.txtKurzname.Text = searchtxt
    End Sub
    Sub New(searchKdnr As Integer)
        InitializeComponent()
        Me.txtKundenNr.Text = searchKdnr
    End Sub
    Private Sub frmKundenSuche_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.Height = Me.Height - pnlErweiterteSuche.Height

        cbxLandKz.fillWithSQL("SELECT Währungstabelle.LandKz, [Länderverzeichnis für die Außenhandelsstatistik].LandBez " &
                            " FROM [Länderverzeichnis für die Außenhandelsstatistik] LEFT JOIN Währungstabelle ON [Länderverzeichnis für die Außenhandelsstatistik].LandNr = Währungstabelle.Währungsschlüssel " &
                                    " WHERE Währungstabelle.LandKz Is Not NULL " &
                            " ORDER BY [Länderverzeichnis für die Außenhandelsstatistik].LandBez ", True, "FMZOLL")
        cbxLandKz.Items.Insert(0, New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))

        For Each p In Panel1.Controls
            If p.GetType.Name = "TextBox" Then
                RemoveHandler DirectCast(p, TextBox).KeyUp, AddressOf checkKey
                AddHandler DirectCast(p, TextBox).KeyUp, AddressOf checkKey
            ElseIf p.GetType.Name = "MyTextBox" Then
                RemoveHandler DirectCast(p, VERAG_PROG_ALLGEMEIN.MyTextBox).KeyUp, AddressOf checkKey
                AddHandler DirectCast(p, VERAG_PROG_ALLGEMEIN.MyTextBox).KeyUp, AddressOf checkKey
            ElseIf p.GetType.Name = "MyComboBox" Then
                RemoveHandler DirectCast(p, VERAG_PROG_ALLGEMEIN.MyComboBox).KeyUp, AddressOf checkKey
                AddHandler DirectCast(p, VERAG_PROG_ALLGEMEIN.MyComboBox).KeyUp, AddressOf checkKey
            End If
        Next

    End Sub

    Private Sub FlatButton3_Click(sender As Object, e As EventArgs) Handles FlatButton3.Click
        initSearch()
    End Sub
    Sub initSearch()
        With DataGridView1
            .Columns.Clear()
            Dim SQLstr As String = " SELECT top 10 Ordnungsbegriff as Firma, AdressenNr as KdNr,Adressen.[LandKz] + ' ' + Adressen.[PLZ] + ' - ' + Adressen.[Ort] + ' ' + Adressen.[Straße] as Adresse, (Adressen.[UstIdKz] + Adressen.[UstIdNr]) as UID,EORITIN as EORI  "

            If VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "FRONTOFFICE" Or VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "VERIMEX" Or VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("AVISO_IMEX", "AVISO") Or VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("OFFERTE_FREMD_VERAGIMEX", "SDL") Then SQLstr &= ",  Filialen.Firma as Firma_Intern  "

            SQLstr &= "    From [Adressen] INNER JOIN Kunden ON KundenNr=AdressenNr inner join Filialen on Filialen.FilialenNr=Kunden.FilialenNr WHERE 1=1 "
            Dim suchparam As Boolean = False
            If txtKurzname.Text <> "" Then SQLstr &= " AND Ordnungsbegriff LIKE '" & txtKurzname.Text & "%'" : suchparam = True
            If txtKundenNr.Text <> "" Then SQLstr &= " AND AdressenNr LIKE '" & txtKundenNr.Text & "%'" : suchparam = True
            If txtStrasse.Text <> "" Then SQLstr &= " AND Adressen.Straße LIKE '" & txtStrasse.Text & "%'" : suchparam = True
            If txtOrt.Text <> "" Then SQLstr &= " AND Adressen.Ort LIKE '" & txtOrt.Text & "%'" : suchparam = True
            If txtPlz.Text <> "" Then SQLstr &= " AND Adressen.[PLZ] LIKE '" & txtPlz.Text & "%'" : suchparam = True
            If txtUID.Text <> "" Then SQLstr &= " AND (Adressen.[UstIdKz] + Adressen.[UstIdNr]) LIKE '" & txtUID.Text.Replace("*", "%") & "'" : suchparam = True
            If txtEORI.Text <> "" Then SQLstr &= " AND [EORITIN] LIKE '" & txtEORI.Text.Replace("*", "%") & "'" : suchparam = True
            If cbxLandKz.Text <> "" Then SQLstr &= " AND Adressen.LandKz = '" & DirectCast(cbxLandKz.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value & "'" : suchparam = True
            Select Case cbxAktiv.CheckState
                Case CheckState.Checked : SQLstr &= " AND Auswahl='A' " : suchparam = True
                Case CheckState.Unchecked : SQLstr &= " AND Auswahl='I' " : suchparam = True
            End Select
            Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
                Case "FRONTOFFICE" : SQLstr &= "  AND '" & VERAG_PROG_ALLGEMEIN.cAllgemein.CLUSTER & "' IN (Filialen.Firma,Filialen.Cluster) " ' Wenn die Firmenbezeichnung im CLUSter vorkommt .--> Frontoffice
                Case "VERIMEX" : SQLstr &= "  AND '" & VERAG_PROG_ALLGEMEIN.cAllgemein.CLUSTER & "' IN (Filialen.Firma,Filialen.Cluster) " ' Wenn die Firmenbezeichnung im CLUSter vorkommt .--> Frontoffice
                Case "VERAG", "ATILLA" : SQLstr &= " AND isnull(Kunden.[FilialenNr],0) NOT IN ('5501','5601','5701','5801') " : suchparam = True
                Case Else : SQLstr &= "  AND Filialen.Firma='" & VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA & "' " : suchparam = True
            End Select



            'If txtPostfachOrt.Text <> "" Then SQLstr &= " AND Postfach LIKE '%" & txtPostfachOrt.Text & "%'" : suchparam = True
            'If txtPostfachPlz.Text <> "" Then SQLstr &= " AND PLZPf LIKE '%" & txtPostfachPlz.Text & "%'" : suchparam = True
            'MsgBox(SQLstr)
            SQLstr &= " order by Ordnungsbegriff "
            If suchparam Then
                .DataSource = sql.loadDgvBySql(SQLstr, "FMZOLL")
            Else
                '  MsgBox("Bitte geben Sie einen Suchbegriff ein!")
            End If
            If .RowCount = 0 Then Exit Sub
            .Columns("KdNr").Width = 60
            .Columns("Firma").Width = 250
            If .Columns("Firma_intern") IsNot Nothing Then .Columns("Firma_intern").HeaderText = "Firma"
            .Columns("Adresse").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            '  .Focus()
        End With
    End Sub

    Private Sub checkKey(sender As Object, e As KeyEventArgs) Handles txtKurzname.KeyDown, txtKundenNr.KeyDown, txtStrasse.KeyDown, txtOrt.KeyDown, txtPlz.KeyDown
        If DataGridView1.RowCount > 0 Then
            Select Case e.KeyCode
                Case Keys.Return : FlatButton1.PerformClick()
                Case Keys.Down
                    Dim i As Integer = DataGridView1.CurrentRow.Index
                    If (i + 1 < DataGridView1.RowCount) Then
                        DataGridView1.CurrentCell = DataGridView1.Item(2, i + 1)
                    End If
                    e.Handled = True : e.SuppressKeyPress = True
                Case Keys.Up
                    Dim i As Integer = DataGridView1.CurrentRow.Index
                    If (i > 0) Then
                        DataGridView1.CurrentCell = DataGridView1.Item(2, i - 1)
                    End If
                    e.Handled = True : e.SuppressKeyPress = True
            End Select
        End If

    End Sub

    Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1.Click
        If DataGridView1.SelectedCells.Count > 0 AndAlso IsNumeric(DataGridView1.CurrentRow.Cells("KdNr").Value) Then
            kundenNrSearch.Text = DataGridView1.CurrentRow.Cells("KdNr").Value
        Else
            Me.DialogResult = DialogResult.None
        End If
    End Sub

    Private Sub DataGridView1Keydown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Return Then
            FlatButton1.PerformClick()
        End If
        e.Handled = True
        e.SuppressKeyPress = True
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        FlatButton1.PerformClick()
    End Sub


    Private Sub txtKurzname_TextChanged(sender As Object, e As EventArgs) Handles txtKurzname.TextChanged, txtKundenNr.TextChanged, txtStrasse.TextChanged, txtOrt.TextChanged, cbxLandKz.TextChanged, txtPlz.TextChanged
        initSearch()
    End Sub

    Private Sub lklErweitert_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lklErweitert.LinkClicked
        SucheErweitern()
    End Sub

    Public Sub SucheErweitern()
        pnlErweiterteSuche.Visible = Not pnlErweiterteSuche.Visible
        If pnlErweiterteSuche.Visible Then
            Me.Height = Me.Height + pnlErweiterteSuche.Height
        Else
            Me.Height = Me.Height - pnlErweiterteSuche.Height
        End If
    End Sub
    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged

        FlatButton1.Enabled = (DataGridView1.SelectedCells.Count > 0 AndAlso IsNumeric(DataGridView1.CurrentRow.Cells("KdNr").Value))

    End Sub

    Private Sub frmKundenSuche_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If erweitern Then
            SucheErweitern()
        End If
        DataGridView1.Columns.Clear()
    End Sub

    Private Sub cbxAktiv_CheckedChanged(sender As Object, e As EventArgs) Handles cbxAktiv.CheckedChanged
        initSearch()
    End Sub

    Private Sub pnlErweiterteSuche_Paint(sender As Object, e As PaintEventArgs) Handles pnlErweiterteSuche.Paint

    End Sub
End Class