Imports System.Linq.Expressions
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports Newtonsoft.Json
Imports Serialize.Linq.Serializers
Imports WebIntranet.ClasesDatosAdmin

Public Class desgVidaAdministrativa
    Inherits System.Web.UI.Page

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

        If Session("Persona") Is Nothing Then
            Response.Redirect("default.aspx")
        End If
        Dim sessionUsuario As String = Session("usuario")
        Dim idVidaAd = Request.QueryString("idVi")
        'Session.Add("idVi", idVidaAd)
        Dim cl = Request.QueryString("cl")
        Dim sCl2 = tsl5.crypt.SHA1("M3Soft." & idVidaAd.ToString)
        If sCl2 <> cl Then
            Response.Redirect("/default.aspx", False)
        Else
            Dim client As New HttpClient()
            Dim token As String = Session("token")
            client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", token)
            client.BaseAddress = New Uri(ConfigurationManager.AppSettings("SwaggerVB"))
            Dim VidaAdministrativaEnlazada As VIDA_ADMINISTRATIVA
            Dim response = Await client.GetAsync("/api/VIDA_ADMINISTRATIVA/" + idVidaAd.ToString())
            Dim responseContent = Await response.Content.ReadAsStringAsync()
            VidaAdministrativaEnlazada = If(JsonConvert.DeserializeObject(Of VIDA_ADMINISTRATIVA)(responseContent), New VIDA_ADMINISTRATIVA())

            '    Dim serializer = New ExpressionSerializer(New Serialize.Linq.Serializers.JsonSerializer())
            '    Dim IDVIDA As Integer = VidaAdministrativaEnlazada.IDVIDA
            '    Dim filtroPue As Expression(Of Func(Of LINEASVIDAADMINISTRATIVA, Boolean)) = Function(x) x.IDVIDAADMIN = IDVIDA
            'Dim PueExpression = serializer.SerializeText(filtroPue)
            'Dim PuejsonContent = JsonConvert.SerializeObject(New With {
            '    .Expression = PueExpression})
            'Dim Puecontent = New StringContent(PuejsonContent, Encoding.UTF8, "application/json")
            Dim lineasVidaAdministrativaEnlazada As List(Of LINEASVIDAADMINISTRATIVA)

            Dim response1 = Await client.GetAsync("/api/LINEASVIDAADMINISTRATIVA/LineasVidaAdminGridbyId/" + idVidaAd)
            Dim responseContent1 = Await response1.Content.ReadAsStringAsync()
            lineasVidaAdministrativaEnlazada = If(JsonConvert.DeserializeObject(Of List(Of LINEASVIDAADMINISTRATIVA))(responseContent1), New List(Of LINEASVIDAADMINISTRATIVA)())



            actAdmVA.Value = VidaAdministrativaEnlazada.IDMOTIVONavigation.DESCRIPCION.Trim()
            CausaVA.Value = VidaAdministrativaEnlazada.IDMOTIVONavigation.DESCRIPCION
            'If VidaAdministrativaEnlazada.FECHA IsNot Nothing Then

            'End If
            fechaEfcVA.Value = If(VidaAdministrativaEnlazada.FECHA.HasValue, CDate(VidaAdministrativaEnlazada.FECHA).ToString("dd/MM/yyyy"), "")
            fechaResVA.Value = If(VidaAdministrativaEnlazada.FECHARESOLUCION.HasValue, CDate(VidaAdministrativaEnlazada.FECHARESOLUCION).ToString("dd/MM/yyyy"), "")
            NRPVA.Value = VidaAdministrativaEnlazada.NRP
            PuestoVA.Value = VidaAdministrativaEnlazada.IDPUESTONavigation?.DESPUESCOM
            ObservacionesVA.Value = VidaAdministrativaEnlazada.OBSERVACIONES

            Dim htmlTabla = ""

            htmlTabla += "<table class='tablaPicadas'><tr>" +
                            "<th>Código</th>" +
                            "<th>Descripción</th>" +
                            "<th>Tipo Documento</th>" +
                            "<th>Fecha</th>" +
                            "<th>Fichero</th>" +
                            "</tr>"
            For Each linAd As LINEASVIDAADMINISTRATIVA In lineasVidaAdministrativaEnlazada
                htmlTabla += "<tr>" +
                                "<td>" + linAd.IDLINEAVIDAADMIN.ToString + "</td>" +
                                "<td>" + linAd.DESCRIPCION + "</td>" +
                                "<td>" + linAd.IDTIPONavigation?.DESCRIPCION + "</td>" +
                                "<td>" + CDate(linAd.FECHASUBIDA).ToString("dd/MM/yyyy") + "</td>"

                Dim ruta = "/fichero.aspx?id=" + linAd.IDLINEAVIDAADMIN.ToString() + "&cl=" + tsl5.crypt.SHA1("M3Soft." & linAd.IDLINEAVIDAADMIN.ToString) + "&t=LINEAVIDAADMINISTRATIVA"

                If linAd.RUTA IsNot Nothing Then
                    htmlTabla += "<td><a href='" + ruta + "' style='color:blue;cursor:pointer' target='_blanck'>Visualizar</a></td>" +
                               "</tr>"
                Else
                    htmlTabla += "<td></td>" +
                               "</tr>"
                End If

            Next

            htmlTabla += "</table>"

            divTablaLiViAd.InnerHtml = htmlTabla
        End If

    End Sub



End Class