@page "/listemails"

@inject ReAct_PME.WebUI.ServicesUI.ApiService ApiService
@using Services;
@using ReAct_PME.WebUI.ServicesUI;
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager Navigation
@inject HttpClient Http
@using Microsoft.AspNetCore.Components.Authorization;

<PageTitle>Mails non lus</PageTitle>

<h3>Liste des Mails non lus : @(items == null ? 0 : items.Count())</h3>

@if (items != null)
{
    <div class="table-responsive">
        <table class="table table_standard table-striped table-bordered">
            <thead class="table-dark">
                <tr>
                    <th class="hover-cursor th_td_standard" @onclick="() => SortByColumn(nameof(EmailMessage.From))">DE  @GetSortIcon(nameof(EmailMessage.From))</th>
                    <th class="hover-cursor th_td_standard" @onclick="() => SortByColumn(nameof(EmailMessage.Date))">DATE @GetSortIcon(nameof(EmailMessage.Date))</th>
                    <th class="hover-cursor th_td_standard" @onclick="() => SortByColumn(nameof(EmailMessage.Subject))">OBJET @GetSortIcon(nameof(EmailMessage.Subject))</th>
                    <th class="hover-cursor th_td_standard" @onclick="() => SortByColumn(nameof(EmailMessage.PJ_NBR))">PJ @GetSortIcon(nameof(EmailMessage.PJ_NBR))</th>
                    <th class="hover-cursor th_td_standard" @onclick="() => SortByColumn(nameof(EmailMessage.ImportanceSTR))">Importance @GetSortIcon(nameof(EmailMessage.Subject))</th>
                    <th class="hover-cursor th_td_standard" @onclick="() => SortByColumn(nameof(EmailMessage.UrgenceSTR))">Urgence @GetSortIcon(nameof(EmailMessage.Subject))</th>

                    <th class="th_td_standard">Actions</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in itemsFilteredSorted)
                {
                    <tr>

                        <td class=" th_td_standard">@item.From</td>
                        <td class=" th_td_standard">@item.Date.ToString("dd/MM/yyyy")</td>
                        <td class=" th_td_standard">@item.Subject</td>
                        <td class=" th_td_standard">@item.PJ_NBR</td>
                        <td class=" th_td_standard">@item.ImportanceSTR</td>
                        <td class=" th_td_standard">@item.UrgenceSTR</td>

                        <td class="text-center">
                            <div class="d-flex">
                                <button class="btn btn-warning btn-sm mx-1" @onclick="() => EditItem(item.UidString!)" data-bs-toggle="tooltip" data-bs-placement="top" title="Editer cet élément">
                                    <i class="bi bi-pencil"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}
else
{
    <div class="alert alert-info text-center p-2" role="alert">
        <i class="bi bi-info-circle"></i> En cours de chargement ou pas de données disponibles.
    </div>
}



@code {

    private IEnumerable<EmailMessage> itemsFilteredSorted =>
    items == null ? Enumerable.Empty<EmailMessage>() :
    items
    .OrderByDynamic(CurrentSortColumn!, IsAscending);


    private string? CurrentSortColumn = null;
    private bool IsAscending = true;
    private IEnumerable<EmailMessage>? items;

    protected override async Task OnInitializedAsync()
    {
        items = await ApiService.GetAllAsync<IEnumerable<Services.EmailMessage>>("api/ListeMails");

	}

    private void EditItem(string id)
    {
        Navigation.NavigateTo($"/mail_edit/{id}");
    }

    private void SortByColumn(string columnName)
    {
        if (CurrentSortColumn == columnName)
        {
            IsAscending = !IsAscending; // Inverse le tri si déjà trié par cette colonne
        }
        else
        {
            CurrentSortColumn = columnName;
            IsAscending = true; // Tri par défaut en ordre ascendant
        }
    }

    private string GetSortIcon(string columnName)
    {
        bool b = false;
        if (CurrentSortColumn != null)
        {
            var tab = CurrentSortColumn.Split('.');
            b = (tab.Length == 2 && columnName.Contains(tab[0]) && columnName.Contains(tab[1]));
        }

        if (CurrentSortColumn == columnName)
        {
            return IsAscending ? " ▲" : " ▼"; // Flèche selon l'ordre
        }
        return string.Empty; // Pas d'icône si la colonne n'est pas triée
    }

}
