Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class NotizenControl
    Private _IDNR As Integer
    Property IDNR As Integer
        Get
            Return _IDNR
        End Get
        Set(value As Integer)

            Dim idnrChanged = _IDNR <> value

            _IDNR = value
            If idnrChanged Then
                LadeNotizen()
            End If
        End Set
    End Property
    Dim COLUMN_MITARBEITER = 3

    Public Sub LadeNotizen()
        If IDNR > 0 Then
            OfanrufeTableAdapter.FillByAdrNr(DsAnrufe.ofanrufe, IDNR)
        End If
    End Sub

    Private Sub ButtonNotiz_Click(sender As Object, e As EventArgs) Handles ButtonNotiz.Click
        Try
            Dim anruf = OeffneAnrufNeu(IDNR)
            AddHandler anruf.NotizErstellt, AddressOf NotizErstelltHandler
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    'Liste neu Laden 
    Private Sub NotizErstelltHandler()
        LadeNotizen()
    End Sub

    Private Sub ButtonSpeichern_Click(sender As Object, e As EventArgs) Handles ButtonSpeichern.Click
        Save()
    End Sub

    Private Sub DataGridViewAnrufe_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridViewAnrufe.RowHeaderMouseDoubleClick
        Try
            Dim AnrufNR = DataGridViewAnrufe.SelectedRows(0).Cells(0).Value
            Dim anruf = OeffneAnruf(AnrufNR)
            AddHandler anruf.NotizErstellt, AddressOf NotizErstelltHandler
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub

    Sub Save()
        Try
            OfanrufeBindingSource.EndEdit()
            OfanrufeTableAdapter.Update(DsAnrufe)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub DataGridViewAnrufe_UserAddedRow(sender As Object, e As DataGridViewRowEventArgs) Handles DataGridViewAnrufe.UserAddedRow
        Try
            'AddHandler  Me.OfanrufeBindingSource.new
            Dim r As dsAnrufe.ofanrufeRow = OfanrufeBindingSource.Current.row
            r.AdrNr = IDNR
            r.MitarbeiterNr = MitarbeiterID
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub DataGridViewAnrufe_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridViewAnrufe.CellFormatting
        Try
            Dim dr = DataGridViewAnrufe.Rows(e.RowIndex)
            Dim dbi = DataGridViewAnrufe.Rows(e.RowIndex).DataBoundItem

            If dbi Is Nothing Then
                Exit Sub
            End If
            Dim anruf As dsAnrufe.ofanrufeRow = dbi.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
        Catch ex As Exception

        End Try
    End Sub
End Class
