Imports System.Windows.Media
Imports com.sun.jndi.toolkit
Imports com.sun.org.apache.bcel.internal.classfile
Imports com.sun.org.apache.xml.internal.utils
Imports GrapeCity.ActiveReports
Imports VERAG_PROG_ALLGEMEIN

Public Class frmTIR

    Dim dt As DataTable
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

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

        Me.Height = Screen.FromPoint(Cursor.Position).WorkingArea.Size.Height
        Me.Top = 0


        AddHandler UsrCntlTIR1.DGVchanged, AddressOf UsrCntlTIR1_SomethChanged

        If Not VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM Then
            btnLaden.Visible = False
        End If

        lblGedruckt.Visible = False

        cboAbriss777.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("0", "0"))
        cboAbriss777.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("12", "12"))
        cboAbriss777.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("14", "14"))
        cboAbriss777.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("17", "17"))
        cboAbriss777.changeItem("12")

        Dim sql_einheiten As String = "SELECT [Code] ,[Code] + ', ' +  [Description] as Verpackungsart FROM tbl_DY_ZollDE_C0017_PackstueckeVerpackungsarten where (StartDate is null OR StartDate< GETDATE()) AND (EndDate is null OR EndDate> GETDATE())"
        Dim dt_einheiten As DataTable
        dt_einheiten = SQL.loadDgvBySql(sql_einheiten, "FMZOLL", 100, True)

        Dim sql_zollstellen As String = "SELECT [Description] FROM tbl_DY_ZollDE_C0141_Zollstellen where (StartDate is null OR StartDate< GETDATE()) AND (EndDate is null OR EndDate> GETDATE())"
        Dim dt_zollstellen As DataTable
        dt_zollstellen = SQL.loadDgvBySql(sql_zollstellen, "FMZOLL", 100, True)
        dt_zollstellen.Columns.Add("")

        Dim sql_laender As String = "Select [Description] FROM tbl_DY_ZollDE_C0008_LaenderFull where (StartDate Is null Or StartDate< GETDATE()) And (EndDate Is null Or EndDate> GETDATE())"
        Dim dt_laender As DataTable
        dt_laender = SQL.loadDgvBySql(sql_laender, "FMZOLL", 100, True)
        dt_laender.Columns.Add("")



        Dim combo As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
        combo.DataSource = dt_einheiten
        combo.DataPropertyName = "Einheit"
        combo.ValueMember = dt_einheiten.Columns("Verpackungsart").ToString
        combo.DisplayMember = dt_einheiten.Columns("Verpackungsart").ToString
        combo.Name = "Einheit"
        combo.Width = 50
        combo.DropDownWidth = 300
        UsrCntlTIR1.dgvGegenstände.Columns.Insert(2, combo)

        For Each c As Control In UsrCntlTIR1.Controls

            If c.Name.ToLower.Contains("cbxabgangszollstelle") And c.GetType.FullName = "VERAG_PROG_ALLGEMEIN.MyComboBox" Then
                Dim cbx = DirectCast(c, MyComboBox)
                cbx.AutoCompleteSource = AutoCompleteSource.ListItems
                cbx.AutoCompleteMode = AutoCompleteMode.Suggest
                cbx.Items.AddRange(dt_zollstellen.AsEnumerable().Select(Function(d) DirectCast(d(0).ToString(), Object)).ToArray())
            End If

            If c.Name.ToLower.Contains("cbxabfahrtsland") And c.GetType.FullName = "VERAG_PROG_ALLGEMEIN.MyComboBox" Then
                Dim cbx = DirectCast(c, MyComboBox)
                cbx.AutoCompleteSource = AutoCompleteSource.ListItems
                cbx.AutoCompleteMode = AutoCompleteMode.Suggest
                cbx.Items.AddRange(dt_laender.AsEnumerable().Select(Function(d) DirectCast(d(0).ToString(), Object)).ToArray())

            End If

            If c.Name.ToLower.Contains("cbxankunftsland") And c.GetType.FullName = "VERAG_PROG_ALLGEMEIN.MyComboBox" Then
                Dim cbx = DirectCast(c, MyComboBox)
                cbx.AutoCompleteSource = AutoCompleteSource.ListItems
                cbx.AutoCompleteMode = AutoCompleteMode.Suggest
                cbx.Items.AddRange(dt_laender.AsEnumerable().Select(Function(d) DirectCast(d(0).ToString(), Object)).ToArray())
            End If

        Next


        cProgramFunctions.initDrucker(cboPrinter, "TALLY")

    End Sub

    Private Sub print_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Cursor = Cursors.WaitCursor
        Dim rpt As New rptTIR(cboAbriss777._value)

        Dim anzahl_778 As Integer = 0

        rpt.txtAbgangszollstelle1.Text = If(UsrCntlTIR1.cbxAbgangszollstelle1.Text, "")
        rpt.txtAbgangszollstelle2.Text = If(UsrCntlTIR1.cbxAbgangszollstelle2.Text, "")
        rpt.txtAbgangszollstelle3.Text = If(UsrCntlTIR1.cbxAbgangszollstelle3.Text, "")
        rpt.txtAbgangszollstelle4.Text = If(UsrCntlTIR1.cbxAbgangszollstelle4.Text, "")
        rpt.txtAbgangszollstelle5.Text = If(UsrCntlTIR1.cbxAbgangszollstelle5.Text, "")
        rpt.txtAbgangszollstelle6.Text = If(UsrCntlTIR1.cbxAbgangszollstelle6.Text, "")
        rpt.txtAbgangszollstelle7.Text = If(UsrCntlTIR1.cbxAbgangszollstelle7.Text, "")

        rpt.txtAbfahrsland.Text = If(UsrCntlTIR1.cbxAbfahrtsland.Text, "")

        rpt.txtFahrzeug.Text = If(UsrCntlTIR1.txtFahrzeug1.Text, "")
        rpt.txtZusDok.Text = If(UsrCntlTIR1.txtZusDocument.Text, "")




        rpt.txtGesamtpaket1.Text = If(UsrCntlTIR1.txtGesamtpaket1.Text, "")
        rpt.txtGesamtpaket2.Text = If(UsrCntlTIR1.txtGesamtpaket2.Text, "")
        rpt.txtGesamtpaket3.Text = If(UsrCntlTIR1.txtGesamtpaket3.Text, "")
        rpt.txtGesamtpaket4.Text = If(UsrCntlTIR1.txtGesamtpaket4.Text, "")
        rpt.txtGesamtpaket5.Text = If(UsrCntlTIR1.txtGesamtpaket5.Text, "")
        rpt.txtGesamtpaket6.Text = If(UsrCntlTIR1.txtGesamtpaket6.Text, "")
        rpt.txtGesamtpaket7.Text = If(UsrCntlTIR1.txtGesamtpaket7.Text, "")


        rpt.txtGesamtpaketName1.Text = If(UsrCntlTIR1.txtGesamtpaketName1.Text, "")
        rpt.txtGesamtpaketName2.Text = If(UsrCntlTIR1.txtGesamtpaketName2.Text, "")
        rpt.txtGesamtpaketName3.Text = If(UsrCntlTIR1.txtGesamtpaketName3.Text, "")
        rpt.txtGesamtpaketName4.Text = If(UsrCntlTIR1.txtGesamtpaketName4.Text, "")
        rpt.txtGesamtpaketName5.Text = If(UsrCntlTIR1.txtGesamtpaketName5.Text, "")
        rpt.txtGesamtpaketName6.Text = If(UsrCntlTIR1.txtGesamtpaketName6.Text, "")
        rpt.txtGesamtpaketName7.Text = If(UsrCntlTIR1.txtGesamtpaketName7.Text, "")

        rpt.txtMain1.Text = ""
        rpt.txtMain2.Text = ""
        rpt.txtMain3.Text = ""


        For Each r As DataGridViewRow In UsrCntlTIR1.dgvGegenstände.Rows
            If r IsNot Nothing Then

                rpt.txtMain1.Text &= If(r.Cells(0).Value <> "", r.Cells(0).Value & vbNewLine, "")
                rpt.txtMain2.Text &= If(r.Cells(1).Value <> "", r.Cells(1).Value & " ", "")
                rpt.txtMain2.Text &= If(r.Cells(2).Value <> "", r.Cells(3).Value & vbNewLine, "")
                rpt.txtMain3.Text &= If(r.Cells(3).Value <> "", r.Cells(4).Value & vbNewLine, "")


            End If
        Next

        rpt.txtSumme.Text = lblEigenmasse.Text

        Try
            rpt.Run(False)
            rpt.Document.Printer.PrinterName = cboPrinter._value
            rpt.Document.Print(CBool(cboPrinter.Text = ""), False)
            rpt.Dispose()
            lblGedruckt.Visible = True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Me.Cursor = Cursors.Default

    End Sub

    Private Sub btnNeu_Click(sender As Object, e As EventArgs) Handles btnNeu.Click

        For Each c As Control In UsrCntlTIR1.Controls
            If TypeOf c Is RichTextBox Then
                c.Text = ""
            End If




            If TypeOf c Is DataGridView Then
                Dim dgv As DataGridView = c
                dgv.Rows.Clear()
                For i As Integer = 0 To 10

                    dgv.Rows.Add("", "", "", "", "", "")


                Next

            End If

        Next
        lblGedruckt.Visible = False

    End Sub

    Private Sub btnLaden_Click(sender As Object, e As EventArgs) Handles btnLaden.Click

        UsrCntlTIR1.cbxAbgangszollstelle1.Text = "Testtext1"
        UsrCntlTIR1.cbxAbgangszollstelle2.Text = "Testtext2"
        UsrCntlTIR1.cbxAbgangszollstelle3.Text = "Testtext3"
        UsrCntlTIR1.cbxAbgangszollstelle4.Text = "Testtext4"
        UsrCntlTIR1.cbxAbgangszollstelle5.Text = "Testtext5"
        UsrCntlTIR1.cbxAbgangszollstelle6.Text = "Testtext6"
        UsrCntlTIR1.cbxAbgangszollstelle7.Text = "Testtext7"

        UsrCntlTIR1.cbxAbfahrtsland.Text = "TestABFL"

        UsrCntlTIR1.txtFahrzeug1.Text = "XXXXXXX" & vbNewLine & "Fahrezeug 1"
        UsrCntlTIR1.txtZusDocument.Text = "Testtext1"

        UsrCntlTIR1.dgvGegenstände.Rows.Clear()

        For i As Integer = 0 To 10
            If i < 4 Then
                UsrCntlTIR1.dgvGegenstände.Rows.Add(i & "-Test", "Testtext", "Text", i & "")
            Else
                UsrCntlTIR1.dgvGegenstände.Rows.Add("", "", "", "")
            End If

        Next

        UsrCntlTIR1.txtGesamtpaket1.Text = "Paket1"
        UsrCntlTIR1.txtGesamtpaket2.Text = "Paket2"
        UsrCntlTIR1.txtGesamtpaket3.Text = "Paket3"
        UsrCntlTIR1.txtGesamtpaket4.Text = "Paket4"
        UsrCntlTIR1.txtGesamtpaket5.Text = "Paket5"
        UsrCntlTIR1.txtGesamtpaket6.Text = "Paket6"
        UsrCntlTIR1.txtGesamtpaket7.Text = "Paket7"


        UsrCntlTIR1.txtGesamtpaketName1.Text = "1"
        UsrCntlTIR1.txtGesamtpaketName2.Text = "2"
        UsrCntlTIR1.txtGesamtpaketName3.Text = "3"
        UsrCntlTIR1.txtGesamtpaketName4.Text = "4"
        UsrCntlTIR1.txtGesamtpaketName5.Text = "5"
        UsrCntlTIR1.txtGesamtpaketName6.Text = "6"
        UsrCntlTIR1.txtGesamtpaketName7.Text = "7"



    End Sub

    Private Sub UsrCntlTIR1_SomethChanged() Handles UsrCntlTIR1.DGVchanged

        Dim gewicht As Double = 0
        Dim colli As Double = 0

        For Each r As DataGridViewRow In UsrCntlTIR1.dgvGegenstände.Rows

            If r IsNot Nothing And IsNumeric(r.Cells(4).Value) Then
                gewicht += CDbl(r.Cells(4).Value)
            End If

            If r IsNot Nothing And IsNumeric(r.Cells(1).Value) Then
                colli += CDbl(r.Cells(1).Value)
            End If


        Next

        lblEigenmasse.Text = gewicht & " kg"
        lblAnzahl.Text = colli

    End Sub
End Class