Public Class usrCntlKundeDetails
    Public kdNr As Integer = -1
    Dim KUNDE As New cKunden
    Dim kundenSQL As New kundenSQL
    Dim SQL As New SQL
    Dim PF As New cProgramFunctions
    Private Sub Details_Click(sender As Object, e As EventArgs) Handles Details.Click

    End Sub

    Private Sub usrCntlKundeDetails_Load(sender As Object, e As EventArgs) Handles Me.Load
        init()
    End Sub
    Sub init()

        If kdNr >= 0 Then
            initDgvOfferte()
            initDgvSperrliste()
            initDgvSDLLeistungen()
            initDgvLKWs()
            lblKdNr.Text = "KdNr: " & kdNr
            KUNDE = kundenSQL.getKundeByKdNr(kdNr)
            If Not KUNDE Is Nothing Then
                lblFirma.Text = KUNDE.Name1
                lblStrasse.Text = KUNDE.Strasse
                lblLandPlzOrt.Text = KUNDE.LandKz & " " & KUNDE.PlzOrt & " - " & KUNDE.Ort
            End If
        Else
            pnl.Visible = False
        End If

        Try
            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("GRAFIK", "FIRMENLOGOS", "", "", "", kdNr, kdNr)

            'Dim cDATENSERVER As New VERAG_PROG_ALLGEMEIN.cDATENSERVER
            Dim path = DS.GET_TOP1_PATH 'cDATENSERVER.getTOP1Path("GRAFIK", "FIRMENLOGOS", kdNr)
            If path <> "" Then
                pic.ImageLocation = path
            End If
        Catch ex As Exception

        End Try

    End Sub


    Sub initDgvSDLLeistungen()
        Exit Sub
        With New DataGridView

            .DataSource = SQL.loadDgvBySql("SELECT KundenSDL.SDLNr, Speditionsdienstleistungen.SDLBez FROM KundenSDL,Speditionsdienstleistungen WHERE KundenSDL.KundenNr=" & kdNr & " AND  Speditionsdienstleistungen.SDLNr=KundenSDL.SDLNr ")

            .RowTemplate.Height = 20
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .RowTemplate.ReadOnly = True
            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            .AllowUserToOrderColumns = True

            .Columns(0).Width = 50
            .Columns(0).HeaderText = "Nr"
            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

            .Columns(1).Width = 180
            .Columns(1).HeaderText = "Bezeichnung"
            .Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End With

    End Sub
    


    Sub initDgvLKWs()
        With DataGridView1

            .DataSource = SQL.loadDgvBySql("SELECT KfzKennzeichen, Nationalität, Abgemeldet, Verkauft FROM LKW WHERE KundenNr=" & kdNr & " ORDER BY KfzKennzeichen ")

            .RowTemplate.Height = 20
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .RowTemplate.ReadOnly = True
            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            .AllowUserToOrderColumns = True

            .Columns(0).MinimumWidth = 60
            .Columns(0).HeaderText = "KfzKennzeichen"
            .Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .Columns(1).Width = 30
            .Columns(1).HeaderText = "Nat."

            .Columns(2).Width = 30
            .Columns(2).HeaderText = "Abg."

            .Columns(3).Width = 30
            .Columns(3).HeaderText = "Verk."

        End With

    End Sub

    
    Sub initDgvOfferte()
        With dgvOfferte
            .DataSource = SQL.loadDgvBySql("SELECT Offertenstamm.OffertenNr, Offertenstamm.OffertenBez FROM Kunden,Offerten,Offertenstamm WHERE Offerten.KundenNr=" & kdNr & " AND Kunden.KundenNr=Offerten.KundenNr AND Offertenstamm.OffertenNr=Offerten.OffertenNr ")
            .RowTemplate.Height = 20
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .RowTemplate.ReadOnly = True
            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            .AllowUserToOrderColumns = True

            .Columns(0).Width = 50
            .Columns(0).HeaderText = "Nr"
            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

            .Columns(1).Width = 180
            .Columns(1).HeaderText = "Bezeichnung"
            .Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

        End With

    End Sub
    
    Sub initDgvSperrliste()
        With dgvSperrliste
            '.DefaultCellStyle.ForeColor = Color.Red
            .DataSource = SQL.loadDgvBySql(" SELECT KundenSDL.SDLNr, Speditionsdienstleistungen.SDLBez,Sperrnummernverzeichnis.SperrNr,Sperrnummernverzeichnis.SperrBez, [Sperrdatum] " & _
                                               " FROM KundenSDL INNER JOIN Speditionsdienstleistungen ON Speditionsdienstleistungen.SDLNr=KundenSDL.SDLNr INNER JOIN Sperrnummernverzeichnis ON Sperrnummernverzeichnis.SperrNr=KundenSDL.Sperre " &
                                               " WHERE KundenSDL.KundenNr='" & kdNr & "' /*AND [SDLTypNr] <=2 */ ORDER BY Sperre")
            '  .DataSource = SQL.loadDgvBySql("SELECT Speditionsdienstleistungen.SDLNr,Speditionsdienstleistungen.SDLBez,Sperrnummernverzeichnis.SperrNr,Sperrnummernverzeichnis.SperrBez,KundenSDL.Sperrdatum  FROM Speditionsdienstleistungen,KundenSDL,Sperrnummernverzeichnis WHERE KundenSDL.KundenNr=" & kdNr & " AND Speditionsdienstleistungen.SDLNr=KundenSDL.SDLNr AND Sperrnummernverzeichnis.SperrNr=KundenSDL.Sperre /*AND KundenSDL.Sperrdatum IS NOT NULL*/ ORDER BY SperrNr")

            .RowTemplate.Height = 20
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .RowTemplate.ReadOnly = True
            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            .AllowUserToOrderColumns = True

            .Columns(0).Width = 50
            .Columns(0).HeaderText = "SDL-Nr"
            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

            .Columns(1).Width = 150
            .Columns(1).HeaderText = "SDL-Bezeichnung"

            .Columns(2).Width = 50
            .Columns(2).HeaderText = "Sperr-Nr"
            .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

            .Columns(3).Width = 150
            .Columns(3).HeaderText = "Sperr-Bezeichnung"
            .Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .Columns(4).Width = 80
            .Columns(4).HeaderText = "Sperrdatum"
            .Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
  initSDLListeRed
        End With
    End Sub



    Public Sub initSDLListeRed()
        For Each r As DataGridViewRow In dgvSperrliste.Rows
            If r.Cells("SperrNr").Value > 0 Then
                r.DefaultCellStyle.ForeColor = Color.Red
            Else
                r.Cells("SperrBez").Value = ""
            End If
        Next

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnShowKd.Click
        
        If kdNr < 0 Then
            frmMain.setInfo("info", "Bitte wählen Sie einen Kunden aus der Liste.", 3)
        Else
            Dim pf As New cProgramFunctions
            cProgramFunctions.openKundenblatt(kdNr, Me.FindForm, True)
        End If
    End Sub

   
    Private Sub dgvOfferte_SelectionChanged(sender As Object, e As EventArgs) Handles dgvOfferte.SelectionChanged, dgvSperrliste.SelectionChanged
        DirectCast(sender, DataGridView).ClearSelection()
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        If kdNr < 0 Then
            frmMain.setInfo("info", "Bitte wählen Sie einen Kunden aus der Liste.", 3)
        Else
            Dim frmkd As New frmKundenblatt

            frmkd.kdNr = kdNr
            frmkd.Show()
            frmkd.BringToFront()
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        cProgramFunctions.newFrmData(Me.FindForm, kdNr, New Integer = -1, New String(""), New Integer = -1, True)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim frmGrayOut = cProgramFunctions.grayoutForm(Me)
        Dim l As New frmLKWDetails(kdNr, "", True)
        If l.ShowDialog(frmGrayOut) = DialogResult.OK Then
            init()
        End If
        frmGrayOut.Close()
    End Sub
End Class
