@using Services;
@inject IToastService ToastService
@using Blazored.Toast.Configuration;
@using Blazored.Toast.Services;
@inject NavigationManager Navigation
@inject ReAct_PME.WebUI.ServicesUI.ApiService ApiService

<div class="email-card">
    <div class="email-header">

        <!-- Ligne 1 : -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="fw-bold">@Conversation.Subject</div>
            <span class="badge bg-primary">@(Conversation.FollowUpDraft == null ? "" : "Relance prête")</span>
        </div>

        <!-- Ligne 2 : -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="text-muted small">@Conversation.DateMessageSTR</div>
            <span class="badge bg-danger"> @(Conversation.IsOverdue ? "Relance en retard" : "")</span>
        </div>

        <!-- Ligne 3 : -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="text-muted small">@Conversation.DateLastMessageSTR</div>
            <div class="text-muted small"></div>
        </div>

        <!-- Ligne 4 : -->
        <div class="d-flex justify-content-between align-items-center">
            <div class="d-flex align-items-center gap-2">
                <div class="fw-bold">
                    Messages : @Conversation.Messages.Count
                </div>
            </div>

            <!-- Sujet clos ? -->
            <div class="rounded">
                <span class="badge bg-light" style="color:@GetAnalyseColor();">
                    @(Conversation.SubjectClosed ? "Sujet clos":"Points en suspens")
                </span>
            </div>
        </div>
    </div>

    <!-- Ligne 5 :  -->
    <div class="email-body mt-3 d-flex justify-content-between align-items-center">
        <div class="fw-bold"></div>
        <span class="text-muted small"></span>
    </div>

    <!-- Footer -->
    <div class="email-footer mt-3 d-flex justify-content-between">
        <button class="btn btn-outline-light" @onclick="() => EditItem(Conversation.Id!)" title="Voir le détail">Voir le détail</button>
        <button class="btn btn-outline-light" @onclick="() => ShowRelanceItem(Conversation.Id!)" title="Voir la relance" disabled="@IsShowRelanceDisabled">Voir la relance</button>
        <button class="btn btn-outline-light" @onclick="() => ArchiveItem(Conversation.Id!)" title="Archiver">Archiver</button>
    </div>
</div>



@code {
    @inject IJSRuntime JS
    [Parameter] public ThreadItem Conversation { get; set; } = new();

    private bool IsShowRelanceDisabled;
    private async Task AdjustHeight(string id)
    {
        await JS.InvokeVoidAsync("resizeTextarea", id);
    }

    protected override void OnInitialized()
    {
        IsShowRelanceDisabled = !(Conversation.FollowUpDraft != null);
    }


    private void EditItem(string id)
    {
        //ToastService.ShowWarning($"EditItem : {id}.", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });

        Navigation.NavigateTo($"/mailsend_edit/{id}");
    }

    private void ShowRelanceItem(string id)
    {
        //ToastService.ShowWarning($"ShowRelanceItem : {id}.", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });
        Navigation.NavigateTo($"/mailsendrelance_edit/{id}");
    }

    private async Task ArchiveItem(string id)
    {
        var mini = new Services.ThreadItemMini();
        mini.Id = Conversation.Id;
        mini.FollowUpDraft = Conversation.FollowUpDraft;
        var b = await ApiService!.SauvegardeItem<Services.ThreadItemMini>($"api/ListeMails/sendarchiverestore", mini!);
        if(b)
        {
            Navigation.NavigateTo($"/listemailssend_card", forceLoad: true);
            ToastService.ShowWarning($"ArchiveItem : {id}.", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });
        }
        //Navigation.NavigateTo($"/logs_edit/{id}");
    }

    private string GetAnalyseColor()
    {
        return Conversation.SubjectClosed switch
        {
            true => "green",
            false => "red"
        };
    }
}
