Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class Zustellgebiete
    Inherits AbstractForm
    Implements InterfaceExportableGrid

    Private Sub Form_Verlassen() Handles MyBase.Verlassen
        Me.Close()
    End Sub

    Public Overloads Sub Export(sender As Object) Implements InterfaceExportableGrid.Export
        FunctionsDataGrid.ExportDGToCSV(dgStrassen)
    End Sub

    Public Land As ComboBox
    Public PLZ As ComboBox
    Public Ort As ComboBox
    Public Strasse As ComboBox


    Private Sub Zustellgebiete_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            GrstrassenTableAdapter.Fill(DsPLZ.grstrassen)
            ds = DsPLZ
            ParentBindingNavigator.BindingSource = GrstrassenBindingSource
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub BindingNavigatorSaveItem_Click(sender As Object, e As System.EventArgs) Handles MyBase.Save
        Try
            Validate()
            GrstrassenBindingSource.EndEdit()
            GrstrassenTableAdapter.Update(DsPLZ)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonHerunterladen_Click(sender As Object, e As EventArgs) Handles ButtonHerunterladen.Click
        Try
            Cursor = Cursors.WaitCursor
            Dim cnt As Integer = 0
            Dim file As String = Nothing

            If FunctionsPLZ.DownloadPLZ(TextBoxPLZ.Text, cnt) Then
                MsgBox(String.Format(Messages.Generell_Datensaetze_importiert, cnt), vbInformation)
                Zustellgebiete_Load(Nothing, Nothing)
            Else
                Dim msg = String.Format(Messages.Generell_Datei_nicht_vorhanden, file)
                MsgBox(msg, vbInformation)
            End If
        Catch ex As Exception
            Cursor = Cursors.Default
            HandleAppError(ex)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnUebernehmen_Click(sender As Object, e As EventArgs) Handles btnUebernehmen.Click
        Try
            Cursor = Cursors.WaitCursor
            Dim r = dgStrassen.SelectedRows(0)
            Dim plzText = r.Cells(1).Value
            Dim strasseText = r.Cells(2).Value
            If Land IsNot Nothing Then
                'Land.SelectedValue = r.Cells(4).Value
                Land.Text = r.Cells(1).Value
            End If

            If PLZ IsNot Nothing Then
                PLZ.Text = plzText
                'PLZ.SelectedValue = r.Cells(0).Value
            End If

            If Ort IsNot Nothing Then
                Ort.Text = FirstRow("select ort from grPLZ where PLZ='" & plzText & "'")
            End If

            If Strasse IsNot Nothing Then
                Strasse.Text = strasseText
            End If

            Close()

        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub


    Private Sub txtSuche_TextChanged(sender As Object, e As System.EventArgs) Handles txtSuche.TextChanged
        Try
            Cursor = Cursors.WaitCursor
            'filter dataset 
            If txtSuche.Text.Length > 0 Then
                GrstrassenBindingSource.Filter = "Strasse like '%" + txtSuche.Text + "%' or Strasse Like '%" + txtSuche.Text + "%'"
            Else
                GrstrassenBindingSource.Filter = Nothing
            End If
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub
End Class