Public Class usrcntlGestellungsgarantien

    Dim kdnr
    Public Gid = -1
    Public Shared Event clickOK(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Private Sub usrcntlGestellungsgarantien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtT1VerzollungsadresseBestimmungszollstelle.initSearchBox(Me.FindForm, "[Code] ,[Code] as Nr,Description as Dienststelle,left(Code,2) as Land,[Description] +' (' +left(Code,2)+')' as display FROM [tbl_DY_ZollDE_C0141_Zollstellen]", {" [Code]", "Description"}, " (StartDate is null OR StartDate< GETDATE()) AND (EndDate is null OR EndDate> GETDATE()) ", " left(Code,2),[Description]", "Code", "display", "FMZOLL", , 400, 200, {"display", "Code"})
        initDGVGestGarantie()
    End Sub

    Sub initDGVGestGarantie(Optional _kdnr = -1)

        kdnr = _kdnr

        With dgvGestGarantie
            .Columns.Clear()

            If kdnr = -1 Then Exit Sub

            .DataSource = VERAG_PROG_ALLGEMEIN.cGestellungsgarantien.LOAD_LIST(kdnr)
            If .Columns.Count = 0 Then Exit Sub

            .Columns("gg_Id").Visible = False
            .Columns("gg_KundenNr").Visible = False
            .Columns("gg_datenarchivId").Visible = False
            .Columns("gg_erhalten_Datum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("gg_erhalten_Datum").Width = 70
            .Columns("gg_erhalten_Datum").HeaderText = "Datum"
            .Columns("gg_Zollamt").Width = 80
            .Columns("gg_Zollamt").HeaderText = "Zollamt"
            .Columns("gg_Info").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("gg_Info").HeaderText = "Info"


            If True Then
                Dim c As New DataGridViewImageColumn
                c.Name = "clmnPDF" : c.HeaderText = "PDF"
                c.Image = My.Resources.pdf_gray : c.ImageLayout = DataGridViewImageCellLayout.Zoom
                c.Width = 40 : c.DefaultCellStyle.Padding = New Padding(2) : c.DefaultCellStyle.BackColor = Color.White
                c.DefaultCellStyle.Tag = "PDF anzeigen"
                .Columns.Add(c)
                For Each r As DataGridViewRow In .Rows
                    If r.Cells("gg_datenarchivId").Value IsNot DBNull.Value AndAlso CInt(r.Cells("gg_datenarchivId").Value) > 0 Then
                        DirectCast(r.Cells("clmnPDF"), DataGridViewImageCell).Value = My.Resources.pdf
                    End If
                Next
            End If
        End With
        clearGG()
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If (txtT1VerzollungsadresseBestimmungszollstelle._value <> "" Or cbxGG.Checked) And txtGGDatum._value <> "" Then
            Dim GG As New VERAG_PROG_ALLGEMEIN.cGestellungsgarantien
            If Button9.Visible Then
                If dgvGestGarantie.SelectedRows.Count = 1 Then
                    GG = New VERAG_PROG_ALLGEMEIN.cGestellungsgarantien(dgvGestGarantie.SelectedRows(0).Cells("gg_id").Value)
                Else
                    Exit Sub
                End If
            End If
            GG.gg_KundenNr = kdnr
            GG.gg_erhalten_Datum = txtGGDatum._value
            GG.gg_Zollamt = If(cbxGG.Checked, "GENERELL", txtT1VerzollungsadresseBestimmungszollstelle._value)

            If Not Button9.Visible AndAlso VERAG_PROG_ALLGEMEIN.cGestellungsgarantien.ENTRY_Exists(kdnr, GG.gg_Zollamt, txtGGDatum._value) Then
                MsgBox("Der Eintrag existiert bereits!")
                Exit Sub
            End If

            GG.gg_Info = txtT1VerzollungsadresseBestimmungszollstelle.GET_VALUE_OnlyReturn(txtT1VerzollungsadresseBestimmungszollstelle._value, False)
            If GG.SAVEOBJECT Then
                Gid = GG.gg_Id
                txtT1VerzollungsadresseBestimmungszollstelle.SET_VALUE("")
                initDGVGestGarantie(GG.gg_KundenNr)
                txtT1VerzollungsadresseBestimmungszollstelle.Focus()
                markDGVGestGarantie(GG.gg_Id)
                initDGVGestGarantie(GG.gg_KundenNr)
                'RaiseEvent clickOK(sender, e)
            End If
            clearGG()
        End If
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        clearGG()
    End Sub

    Sub clearGG()
        txtT1VerzollungsadresseBestimmungszollstelle.SET_VALUE("")
        txtGGDatum.Text = ""
        cbxGG.Checked = False
        Button8.Text = "Hinzufügen"
        Button9.Visible = False
        If kdnr > 0 Then
            Button8.Enabled = True
        Else
            Button8.Enabled = False
        End If
    End Sub

    Private Sub txtT1VerzollungsadresseBestimmungszollstelle_KeyDown(sender As Object, e As KeyEventArgs) Handles txtT1VerzollungsadresseBestimmungszollstelle.KeyDown
        If e.KeyCode = Keys.Return Then
            If txtT1VerzollungsadresseBestimmungszollstelle.usrcntl.Visible Then Exit Sub
            Button8.PerformClick()
        End If

    End Sub

    Private Sub dgvGestGarantie_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvGestGarantie.CellContentDoubleClick
        If dgvGestGarantie.SelectedRows.Count = 1 Then
            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(dgvGestGarantie.SelectedRows(0).Cells("gg_datenarchivId").Value)
            DS.OPEN_SINGLE()
        End If
    End Sub

    Private Sub markDGVGestGarantie(Optional gid As Integer = 0)


        dgvGestGarantie.ClearSelection()

        If gid > 0 Then
            For Each row As DataGridViewRow In dgvGestGarantie.Rows
                If row.Cells("gg_Id").Value = gid Then
                    row.Selected = True
                End If
            Next
        End If

    End Sub
End Class
