Imports IntraSell_DLL

Public Class AdressenControl

    Public Event IDNRChanged(IDNR As Integer)
    Public DEFAULT_TEXT As String = "Suche nach Tel, Email, Name, Firma"
    Dim loading As Boolean = False

    Private Const SQL_PART1 = "select adr, max(idnr) as idnr from (SELECT IDNR, trim(concat(COALESCE(Firma,''), ' ',  COALESCE(Name,''), ' ', COALESCE(Vorname,''), ' ', COALESCE(Tel,''),  ' ', COALESCE(Mobil,''))) as Adr  " & _
                       " FROM ofAdressen "
    Public Const SQL_IDNR = SQL_PART1 & "WHERE IDNR=&IDNR) a"
    Public Const SQL = SQL_PART1 & "WHERE ((Firma > '' or name  > '' or Vorname > '') or IDNR=@IDNR) LIMIT 300) a  where a.adr <> '' group by adr order by adr"

    Public Const SQL_PREISLISTEN = SQL_PART1 & "WHERE (Firma > '' or name  > '' or Vorname > '') AND exists (select 1 from `grArtikel-VKPreisPerSelection` pl where pl.IDNR=ofAdressen.IDNR) ) a  where a.adr <> '' group by adr order by adr"

    Private _IDNR As Integer = Nothing 'nicht definiert 
    Private _filter As String = Nothing

    Private Function GetSQL() As String
        Dim rSQL = SQL.Replace("@IDNR", IDNR)

        If Filter IsNot Nothing Then
            Return rSQL.Replace("WHERE", "WHERE " & _filter & " AND ")
        Else
            Return rSQL
        End If
    End Function

    Public Overrides Property Text() As String
        Get
            Return AdressenComboBox.Text
        End Get
        Set(value As String)
            AdressenComboBox.Text = value
        End Set
    End Property

    Public Property IDNR() As Integer
        Get
            Return _IDNR
        End Get
        Set(value As Integer)
            If value > 0 Then
                Dim changed As Boolean = _IDNR <> value
                _IDNR = value
                loading = True
                AdressenComboBox.SelectedValue = _IDNR
                loading = False
                If changed Then
                    loading = True
                    Dim SQL = SQL_IDNR.Replace("&IDNR", _IDNR)
                    Dim t = FirstRow(SQL)
                    If Not IsDBNull(t) Then
                        AdressenComboBox.Text = t
                        RaiseEvent IDNRChanged(value)
                    End If
                    UpdateControls()
                    loading = False
                End If
                AdressenComboBox.ForeColor = Color.Black
            ElseIf value = -1 Then
                _IDNR = value
                AdressenComboBox.ForeColor = Color.FloralWhite
            Else
                _IDNR = Nothing
                AdressenComboBox.ForeColor = Color.Gray
            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

    Public Property ShowButtonDetails() As Boolean
        Get
            Return ButtonDetails.Visible
        End Get
        Set(value As Boolean)
            ButtonDetails.Visible = value
        End Set
    End Property

    Public Property ShowResizeCheckBox() As Boolean
        Get
            Return False
        End Get
        Set(value As Boolean)

        End Set
    End Property

    Public Property Filter() As String
        Get
            Return _filter
        End Get
        Set(value As String)
            _filter = value
        End Set
    End Property

    Private Sub AdressenControl_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Reload()
    End Sub

    Public Sub Reload()
        loading = True
        FillComboBox(AdressenComboBox, GetSQL, "Adr", "IDNR")
        AdressenComboBox.Text = DEFAULT_TEXT
        AdressenComboBox.ForeColor = Color.Gray
        UpdateControls()
        loading = False
    End Sub

    Private Sub AdressenSelectorButton_Click(sender As Object, e As System.EventArgs) Handles SelectorButton.Click
        Try
            ZeigeAddressenSelectorFuerNummer(Me.AdressenComboBox.Text)
            'Dim adrSelectorFrom As New AdressenSelector
            'AddHandler adrSelectorFrom.AdresseUebernehmen, AddressOf AdressenSelector_AdresseUebernehmen
            'adrSelectorFrom.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Sub ZeigeAddressenSelectorFuerNummer(telNummer As String)
        Try
            Dim adrSelectorFrom As New AdressenSelector
            adrSelectorFrom.Suche(telNummer)
            AddHandler adrSelectorFrom.AdresseUebernehmen, AddressOf AdressenSelector_AdresseUebernehmen
            adrSelectorFrom.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AdressenSelector_AdresseUebernehmen(newIDNR As Integer)
        IDNR = newIDNR
        UpdateControls()
    End Sub

    Private Sub AddNewButton_Click(sender As Object, e As System.EventArgs) Handles AddNewButton.Click

        Try
            Dim k As New Kunden With {
                .BeginNewFlag = True
            }
            k.ShowDialog()
            'hier wartet bis dialog fenster zu ist
            'If k.DialogResult = Windows.Forms.DialogResult.OK Then
            FillComboBox(AdressenComboBox, GetSQL, "Adr", "IDNR")
            IDNR = k.AdressenDetailControl1.IDNR
            'end if
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub AdressenComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles AdressenComboBox.SelectedIndexChanged
        Try
            If loading Then Exit Sub
            Dim changed As Boolean = AdressenComboBox.SelectedValue <> _IDNR
            If IsNumeric(AdressenComboBox.SelectedValue) Then
                IDNR = AdressenComboBox.SelectedValue
            End If
            If changed Then
                RaiseEvent IDNRChanged(IDNR)
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    ''' <summary>
    ''' Remove selected idnr   
    ''' </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
                IDNR = Nothing
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    'reload data 
    Public Shadows Sub Refresh()
        Try
            loading = True
            Parent.Refresh()
            FillComboBox(AdressenComboBox, GetSQL, "Adr", "IDNR")
            AdressenComboBox.Text = ""
            loading = False
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AdressenComboBox_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles AdressenComboBox.MouseDoubleClick
        Try
            Dim k As New Kunden With {
                .MdiParent = Main
            }
            k.Show()
            k.LadeKunde(IDNR)
            k.FilterBy("IDNR=" & IDNR)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub JumpButton_Click(sender As Object, e As System.EventArgs)
        AdressenComboBox_MouseDoubleClick(Nothing, Nothing)
    End Sub

    Private Sub ToolStripMenuItem1_Click(sender As Object, e As System.EventArgs) Handles ToolStripMenuItem1.Click
        AdressenComboBox_MouseDoubleClick(Nothing, Nothing)
    End Sub

    ''' <summary>
    ''' Sucht nach Firma, Tel Nummer und Email 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Friend Sub AdressenComboBox_TextChanged(sender As Object, e As EventArgs) Handles AdressenComboBox.TextChanged
        Try
            If loading Then Exit Sub
            Dim txt = AdressenComboBox.Text

            Dim gefundenIDNR = KundeSuchen(txt)

            If gefundenIDNR IsNot Nothing Then
                If gefundenIDNR.Count = 1 Then 'Found 
                    IDNR = gefundenIDNR(0)
                    AdressenComboBox.BackColor = Color.Green
                    'AdressenComboBox.ForeColor = Color.DarkGreen
                End If
            Else
                AdressenComboBox.BackColor = Color.White
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AdressenComboBox_Enter(sender As Object, e As EventArgs) Handles AdressenComboBox.Enter
        Try
            If AdressenComboBox.Text = DEFAULT_TEXT Then
                AdressenComboBox.Text = ""
            End If
            AdressenComboBox.ForeColor = Color.Black
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonDetails_Click(sender As Object, e As EventArgs) Handles ButtonDetails.Click
        Try
            If IsNumeric(IDNR) Then
                If KundeNamen(IDNR) IsNot Nothing Then
                    OeffneKundenMaske(IDNR, True)
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub UpdateControls()
        Try
            ButtonDetails.Visible = IsNumeric(IDNR) And KundeNamen(IDNR) IsNot Nothing
        Catch ex As Exception
        End Try
    End Sub


End Class
