@page "/ValorNomina"
@page "/ValorNomina/{cl}"
@using BlazorBootstrap
@using GestionPersonalWeb.Components.Pages.FicherosMaestros.TipoPuestoTrabajoComponents
@using Newtonsoft.Json
@using GestionPersonalWeb.Components.Layout
@using Microsoft.AspNetCore.WebUtilities
@using GestionPersonalWeb.Components.Pages.InfoPersComponents
@using GestionPersonalWeb.Models
@using System.Linq.Expressions
@using Serialize.Linq.Serializers
@using System.Net.Http.Headers
@using bdAntifraude.clases
@using bdAntifraude.db
@using System.Reflection
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@inject IJSRuntime JSRuntime
@inject IJSRuntime JS
@rendermode InteractiveServer
@inject NavigationManager NavigationManager
@inject IHttpClientFactory HttpClientFactory
@inject IHttpContextAccessor HttpContextAccessor
@inject UserState UserState
@inject ProtectedSessionStorage ProtectedSessionStore
@inject NavigationManager Navigation


@*     <div class="Cabecera">
        <h6><small><i class="fas fa-user"></i><b> Tipo puesto de trabajo: @puestoTrabajo.DESCRIPCION </b></small></h6>
    </div> *@
    <style>
    .caj {
        padding: 20px;
        border: solid 1px grey
    }
    .caja {
        padding: 20px;
        border: solid 1px grey;
        margin:5px;
    }
    </style>
<Toasts class="p-3 font-weight-bold" Style="color:white;" AutoHide="true" Delay="4000" Messages="mensajes" Placement="ToastsPlacement.BottomCenter" />
<div class="datos-personales-container mt-3">
    <div>
        <div>
            <div><input type="button" class="btnOAAFAzul mb-2" value="Guardar" @onclick="GuardarPuestoTrabajo" /></div>
            <div class="caj">
                <div class="col-md-12">
                    <div class="row mb-3">
                        <div class="col-md-4">
                            <label for="txtdni" class="fw-bold">Fecha Inicio: </label>
                            <input class="form-control" type="date" id="txtdni" @bind-value="@oValorNomina.FECHAINCIO" />
                        </div>
                        <div class="col-md-4">
                            <label for="txtdni" class="fw-bold">Fecha Fin: </label>
                            <input class="form-control" type="date" id="txtdni" @bind-value="@oValorNomina.FECHAFIN" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="mt-3 mb-5" style="">
                <div class="col-md-12 mb-3">
                     <h6><b> Valor Nomina </b></h6>
                </div>
                <div class="row">
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b> Sueldos </b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOA1" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOA2" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOB" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOC1" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOC2" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOE" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Sueldos Extra</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOA1X" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOA2X" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOBX" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOC1X" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOC2X" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.SUELDOEX" />
                        </div>
                       
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Trienio Administración</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMA1" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMA2" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMB" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMC1" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMC2" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADME" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Trienio Administración Extra</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMA1X" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMA2X" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMBX" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMC1X" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMC2X" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIADMEX" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Trienio Oficina Antifraude</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARA1" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARA2" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARB" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARC1" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARC2" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARE" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Trienio Oficina Antifraude Extra</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARA1X" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARA2X" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARBX" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARC1X" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPARC2X" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.TRIPAREX" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Complemento Carrera Tramo I</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA1" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA2" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRB" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC1" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC2" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRE" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Complemento Carrera Tramo II</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA1II" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA2II" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRBII" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC1II" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC2II" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARREII" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Complemento Carrera Tramo III</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA1III" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA2III" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRBIII" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC1III" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC2III" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARREIII" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Complemento Carrera Tramo IV</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA1IV" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA2IV" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRBIV" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC1IV" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC2IV" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARREIV" />
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="caja">
                            <h6><b>Complemento Carrera Tramo V</b></h6>
                            <label for="txtdni" class="fw-bold">A1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA1V" />
                            <label for="txtdni" class="fw-bold">A2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRA2V" />
                            <label for="txtdni" class="fw-bold">B: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRBV" />
                            <label for="txtdni" class="fw-bold">C1: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC1V" />
                            <label for="txtdni" class="fw-bold">C2: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARRC2V" />
                            <label for="txtdni" class="fw-bold">E: </label>
                            <input class="form-control" id="txtdni" @bind-value="@oValorNomina.COMCARREV" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



@code {
    [Parameter]
    public string? cl { get; set; } = "";
    private static bdAntifraude.db.VALORESNOMINA oValorNomina = new VALORESNOMINA();
    private EditContext editContext = new EditContext(oValorNomina);

    private HttpClient cliente = new HttpClient();
    private ValidationMessageStore? messageStore;
    private string errorMessage = "";
    List<ToastMessage> mensajes = new List<ToastMessage>();
    public bool mostrar { get; set; } = true;
    public bool mostrarBtn { get; set; } = false;



    protected override async Task OnInitializedAsync()
    {
        try
        {
            var url = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
            cliente = Utilidades.ObtenerCliente(UserState.Token, HttpClientFactory);

            if (QueryHelpers.ParseQuery(url.Query).TryGetValue("cl", out var clValue))
            {
                cl = clValue;
            }

            if (string.IsNullOrEmpty(cl))
            {
                oValorNomina = new VALORESNOMINA();
                mostrarBtn = true;
            }
            else
            {
                // ListaSexos = await Utilidades.ObtenerObjeto<List<ENUMERACIONES>>(cliente, "/api/ENUMERACIONES/EnumeracionesGrupo/SEXO");
                string idDesencriptado = Utilidades.Desencriptar(cl);
                int id = int.Parse(idDesencriptado);

                var response = await cliente.GetAsync($"/api/VALORESNOMINA/{id}");
                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception($"Error al obtener los datos del Tipo de Trabajo. Código: {response.StatusCode}");
                }
                var resultContent = await response.Content.ReadAsStringAsync();
                oValorNomina = JsonConvert.DeserializeObject<VALORESNOMINA>(resultContent) ?? throw new Exception("Error al deserializar los datos.");
            }
            editContext = new EditContext(oValorNomina);
            messageStore = new ValidationMessageStore(editContext);

            mostrar = false;
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
            Console.WriteLine($"Error durante la inicialización: {ex.Message}");
            mostrar = false;
        }
        finally
        {
            mostrar = false;
        }
    }

    private async Task GuardarPuestoTrabajo()
    {
        cliente = Utilidades.ObtenerCliente(UserState.Token, HttpClientFactory);
        if (!editContext.GetValidationMessages().Any())
        {
            if (oValorNomina.IDVALNOMINA == 0)//Nuevo
            {
                var response = await Utilidades.NuevoObjeto(cliente, "/api/VALORESNOMINA", oValorNomina, mensajes);
                if (response != null)
                {
                    oValorNomina = response;
                }
                else
                {
                    mensajes.Add(new ToastMessage
                    {
                        Type = ToastType.Success,
                        Message = $"Guardado Correctamente.",
                    });
                }
                await InvokeAsync(StateHasChanged);
            }
            else//Guardar
            {
                var response = await Utilidades.ActualizarObjeto(cliente, "/api/VALORESNOMINA/" + oValorNomina.IDVALNOMINA, oValorNomina, mensajes);
            }
        }
        else
        {
            mensajes.Add(new ToastMessage
            {
                Type = ToastType.Danger,
                Message = Utilidades.ListaErroresHtml(editContext.GetValidationMessages()),
            });
        
        }
    }

}
