Imports System.ComponentModel
Imports System.Text
Imports System.IO

Public Class frmAddArtikelATLAS
    '  Public bindingDataAdapter As New SqlDataAdapter
    Public WithEvents bindingdataTable As New DataTable
    Public bindingdataSet As New DataSet
    Public WithEvents bindingSource As New BindingSource
    Dim bList As New BindingList(Of cArtikel)
    Public WithEvents binding As New cBinding("FMZOLL") 'fürs Binding
    Public WithEvents bindingUnterArt As New cBinding("FMZOLL") 'fürs Binding
    Public kdNr As String = ""
    Dim kundenSQL As New kundenSQL
    Public cursorAtID As String
    Public newEntry As Boolean
    Public editAllowed As Boolean = True
    Dim ATLAS_CONN_ART = "ATLAS" 'ATLAS_SBG
    Public parentKundenblatt As Boolean = True
    Dim currentId As Integer
    Dim acd As List(Of cAutoCompleteData) = kundenSQL.getKundenShort()
    Dim freigabe As Boolean = True
    Private Sub frmAddArtikelATLAS_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Not editAllowed Then e.Cancel = False : Exit Sub
        If btnSave.Enabled Then
            Select Case MessageBox.Show("Möchten Sie die Änderungen speichern? ", "Änderungen speichern", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
                Case vbYes : btnSave.PerformClick()
                    If freigabe Then
                        e.Cancel = False
                    Else
                        e.Cancel = True
                    End If
                Case vbNo : e.Cancel = False
                Case vbCancel : e.Cancel = True
            End Select
        End If
    End Sub

    Private Sub frmAddArtikelATLAS_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not editAllowed Then
            BindingNavigatorAddNewItem.Visible = False
            BindingNavigatorDeleteItem.Visible = False
            ToolStripButton1.Visible = False
            ToolStripButton1.Visible = False
            btnSave.Visible = False
            Panel4.Visible = False
            Panel2.Visible = False
            Me.Height = 480
        End If

        If newEntry Or parentKundenblatt Then
            If parentKundenblatt Then
                Panel2.Visible = False
                Me.Height = Me.Height - Panel2.Height
            Else
                Panel2.Visible = True
            End If
        End If



        initDataBinding()
        BindingNavigator1.BindingSource = binding.bindingSource

        'initDataBindingUnterArt()




        Dim acsc As New AutoCompleteStringCollection

        For Each a In acd
            acsc.Add(a.AutoCompleteString)
            '   cboJumpToKd_Firma.Items.Add(a.AutoCompleteString)
        Next
        cboJumpToKd_Firma.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cboJumpToKd_Firma.AutoCompleteSource = AutoCompleteSource.CustomSource
        cboJumpToKd_Firma.AutoCompleteCustomSource = acsc




        Exit Sub
        Dim ll As New cArtikel
        ll.Kurzbezeichnung = "test"
        ll.Artikelnummer = 1

        bList.Add(ll)

        bindingSource.DataSource = bList
        BindingNavigator1.BindingSource = bindingSource

        AddHandler bList.ListChanged, AddressOf myList_ListChanged

        txtArtikelNr.Focus()
    End Sub
    Sub initDataBinding()
        Try
            '  MsgBox(kdNr)
            'binding
            Dim tmpKdNR As String = ""
            If kdNr <> "" Then tmpKdNR = " AND KundenNr='" & kdNr & "'"
            binding.initBinding("SELECT * FROM tblZollArtikel WHERE 1=1 " & tmpKdNR, "tblZollArtikel") ' " ' AND KfzKennzeichen='" & KfzKennzeichen & "'  AND SDLNr='" & SDLNr & "' " & tmpHist, "SDL")

            RemoveHandler binding.bindingdataTable.ColumnChanged, AddressOf BindingTableColumnChanged
            RemoveHandler binding.bindingdataTable.TableNewRow, AddressOf bindingdataTable_TableNewRow_Adressen

            AddHandler binding.bindingdataTable.ColumnChanged, AddressOf BindingTableColumnChanged
            AddHandler binding.bindingdataTable.TableNewRow, AddressOf bindingdataTable_TableNewRow_Adressen

            txtKdNr.DataBindings.Clear()
            txtKdNr.DataBindings.Add(New Binding("Text", binding.bindingSource, "KundenNr", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            If txtKdNr.Text <> "" Then kdNr = txtKdNr.Text : cboJumpToKd_Firma.Text = ""
            txtArtikelNr.DataBindings.Clear()
            txtArtikelNr.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Artikelnummer", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            txtMerkmal1.DataBindings.Clear()
            txtMerkmal1.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Dynamische_Ergaenzung_1", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            txtMerkmal2.DataBindings.Clear()
            txtMerkmal2.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Dynamische_Ergaenzung_2", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            txtWarencodenummer.DataBindings.Clear()
            txtWarencodenummer.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Warencodenummer", True, DataSourceUpdateMode.OnPropertyChanged, ""))

            txtWarencodeZusatz1.DataBindings.Clear()
            txtWarencodeZusatz1.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Zusatzcode_1", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            txtWarencodeZusatz1.DataBindings.Clear()
            txtWarencodeZusatz1.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Zusatzcode_2", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            txtKurztext.DataBindings.Clear()
            txtKurztext.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Kurzbezeichnung", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            txtBeschreibung.DataBindings.Clear()
            txtBeschreibung.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Warenbeschreibung", True, DataSourceUpdateMode.OnPropertyChanged, ""))

            lblErfassung.DataBindings.Clear()
            lblErfassung.DataBindings.Add(New Binding("Text", binding.bindingSource, "Erfassungsdatum", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            lblAenderung.DataBindings.Clear()
            lblAenderung.DataBindings.Add(New Binding("Text", binding.bindingSource, "Aenderungsdatum", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            lblSachbearb.DataBindings.Clear()
            lblSachbearb.DataBindings.Add(New Binding("Text", binding.bindingSource, "Sachbearbeiter", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            lblStatus.DataBindings.Clear()
            lblStatus.DataBindings.Add(New Binding("Text", binding.bindingSource, "zollArt_Status", True, DataSourceUpdateMode.OnPropertyChanged, ""))

            
       
            Try
                binding.bindingSource.Position = binding.bindingSource.Find("zollArt_id", cursorAtID)
            Catch ex As Exception
                'MsgBox("ERR1: " & ex.Message)
            End Try



            'Dim aktId = binding.bindingSource.Current
            'If newEntry Then binding.bindingdataTable.NewRow() 'NEUER EINTRAG!
            If newEntry Then
                Dim row = binding.bindingdataTable.NewRow()
                binding.bindingdataTable.Rows.Add(row)
                binding.bindingSource.MoveLast()
            End If


        Catch ex As Exception
            MsgBox("ERR2: " & ex.Message)
        End Try




    End Sub

    


    Public Sub bindingdataTable_TableNewRow_Adressen(sender As Object, e As DataTableNewRowEventArgs)
        'Dim currentHisId = 0
        'Try : currentHisId = sender.Select("History=MAX(History)")(0).Item("History") : Catch : End Try
        ' currentHisId += 1
        ' MsgBox(currentHisId)
        Dim kundenSQL As New kundenSQL

        Dim row As DataRow = e.Row
        'MsgBox(kdNr)
        row("KundenNr") = 0
        cboJumpToKd_Firma.Text = ""
        '  txtJumpToKd_KdNR.Text = ""
        If IsNumeric(kdNr) And parentKundenblatt Then
            row("KundenNr") = kdNr
            row("zollArt_Kundennummer") = kdNr
            txtKdNr.Text = kdNr
        End If
        'If parentKundenblatt Then row("KundenNr") = kdNr
        row("zollArt_Status") = "Erfassung"
        row("Erfassungsdatum") = Now
        row("Sachbearbeiter") = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME

        bindingSource.MoveLast()
    End Sub


    Sub BindingTableColumnChanged(sender As Object, e As System.Data.DataColumnChangeEventArgs)
        lblSachbearb.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        lblAenderung.Text = Now.ToString("dd.MM.yyyy HH:mm")
        btnSave.Enabled = True
    End Sub
    Private Sub bindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles bindingSource.CurrentChanged
        Dim l As cArtikel = bindingSource.Current
        txtArtikelNr.Text = l.Artikelnummer
        txtMerkmal1.Text = l.Kurzbezeichnung

    End Sub
    Sub myList_ListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs)
        '   MessageBox.Show(e.ListChangedType.ToString() + ": at index: " + e.NewIndex.ToString() + ", """ + bList(e.NewIndex).Bez + """")
    End Sub
    Private Sub bindingSourcea_CurrentChanged(sender As Object, e As EventArgs) Handles bindingSource.CurrentItemChanged
        'MsgBox("ITEM")

    End Sub
    Private Sub FlatButton2_Click(sender As Object, e As EventArgs)
        Me.Close()
    End Sub

    Function initArtikel() As cArtikel
        Dim artikel As New cArtikel
        artikel.Teilsatzschlüssel = "ART"
        artikel.zollArt_id = binding.bindingSource.Current.Item("zollArt_id").ToString()
        'If lblStatus.Text <> "" And lblStatus.Text <> "Angelegt" And lblStatus.Text <> "Erfassung" Then

        cbxUeberschreiben.Checked = False
        If cbxUeberschreiben.Checked Then
            artikel.Verarbeitungskennzeichen = "1"
        Else
            artikel.Verarbeitungskennzeichen = "0"
        End If

        artikel.Artikelnummer = txtArtikelNr.Text
        artikel.Dynamische_Ergänzung_1 = txtMerkmal1.Text
        artikel.Dynamische_Ergänzung_2 = txtMerkmal2.Text

        artikel.Kurzbezeichnung = txtKurztext.Text

        artikel.Kundennummer = txtKdNr.Text
        artikel.Warencodenummer = txtWarencodenummer.Text
        'artikel.Löschkennzeichen_Warencodenummer As String = ""
        artikel.Zusatzcode_1 = txtWarencodeZusatz1.Text
        'Property Löschkennzeichen_Zusatzcode_1 As String = ""
        artikel.Zusatzcode_2 = txtWarencodeZusatz2.Text
        ' Property Löschkennzeichen_Zusatzcode_2 As String = ""
        artikel.Kennzeichen_Gültig = "1"

        ' Property Löschkennzeichen_Kurzbezeichnung As String = ""
        artikel.Warenbeschreibung = txtBeschreibung.Text
        Return artikel

    End Function

    Private Sub FlatButton3_Click(sender As Object, e As EventArgs) Handles FlatButton3.Click
        btnSave.PerformClick()
        If freigabe Then
            Dim artikel As New List(Of cArtikel)
            artikel.Add(initArtikel)

            frmStatusFSSAtlas.artikel = artikel
            frmStatusFSSAtlas.Show()
            newEntry = False
            AddHandler frmStatusFSSAtlas.FormClosing, AddressOf initDataBinding
        End If
    End Sub


    Function checkPflichtfeld(t As VERAG_PROG_ALLGEMEIN.MyTextBox) As Boolean
        If t.Text <> "" Then
            t.BackColor = Color.White
            Return True
        Else
            t.BackColor = Color.FromArgb(255, 230, 230)
            Return False
        End If
    End Function

    Private Sub FlatButton4_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        freigabe = True
        lblError.Text = ""
        lblError.Visible = False
        If Not checkPflichtfeld(txtKurztext) Then freigabe = False
        If Not checkPflichtfeld(txtBeschreibung) Then freigabe = False
        If Not checkPflichtfeld(txtArtikelNr) Then freigabe = False
        If Not checkPflichtfeld(txtKdNr) Then freigabe = False

        If Not freigabe Then lblError.Text = "Pflichtfelder ausfüllen!" : lblError.Visible = True : Exit Sub

        If lblStatus.Text = "" Or lblStatus.Text = "Erfassung" Then lblStatus.Text = "Angelegt"

        If binding.updateBinding() Then
            btnSave.Enabled = False
        Else
            lblStatus.Text = "Erfassung"
        End If

    End Sub


    Private Sub cboJumpToKd_Firma_KeyDown(sender As Object, e As KeyEventArgs) Handles cboJumpToKd_Firma.KeyUp
        '  txtJumpToKd_KdNR.Text = ""
        If (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Or e.KeyCode = Keys.Tab) AndAlso sender.text <> "" Then
            getKdNrSearch()
        End If

    End Sub


    Sub getKdNrSearch()
        For Each a In acd
            If a.AutoCompleteString.ToLower = cboJumpToKd_Firma.Text.ToLower Then
                kdNr = a.AutoCompleteProperty
                txtKdNr.Text = kdNr
            End If
        Next
    End Sub

    Private Sub txtKdNr_TextChanged_1(sender As Object, e As EventArgs) Handles txtKdNr.TextChanged
        If IsNumeric(txtKdNr.Text) Then
            Dim k As cKundenFMZOLL = kundenSQL.getKundeFMZOLLByKdNr(txtKdNr.Text)
            lblFirma.Text = k.Ordnungsbegriff
            lblEORINr.Text = k.EORITIN
            lblAufschubKtoZoll.Text = k.Aufschubkonto_Zoll
            lblAufschubKtoEUST.Text = k.Aufschubkonto_EUSt
            lblStrasse.Text = k.Straße
            lblOrt.Text = k.LandKz & " " & k.PLZ & " " & k.Ort
            
        Else
            lblFirma.Text = ""
            lblEORINr.Text = ""
            lblAufschubKtoZoll.Text = ""
            lblAufschubKtoEUST.Text = ""
            lblStrasse.Text = ""
            lblOrt.Text = ""

        End If

    End Sub
    Public Sub setStatus(s As String)
        ' lblStatus.Text = s
        ' FlatButton4.PerformClick()
    End Sub


    Private Sub lblStatus_TextChanged(sender As Object, e As EventArgs) Handles lblStatus.TextChanged
        If sender.Text <> "" And sender.Text <> "Angelegt" And sender.Text <> "Erfassung" Then
            'cbxUeberschreiben.Checked = True ' KEIN ÜBERSCHREIBEN MÖGLICH
        Else
            cbxUeberschreiben.Checked = False
        End If
    End Sub


    Private Sub txtWarencodenummer_TextChanged(sender As Object, e As EventArgs) Handles txtWarencodenummer.TextChanged
        errWarencodenummer.Text = ""
        sender.ForeColor = Color.Red
        If sender.text <> "" And IsNumeric(sender.text) Then
            Dim WcNr As kundenSQL.WcNr = kundenSQL.existByWarencodeNummer(sender.text, ATLAS_CONN_ART)
            If WcNr.wcnr_exists Then
                sender.ForeColor = Color.Green

                If WcNr.txtart = "H" Then
                    errWarencodenummer.Text = "Hinweis: Die Warencodenummer ist eine Hierachietext und kann nicht verwendet werden."
                Else
                    errWarencodenummer.Text = WcNr.text
                End If
            End If
        End If
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        btnSave.PerformClick()
    End Sub

    Private Sub FlatButton2_Click_1(sender As Object, e As EventArgs)

    End Sub

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

    Private Sub btnKdSearch_Click(sender As Object, e As EventArgs) Handles btnKdSearch.Click
        getKdNrSearch()
    End Sub

    Private Sub lblFirma_Click(sender As Object, e As EventArgs) Handles lblFirma.Click
        If IsNumeric(txtKdNr.Text) And lblFirma.Text.Trim <> "" Then
            cProgramFunctions.openKundenblatt(txtKdNr.Text, Me, True)
            '    cProgramFunctions.openKundenblatt(txtKdNr.Text)
        End If
    End Sub

    
End Class
