Imports System.Net.Http
Imports Newtonsoft.Json

Public Class AnularPicadas
    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
        estIncPicBL.Visible = False
        If Session("modo") = "PERSONAL" Then
            If Session("persona").Personas(0).Dni = "44286377S" Then
                estIncPicBL.Visible = True
            End If
            Dim persons As List(Of Models.Personal) = Session("persona").Personas
            Dim listaPersonas As New List(Of Models.Personal)
            Dim listDni = Session("usuariosSeleccionados")
            For Each pers As String In listDni
                Dim per = persons.FirstOrDefault(Function(x) x.Dni = pers)
                listaPersonas.Add(per)
            Next
            cbPersonas.DataSource = listaPersonas.OrderBy(Function(x) x.Nombre)
            cbPersonas.DataBind()
            'Dim DivSelect = "<select class='selectIn' style='width:100%'>"
            'For Each pers As Models.Personal In listaPersonas
            '    DivSelect += "<option value='" + pers.Dni + "'>" + pers.Nombre + "</option>"
            'Next
            'DivSelect += "</select>"
            'selPersonas.InnerHtml = DivSelect
        Else
            Response.Redirect("default.aspx")
        End If
    End Sub

    Protected Async Sub cargarPicadas(sender As Object, e As EventArgs)
        Dim persona As New List(Of String)
        persona.Add(cbPersonas.Value)
        Dim Fei = CDate(fechaInicioListInc.Value)
        Dim Fef = CDate(fechaFinListInc.Value)

        Dim consulta As New Models.Consulta
        consulta.listaNif = persona
        consulta.fechaInicio = Fei
        consulta.fechaFin = Fef
        Dim jsonConsulta As String = JsonConvert.SerializeObject(consulta)
        Dim url As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/ListPicadasAnular"
        Dim clienteHTTP As New HttpClient
        Dim contenidoConsultaHTTP As New StringContent(jsonConsulta, Encoding.UTF8, "application/json")
        Debug.WriteLine("CargarListadodePicadasAsync:1===" + Now.ToLongTimeString)
        Dim respuestaHTTP As HttpResponseMessage = Await clienteHTTP.PostAsync(url, contenidoConsultaHTTP).ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:2===" + Now.ToLongTimeString)
        Dim sRespuesta As String = Await respuestaHTTP.Content.ReadAsStringAsync().ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:3===" + Now.ToLongTimeString)
        Dim settings As New Newtonsoft.Json.JsonSerializerSettings()
        settings.CheckAdditionalContent = True
        Dim objetoDatos As List(Of Models.listPicadasAnular) =
            Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of Models.listPicadasAnular))(sRespuesta, settings)

        Dim html = ""

        html += "<table>" +
            "<tr><th style='width:100px'>Inactiva</th><th>Fecha / Hora</th><th>Incidencia</th></tr>"

        For Each picada As Models.listPicadasAnular In objetoDatos
            Dim check = If(picada.activo = True, "checked", "")
            html += "<tr><td style='text-align:center'><input type='checkbox' value='" + picada.id.ToString + "' class='cbPic'  " + check + "></td><td>" + picada.fecha.ToString("dd/MM/yyyy HH:mm") + "</td><td>" + picada.incidencia + "</td></tr>"
        Next

        html += "</table>"

        tablaPicadasAnular.InnerHtml = html
        tablaPicadasAnular.Style.Add("display", "flex !important")
    End Sub
    <System.Web.Services.WebMethod()>
    Public Shared Async Function ModificarPicadas(cbList As String) As Threading.Tasks.Task(Of String)

        Dim objetoConsulta As New List(Of Models.EditarPicadasAnuladas)
        Dim cbList1 = cbList.Split("/")
        For Each str As String In cbList1
            Dim s = str.Split("-")
            Dim nEdPi As New Models.EditarPicadasAnuladas
            nEdPi.idPicada = CInt(s(0))
            nEdPi.estado = CBool(s(1))
            objetoConsulta.Add(nEdPi)
        Next

        Dim jsonConsulta As String = JsonConvert.SerializeObject(objetoConsulta)
        Dim url As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/EditarPicadasAnular"
        Dim clienteHTTP As New HttpClient
        Dim contenidoConsultaHTTP As New StringContent(jsonConsulta, Encoding.UTF8, "application/json")
        Debug.WriteLine("CargarListadodePicadasAsync:1===" + Now.ToLongTimeString)
        Dim respuestaHTTP As HttpResponseMessage = Await clienteHTTP.PostAsync(url, contenidoConsultaHTTP).ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:2===" + Now.ToLongTimeString)
        Dim sRespuesta As String = Await respuestaHTTP.Content.ReadAsStringAsync().ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:3===" + Now.ToLongTimeString)
        Dim settings As New Newtonsoft.Json.JsonSerializerSettings()
        settings.CheckAdditionalContent = True
        Dim objetoDatos As Models.resultadoEditarPicadasAnuladas =
            Newtonsoft.Json.JsonConvert.DeserializeObject(Of Models.resultadoEditarPicadasAnuladas)(sRespuesta, settings)


        Return objetoDatos.mensaje
    End Function
End Class