Imports IntraSell_DLL
Imports IntraSell_Net_Base
Public Class Anruf
    'Implements InterfacePrintable
    Public Event NotizErstellt()

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

    Private Sub Anruf_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            AnruflisteTableAdapter.FillByDatum(DsAnrufe.Anrufliste, DateTime.Today.AddDays(-7))
            ds = DsAnrufe
            ParentBindingNavigator.BindingSource = AnruflisteBindingSource
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

#Region "New"

    Public Sub BindingNavigatorAddNewItem_Click(sender As Object, e As System.EventArgs)
        'Reload 
        AnruflisteTableAdapter.FillByDatum(DsAnrufe.Anrufliste, DateTime.Today.AddDays(-7))
        AddingNewFlag = True
    End Sub

    Private Sub AnruflisteBindingSource_currentchanged(sender As Object,
e As System.EventArgs) _
                          Handles AnruflisteBindingSource.CurrentChanged
        Try
            If AddingNewFlag Then
                BeginNew()
                AddingNewFlag = False
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub BeginNew()
        Try
            AnrufNrTextBox.Text = NextId("ofAnrufe", "AnrufNr")

            MitarbeiterControl.IDNR = MitarbeiterID
            AdressenControl1.IDNR = Nothing
            AdressenProfil1.IDNR = Nothing

            BeginZeitDateTimePicker.Format = DateTimePickerFormat.Custom
            BeginZeitDateTimePicker.CustomFormat = " "

            EndeZeitDateTimePicker.Format = DateTimePickerFormat.Custom
            EndeZeitDateTimePicker.CustomFormat = " "

            NAnrufDateTimePicker.Format = DateTimePickerFormat.Custom
            NAnrufDateTimePicker.CustomFormat = " "

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

    ''' <summary>
    ''' Starte Anruf für IDNR 
    ''' </summary>
    ''' <param name="IDNR"></param>
    ''' <remarks></remarks>
    Public Sub StarteAnruf(IDNR As Object)
        Try

            AnruflisteBindingSource.AddNew()
            BeginNew()
            AdressenControl1.IDNR = IDNR
            AdressenProfil1.IDNR = IDNR


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


#End Region
    'SAVE 
    Public Sub AnruflisteBindingNavigatorSaveItem_Click(sender As Object, e As System.EventArgs) Handles MyBase.Save
        Try
            'Focus another control to finish Netiz editing

            MitarbeiterControl.Focus()
            MitarbeiterControl.IDNR = MitarbeiterID
            NotizenTextBox.Focus()
            Dim row As dsAnrufe.AnruflisteRow = AnruflisteBindingSource.Current.Row
            row.AdrNr = AdressenControl1.IDNR
            row.LAenderung = Now
            AdressenProfil1.UpdateAll()

            Validate()
            AnruflisteBindingSource.EndEdit()
            AnruflisteTableAdapter.Update(DsAnrufe)
            RaiseEvent NotizErstellt()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    'FILTER
    Public Sub FilterByAnrufNr(AnrufNr As Object)
        Try
            AnruflisteTableAdapter.FillByrufNr(DsAnrufe.Anrufliste, AnrufNr)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub StartStopButton_Click(sender As Object, e As System.EventArgs) Handles StartStopButton.Click
        If StartStopButton.Text = "Start" Then
            Timer1.Start()

            StartStopButton.Text = "Stop"
            BeginZeitDateTimePicker.Value = Date.Now
        Else
            Timer1.Stop()
            StartStopButton.Text = "Start"
            EndeZeitDateTimePicker.Value = Date.Now
        End If
    End Sub

    Sub TimeTicks() Handles Timer1.Tick
        EndeZeitDateTimePicker.Value = Date.Now
    End Sub

    Private Sub NAnrufDateTimePicker_ValueChanged(sender As Object, e As System.EventArgs) Handles NAnrufDateTimePicker.ValueChanged
        NAnrufDateTimePicker.Format = DateTimePickerFormat.Short
    End Sub

    Private Sub BeginZeitDateTimePicker_ValueChanged(sender As Object, e As System.EventArgs) Handles BeginZeitDateTimePicker.ValueChanged
        BeginZeitDateTimePicker.Format = DateTimePickerFormat.Time
    End Sub

    Private Sub EndeZeitDateTimePicker_ValueChanged(sender As Object, e As System.EventArgs) Handles EndeZeitDateTimePicker.ValueChanged
        EndeZeitDateTimePicker.Format = DateTimePickerFormat.Time
    End Sub

    Private Sub AdressenAnlageControl1_OnNewIdnrCreated(IDNR As Integer) Handles AdressenAnlageControl1.OnNewIdnrCreated
        Try
            'reload
            AdressenControl1.Refresh()
            AdressenControl1.IDNR = IDNR
            AdressenProfil1.IDNR = IDNR

            'Save Text 
            NotizenTextBox.Text += vbNewLine & "Adresse:" & vbNewLine & AdressenAnlageControl1.AdresseTextBox.Text

            FirmaTextBox.Text = FirstRow("select firma from ofAdressen where idnr = " & IDNR)
            AdresseTextBox.Text = FirstRow("select adresse from ofAdressen where idnr = " & IDNR)
            PLZTextBox.Text = FirstRow("select PLZ from ofAdressen where idnr = " & IDNR)
            OrtTextBox.Text = FirstRow("select Ort from ofAdressen where idnr = " & IDNR)
            TelTextBox.Text = FirstRow("select Tel from ofAdressen where idnr = " & IDNR)
            mobilTextBox.Text = FirstRow("select Mobil from ofAdressen where idnr = " & IDNR)
            EmailTextBox.Text = FirstRow("select Email from ofAdressen where idnr = " & IDNR)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AngebotToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles AngebotToolStripMenuItem.Click
        Try
            Dim v As New Vorgang With {
                .MdiParent = MdiParent
            }
            v.Show()
            v.BeginNewVorgang_AbstractVorgangMittouchForm(VorgangTypen.AN, AdressenControl1.IDNR)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ArchivierenToolStripButton_Click(sender As Object, e As System.EventArgs)
        Try
            ArchiviertCheckBox.Checked = True
            AnruflisteTableAdapter.AnrufArchiviertQuery(True, AnrufNrTextBox.Text)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

End Class