Imports IntraSell_DLL

Public Class Anrufliste

    Const AnrufNr_index As Integer = 0
    Const AdrNr_index As Integer = 1
    Const NAnruf_index As Integer = 4
    Const COLUMN_MITARBEITER = 7

    Private Sub Form_Verlassen() Handles MyBase.Verlassen
        Me.Close()
    End Sub

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


        Try
            Me.DsAnrufe.EnforceConstraints = False

            BeginZeitVonDateTimePicker.Value = Today.AddDays(-7)
            BeginZeitBisDateTimePicker.Value = Today.AddDays(1)
            LoadData()

            LandComboBox.Text = ""
            KundengruppeComboBox.Text = ""
            PreislisteComboBox.Text = ""

            'Me.OfAdressenlisteTableAdapter.Fill(Me.DsAnrufe.ofAdressenliste)
            ds = DsAnrufe
            ParentBindingNavigator.BindingSource = AnruflisteBindingSource

            Dim fU = New FormUtility
            fU.MakeSplitterCustomizable(SplitContainer, "ANRUFLLISTE_SPLITTER_SIZE")

            SpaltenEinstellen.KonfiguriereDataGridViewSpalten(Me.OfAdressenlisteDataGridView)
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub

    Sub LoadData()
        Try
            AnruflisteTableAdapter.FillByDatum(DsAnrufe.Anrufliste, BeginZeitVonDateTimePicker.Value)
            AnruflisteBindingSource.Filter = " (Archiviert is null or Archiviert=false) "
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Public Sub FilterBy(Expression As Object)
        Try
            AnruflisteBindingSource.Filter = Expression
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Public Sub Anrufprotokoll(IDNR As Object)
        Try
            AdressenControl1.IDNR = IDNR
            Call AnrufButton_Click(Nothing, Nothing)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    'Filtern
    Private Sub AnrufButton_Click(sender As Object, e As System.EventArgs) Handles FilterButton.Click
        Try
            'zu langsam
            LoadData()

            Dim filter As String = "1=1 "

            If BeginZeitVonDateTimePicker.Value <> BeginZeitVonDateTimePicker.MinDate Then
                filter &= " and beginZeit >= '" & BeginZeitVonDateTimePicker.Value & "'"
            End If

            If BeginZeitBisDateTimePicker.Value <> BeginZeitBisDateTimePicker.MaxDate Then
                filter &= " and beginZeit <= '" & BeginZeitBisDateTimePicker.Value & "'"
            End If

            If AdressenControl1.IDNR > 0 Then
                filter &= " and AdrNr = " & CStr(AdressenControl1.IDNR)
            End If

            If LandComboBox.Text.Length > 0 Then
                filter += " and Land Like '" & LandComboBox.Text & "%'"
            End If

            If PLZVonTextBox.Text.Length > 0 Then
                filter += " and PLZ >= '" & PLZVonTextBox.Text & "'"
            End If

            If PLZBisTextBox.Text.Length > 0 Then
                filter += " and PLZ <= '" & PLZBisTextBox.Text & "'"
            End If

            If KundengruppeComboBox.Text.Length > 0 Then
                filter += " and Kundengruppe Like '" & KundengruppeComboBox.Text & "%'"
            End If

            If PreislisteComboBox.Text.Length > 0 Then
                filter += " and Preisliste Like '" & PreislisteComboBox.Text & "%'"
            End If

            If MitarbeiterControl.IDNR > 0 Then
                filter &= " and MitarbeiterNr = " & CStr(MitarbeiterControl.IDNR)
            End If

            If NotizTextBox.Text.Length > 0 Then
                filter &= " and Notizen Like '%" & NotizTextBox.Text & "%'"
            End If

            If Me.AngebotCheckBox.CheckState = CheckState.Checked Then
                filter &= " and Angebot=True"
            ElseIf Me.AngebotCheckBox.CheckState = CheckState.Unchecked Then
                filter &= " and Angebot=False"
            End If

            If Me.InfoCheckBox.CheckState = CheckState.Checked Then
                filter &= " and WeitereInformationen=True"
            ElseIf Me.InfoCheckBox.CheckState = CheckState.Unchecked Then
                filter &= " and WeitereInformationen=False"
            End If

            If Me.WettberwerbCheckBox.CheckState = CheckState.Checked Then
                filter &= " and Wettbewerb is not null"
            ElseIf Me.WettberwerbCheckBox.CheckState = CheckState.Unchecked Then
                filter &= " and Wettbewerb is null"
            End If

            If Me.ArchiviertCheckBox.CheckState = CheckState.Checked Then
                filter &= " and Archiviert=true"
            Else
                filter &= " and (Archiviert is null or Archiviert=false)"
            End If

            If filter IsNot Nothing Then
                AnruflisteBindingSource.Filter = filter
            Else
                AnruflisteBindingSource.Filter = Nothing
            End If

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

    'Anruf öffnen 
    Private Sub OfAdressenlisteDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles OfAdressenlisteDataGridView.RowHeaderMouseDoubleClick
        Try
            Dim AnrufNR = OfAdressenlisteDataGridView.SelectedRows(0).Cells(0).Value
            OeffneAnruf(AnrufNR)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    'Format 
    Private Sub DataGridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles OfAdressenlisteDataGridView.CellFormatting
        Dim dr = OfAdressenlisteDataGridView.Rows(e.RowIndex)
        Dim anruf As dsAnrufe.AnruflisteRow = OfAdressenlisteDataGridView.Rows(e.RowIndex).DataBoundItem.row

        If e.ColumnIndex = COLUMN_MITARBEITER Then
            If Not anruf.IsMitarbeiterNrNull Then
                dr.Cells(COLUMN_MITARBEITER).Style.BackColor = MitarbeiterFarbe(anruf.MitarbeiterNr)
            End If
        End If

        If Not IsDBNull(dr.Cells(NAnruf_index).Value) Then
            Dim nAnruf As Date = dr.Cells(NAnruf_index).Value

            'Anruf zu spät
            If nAnruf < Today.AddDays(-1) Then
                e.CellStyle.BackColor = Color.Red
            End If

            'Heute Anrufen 
            If nAnruf > Today.AddDays(-1) AndAlso nAnruf <= Today Then 'Abgeschlossen
                e.CellStyle.BackColor = Color.Lime
            End If

            'Morgen anrufen 
            If nAnruf > Today Then 'Zukunft 
                e.CellStyle.BackColor = Color.Wheat
            End If
        End If
    End Sub

    Private Sub NotizTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles NotizTextBox.TextChanged
        AnrufButton_Click(Nothing, Nothing)
    End Sub



    'Enable Menu angebot erstellen 
    Private Sub OfAdressenlisteDataGridView_SelectionChanged(sender As Object, e As System.EventArgs) Handles OfAdressenlisteDataGridView.SelectionChanged
        Try
            If OfAdressenlisteDataGridView.SelectedRows.Count > 0 Then
                AktionenToolStripMenuItem.Enabled = OfAdressenlisteDataGridView.SelectedRows.Count > 0 _
               And OfAdressenlisteDataGridView.SelectedRows(0).Cells(1).Value > 0
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AngebotToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles AktionenToolStripMenuItem.Click
        'Ignore
    End Sub

    Private Sub PLZVonTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles PLZVonTextBox.TextChanged
        PLZBisTextBox.Text = PLZVonTextBox.Text
    End Sub

    Private Sub KundenmaskeÖffnenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KundenmaskeÖffnenToolStripMenuItem.Click
        Try
            Dim KundNr = OfAdressenlisteDataGridView.SelectedRows(0).Cells(1).Value
            FormManager.OeffneKundenMaske(KundNr, NeuesFenster:=True)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub TelefonischBestellungToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TelefonischBestellungToolStripMenuItem.Click
        Try
            Dim KundNr = OfAdressenlisteDataGridView.SelectedRows(0).Cells(1).Value
            FormManager.OeffneTelefonischeBestellung(KundNr)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AngebotErstellenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AngebotErstellenToolStripMenuItem.Click
        Try
            Dim KundNr = OfAdressenlisteDataGridView.SelectedRows(0).Cells(1).Value
            Dim v As New Vorgang With {
                .MdiParent = MdiParent
            }
            v.Show()
            v.BeginNewVorgang_AbstractVorgangMittouchForm(VorgangTypen.AN, KundNr)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub btnSpalten_Click(sender As Object, e As EventArgs) Handles btnSpalten.Click
        Try
            SpaltenEinstellen.KonfiguriereDataGridView(Me.OfAdressenlisteDataGridView)
            SpaltenEinstellen.ShowDialog()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
End Class