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

Public Class VidaAdministrativa
    Inherits System.Web.UI.Page

    Protected Async Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim vidaAdministrativaItems As New List(Of VIDA_ADMINISTRATIVA)
        Dim TiposActos As New List(Of TIPOAPTOSADMINISTRATIVOS)
        Dim token As String = Session("token")
        Dim client As New HttpClient()
        client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", token)
        client.BaseAddress = New Uri(ConfigurationManager.AppSettings("SwaggerVB"))
        Dim idPers = Session("persona").Personas(0).idPersona
        Dim response = Await client.GetAsync("/api/VIDA_ADMINISTRATIVA/vidaspersona/" + idPers.ToString)

        If response.IsSuccessStatusCode Then
            Dim resultContent = Await response.Content.ReadAsStringAsync()
            vidaAdministrativaItems = If(JsonConvert.DeserializeObject(Of List(Of VIDA_ADMINISTRATIVA))(resultContent), New List(Of VIDA_ADMINISTRATIVA)())
            vidaAdministrativaItems = vidaAdministrativaItems.OrderByDescending(Function(x) x.FECHA).ThenBy(Function(x) x.NUM_REG).ThenBy(Function(x) x.IDMOTIVONavigation?.IDTIPOAPTOSNavigation?.DESCRIPCION).ToList()
        Else
            Console.WriteLine($"Error al cargar Vida Administrativa: {response.StatusCode}")
        End If

        Dim TiApAd = Await client.GetAsync("/api/TIPOAPTOSADMINISTRATIVOS")
        Dim TiApAdContent = Await TiApAd.Content.ReadAsStringAsync()
        If TiApAd.IsSuccessStatusCode Then
            TiposActos = If(JsonConvert.DeserializeObject(Of List(Of TIPOAPTOSADMINISTRATIVOS))(TiApAdContent), New List(Of TIPOAPTOSADMINISTRATIVOS)())
        End If
        TiposActos = If(TiposActos?.OrderBy(Function(x) x.DESCRIPCION).ToList(), New List(Of TIPOAPTOSADMINISTRATIVOS)())
        Dim htmlTabla = ""
        If vidaAdministrativaItems.Count > 0 Then

            htmlTabla += "<table class='tablaPicadas'>" +
                     "<tr><th>Tiene documentos</th>" +
                     "<th>Actos administrativos</th>" +
                     "<th>Departamento</th>" +
                     "<th>Causa</th>" +
                     "<th>Observaciones</th>" +
                     "<th>Descripción</th>" +
                     "<th>Fecha efecto</th>" +
                     "<th>Fecha resolución</th>" +
                     "<th>Caracter ocupación</th></tr>"
            For Each itm As VIDA_ADMINISTRATIVA In vidaAdministrativaItems
                Dim descActo = TiposActos.FirstOrDefault(Function(x) x.IDTIPO = itm.IDMOTIVONavigation.IDTIPOAPTOS).DESCRIPCION
                Dim linkVidaAd = "/DatosAdministrativos/desgVidaAdministrativa.aspx?idVi=" + itm.IDVIDA.ToString().Trim() + "&cl=" + tsl5.crypt.SHA1("M3Soft." & itm.IDVIDA)
                htmlTabla += "<tr>"
                htmlTabla += "<td>" + If(itm.TIENELINEAS = True, "<i class=""fa fa-check""></i>", "") + "</td>"
                htmlTabla += "<td>" + "<a href='" + linkVidaAd + "'>" + descActo + "</td>"
                htmlTabla += "<td>" + If(itm.IDPUESTONavigation?.IDRPTDESNavigation?.IDDEPARTAMENTONavigation?.DESCRIPCION IsNot Nothing, itm.IDPUESTONavigation?.IDRPTDESNavigation?.IDDEPARTAMENTONavigation?.DESCRIPCION, "") + "</td>"
                htmlTabla += "<td></td>"
                htmlTabla += "<td>" + If(itm.OBSERVACIONES IsNot Nothing, itm.OBSERVACIONES, "") + "</td>"
                htmlTabla += "<td></td>"
                htmlTabla += "<td>" + If(itm.FECHA IsNot Nothing, itm.FECHA.ToString.Substring(0, 10), "") + "</td>"
                htmlTabla += "<td>" + If(itm.FECHARESOLUCION IsNot Nothing, itm.FECHARESOLUCION.ToString.Substring(0, 10), "") + "</td>"
                htmlTabla += "<td>" + If(itm.OCUPACION_OLDNavigation?.DESCRIPCION IsNot Nothing, itm.OCUPACION_OLDNavigation.DESCRIPCION, "") + "</td></tr>"
            Next
            htmlTabla += "</table>"
        Else
            htmlTabla += "<p style='color:red;font-weight:bold'>No se han encontrado resultados</p>"
        End If
        divTabla.InnerHtml = htmlTabla
    End Sub

    Private Function CustomTypeResolver(typeName As String) As FactorySettings
        If typeName <> "Microsoft.VisualBasic.CompilerServices.Operators" Then
            Return Nothing
        End If
        Return Nothing
    End Function

    'Private Function genrarlink(idvida As Integer, idpersonas As Integer) As String

    'End Function

End Class
Public Class CustomTypeResolver

    Public Function Resolve(typeName As String) As Type
        If typeName = "Microsoft.VisualBasic.CompilerServices.Operators" Then
            Return Nothing
        End If
        Return Type.GetType(typeName)
    End Function
End Class


