Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class AdressenDetailControl

    Event PLZSelectorClicked(ByRef plzSelector As PLZSelector)


#Region "Properties"

    Public Property IDNR As String
        Get
            Return IDNRTextBox.Text
        End Get
        Set(value As String)
            IDNRTextBox.Text = value
        End Set
    End Property

    Public Property Status As String
        Get
            Return StatusComboBox.Text
        End Get
        Set(value As String)
            StatusComboBox.Text = value
        End Set
    End Property

    Public Property Branche As String
        Get
            Return BrancheComboBox.SelectedValue
        End Get
        Set(value As String)
            If value IsNot Nothing And Not value = "" Then BrancheComboBox.SelectedValue = value
        End Set
    End Property

    Public Property Firma As String
        Get
            Return FirmaTextBox.Text
        End Get
        Set(value As String)
            FirmaTextBox.Text = value
        End Set
    End Property


    Public Property Briefanrede As String
        Get
            Return BriefanredeComboBox.SelectedValue
        End Get
        Set(value As String)
            If value IsNot Nothing And Not value = "" Then BriefanredeComboBox.SelectedValue = value
        End Set
    End Property


    Public Property Anrede As String
        Get
            Return AnredeComboBox.SelectedValue
        End Get
        Set(value As String)
            If value IsNot Nothing And Not value = "" Then AnredeComboBox.SelectedValue = value
        End Set
    End Property


    Public Property Titel As String
        Get
            Return TitelComboBox.SelectedValue
        End Get
        Set(value As String)
            If value IsNot Nothing And Not value = "" Then TitelComboBox.SelectedValue = value
        End Set
    End Property

    Public Property NameFamilie As String
        Get
            Return NameTextBox.Text
        End Get
        Set(value As String)
            NameTextBox.Text = value
        End Set
    End Property

    Public Property Vorname As String
        Get
            Return VornameTextBox.Text
        End Get
        Set(value As String)
            VornameTextBox.Text = value
        End Set
    End Property

    Public Property Name1 As String
        Get
            Return Name1TextBox.Text
        End Get
        Set(value As String)
            Name1TextBox.Text = value
        End Set
    End Property

    Public Property Geburtstag As Date
        Get
            Return GeburtstagDateTimePicker.Value
        End Get
        Set(value As Date)
            Try
                GeburtstagDateTimePicker.Value = value
            Catch
            End Try
        End Set
    End Property
    Public Property Land As String
        Get
            Return LandComboBox.SelectedValue
        End Get
        Set(value As String)
            If value IsNot Nothing And Not value = "" Then LandComboBox.SelectedValue = value
        End Set
    End Property

    Public Property PLZ As String
        Get
            Return PLZComboBox.Text
        End Get
        Set(value As String)
            PLZComboBox.Text = value
        End Set
    End Property

    Public Property Ort As String
        Get
            Return OrtComboBox.Text
        End Get
        Set(value As String)
            OrtComboBox.Text = value
        End Set
    End Property

    Public Property Adresse As String
        Get
            Return AdresseTextBox.Text
        End Get
        Set(value As String)
            AdresseTextBox.Text = value
        End Set
    End Property

    Public Property Tel As String
        Get
            Return TelTextBox.Text
        End Get
        Set(value As String)
            TelTextBox.Text = value
        End Set
    End Property

    Public Property Tel2 As String
        Get
            Return Tel2TextBox.Text
        End Get
        Set(value As String)
            Tel2TextBox.Text = value
        End Set
    End Property

    Public Property Fax As String
        Get
            Return FaxTextBox.Text
        End Get
        Set(value As String)
            FaxTextBox.Text = value
        End Set
    End Property

    Public Property Mobil As String
        Get
            Return MobilTextBox.Text
        End Get
        Set(value As String)
            MobilTextBox.Text = value
        End Set
    End Property

    Public Property Email As String
        Get
            Return EmailTextBox.Text
        End Get
        Set(value As String)
            EmailTextBox.Text = value
        End Set
    End Property

    Public Property Web As String
        Get
            Return WebTextBox.Text
        End Get
        Set(value As String)
            WebTextBox.Text = value
        End Set
    End Property


    'TODO: Tel, Email ....

#End Region

    Dim fPLZ As New FunctionsPLZ

    Private Sub Control_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try

            'Branche 
            FillComboBox(BrancheComboBox, "select distinct BrNr, Bezeichnung from grBranchen order by Bezeichnung", "Bezeichnung", "BrNr")
            'Titel
            FillComboBox(TitelComboBox, "select distinct Titel from grTitel group by Titel order by Titel", "Titel", "Titel")
            'Anrede 
            FillComboBox(AnredeComboBox, "select distinct Anrede from grAnrede group by Anrede order by Anrede", "Anrede", "Anrede")
            'Briefanrede 
            FillComboBox(BriefanredeComboBox, "select distinct Briefanrede from grAnrede group by Briefanrede order by Briefanrede", "Briefanrede", "Briefanrede")
            'Satus - gibt es nicht in Weitere Adressen oder Lieferanten
            FillComboBox(StatusComboBox, "select distinct Status from `ofAdressen-status` group by Status order by Status", "Status", "Status")

            fPLZ.Init(LandComboBox, PLZComboBox, OrtComboBox, AdresseTextBox)

            fPLZ.FillLand()

            'Me.OfAdressenTableAdapter.Fill(Me.DataSetKunden.ofadressen)

        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub


    Private Sub ButtonPLZSelect_Click(sender As Object, e As System.EventArgs) Handles ButtonPLZSelect.Click
        Try
            Dim plz As New PLZSelector With {
                .Ort = OrtComboBox,
                .PLZ = PLZComboBox,
                .Land = LandComboBox
            }
            RaiseEvent PLZSelectorClicked(plz)
            plz.ShowDialog()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub



    Private Sub PLZComboBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles PLZComboBox.Validating
        Try
            If LandComboBox.SelectedValue IsNot Nothing Then
                Dim ort = FirstRow("select Ort from grPLZ where Land =" & LandComboBox.SelectedValue & " and PLZ ='" & PLZComboBox.SelectedValue & "'")
                If Not IsNull(ort) Then
                    OrtComboBox.Text = ort
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub

    'Private Sub OrtComboBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OrtComboBox.Validating
    '    Try
    '        'Enter new PLZ + Ort if not Existing 
    '        Dim newValue As String = IntraSellKunden.getPLZCreateIfNeeded(Me.LandComboBox.SelectedValue, Me.OrtComboBox.Text, Me.PLZComboBox.Text)
    '        'Me.GrLandPlzTableAdapter.Fill(Me.DsPLZ.grLandPlz)
    '        Me.PLZComboBox.SelectedValue = newValue
    '    Catch ex As Exception
    '        HandleAppError(ex)
    '    End Try

    'End Sub

    Private Sub LandComboBox_Leave(sender As Object, e As EventArgs) Handles LandComboBox.Leave
        'PLZComboBox.Focus()
    End Sub

    Private Sub PLZComboBox_Leave(sender As Object, e As EventArgs) Handles PLZComboBox.Leave
        'OrtComboBox.Focus()
    End Sub

    Private Sub OrtComboBox_Leave(sender As Object, e As EventArgs) Handles OrtComboBox.Leave
        'AdresseTextBox.Focus()
    End Sub

    Private Sub ButtonCall_Click(sender As Object, e As EventArgs) Handles ButtonCall.Click
        Try
            ModuleGlobal.RequestCommand(New IntraSellCallCommand With {.Number = Tel})
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            ModuleGlobal.RequestCommand(New IntraSellCallCommand With {.Number = Mobil})
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub TextBoxNr_Validated(sender As Object, e As EventArgs) Handles TextBoxNr.Validated
        AdresseTextBox.Text = AdresseTextBox.Text & " " & TextBoxNr.Text
    End Sub

    Private Sub AdressenDetailControl_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
        Try
            AnredeComboBox.SelectionLength = 0
            BriefanredeComboBox.SelectionLength = 0
            BrancheComboBox.SelectionLength = 0
            TitelComboBox.SelectionLength = 0
            StatusComboBox.SelectionLength = 0
            PLZComboBox.SelectionLength = 0
            OrtComboBox.SelectionLength = 0
            LandComboBox.SelectionLength = 0
            AdresseTextBox.SelectionLength = 0

        Catch ex As Exception

        End Try
    End Sub
End Class
