Imports ApRiaServices.Web
Imports System.ServiceModel.DomainServices.Client

Partial Public Class LoadOperation
  Inherits Page

  Private idAlumnoActual As Integer

  Public Sub New()
    InitializeComponent()
  End Sub

  'Se ejecuta cuando el usuario navega a esta página.
  Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
    Dim contextoDom As New NotasAlumsDomSvc()
    Dim opDeCarga As LoadOperation(Of alumno) =
      contextoDom.Load(contextoDom.GetAlumnosOrdQuery())
    Me.dgAlumnos.ItemsSource = opDeCarga.Entities

    'Dim contextoDom As New NotasAlumsDomSvc()
    'Dim consulta As EntityQuery(Of alumno) =
    '  From alum In contextoDom.GetAlumnosQuery()
    '  Order By alum.nombre
    '  Select alum
    'contextoDom.Load(consulta)
    'Me.dgAlumnos.ItemsSource = contextoDom.alumnos

    'Dim contextoDom As New NotasAlumsDomSvc()
    'contextoDom.Load(contextoDom.GetAlumnosOrdQuery())
    'Me.dgAlumnos.ItemsSource = contextoDom.alumnos
  End Sub

  Private Sub dgAlumnos_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
    If e.AddedItems.Count = 0 Then Return
    Dim alum As alumno = TryCast(e.AddedItems(0), alumno)
    If alum Is Nothing Then Return

    Dim contextoDom As New NotasAlumsDomSvc()
    contextoDom.Load(contextoDom.GetAsignaturasAlumQuery(alum.id_alumno))
    Me.dgAsignaturas.ItemsSource = contextoDom.asignaturas

    idAlumnoActual = alum.id_alumno
  End Sub

  Private Sub dgAsignaturas_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
    If e.AddedItems.Count = 0 Then
      Return
    End If
    Dim asig As asignatura = TryCast(e.AddedItems(0), asignatura)
    If asig Is Nothing Then
      Return
    End If

    Dim contextoDom As New NotasAlumsDomSvc()
    Dim opDeCarga As LoadOperation(Of alum_asig) =
      contextoDom.Load(
        contextoDom.GetNotaAlumAsigQuery(idAlumnoActual,
                                         asig.id_asignatura))
    AddHandler opDeCarga.Completed, AddressOf opDeCarga_Completed
  End Sub

  Private Sub opDeCarga_Completed(sender As Object, e As EventArgs)
    Dim opDeCarga As LoadOperation(Of alum_asig) =
      TryCast(sender, LoadOperation(Of alum_asig))
    Me.tbNota.DataContext = opDeCarga.Entities.First()
  End Sub
End Class
