Imports System.Net
Imports System.Net.Security

Public Class Nominas
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("usuario") = "" Then
            Response.Redirect("default.aspx")
        End If

    End Sub

    Private Sub BuscarNominas_Click(sender As Object, e As EventArgs)
        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")
        Dim Anno = inAnno.Value ' pedir año
        'Dim Nif = Session("usuario") ' se obtiene de la identificacion
        Dim nominas = NominasPersona(Anno) ' datos para el combo de nominas
        Dim htmlLista = ""

        For Each nomina As NominaTrabajador In nominas
            htmlLista += "<li class=""d-flex align-items-center"" onclick=""mostrarPDF('" + nomina.idNominaTrabajador + "')""><p>" + nomina.Descripcion + "</p></li>"
        Next
        htmlLista += ""

        lisNominasTrabajador.InnerHtml = htmlLista

    End Sub


    Private Function NominasPersona(anno As String) As List(Of NominaTrabajador)
        Dim usuario As String = "tecnosis"
        Dim contraseña As String = "patata2022"
        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim url3 As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/NominasTrabajadorAnno?Nif=" + sessionUsuario + "&Anno=" + anno
        Dim clienteHTTP3 As New Net.WebClient()
        clienteHTTP3.Encoding = System.Text.Encoding.UTF8
        Dim respuesta3 As String = clienteHTTP3.DownloadString(url3)
        Dim deserializador3 As New System.Web.Script.Serialization.JavaScriptSerializer()
        deserializador3.MaxJsonLength = Integer.MaxValue
        Dim listaNominas As List(Of NominaTrabajador) =
        deserializador3.Deserialize(Of List(Of NominaTrabajador))(respuesta3)
        Return listaNominas

    End Function

    <System.Web.Services.WebMethod()>
    Public Shared Function ObtenerPdf(idNomina As String) As String
        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")
        Dim nif = sessionUsuario
        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim url3 As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/NominaPdf?Nif=" + nif + "&Idnomina=" + idNomina
        Dim credenciales3 As New Net.CredentialCache()
        Dim clienteHTTP3 As New Net.WebClient()
        clienteHTTP3.Encoding = System.Text.Encoding.UTF8
        clienteHTTP3.Credentials = credenciales3
        Dim respuesta3 As String = clienteHTTP3.DownloadString(url3)
        Dim deserializador3 As New System.Web.Script.Serialization.JavaScriptSerializer()
        deserializador3.MaxJsonLength = 50000000
        Dim sRespuesta3 As String = deserializador3.Deserialize(Of String)(respuesta3)

        Return "[{""nombre"":""" + nif + "_" + idNomina + ".pdf"",""base64String"":""" + sRespuesta3 + """}]"

    End Function

    Protected Sub Unnamed_ServerClick(sender As Object, e As EventArgs)
        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")
        Dim Anno = inAnno.Value ' pedir año
        'Dim Nif = Session("usuario") ' se obtiene de la identificacion
        Dim nominas = NominasPersona(Anno) ' datos para el combo de nominas
        Dim htmlLista = ""

        For Each nomina As NominaTrabajador In nominas
            htmlLista += "<li class=""d-flex align-items-center"" onclick=""mostrarPDF('" + nomina.idNominaTrabajador + "','" + nomina.Descripcion + "')""><p>" + nomina.Descripcion + "</p></li>"
        Next
        htmlLista += ""
        If htmlLista = "" Then
            htmlLista += "<p style=""text-align:center;color:red;font-weight:bold"">Este año no contiene nóminas</p>"
        End If
        lisNominasTrabajador.InnerHtml = htmlLista

    End Sub
End Class
