Imports System.ComponentModel
Imports System.Data.SqlClient

Public Class frmFormularvorlageSuche
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Dim ART = ""
    Public FV As cFormularVorlagen = Nothing

    Sub New(ART)
        InitializeComponent()
        Me.ART = ART
    End Sub
    Private Sub txtSuche_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSuche.KeyDown
        If e.KeyCode = Keys.Return Then
            takeIt()
        End If
        If e.KeyCode = Keys.Down Then
            If dgvFD.SelectedRows.Count > 0 AndAlso dgvFD.SelectedRows(0).Index < dgvFD.RowCount - 1 Then
                Dim id = dgvFD.SelectedRows(0).Index
                dgvFD.ClearSelection()
                dgvFD.Rows(id + 1).Selected = True
            End If
        End If
        If e.KeyCode = Keys.Up Then
            If dgvFD.SelectedRows.Count > 0 AndAlso dgvFD.SelectedRows(0).Index > 0 Then
                Dim id = dgvFD.SelectedRows(0).Index
                dgvFD.ClearSelection()
                dgvFD.Rows(id - 1).Selected = True
            End If
        End If
    End Sub


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

    Sub showDGV()

        Dim sqlstr As String = "SELECT top 100 [tblform_id], [tblform_art],[tblform_begriff], [tblform_KundenNr],Ordnungsbegriff  FROM [tblFormulareVorlagen] left join Adressen on AdressenNr=[tblform_KundenNr] "
        Dim where As String = ""
        Dim andstr As String = ""
        If txtSuche.Text <> "" Then
            Dim suchstr As String = txtSuche.Text
            suchstr = suchstr.Replace(",", " ")
            suchstr = suchstr.Replace(";", " ")
            Dim split = suchstr.Split(" ")
            For Each s In split
                where &= andstr & " (tblform_begriff LIKE '%" & s & "%' OR Ordnungsbegriff LIKE '%" & s & "%'  OR tblform_KundenNr LIKE '%" & s & "%' ) "
                andstr = " AND "
            Next
        End If
        where &= andstr & " tblform_art ='" & ART & "'"
        If where <> "" Then sqlstr &= " WHERE " & where
        sqlstr &= " ORDER BY tblform_KundenNr,tblform_begriff"

        dgvFD.DataSource = SQL.loadDgvBySql(sqlstr, "FMZOLL")

        setColumns()


    End Sub

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

            .AllowUserToOrderColumns = True
            
            .Columns("tblform_id").Visible = False     'ID nicht anzeigen
            .Columns("tblform_art").Visible = False     ' nicht anzeigen
            
            .Columns("tblform_begriff").Width = 250
            .Columns("tblform_begriff").HeaderText = "Vorlagenname"

            .Columns("tblform_KundenNr").Width = 100
            .Columns("tblform_KundenNr").HeaderText = "KundenNr"

            .Columns("Ordnungsbegriff").MinimumWidth = 200
            .Columns("Ordnungsbegriff").HeaderText = "Kunde"
            .Columns("Ordnungsbegriff").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

        End With
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        takeIt()
    End Sub

    Sub takeIt()
        If dgvFD.SelectedRows.Count > 0 Then
            FV = New cFormularVorlagen(dgvFD.SelectedRows(0).Cells("tblform_id").Value)
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        Else
            Me.DialogResult = Windows.Forms.DialogResult.None
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)
        If dgvFD.SelectedRows.Count > 0 Then
            If vbYes = MsgBox("Eintag wirklich löschen?", vbYesNoCancel) Then
                cFahrerDaten.delData(dgvFD.SelectedRows(0).Cells(0).Value)
                showDGV()
            End If
        End If
    End Sub


    Private Sub dgvFD_DoubleClick(sender As Object, e As EventArgs) Handles dgvFD.DoubleClick
        takeIt()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub frmFormularvorlageSuche_Load(sender As Object, e As EventArgs) Handles Me.Load
        showDGV()
    End Sub

    Private Sub frmFahrerDatenSuche_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        txtSuche.Focus()
    End Sub
End Class
