Imports IntraSell_DLL

Public Class PLZSelector
    Public Land As ComboBox
    Public PLZ As ComboBox
    Public Ort As ComboBox

    Private Sub PLZSelector_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            GrLandPlzTableAdapter.Fill(DsPLZ.grLandPlz)

            FillComboBox(ComboBoxLand, "select IDNR, Name from grLand order by Name", "Name", "IDNR")
            Try
                ComboBoxLand.SelectedValue = Land.SelectedValue
            Catch ex As Exception

            End Try
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub txtSuche_TextChanged(sender As Object, e As System.EventArgs) Handles txtSuche.TextChanged
        Try
            'filter dataset 
            If txtSuche.Text.Length > 0 Then
                Dim fltr = "Ort like '%" + txtSuche.Text + "%' or PLZ Like '%" + txtSuche.Text + "%'"
                If ComboBoxLand.Text IsNot Nothing And ComboBoxLand.Text <> "" Then
                    fltr = "Name = '" & ComboBoxLand.Text & "' and (" & fltr & ")"
                End If
                GrLandPlzBindingSource.Filter = fltr
            Else
                GrLandPlzBindingSource.Filter = Nothing
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub btnUebernehmen_Click(sender As Object, e As System.EventArgs) Handles btnUebernehmen.Click
        AuswahlUebernehmen()
    End Sub

    Sub Auswahluebernehmen()
        Try

            Dim r = dgPLZ.CurrentRow
            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 = r.Cells(2).Value
                PLZ.SelectedValue = r.Cells(2).Value
            End If

            If Ort IsNot Nothing Then
                Ort.Text = r.Cells(3).Value
            End If

            Close()

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonAnlegenUndUebernehmen_Click(sender As Object, e As EventArgs) Handles ButtonAnlegenUndUebernehmen.Click
        Try
            If TextBoxOrt.Text = "" Then
                TextBoxOrt.BackColor = Color.Red
                Exit Sub
            End If

            If TextBoxPLZ.Text = "" Then
                TextBoxPLZ.BackColor = Color.Red
                Exit Sub
            End If


            Dim existValue = IntraSellKunden.GetPLZ(ComboBoxLand.SelectedValue, TextBoxOrt.Text, TextBoxPLZ.Text)
            If existValue Is Nothing Then
                IntraSellKunden.GetPLZMitPrompt(ComboBoxLand.SelectedValue, TextBoxOrt.Text, TextBoxPLZ.Text, PromptNeuePLZ:=True)
            End If
            existValue = IntraSellKunden.GetPLZ(ComboBoxLand.SelectedValue, TextBoxOrt.Text, TextBoxPLZ.Text)

            If existValue Is Nothing Then
                MsgBox("PLZ/Ort wurde nicht angelegt!", vbCritical)
                Exit Sub
            End If
            'Reload Datagrid 
            GrLandPlzTableAdapter.Fill(DsPLZ.grLandPlz)

            'Select ROW  
            Dim itemFound = GrLandPlzBindingSource.Find("IDNR", existValue)
            If itemFound > -1 Then
                GrLandPlzBindingSource.Position = itemFound

                FunctionsPLZ.FillPLZ(PLZ, Land:=ComboBoxLand.SelectedValue)
                FunctionsPLZ.FillOrt(Ort, Land:=ComboBoxLand.SelectedValue)
                Auswahluebernehmen()
            Else
                MsgBox("PLZ/Ort wurde nicht gefunden!", vbCritical)
            End If

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub dgPLZ_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgPLZ.RowHeaderMouseDoubleClick
        Auswahluebernehmen()
    End Sub
End Class