Imports System.Data.SqlClient
Imports SDL.SDLDataSet
Imports System.ComponentModel
Imports SDL.cProgramFunctions

Public Class frmVERAGCardDetails
    '  Dim dontClose As Boolean = False
    '   Public newEntry As Boolean = False
    '  Public onlyKdNr As Boolean = False

    ' Public KdNr As Integer
    ' Public cursorAtKartenNr As String = ""

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Dim VERAGCARD As cVERAG_Card = Nothing
    Dim loadCompleted = False
    Public PARENT_DGV As VERAG_PROG_ALLGEMEIN.MyDatagridview = Nothing
    Dim changed = False
    Dim WithEvents binding As New cBinding


    Sub New(KartenNr, PARENT_DGV)
        InitializeComponent()
        Me.VERAGCARD = New cVERAG_Card(KartenNr)
        Me.PARENT_DGV = PARENT_DGV
    End Sub

    Private Sub frmVERAGCardDetails_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        If Me.ParentForm IsNot Nothing Then
            MsgBox(Me.ParentForm.Name)
            Me.ParentForm.BringToFront()
        End If
    End Sub

    Private Sub frmVERAGCardDetails_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KdSearchBox.initKdBox(Me)
        KdSearchBox.usrcntlHEIGHT = 150
        KdSearchBox.usrcntlWIDTH = KdSearchBox.Width
        initForm()

        ' If newEntry Then
        'txtKdNr.Text = KdNr
        ' cbxErsatzkarte.Checked = False
        ' cbxGesperrt.Checked = False
        ' cbxDefekt.Checked = False
        ' cbxVerloren.Checked = False
        ' cbxGestohlen.Checked = False
        '  End If
    End Sub


    Sub initForm()
        loadCompleted = False
        txtKartenNr.Text = VERAGCARD.KartenNr
        KdSearchBox.KdNr = If(VERAGCARD.KundenNr, "")
        txtKfz.Text = If(VERAGCARD.KfzKennzeichen, "")
        txtFahrer.Text = If(VERAGCARD.Fahrer, "")
        txtDatum._value = If(VERAGCARD.Datum, "")
        txtKaution._value = If(VERAGCARD.Kaution, "")
        txtBeleg.Text = If(VERAGCARD.BelegNr, "")

        If VERAGCARD.Ersatzkarte Is Nothing Then
            cbxErsatzkarte.CheckState = CheckState.Indeterminate
        Else
            cbxErsatzkarte.Checked = CBool(VERAGCARD.Ersatzkarte)
        End If

        If VERAGCARD.Gesperrt Is Nothing Then
            cbxGesperrt.CheckState = CheckState.Indeterminate
        Else
            cbxGesperrt.Checked = CBool(VERAGCARD.Gesperrt)
        End If
        If VERAGCARD.Defekt Is Nothing Then
            cbxDefekt.CheckState = CheckState.Indeterminate
        Else
            cbxDefekt.Checked = CBool(VERAGCARD.Defekt)
        End If
        If VERAGCARD.Verloren Is Nothing Then
            cbxVerloren.CheckState = CheckState.Indeterminate
        Else
            cbxVerloren.Checked = CBool(VERAGCARD.Verloren)
        End If
        If VERAGCARD.Gestohlen Is Nothing Then
            cbxGestohlen.CheckState = CheckState.Indeterminate
        Else
            cbxGestohlen.Checked = CBool(VERAGCARD.Gestohlen)
        End If


        txtVermerk.Text = If(VERAGCARD.Vermerk, "")
        lblErfassung.Text = If(VERAGCARD.Erfassungsdatum, "")
        lblAenderung.Text = If(VERAGCARD.Änderungsdatum, "")
        lblSachbearbeiter.Text = If(VERAGCARD.Sachbearbeiter, "")
        changed = False
        loadCompleted = True
    End Sub

    



    Sub loadDataInClass()
        VERAGCARD.KartenNr = txtKartenNr.Text
        VERAGCARD.KundenNr = If(KdSearchBox.KdNr_value <= 0, Nothing, KdSearchBox.KdNr_value)
        VERAGCARD.KfzKennzeichen = isLeerNothing(txtKfz.Text)
        VERAGCARD.Fahrer = isLeerNothing(txtFahrer.Text)
        VERAGCARD.Datum = isLeerNothing(txtDatum._value)
        VERAGCARD.Kaution = isLeerNothingDbl(txtKaution._value)
        VERAGCARD.BelegNr = isLeerNothing(txtBeleg.Text)
        VERAGCARD.Ersatzkarte = If(cbxErsatzkarte.CheckState = CheckState.Indeterminate, Nothing, cbxErsatzkarte.Checked)
        VERAGCARD.Gesperrt = If(cbxGesperrt.CheckState = CheckState.Indeterminate, Nothing, cbxGesperrt.Checked)
        VERAGCARD.Defekt = If(cbxDefekt.CheckState = CheckState.Indeterminate, Nothing, cbxDefekt.Checked)
        VERAGCARD.Verloren = If(cbxVerloren.CheckState = CheckState.Indeterminate, Nothing, cbxVerloren.Checked)
        VERAGCARD.Gestohlen = If(cbxGestohlen.CheckState = CheckState.Indeterminate, Nothing, cbxGestohlen.Checked)
        VERAGCARD.Vermerk = isLeerNothing(txtVermerk.Text)
        VERAGCARD.Erfassungsdatum = isLeerNothing(lblErfassung.Text)


    End Sub


    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        If SaveMe() Then Me.Close()
    End Sub

    Function SaveMe() As Boolean
        If changed Then
            loadDataInClass()
            If VERAGCARD.SAVE Then
                PARENT_DGV.RELOAD()
                Return True
            Else
                Return False
            End If
        Else
            'keine Änderung -->  nicht speichern
            Return True
        End If
    End Function

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnAbbrechen.Click
        Me.Close()
    End Sub




    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs)
        If vbYes = MsgBox("Möchten Sie diese Karte wirklich zurücksetzen?", vbYesNoCancel) Then
            KdSearchBox.KdNr = ""
            txtKfz.Text = ""
            txtFahrer.Text = ""
            txtDatum._value = ""
            txtKaution.Text = ""
            txtBeleg.Text = ""
            cbxErsatzkarte.Checked = False
            cbxGesperrt.Checked = False
            cbxDefekt.Checked = False
            cbxVerloren.Checked = False
            cbxGestohlen.Checked = False
            txtVermerk.Text = ""
            '  lblErfassung = DBNull.Value
            '  lblAenderung = DBNull.Value
            '  lblSachbearbeiter = DBNull.Value
            '  SAVE_Click(ToolStripButton1, New EventArgs)
            Me.frmVERAGCardDetails_Load(Me, New EventArgs)
        End If
    End Sub


    Private Sub Label6_Click(sender As Object, e As EventArgs)
        Dim pf As New cProgramFunctions
        cProgramFunctions.openKundenblatt(KdSearchBox.KdNr_value, Me, True)
    End Sub


    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
        If SaveMe() Then
            Try
                Dim i = PARENT_DGV.SelectedRows(0).Index
                PARENT_DGV.ClearSelection()
                PARENT_DGV.Rows(i + 1).Selected = True

                initMe()
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If SaveMe() Then
            Try
                Dim i = PARENT_DGV.SelectedRows(0).Index
                PARENT_DGV.ClearSelection()
                PARENT_DGV.Rows(i - 1).Selected = True

                initMe()
            
            Catch ex As Exception

            End Try
        End If
    End Sub

    Public Sub initMe(Optional KartenNr = "")
        SaveMe()
        If VERAGCARD IsNot Nothing Then
            If KartenNr <> CStr(VERAGCARD.KartenNr) Then
                If KartenNr = "" Then KartenNr = PARENT_DGV.SelectedRows(0).Cells("KartenNr").Value
                Me.VERAGCARD = New cVERAG_Card(KartenNr)
                initForm()
            End If
        End If
    End Sub
    


    Private Sub txtKartenNr_TextChanged(sender As Object, e As EventArgs) Handles txtKartenNr.TextChanged, txtBeleg.TextChanged, KdSearchBox.PropertyChanged, txtDatum.TextChanged, txtKfz.TextChanged, txtKaution.TextChanged, txtFahrer.TextChanged,
        cbxErsatzkarte.CheckedChanged, cbxDefekt.CheckedChanged, cbxVerloren.CheckedChanged, cbxGesperrt.CheckedChanged, cbxGestohlen.CheckedChanged
        If loadCompleted Then
            changed = True
            VERAGCARD.Änderungsdatum = Now
            VERAGCARD.Sachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
            lblSachbearbeiter.Text = VERAGCARD.Sachbearbeiter
            lblAenderung.Text = CDate(VERAGCARD.Änderungsdatum).ToString("dd.MM.yyyy HH:mm")
        End If
    End Sub

    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        txtDatum._value = Now.ToShortDateString
    End Sub
End Class