Imports DevExpress.Web
Imports DevExpress.XtraPrinting
Imports DevExpress.Export
Imports System.Drawing

Public Class ucPostulantes
    Inherits ucBase

    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
        'btnReportePostulantes.ClientSideEvents.Click = String.Format("function(s, e)  {{ window.open('wfReporteEmpleos.aspx?r=postulante&p={0}','_blank'); }}", Session("plaza"))
        menuImprimirPostulantes.ClientSideEvents.ItemClick = String.Format("function(s, e)  {{ var i = e.item.name; if(i == 'codigo') window.open('wfReporteEmpleos.aspx?r=postulanteCodigo&p={0}','_blank'); if(i == 'completo') window.open('wfReporteEmpleos.aspx?r=postulante&p={0}','_blank'); }}", Session("plaza"))
        If _calificar = False Then
            btnFinalizar.Visible = False
        End If
    End Sub

    Protected Sub gridReferencias_BeforePerformDataSelect(sender As Object, e As EventArgs)
        Dim grid As ASPxGridView = CType(sender, ASPxGridView)
        Session("idUsuario") = grid.GetMasterRowFieldValues("idUsuario").ToString()

    End Sub

    'Protected Sub btnExportar_Click(sender As Object, e As EventArgs) Handles btnExportar.Click
    '    gridExporter.WriteXlsxToResponse(New XlsxExportOptionsEx With {.ExportType = ExportType.WYSIWYG})
    'End Sub


    Protected Sub gridExporter_RenderBrick(sender As Object, e As ASPxGridViewExportRenderingEventArgs) Handles gridExporter.RenderBrick
        Dim dataColumn As GridViewDataColumn = TryCast(e.Column, GridViewDataColumn)
        If e.RowType = GridViewRowType.Data AndAlso dataColumn IsNot Nothing AndAlso dataColumn.FieldName = "CodigoPostulante" Then
            'e.BrickStyle.ForeColor = If(e.GetValue("sexo").ToString.Equals("M"), System.Drawing.Color.Blue, System.Drawing.Color.Pink)
            'e.BrickStyle.BackColor = System.Drawing.Color.
            e.BrickStyle.Font = New Font(e.BrickStyle.Font, FontStyle.Bold)
        End If

        If e.Column.Grid.ID = "gridPreSeleccionados" Then
            If e.RowType = GridViewRowType.Header Then
                e.BrickStyle.BackColor = Color.Gray
                e.BrickStyle.ForeColor = Color.White
            ElseIf e.RowType = GridViewRowType.Data Then
                e.BrickStyle.BackColor = System.Drawing.Color.Azure
            End If
        End If
    End Sub

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

    Protected Sub gridPostulanteEspecialidades_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub


    Protected Sub gridPostulanteIdioma_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub

    Protected Sub gridPostulanteCapacitaciones_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False

        End If
    End Sub

    Protected Sub gridReferencias_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub

    Protected Sub gridRefLaborales_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub

    Protected Sub gridPostulanteExperiencia_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub

    Protected Sub gridPostulanteIdioma_CustomUnboundColumnData(sender As Object, e As ASPxGridViewColumnDataEventArgs)
        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 gridPostulanteExperiencia_CustomUnboundColumnData(sender As Object, e As ASPxGridViewColumnDataEventArgs)
        If e.Column.FieldName = "tiempo" Then
            Dim inicio As New Date(e.GetListSourceFieldValue("inicioAnio"), e.GetListSourceFieldValue("inicioMes"), 1)
            Dim fin As Date
            If CBool(e.GetListSourceFieldValue("activo")) Then
                fin = Date.Now
            Else
                fin = New Date(e.GetListSourceFieldValue("finAnio"), e.GetListSourceFieldValue("finMes"), 1)
            End If
            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 gridDiscapacidad_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub

    Protected Sub btnFinalizar_Click(sender As Object, e As EventArgs) Handles btnFinalizar.Click


        Dim msg As New MSG("actualizarEstado")

 

        Dim oPlaza As New PlazaBusinessObject(CInt(Session("plaza")))
        If Now.Date > oPlaza.Entity.fechaCierreInscripcion Then
            Try
                Transaction.Begin()
                oPlaza.Entity.idEstadoPlaza = 5

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

                msg = PlazaBusinessObject.ExecuteCreacionConfiguracion(CInt(Session("plaza")), 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("Ahora que la etapa de Pre-Selección finalizó, pasara a la configuración de calificaciones de este concurso", "Pre-Selección Finalizado", , "wfConfiguracionEvaluacion.aspx")
            Catch ex As Exception
                Transaction.RollBack()
                msg.FriendlyMessage = "No fue posible finalizar el estado de Pre-Selección"
                msg.TechnicalMessage = ex.Message

            End Try
        Else
            CustomMessage("Concurso aun en período de inscripción. No puede finalizar esta etapa hasta que pase la fecha de cierre.", "Error en cierre de inscripción")
        End If


    End Sub

    Protected Sub gridPostulanteConocimientos_DataBound(sender As Object, e As EventArgs)
        Dim g As ASPxGridView = TryCast(sender, ASPxGridView)
        If g.VisibleRowCount = 0 Then
            g.ClientVisible = False
        End If
    End Sub
End Class