Imports IntraSell_DLL

Public Class AdressenWeitereControl

    Dim loading As Boolean = False

    Public Event IDChanged(ID As Object)

    Public Const SQL = "SELECT a.ID, a.IDNR, concat(IFNULL(concat(Firma, ', '),''), IFNULL(concat(Name, ', '),''), IFNULL(Vorname,', '), ', ', IFNULL(a.Adresse,''), ' ', IFNULL(a.PLZ,''), '-', IFNULL(a.Ort,''),  CASE when standard then ' (S)' ELSE '' END) as Adr " &
                       " FROM `ofAdressen-Weitere` a " & _
                       " where (a.ID=@ID or a.IDNR=@IDNR) and (Firma is not null or name is not null or Vorname is not null)" & _
                       " ORDER BY a.standard desc, a.Firma, a.Name, a.Vorname"

    Private SQLcurrent As String
    Private _ID As Integer

    Public Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(value As Integer)
            _ID = value
            AdressenComboBox.SelectedValue = _ID
        End Set
    End Property


    Private _IDNR As Integer


    Public Property IDNR() As Integer
        Get
            Return _IDNR
        End Get
        Set(value As Integer)
            _IDNR = value
            ReloadCombo()
            'Me.AdressenComboBox.Text = ""
            If _ID > 0 Then
                AdressenComboBox.SelectedValue = _ID
            End If
        End Set
    End Property


    Public Property ShowAddNew() As Boolean
        Get
            Return AddNewButton.Visible
        End Get
        Set(value As Boolean)
            AddNewButton.Visible = value
        End Set

    End Property

    Private Sub ReloadCombo()

        If _IDNR > 0 Then
            loading = True
            Dim sqlWithIDNR = Replace(SQL, "@IDNR", _IDNR).Replace("@ID", _ID)

            If SQLcurrent <> sqlWithIDNR Then
                SQLcurrent = sqlWithIDNR
                FillComboBox(AdressenComboBox, SQLcurrent, "Adr", "ID")
            End If
            loading = False
        End If

    End Sub

    Private Sub AdressenControl_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        ReloadCombo()
        AdressenComboBox.Text = ""
    End Sub

    Private Sub AdressenSelectorButton_Click(sender As Object, e As System.EventArgs) Handles ArtikelSelectorButton.Click
        Try
            Dim adrSelectorForm = New AdressenSelector
            AddHandler adrSelectorForm.AdresseUebernehmen, AddressOf AdressenSelector_AdresseUebernehmen
            adrSelectorForm.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AdressenSelector_AdresseUebernehmen(IDNR As Integer)
        AdressenComboBox.SelectedValue = IDNR
    End Sub


    Private Sub AdressenComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles AdressenComboBox.SelectedIndexChanged
        '  Try
        '      If loading Then Exit Sub
        '      If IsNumeric(AdressenComboBox.SelectedValue) Then
        '          ID = AdressenComboBox.SelectedValue
        '      End If
        '  Catch ex As Exception
        '      HandleAppError(ex)
        '  End Try
    End Sub

    Private Sub AdressenComboBox_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles AdressenComboBox.SelectionChangeCommitted
        Try
            If loading Then Exit Sub
            If IsNumeric(AdressenComboBox.SelectedValue) Then
                ID = AdressenComboBox.SelectedValue


                RaiseEvent IDChanged(ID)

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

    'reload data 
    Public Shadows Sub Refresh()
        Try
            Parent.Refresh()
            ReloadCombo()
            AdressenComboBox.Text = ""
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AddNewButton_Click(sender As Object, e As System.EventArgs) Handles AddNewButton.Click
        Try
            Dim a As New AdresseWeitere
            a.StarteNeueAdresse(IDNR:=IDNR, VorgangTyp:=VorgangTypen.LI)
            a.TypComboBox.SelectedValue = VorgangTypen.LI
            a.ShowDialog()
            'refresh data 
            FillComboBox(AdressenComboBox, SQLcurrent, "Adr", "ID")
            AdressenComboBox.SelectedValue = a.ID
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AdressenComboBox_MouseClick(sender As Object, e As MouseEventArgs) Handles AdressenComboBox.MouseClick
        Try
            Debug.Print("Refresh Combo Box")
            Dim oldSelVal = AdressenComboBox.SelectedValue
            FillComboBox(AdressenComboBox, SQLcurrent, "Adr", "ID")
            If oldSelVal IsNot Nothing Then
                AdressenComboBox.SelectedValue = oldSelVal
            End If

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

    ''' <summary>
    ''' Remove selected ID (for Filters etc.)
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub AdressenComboBox_TextUpdate(sender As Object, e As System.EventArgs) Handles AdressenComboBox.TextUpdate
        Try
            If AdressenComboBox.Text = "" Then
                ID = Nothing
                RaiseEvent IDChanged(ID)
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


End Class
