Imports System.Data.SqlClient

Public Class frmBelastAufheben
    Private BRG As New cBrgDb
    'Private SQL As New cSqlDb

    Private Sub frmZusBelast_Load(sender As Object, e As EventArgs) Handles Me.Load
        'cboBrg.Items.Add(New MyListItem("Aufschub VERAG Spedition AG (Deutschland)", "1"))
        cboBrg.Items.Add(New MyListItem("Aufschub VERAG Spedition AG (Österreich)", "3025365"))
        cboBrg.Items.Add(New MyListItem("Aufschub VERAG Customs Service GmbH (Österreich)", "3517233"))
        cboBrg.Items.Add(New MyListItem("Aufschub VERAG Zollservice GmbH (Österreich)", "3026400"))
        cboBrg.SelectedIndex = 1


        ComboBox1.Items.Add(New MyListItem("", ""))
        ComboBox1.Items.Add(New MyListItem("1 Zentrale", "1"))
        ComboBox1.Items.Add(New MyListItem("2 Salzburg", "2"))
        ComboBox1.Items.Add(New MyListItem("3 Nickelsdorf", "3"))
        ComboBox1.Items.Add(New MyListItem("4 Suben WEST", "4"))
        ComboBox1.Items.Add(New MyListItem("5 ATILLA", "5"))
        ComboBox1.Items.Add(New MyListItem("6 VERAG Halle WO ZV", "6"))
        ComboBox1.Items.Add(New MyListItem("7 VERAG Asfinag WO ZV", "7"))
        ComboBox1.SelectedIndex = 0

        datVon.Value = Now.AddDays(-7)

    End Sub

    Sub initDgv()
        Dim oItem As MyListItem = CType(cboBrg.SelectedItem, MyListItem)
        Dim where = ""
        If txtArt.Text <> "" Then where &= " AND CRN LIKE '" & txtArt.Text.Replace("*", "%") & "%'"
        If TextBox2.Text <> "" Then where &= " AND LRN LIKE '" & TextBox2.Text.Replace("*", "%") & "%'"
        If IsNumeric(TextBox1.Text) Then where &= " AND Betrag <= '" & TextBox1.Text.Replace(",", ".") & "'"
        If IsNumeric(cbotxtBetrag.Text) Then where &= " AND Betrag >= '" & cbotxtBetrag.Text.Replace(",", ".") & "'"
        If DirectCast(ComboBox1.SelectedItem, MyListItem).Value <> "" Then where &= " AND OperatorId = '" & DirectCast(ComboBox1.SelectedItem, MyListItem).Value & "'"

        With dgvEinzahlungen
            .DataSource = BRG.loadDgvBySqlZOLARIS(datVon.Value, DateTimePicker1.Value, oItem.Value, where)
            Label9.Text = dgvEinzahlungen.RowCount & " Einträge"
            For Each r As DataGridViewRow In .Rows
                If CBool(r.Cells("brgAT_Aufgehoben").Value) Then
                    r.DefaultCellStyle.ForeColor = Color.Green
                End If
            Next
            .Columns("brgAT_id").Visible = True
            .Columns("brgAT_Aufgehoben").DisplayIndex = 0
            .Columns("brgAT_Aufgehoben").HeaderText = "Aufgehoben"
            .Columns("Betrag").DefaultCellStyle.Format = "C2"
            .Columns("Berechnungsgrundlage").DefaultCellStyle.Format = "C2"
            .Columns("Betrag").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns("Berechnungsgrundlage").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        End With
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If dgvEinzahlungen.SelectedRows.Count > 0 Then
            Aufheben(dgvEinzahlungen.SelectedRows(0).Index)
        End If
    End Sub

    Sub Aufheben(rowIndex)
        If rowIndex >= 0 Then
            If vbYes = MsgBox("Möchten Sie die Belastung für den Eintrag '" & dgvEinzahlungen.Rows(rowIndex).Cells("CRN").Value & " - " & dgvEinzahlungen.Rows(rowIndex).Cells("Betrag").Value & "' wirklich aufheben?", vbYesNoCancel) Then

                Dim sql = "UPDATE [tblBrgAufschubDataAT] " &
                          " SET brgAT_Aufgehoben=@brgAT_Aufgehoben, brgAT_AufgehobenDatum=@brgAT_AufgehobenDatum " &
                             " WHERE brgAT_id=@brgAT_id "
                ', brgAT_Aufgehoben_mitID=@brgAT_Aufgehoben_mitID " &
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN
                    Using cmd As New SqlCommand(sql, conn)
                        'msgbo

                        cmd.Parameters.AddWithValue("@brgAT_id", dgvEinzahlungen.Rows(rowIndex).Cells("brgAT_id").Value)
                        cmd.Parameters.AddWithValue("@brgAT_Aufgehoben", CInt(Not (dgvEinzahlungen.Rows(rowIndex).Cells("brgAT_Aufgehoben").Value)))
                        cmd.Parameters.AddWithValue("@brgAT_AufgehobenDatum", Now)

                        Try
                            cmd.ExecuteNonQuery()
                        Catch ex As SqlException
                            MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Die Version kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Version")
                        End Try
                    End Using
                End Using
                initDgv()
            End If
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles cbotxtBetrag.TextChanged
        If isDouble(sender.text) Then
            sender.ForeColor = Color.DarkGreen
        Else
            sender.ForeColor = Color.DarkRed
        End If
    End Sub
    Function isDouble(s)
        Try : Dim d = CDbl(s) : Return True
        Catch : Return False : End Try
    End Function

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


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        initDgv()
    End Sub

    Private Sub dgvEinzahlungen_DoubleClick(sender As Object, e As EventArgs) Handles dgvEinzahlungen.DoubleClick
        If dgvEinzahlungen.SelectedRows.Count > 0 Then
            Aufheben(dgvEinzahlungen.SelectedRows(0).Index)
        End If
    End Sub

    Private Sub frmBelastAufheben_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        initDgv()

    End Sub
End Class