Imports System.Net
Imports System.Net.Http
Imports System.Net.Security
Imports System.Threading.Tasks
Imports DevExpress.XtraPrinting.Native
Imports Newtonsoft.Json
Imports WebIntranet.Models

Public Class gestionTeletrabajo
    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
        Else
            Response.Redirect("default.aspx")
        End If

    End Sub
    <System.Web.Services.WebMethod()>
    Public Shared Async Function comprobarFiestas(fechaInicio As String, fechaFinal As String) As Task(Of String)

        Dim fechaInicial As Date = DateTime.Parse(fechaInicio)

        Dim fcFinal As Date = DateTime.Parse(fechaFinal)


        Dim fechaInicioFormato = fechaInicial.Year.ToString + "-" + fechaInicial.Month.ToString.PadLeft(2, "0") + "-" + fechaInicial.Day.ToString.PadLeft(2, "0") + "T00:00:00.000Z"
        Dim fechaFinalFormato = fcFinal.Year.ToString + "-" + fcFinal.Month.ToString.PadLeft(2, "0") + "-" + fcFinal.Day.ToString.PadLeft(2, "0") + "T00:00:00.000Z"
        Dim objetoconsulta As New FechaIniFin
        objetoconsulta.fechaInicio = fechaInicioFormato
        objetoconsulta.fechaFin = fechaFinalFormato
        Dim jsonConsulta As String = JsonConvert.SerializeObject(objetoconsulta)
        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim clienteHTTP1 As New Net.WebClient()
        clienteHTTP1.Encoding = System.Text.Encoding.UTF8

        Dim url As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/FiestasEntreFechas"
        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)

        Return sRespuesta

    End Function
    <System.Web.Services.WebMethod()>
    Public Shared Async Function cargarListaTeletrabajo(fechaInicio As String) As Task(Of String)
        '', fechaFinal As String
        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")
        Dim nif = ""
        Dim expediente = ""
        Dim us As New List(Of String)
        'If usuariosSeleccionados IsNot Nothing Then
        '    For Each per As String In usuariosSeleccionados
        '        us.Add(per)
        '    Next
        'Else
        us.Add(sessionUsuario)
        'End If

        'Dim fcIn As Date = DateTime.Parse(fechaInicio)
        Dim fcIn As Date = Now
        fcIn = New Date((fcIn.Year - 5).ToString, fcIn.Month.ToString, 1.ToString)

        'Dim fcFn As Date = fcIn.AddMonths(1).AddDays(-1) ' DateTime.Parse(fechaFinal)
        Dim fcFn As Date = fcIn.AddYears(5)
        Dim fechaIni = fcIn.Month.ToString.PadLeft(2, "0") + "%2F01%2F" + fcIn.Year.ToString
        Dim fechaEnd = fcFn.Month.ToString.PadLeft(2, "0") + "%2F" + fcFn.Day.ToString.PadLeft(2, "0") + "%2F" + fcFn.Year.ToString


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

        Dim objetoConsulta As New Models.Consulta
        objetoConsulta.fechaInicio = fcIn
        objetoConsulta.fechaFin = fcFn
        objetoConsulta.listaNif = us


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


        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.ResultadoListTeletrabajoEntreFechas =
            Newtonsoft.Json.JsonConvert.DeserializeObject(Of Models.ResultadoListTeletrabajoEntreFechas)(sRespuesta, settings)
        Return sRespuesta

    End Function
    <System.Web.Services.WebMethod()>
    Public Shared Async Function cargarTeletrabajo(idTele As String) As Task(Of String)
        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim url3 As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/ObtenerTeletrabajo?id=" + idTele
        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 ModTele(id As Integer, excepcion As Boolean, nuDias As Integer, lunes As Boolean, martes As Boolean, miercoles As Boolean, jueves As Boolean, viernes As Boolean, motivo As String, Fei As Date, Fef As Date) As Task(Of String)


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

        Dim objetoConsulta As New Models.TramosTeletrabajo
        objetoConsulta.id = id
        objetoConsulta.excepcion = excepcion
        objetoConsulta.nuDias = nuDias
        objetoConsulta.lunes = lunes
        objetoConsulta.martes = martes
        objetoConsulta.miercoles = miercoles
        objetoConsulta.jueves = jueves
        objetoConsulta.viernes = viernes
        objetoConsulta.motivo = motivo
        objetoConsulta.Fei = Fei
        objetoConsulta.Fef = Fef


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


        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 sRespuesta

    End Function
    <System.Web.Services.WebMethod()>
    Public Shared Async Function DelTele(id As Integer) As Task(Of String)
        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim clienteHTTP1 As New Net.WebClient()
        clienteHTTP1.Encoding = System.Text.Encoding.UTF8

        Dim objetoConsulta As New Models.TramosTeletrabajo
        objetoConsulta.id = id
        objetoConsulta.excepcion = False
        objetoConsulta.nuDias = 0
        objetoConsulta.lunes = False
        objetoConsulta.martes = False
        objetoConsulta.miercoles = False
        objetoConsulta.jueves = False
        objetoConsulta.viernes = False
        objetoConsulta.motivo = ""
        objetoConsulta.Fei = Nothing
        objetoConsulta.Fef = Nothing


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


        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 sRespuesta
    End Function

End Class