Imports System.Net
Imports System.Net.Http
Imports System.Net.Security
Imports System.Threading.Tasks
Imports Newtonsoft.Json

Public Class gestionTeletrabajoPers
    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
        If Session("modo") = "PERSONAL" AndAlso Session("persona").Personas(0).Roll = "SUPERVISORDETODO" Then

            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
            Dim usuario = Session("persona").Personas(0)
            listaPersonas.Add(usuario)
            listaPersonas = listaPersonas.OrderBy(Function(x) x.Nombre).ToList
            Dim html = ""
            html += "<select id='selPer' class='selectIn w-100' onchange='obtenerTT()'>"
            For Each itm As Models.Personal In listaPersonas
                html += "<option value='" + itm.idPersona.ToString() + "'>" + itm.Nombre + "</option>"
            Next
            html += "</select>"
            selPerDiv.InnerHtml = html

            'cbPersonas.DataSource = listaPersonas.OrderBy(Function(x) x.Nombre)
            'cbPersonas.DataBind()
        Else
            Response.Redirect("default.aspx")
        End If

    End Sub
    <System.Web.Services.WebMethod()>
    Public Shared Async Function AnadirEditarTT(idTT As String, Fei As String, Fef As String, motivo As String, idPers As String) As Task(Of String)

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim clienteHTTP1 As New Net.WebClient()
        clienteHTTP1.Encoding = System.Text.Encoding.UTF8

        Dim persons As List(Of Models.Personal) = HttpContext.Current.Session("persona").Personas
        Dim idp = idPers ' persons.FirstOrDefault(Function(x) x.Dni = idPers).idPersona
        If idp <> Nothing Then
            Dim objetoConsulta As New Models.TramoTeletrabajoExcepcion
            objetoConsulta.fechaInicio = New Date(Convert.ToInt64(Fei))
            objetoConsulta.fechaFin = New Date(Convert.ToInt64(Fef))
            objetoConsulta.idExcepTel = CInt(idTT)
            objetoConsulta.idPersona = idp
            objetoConsulta.motivo = motivo


            Dim jsonConsulta As String = JsonConvert.SerializeObject(objetoConsulta)
            Dim url As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/AnadirEditarExcepcionTT"


            Dim clienteHTTP As New HttpClient
            Dim contenidoConsultaHTTP As New StringContent(jsonConsulta, Encoding.UTF8, "application/json")
            Dim respuestaHTTP As HttpResponseMessage = Await clienteHTTP.PostAsync(url, contenidoConsultaHTTP).ConfigureAwait(False)
            Dim sRespuesta As String = Await respuestaHTTP.Content.ReadAsStringAsync().ConfigureAwait(False)
            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 sRespuesta
        Else
            Dim result = New Models.resultadoEditarPicadasAnuladas With {
            .resultado = 1,
            .mensaje = "La persona a la que corresponde esta excepción no es accesible en este momento"
            }
            Dim JsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(result)
            Return JsonResult
        End If
    End Function
    <System.Web.Services.WebMethod()>
    Public Shared Async Function ObtTT(idPer As String, todas As Boolean) As Task(Of String)

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim url3 As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/listaExcepciones?id=" + idPer + "&todas=" + todas.ToString()
        Dim clienteHTTP3 As New Net.WebClient()
        clienteHTTP3.Encoding = System.Text.Encoding.UTF8
        Dim respuesta3 As String = clienteHTTP3.DownloadString(url3)
        Return respuesta3
    End Function
    <System.Web.Services.WebMethod()>
    Public Shared Async Function EliminarTT(idTT As Integer) As Task(Of String)

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim url3 As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/EliminarExcepcion?id=" + idTT.ToString()
        Dim clienteHTTP3 As New Net.WebClient()
        clienteHTTP3.Encoding = System.Text.Encoding.UTF8
        Dim respuesta3 As String = clienteHTTP3.DownloadString(url3)
        Return respuesta3
    End Function
    <System.Web.Services.WebMethod()>
    Public Shared Async Function TTbyId(idTT As Integer, idPer As String) As Task(Of String)

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim url3 As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/listaExcepciones?id=" + idPer + "&todas=true"
        Dim clienteHTTP3 As New Net.WebClient()
        clienteHTTP3.Encoding = System.Text.Encoding.UTF8
        Dim respuesta3 As String = clienteHTTP3.DownloadString(url3)
        Dim settings As New Newtonsoft.Json.JsonSerializerSettings()
        settings.CheckAdditionalContent = True
        Dim objetoDatos As Models.ResultadoListadoExcepciones =
                Newtonsoft.Json.JsonConvert.DeserializeObject(Of Models.ResultadoListadoExcepciones)(respuesta3, settings)
        Dim TT = objetoDatos.listado.FirstOrDefault(Function(x) x.idExcepTel = idTT)
        If TT IsNot Nothing Then
            Dim listaTT = New List(Of Models.TramoTeletrabajoExcepcion)
            listaTT.Add(TT)
            Dim resultado = New Models.ResultadoListadoExcepciones With {
                .listado = listaTT,
                .resultado = 0,
                .mensaje = ""
                }
            Return JsonConvert.SerializeObject(resultado)
        Else
            Dim resultado = New Models.ResultadoListadoExcepciones With {
                            .resultado = 1,
                            .mensaje = "No existe esta excepción"
                            }
            Return JsonConvert.SerializeObject(resultado)
        End If
        Return respuesta3
    End Function
End Class