Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class AdressenSelector

    ' Public AdressenComboBox As ComboBox
    Public Event AdresseUebernehmen(IDNR As Integer)

    Private Sub PLZSelector_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            SpaltenBreitenSetzen()
            OfAdressenlisteTableAdapter.Fill(DsAdressen.ofAdressenliste)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub SpaltenBreitenSetzen()
        For Each r In dgAdressen.Columns
            If ExistsVarValue(Variablen.ADRESSEN_SELECTOR_SPALTE_ & r.Index) Then
                dgAdressen.Columns(r.Index).Width = VarValue(Variablen.ADRESSEN_SELECTOR_SPALTE_ & r.Index)
            End If
        Next
    End Sub

    Private Sub dgAdressen_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgAdressen.ColumnWidthChanged
        Dim neueBreite = e.Column.Width
        Dim index = e.Column.Index
        SetVarValue(Variablen.ADRESSEN_SELECTOR_SPALTE_ & index, neueBreite)
    End Sub

    Friend Sub Suche(telNr As String)
        txtSuche.Text = telNr
        txtSuche_TextChanged(Nothing, Nothing)
    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
                OfAdressenlisteBindingSource.Filter = "IDNR + '' like '%" + txtSuche.Text + "%' or Firma like '%" + txtSuche.Text + "%' or Name Like '%" + txtSuche.Text + "%' or Adresse Like '%" + txtSuche.Text + "%' or Vorname Like '%" + txtSuche.Text + "%'  or Mobil Like '%" + txtSuche.Text + "%'  or Tel Like '%" + txtSuche.Text + "%'  or Email Like '%" + txtSuche.Text + "%'"
            Else
                OfAdressenlisteBindingSource.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
        Try
            Dim idnr = GetSelectedIDNR()
            If idnr Is Nothing Then Exit Sub
            RaiseEvent AdresseUebernehmen(idnr)
            Close()

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

    Private Sub btnDetails_Click(sender As Object, e As System.EventArgs) Handles btnDetails.Click
        Try
            Dim IDNR = GetSelectedIDNR()
            If IDNR Is Nothing Then Exit Sub
            Dim k As New Kunden With {
                .MdiParent = Me.MdiParent
            }
            k.Show()
            k.LadeKunde(IDNR)
            k.FilterBy("IDNR=" & idnr)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Function GetSelectedIDNR() As String
        Dim row = Nothing
        If dgAdressen.SelectedRows.Count > 0 Then
            row = dgAdressen.SelectedRows(0)
        ElseIf dgAdressen.SelectedCells.Count > 0 Then
            Dim cell = dgAdressen.SelectedCells(0)
            row = dgAdressen.Rows(cell.RowIndex)
        End If
        If row IsNot Nothing Then
            Return row.cells(0).Value
        Else
            Return Nothing
        End If
    End Function

    Private Sub dgAdressen_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgAdressen.CellDoubleClick
        Try
            btnUebernehmen_Click(sender, e)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub



End Class