Imports System.Data.SqlClient

Public Class usrCntlVERAGCard


    Dim kundenSQL As New kundenSQL
    '  Private bindingSDL As New BindingSource
    'Private dataAdapterLKW As New SqlDataAdapter
    Dim SQL As New SQL
    Dim FUNC As New cProgramFunctions
    Dim bindingString As String
    Dim aktbtn As String = ""
    Dim dgvInitWait As Boolean = False
    Public kdnr As Integer
    Dim txtchanged As Boolean = False

    Public displayFilter As Boolean = True
    Public onlyKdNr As Boolean = False

    Private Sub usrCntlVERAGCard_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' btnFilter.Left = Panel4.Width - btnFilter.Width
        pnlFilter.AutoScroll = False
        'Me.dgvSDL.DataSource = Me.bindingSDL

        '        newBinding("SELECT KartenNr, KundenNr, KfzKennzeichen, Fahrer, Datum, Kaution, BelegNr, Ersatzkarte, Gesperrt, Defekt, Verloren, Gestohlen, Vermerk, Erfassungsdatum, Änderungsdatum, Sachbearbeiter FROM Kartenpool WHERE 1=1 " & where)
        initDGV()
        If displayFilter Then : initFilter()
        Else : pnlFilter.Visible = False : Panel1.Height = 43
        End If 
        If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_Leistungen_bearbeiten", "SDL") Then
            btnNew.Enabled = False
            btnEdit.Enabled = False
        End If

    End Sub

    Sub initDGV()
        Try

            dgvInitWait = True
            txtchanged = True
            With dgvSDL
                Dim sqlstr = "SELECT KartenNr, KundenNr, KfzKennzeichen, Fahrer, Datum, Kaution, BelegNr, Ersatzkarte, Gesperrt, Defekt, Verloren, Gestohlen, Vermerk, Erfassungsdatum, Änderungsdatum, Sachbearbeiter FROM Kartenpool WHERE 1=1 "
                dgvSDL.Columns.Clear()
                '  clearFilter()
                If kdnr > 0 Then sqlstr &= " AND KundenNr='" & kdnr & "' "
                Dim filter As String = getFilter()
                If filter <> "" Then sqlstr &= " AND " & filter
                ' MsgBox(sqlstr)
                dgvSDL.SET_SQL(sqlstr & "ORDER BY KartenNr", "SDL")
                dgvSDL.LOAD()
                If dgvSDL IsNot Nothing And dgvSDL.ColumnCount > 0 Then
                    .RowTemplate.Height = 20
                    .AllowUserToOrderColumns = False
                    .AllowUserToResizeRows = False
                    .Columns("Vermerk").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Columns("Vermerk").MinimumWidth = 150
                    .Columns("Kaution").DefaultCellStyle.Format = String.Format("C2")
                    .Columns("Erfassungsdatum").ReadOnly = True
                    .Columns("Änderungsdatum").ReadOnly = True
                    .Columns("Sachbearbeiter").ReadOnly = True
                    If False Then


                        .Columns("Vermerk").MinimumWidth = 150
                        .Columns("Erfassungsdatum").Visible = False
                        .Columns("Änderungsdatum").Visible = False
                        .Columns("Sachbearbeiter").Visible = False
                        .Columns("SDLNr").Width = 50
                        If Not aktbtn = "btnSDL_Alle" Then .Columns("SDLNr").Visible = False
                        Try
                            .Columns("SDLBez").ReadOnly = True
                            .Columns("SDLBez").Width = 150
                        Catch : End Try
                        .Columns("Vermerk").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                        .Columns("KfzKennzeichen").AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                        .Columns("Vermerk").Width = 250
                        .Columns("Ersatzkarte").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                        .Columns("Gesperrt").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                        .Columns("Defekt").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                        .Columns("Verloren").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                        .Columns("Gestohlen").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                    End If
                End If
            End With

            txtchanged = False
            dgvInitWait = False
            If dgvSDL.RowCount > 0 Then dgvSDL.Rows(0).Cells(0).Selected = True

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
    End Sub

  
 
    Sub initFilter()
        dgvInitWait = True
        Dim left_value As Integer = 40 - dgvSDL.HorizontalScrollingOffset
        pnlFilter.Controls.Clear()
        Dim l As New Label
        l.Left = left_value - 38 : l.Top = 12
        l.Width = 36 : l.Text = "Filter:"
        pnlFilter.Controls.Add(l)

        If dgvSDL.Rows.Count > 0 Then
            For i As Integer = 0 To dgvSDL.ColumnCount - 1 Step 1
                Dim c As DataGridViewColumn = dgvSDL.Columns(i)
                If c.Visible Then '
                    Try
                        If c.ValueType.Name = "Boolean" Then
                            Dim t As New CheckBox
                            t.Left = left_value
                            t.Top = 10
                            t.CheckAlign = ContentAlignment.MiddleCenter
                            t.ThreeState = True
                            t.CheckState = CheckState.Indeterminate
                            t.Name = "cbxSrch_" & c.Name
                            AddHandler t.CheckStateChanged, Sub(snd As Object, ev As KeyEventArgs)
                                                                If ev.KeyCode = Keys.Return Then
                                                                    initSearch()
                                                                End If
                                                            End Sub
                            t.Width = c.Width
                            pnlFilter.Controls.Add(t)
                        End If
                        If c.ValueType.Name = "String" Or c.ValueType.Name = "Int32" Or c.ValueType.Name = "Decimal" Then
                            Dim t As New TextBox
                            t.Left = left_value
                            t.Top = 10
                            t.Name = "txtSrch_" & c.Name
                            AddHandler t.KeyDown, Sub(snd As Object, ev As KeyEventArgs)
                                                      If ev.KeyCode = Keys.Return Then
                                                          initSearch()
                                                      End If
                                                  End Sub
                            t.Width = c.Width
                            pnlFilter.Controls.Add(t)
                        End If
                        If c.ValueType.Name = "DateTime" Then
                            Dim t As New TextBox
                            t.Left = left_value
                            t.Top = 10
                            t.Name = "datSrch_" & c.Name
                            AddHandler t.KeyDown, Sub(snd As Object, ev As KeyEventArgs)
                                                      If ev.KeyCode = Keys.Return Then
                                                          initSearch()
                                                      End If
                                                  End Sub
                            t.Width = c.Width
                            pnlFilter.Controls.Add(t)
                        End If
                        left_value += c.Width

                    Catch ex As Exception

                    End Try
                End If
            Next
        End If
        dgvInitWait = False
    End Sub
  
    Sub initSearch() '(sender As Object, e As EventArgs)
        txtchanged = True
        initDGV()
        txtchanged = False
        If dgvSDL.RowCount > 0 Then dgvSDL.Rows(0).Cells(0).Selected = True

    End Sub

    Function getSqlText(t) As String
        Return t.Replace("'", "")
    End Function
    Function getFilter() As String
        Dim filter As String = ""
        Dim andstr As String = ""
        If pnlFilter.HasChildren Then
            For Each c In pnlFilter.Controls
                If c.GetType.Name.EndsWith("TextBox") Then
                    Try
                        Dim t As TextBox = DirectCast(c, TextBox)
                        If t.Text <> "" Then
                            If t.Name.Contains("txtSrch_") Then
                                filter &= andstr & " [" & t.Name.Replace("txtSrch_", "") & "] LIKE '%" & getSqlText(t.Text) & "%' "
                                andstr = " AND "
                            End If
                            If t.Name.Contains("datSrch_") AndAlso t.Text.Length = 10 Then
                                filter &= andstr & " ( [" & t.Name.Replace("datSrch_", "") & "] BETWEEN '" & getSqlText(t.Text) & "' AND '" & getSqlText(t.Text) & "' ) "
                                andstr = " AND "
                            End If

                        End If
                    Catch ex As Exception
                    End Try
                ElseIf c.GetType.Name.ToString.EndsWith("CheckBox") Then
                    Try
                        Dim t As CheckBox = DirectCast(c, CheckBox)
                        If Not t.CheckState = CheckState.Indeterminate Then
                            filter &= andstr & " [" & t.Name.Replace("cbxSrch_", "") & "] = '" & t.CheckState & "' "
                            andstr = " AND "
                        End If

                    Catch ex As Exception
                    End Try
                End If
            Next
            '  Me.bindingSDL = New BindingSource
            '   Me.dgvSDL.DataSource = Me.bindingSDL
        End If
        Return filter
    End Function

    Sub clearFilter()
        If pnlFilter.HasChildren Then
            pnlFilter.Controls.Clear()
        End If
    End Sub


    Private Sub dgvSDL_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgvSDL.ColumnWidthChanged
        If Not txtchanged Then initFilter()
    End Sub

    Sub setBindingstring(ByVal b As String)
        bindingString = b
    End Sub


    Private Sub dgvLKW_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSDL.CellValueChanged
        btnUpdate.Enabled = True
    End Sub


    Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
        '      showDetails(kdnr, True, onlyKdNr)
    End Sub



    Private Sub dgvSDL_CellContentClick(sender As Object, e As MouseEventArgs) Handles dgvSDL.MouseDoubleClick ', dgvSDL.CellContentClick
        showDetail(dgvSDL.CurrentRow.Cells("KartenNr").Value)
    End Sub

    Sub showDetail(KarteNnr)
        If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_Leistungen_bearbeiten", "SDL") Then
            MsgBox("Keine Berechtigung!")
            Exit Sub
        End If
        txtchanged = False
        Dim newEntry As New frmVERAGCardDetails(KarteNnr, dgvSDL)
        newEntry.Show()
        txtchanged = True
        '   dgvSDL.RELOAD()
        ' frmGrayOut.Close()
    End Sub
    Private Sub FlatButton1_Click(sender As Object, e As MouseEventArgs) Handles Button8.MouseClick ', Button13.Click
        sender.ContextMenuStrip = cntxtExcel
        sender.ContextMenuStrip.Show(sender, e.Location)
    End Sub

    Private Sub ToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem3.Click
        Me.Cursor = Cursors.WaitCursor
        cProgramFunctions.genExcelFromDGV(dgvSDL, True)
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub ToolStripMenuItem6_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem6.Click
        Me.Cursor = Cursors.WaitCursor
        cProgramFunctions.genExcelFromDGV_NEW(dgvSDL, True)
        Me.Cursor = Cursors.Default
    End Sub

    

    Private Sub dgvSDL_SelectionChanged(sender As Object, e As EventArgs) Handles dgvSDL.Click
        initDetails()
    End Sub

    Sub initDetails()
        If dgvSDL.SelectedRows.Count > 0 Then
            For Each openForm In Application.OpenForms()
                If TypeOf (openForm) Is frmVERAGCardDetails Then _
                                               CType(openForm, frmVERAGCardDetails).initMe()
            Next

        End If
    End Sub

    Private Sub dgvSDL_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvSDL.KeyDown
        initDetails()
    End Sub

    Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
        showDetail(dgvSDL.CurrentRow.Cells("KartenNr").Value)
    End Sub
End Class
