@page "/dashboardpanel"
@inject IModalService modalService
@inject TicketsService ticketService

<div class="card" style="zoom:80%">
    <div class="card-header">
        <h3 class="card-title">Panel de control</h3>
    </div>
    <div class="card-body">
        <Dashboard DashboardClass="col-lg-3" Items="@Items"></Dashboard>
    </div>
</div>
<br />

<div class="form-group">
    <button type="button" class="btn btn-primary" @onclick="NuevoTicket">
        <span class=" fa fa-plus-square"></span> Nuevo Ticket</button>
</div>
<br/>
<div class="card" style="zoom:80%">
    <div class="card-header">
        <h2 class="card-title">Datos</h2>
    </div>
    <div class="card-body">
        <PhTable Columns="columnas" Data="datos" SelectionField="Estado" RenderSelect="true" RenderButtons="true" Buttons="botones" RowsPerPage="10" SorteableCols="columnas"></PhTable> 
    </div>
</div>
@code {
    private List<Dashboard.DashboardItem> Items = new List<Dashboard.DashboardItem>()
    {
        new Dashboard.DashboardItem
        {
            Title="Tickets",
            Description="Tickets totales",
            Value="0",
            Icon="fa fa-box-archive fa-fw fa-2x",
            MainColor="dark-gray",
            OnClickParam="Todos"
        },
        new Dashboard.DashboardItem
        {
            Title="Pendientes",
            Description="Tickets pendientes",
            Value="0",
            Icon="fa fa-clock fa-fw fa-2x",
            MainColor="red",
            OnClickParam="Pendiente"
        },
        new Dashboard.DashboardItem
        {
            Title="En proceso",
            Description="Tickets en proceso",
            Value="0",
            Icon="fa fa-wrench fa-fw fa-2x",
            MainColor="purple",
            OnClickParam="En proceso"
            //Url="/DashboardDetail/En%20Proceso"
        },
        new Dashboard.DashboardItem
        {
            Title="Completados",
            Description="Tickets completados",
            Value="0",
            Icon="fa fa-check-square fa-fw fa-2x",
            MainColor="green",
            OnClickParam="Completado"
        }
    };

    private Task HandleModalDetalle(string? grupo)
    {
        var parameters = new ModalParameters()
            .Add(nameof(DashboardDetail.Group), grupo);
        var options = new ModalOptions
            {
                Size = ModalSize.ExtraLarge,
                HideCloseButton = true,
                HideHeader = true
            };
        modalService.Show<DashboardDetail>(string.Empty, parameters, options);
        return Task.CompletedTask;
    }


    private IEnumerable<ETickets_GetSummary>? summaryResult;

    List<string> columnas = new List<string> { "Estado", "Cantidad" };
    List<Dictionary<string, object>> datos = new List<Dictionary<string, object>>();
    protected async override Task OnInitializedAsync()
    {
        summaryResult = await ticketService.GetSummary();
        if (summaryResult is not null)
        {
            // Asignar el valor calculado al elemento "Total"
            Items[0].Value = summaryResult.Sum(r => r.Cantidad).ToString() ?? "0";
            Items[0].OnClickAction = grupo => HandleModalDetalle(Items[0].OnClickParam);
            Items[1].OnClickAction = grupo => HandleModalDetalle(Items[1].OnClickParam);
            Items[2].OnClickAction = grupo => HandleModalDetalle(Items[2].OnClickParam);
            Items[3].OnClickAction = grupo => HandleModalDetalle(Items[3].OnClickParam);

            Items[1].Value = summaryResult.FirstOrDefault(r => r.Estado == "Pendiente")?.Cantidad.ToString() ?? "0";
            Items[2].Value = summaryResult.FirstOrDefault(r => r.Estado == "En Proceso")?.Cantidad.ToString() ?? "0";
            Items[3].Value = summaryResult.FirstOrDefault(r => r.Estado == "Completado")?.Cantidad.ToString() ?? "0";
            datos = ObtenerDatos(summaryResult);
        }
    }
    List<Dictionary<string,object>> ObtenerDatos(IEnumerable<ETickets_GetSummary> summary)
    {
        var datos = new List<Dictionary<string, object>>();
        foreach (var item in summary)
        {
            var fila = new Dictionary<string, object>();
            fila.Add(columnas[0], item.Estado);
            fila.Add(columnas[1], item.Cantidad.ToString() ?? "0" );
            datos.Add(fila);
        }
        return datos;
    }
    List<PhTable.ButtonOptions> botones = new List<PhTable.ButtonOptions>
    {
        new PhTable.ButtonOptions{ Caption="Editar", ElementClass="btn btn-primary"}
    };
    
    private void NuevoTicket()
    {
        var options = new ModalOptions()
            {
                Size = ModalSize.ExtraLarge,
                HideCloseButton = true
            };
        modalService.Show<Tickets.TicketForm>("Nuevo Ticket", options);
    }
}