Imports DevExpress.Web

Public Class ucFormularioEvaluacion
    Inherits ucBase
    Public Property idPlaza() As Integer
        Get
            If String.IsNullOrEmpty(Session("plaza")) Then Session("plaza") = 0
            Return Session("plaza")
        End Get
        Set(ByVal value As Integer)
            Session("plaza") = value
        End Set
    End Property

    Private _calificar As Boolean
    Public Property Calificar() As Boolean
        Get
            Return _calificar
        End Get
        Set(ByVal value As Boolean)
            _calificar = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RegisterCustomMessage(cbpBotones)

        If idPlaza <> 0 Then
            Dim oPlaza As New PlazaBusinessObject(idPlaza)
            If Now.Date <= oPlaza.Entity.fechaCierreInscripcion Then
                ltlTitulo.Text = String.Format("{0} | {1} <span style='background-color:yellow;'>(Concurso aún en período de inscripción)</span>", oPlaza.Entity.Identificador, oPlaza.Entity.nombrePuesto)
            Else
                ltlTitulo.Text = String.Format("{0} | {1}", oPlaza.Entity.Identificador, oPlaza.Entity.nombrePuesto)
            End If

        End If
        If _calificar = False Then
            btnGuardar.Visible = False
            rbConocimiento.Enabled = False
            rbFormacion.Enabled = False
            rbExperiencia.Enabled = False
        End If
        'btnPostulantes.ClientSideEvents.Click = String.Format("function(s, e)  {{ window.open('wfReporteEmpleos.aspx?r=ingresados&p={0}','_blank'); }}", Session("plaza"))
        menuImprimir.ClientSideEvents.ItemClick = String.Format("function(s, e)  {{ var i = e.item.name; if(i == 'codigo') window.open('wfReporteEmpleos.aspx?r=ingresadosCodigo&p={0}','_blank'); if(i == 'completo') window.open('wfReporteEmpleos.aspx?r=ingresados&p={0}','_blank'); }}", Session("plaza"))

    End Sub

    Protected Sub cbpDetallePostulante_Callback(sender As Object, e As DevExpress.Web.CallbackEventArgsBase) Handles cbpDetallePostulante.Callback

        If Not e.Parameter.ToString.Equals("null") Then
            Dim oPostulante As New ConcursoPostulanteBusinessObject(CInt(e.Parameter))
            Dim idUser As Integer = oPostulante.Entity.idUsuario

            rbFormacion.Value = oPostulante.Entity.vbFormacion
            rbConocimiento.Value = oPostulante.Entity.vbConocimientos
            rbExperiencia.Value = oPostulante.Entity.vbExperiencia

            If _calificar = True Then
                If oPostulante.Entity.vbFormacion = False Or oPostulante.Entity.vbConocimientos = False Or oPostulante.Entity.vbExperiencia = False Then
                    txtComentario.ClientVisible = True
                End If
            End If

            Dim oUser As New UsuariosBusinessObject(idUser)
            lblEmail.Text = String.Format("<a href='mailto:{0}'>{0}</a>", oUser.Entity.email)
            lblApellidos.Text = oUser.Entity.apellidos
            lblTelefono1.Text = oUser.Entity.telefonoContacto1
            lblTelefono2.Text = oUser.Entity.telefonoContacto2
            txtComentario.Text = ""

            Session("idConcursoPostulante") = e.Parameter
            'ObjectConcursoPostulante.SelectParameters("idUsuario").DefaultValue = idUser
            'ObjectEspecialidades.SelectParameters("idConcursoPostulante").DefaultValue = CInt(e.Parameter)
            'ObjectPostulanteExperiencia.SelectParameters("idConcursoPostulante").DefaultValue = CInt(e.Parameter)
            'ObjectPostulanteIdioma.SelectParameters("idConcursoPostulante").DefaultValue = CInt(e.Parameter)
            'ObjectPostulanteCapacitacion.SelectParameters("idConcursoPostulante").DefaultValue = CInt(e.Parameter)
        Else
            Session("idConcursoPostulante") = Nothing
        End If


    End Sub

    Protected Sub gridPostulanteEspecialidades_RowUpdating(sender As Object, e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) Handles gridPostulanteEspecialidades.RowUpdating
        e.NewValues("idPostulanteEspecialidad") = (CType(sender, ASPxGridView)).GetRowValuesByKeyValue(e.Keys(0), New String() {"idPostulanteEspecialidad"})
        e.NewValues("idConcursoPostulante") = (CType(sender, ASPxGridView)).GetRowValuesByKeyValue(e.Keys(0), New String() {"idConcursoPostulante"})

        'ObjectEspecialidades.SelectParameters("idConcursoPostulante").DefaultValue = idConcursoPostulante

        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        g.JSProperties("cpUpdate") = True
    End Sub

    Protected Sub gridPostulanteExperiencia_CustomUnboundColumnData(sender As Object, e As ASPxGridViewColumnDataEventArgs) Handles gridPostulanteExperiencia.CustomUnboundColumnData
        If e.Column.FieldName = "tiempo" Then
            Dim inicio As New Date(e.GetListSourceFieldValue("inicioAnio"), e.GetListSourceFieldValue("inicioMes"), 1)
            Dim fin As Date
            If Not e.GetListSourceFieldValue("activo") Is DBNull.Value Then
                If CBool(e.GetListSourceFieldValue("activo")) Then
                    fin = Date.Now
                Else

                    fin = New Date(e.GetListSourceFieldValue("finAnio"), e.GetListSourceFieldValue("finMes"), 1)
                End If
            Else
                fin = New Date(e.GetListSourceFieldValue("finAnio"), e.GetListSourceFieldValue("finMes"), 1)
            End If

            e.Value = GetTimeExperiencia(inicio, fin)

            'Dim anios As Integer = DateDiff(DateInterval.Year, inicio, fin)
            'Dim meses As Integer = DateDiff(DateInterval.Month, inicio, fin)
            'meses = meses Mod 12

            'e.Value = String.Format("{0} año(s), {1} mes(es)", anios, meses)
        End If
    End Sub

    Protected Sub gridPostulanteIdioma_CustomUnboundColumnData(sender As Object, e As ASPxGridViewColumnDataEventArgs) Handles gridPostulanteIdioma.CustomUnboundColumnData
        If e.Column.FieldName = "calidad" Then
            e.Value = CInt(e.GetListSourceFieldValue("IDI_Lee")) + CInt(e.GetListSourceFieldValue("IDI_Escribe")) + CInt(e.GetListSourceFieldValue("IDI_Habla"))
        End If
    End Sub


    Protected Sub gridGrado_DataBound(sender As Object, e As EventArgs) Handles gridGrado.DataBound, gridEspecialidad.DataBound, gridCompTecnicas.DataBound, gridIdioma.DataBound, gridExperiencia.DataBound
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub




    Protected Sub cbpBotones_Callback(sender As Object, e As CallbackEventArgsBase) Handles cbpBotones.Callback
        If e.Parameter.Equals("1") Then

            Dim oConcursoPostulante As New ConcursoPostulanteBusinessObject(CInt(Session("idConcursoPostulante")))

            If oConcursoPostulante.Entity.idEstadoConcursoPostulante = 2 Then
                CustomMessage("Este Postulante abandonó el Concurso.", "No se puede Aprobar o Rechazar", cbpBotones)
                Exit Sub
            End If
            If oConcursoPostulante.Entity.idEstadoConcursoPostulante = 3 Then
                CustomMessage("Este Postulante tiene documentos falsos. Si no es asi, favor cambiar la verificación de documentos en Formación", "No se puede Aprobar o Rechazar", cbpBotones)
                Exit Sub
            End If

            oConcursoPostulante.Entity.vbFormacion = CBool(rbFormacion.Value)
            oConcursoPostulante.Entity.vbConocimientos = CBool(rbConocimiento.Value)
            oConcursoPostulante.Entity.vbExperiencia = CBool(rbExperiencia.Value)
            oConcursoPostulante.SaveChanges()

            If Not String.IsNullOrEmpty(txtComentario.Text) Then
                Dim oComentario As New TecnicoComentarioBusinessObject()
                oComentario.Entity.idConcursoPostulante = CInt(Session("idConcursoPostulante"))
                oComentario.Entity.comentario = txtComentario.Text
                oComentario.Entity.usuario = CInt(Page.User.Identity.Name)
                oComentario.Entity.fecha = Now
                oComentario.SaveChanges()

            End If

            cbpBotones.JSProperties("cpUpdate") = True
        End If

    End Sub

    Protected Sub gridConcursoPostulante_HtmlDataCellPrepared(sender As Object, e As ASPxGridViewTableDataCellEventArgs) Handles gridConcursoPostulante.HtmlDataCellPrepared
        If e.DataColumn.FieldName = "idEstadoConcursoPostulante" Then
            Select Case Convert.ToInt32(e.CellValue)
                Case 2, 3, 4
                    e.Cell.BackColor = System.Drawing.Color.FromArgb(&H99, &H0, &H0)
                    e.Cell.ForeColor = Drawing.Color.White
                Case 5
                    e.Cell.BackColor = System.Drawing.Color.LightGreen
                Case Else
                    Exit Select
            End Select
        End If
    End Sub


    Protected Shared Function GetImageUrl(ByVal container As GridViewDataItemTemplateContainer) As String
        Dim categoryID As Object = container.Grid.GetRowValuesByKeyValue(container.KeyValue, New String() {"idEstadoConcursoPostulante"})
        Dim imgUrl As String
        Select Case CInt(categoryID)
            Case 1
                imgUrl = "~/Content/Images/Customer_16x16.png"
            Case 2
                imgUrl = "~/Content/Images/AssignTo_16x16.png"
            Case 3
                imgUrl = "~/Content/Images/DeleteList2_16x16.png"
            Case 4
                imgUrl = "~/Content/Images/Cancel_16x16.png"
            Case 5
                imgUrl = "~/Content/Images/success.png"
            Case Else
                imgUrl = "~/Content/Images/Customer_16x16.png"
        End Select
        Return imgUrl
    End Function

    Protected Shared Function SetToolTip(container As GridViewDataItemTemplateContainer) As String
        Dim categoryID As Object = container.Grid.GetRowValuesByKeyValue(container.KeyValue, New String() {"idEstadoConcursoPostulante"})
        Dim tooltip As String
        Select Case CInt(categoryID)
            Case 1
                tooltip = "Ingresado"
            Case 2
                tooltip = "Abandonó"
            Case 3
                tooltip = "Documento No Válido"
            Case 4
                tooltip = "Faltaron aptitudes para el puesto"
            Case 5
                tooltip = "Aprobado"
            Case Else
                tooltip = "Ingresado"
        End Select
        Return tooltip
    End Function

    Dim totalSum As Integer
    Protected Sub gridPostulantes_CustomSummaryCalculate(sender As Object, e As DevExpress.Data.CustomSummaryEventArgs) Handles gridPostulantes.CustomSummaryCalculate
        If e.SummaryProcess = DevExpress.Data.CustomSummaryProcess.Start Then
            totalSum = 0
        End If
        ' Calculation.
        If e.SummaryProcess = DevExpress.Data.CustomSummaryProcess.Calculate Then
            If CInt(e.FieldValue) = 0 Then
                totalSum += 1
            End If
        End If
        ' Finalization.
        If e.SummaryProcess = DevExpress.Data.CustomSummaryProcess.Finalize Then
            e.TotalValue = totalSum
        End If
    End Sub


    Protected Sub gridPostulantes_DataBound(sender As Object, e As EventArgs) Handles gridPostulantes.DataBound
        Dim gridview As ASPxGridView = TryCast(sender, ASPxGridView)
        gridview.JSProperties("cpSummary") = gridview.GetTotalSummaryValue(gridview.TotalSummary("Calificado"))
    End Sub


    Protected Sub gridComentarios_RowUpdating(sender As Object, e As Data.ASPxDataUpdatingEventArgs) Handles gridComentarios.RowUpdating
        e.NewValues("fecha") = Now
    End Sub

    Protected Sub gridPostulanteExperiencia_HtmlRowCreated(sender As Object, e As ASPxGridViewTableRowEventArgs) Handles gridPostulanteExperiencia.HtmlRowCreated
        If e.RowType <> GridViewRowType.Data Then
            Return
        End If
        Dim lblactual As ASPxLabel = TryCast(gridPostulanteExperiencia.FindRowCellTemplateControl(e.VisibleIndex, Nothing, "lblactual"), ASPxLabel)
        Dim lblfechafin As ASPxLabel = TryCast(gridPostulanteExperiencia.FindRowCellTemplateControl(e.VisibleIndex, Nothing, "lblfechafin"), ASPxLabel)
        Dim actual As Boolean = False
        If Not gridPostulanteExperiencia.GetRowValues(e.VisibleIndex, "activo").ToString = "" Then
            actual = CBool(gridPostulanteExperiencia.GetRowValues(e.VisibleIndex, "activo"))
        End If

        If actual Then
            lblfechafin.Visible = False
            lblactual.Visible = True
        Else
            lblfechafin.Visible = True
            lblactual.Visible = False
        End If
    End Sub
End Class