Imports DevExpress.Web

Public Class ucConfiguracionEvaluacion
    Inherits ucBase

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RegisterCustomMessage(cbpConfiguracion)
        If Not IsPostBack Then
            Session("plaza") = ""
        End If
        SqlConcursoAEvaluar.SelectParameters("idUser").DefaultValue = CInt(Page.User.Identity.Name)
    End Sub

    Protected Sub cbpConfiguracion_Callback(sender As Object, e As DevExpress.Web.CallbackEventArgsBase) Handles cbpConfiguracion.Callback
        'CustomMessage(e.Parameter.ToString(), "prueba", cbpConfiguracion)
    End Sub

    Protected Sub gridCalificaciones_BeforePerformDataSelect(sender As Object, e As EventArgs)
        Session("idConfiguracionEvaluacion") = (TryCast(sender, ASPxGridView)).GetMasterRowKeyValue()
    End Sub

    Protected Sub gridEvaluacion_DataBound(sender As Object, e As EventArgs) Handles gridEvaluacion.DataBound
        TryCast(sender, ASPxGridView).DetailRows.ExpandAllRows()
        Dim gridview As ASPxGridView = TryCast(sender, ASPxGridView)
        gridview.JSProperties("cpSummary") = gridview.GetTotalSummaryValue(gridview.TotalSummary("puntaje"))
    End Sub


    Protected Sub gridEvaluacion_HtmlFooterCellPrepared(sender As Object, e As ASPxGridViewTableFooterCellEventArgs) Handles gridEvaluacion.HtmlFooterCellPrepared
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If e.GetSummaryValue(g.TotalSummary("puntaje", DevExpress.Data.SummaryItemType.Sum)) = 100 Then
            e.Cell.BackColor = Drawing.Color.LimeGreen
        Else
            e.Cell.BackColor = Drawing.Color.IndianRed
        End If
    End Sub

    Protected Sub gridCalificaciones_HtmlFooterCellPrepared(sender As Object, e As ASPxGridViewTableFooterCellEventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If e.GetSummaryValue(g.TotalSummary("peso", DevExpress.Data.SummaryItemType.Sum)) = 100 Then
            e.Cell.BackColor = Drawing.Color.LimeGreen
        Else
            e.Cell.BackColor = Drawing.Color.IndianRed
        End If
    End Sub

    Protected Sub gridCalificaciones_DataBound(sender As Object, e As EventArgs)
        TryCast(sender, ASPxGridView).DetailRows.ExpandAllRows()
    End Sub

    Protected Sub gridCalificaciones_RowUpdating1(sender As Object, e As Data.ASPxDataUpdatingEventArgs)
        e.NewValues("idConfiguracionEvaluacion") = (TryCast(sender, ASPxGridView)).GetMasterRowKeyValue()
        e.NewValues("idConfiguracionCalificacion") = (CType(sender, ASPxGridView)).GetRowValuesByKeyValue(e.Keys(0), New String() {"idConfiguracionCalificacion"})

    End Sub

    Protected Sub gridCalificaciones_RowInserting(sender As Object, e As Data.ASPxDataInsertingEventArgs)
        e.NewValues("idConfiguracionEvaluacion") = (TryCast(sender, ASPxGridView)).GetMasterRowKeyValue()

    End Sub

    Protected Sub btnConfirmar_Click(sender As Object, e As EventArgs) Handles btnConfirmar.Click
        Dim valorMin As Double = Convert.ToDouble(gridEvaluacion.GetTotalSummaryValue(gridEvaluacion.TotalSummary("porcentaje", DevExpress.Data.SummaryItemType.Min)))
        Dim valorMax As Double = Convert.ToDouble(gridEvaluacion.GetTotalSummaryValue(gridEvaluacion.TotalSummary("porcentaje", DevExpress.Data.SummaryItemType.Max)))
        Dim msg As New MSG("configuración")

        If valorMin = 100 AndAlso valorMax = 100 Then
            Try
                Transaction.Begin()

                Dim oPlaza As New PlazaBusinessObject(CInt(cmbConcursoEvaluar.Value))
                oPlaza.Entity.idEstadoPlaza = 6
                msg = oPlaza.SaveChanges()

                If msg.EventType = dGen.Library.Entity.MSG.MsgType.Errors Then
                    Transaction.RollBack()
                    CustomMessage(msg, "error")
                End If

                msg = PlazaBusinessObject.ExecuteCreacionRegistrosConfiguracion(CInt(cmbConcursoEvaluar.Value), CInt(Page.User.Identity.Name))
                If msg.EventType = dGen.Library.Entity.MSG.MsgType.Errors Then
                    Transaction.RollBack()
                    CustomMessage(msg, "error")
                End If
                Transaction.Commit()

                CustomMessage("Se ha terminado de configurar las evaluaciones, ahora pasará a ingresar la nota en cada postulante.", "Configuración terminada", , "wfEvaluarCompetencias.aspx")

            Catch ex As Exception
                Transaction.RollBack()
                msg.FriendlyMessage = "No fue posible finalizar el estado de Configuración"
                msg.TechnicalMessage = ex.Message

            End Try
        Else
            CustomMessage("Tiene evaluaciones que no han llegado al 100%", "Todos los porcentajes deben de llegar al 100%")
        End If

        
    End Sub

    Protected Sub cmbConcursoEvaluar_DataBound(sender As Object, e As EventArgs) Handles cmbConcursoEvaluar.DataBound
        CType(sender, ASPxComboBox).NullText = CType(sender, ASPxComboBox).Items.Count & " concursos disponibles."
    End Sub
End Class