@page "/tasks"
@using FilterCair.Shared.Models


<div class="container py-4 fade-in">
    <h4 class="mb-4 text-primary text-center">
        <i class="bi bi-list-check me-2"></i> Meine Aufgaben
    </h4>

    <div class="row g-3">
        @if (tasks.Count == 0)
        {
            <div class="text-center text-muted py-5">
                <i class="bi bi-emoji-neutral display-4 d-block mb-3"></i>
                Keine Aufgaben vorhanden.
            </div>
        }
        else
        {
            @foreach (var t in tasks)
            {
                <div class="col-12 col-md-6 col-lg-4">
                    <div class="card task-card shadow-sm border-0 rounded-4">
                        <div class="card-body">
                            <h5 class="fw-semibold">@t.Title</h5>
                            <p class="text-muted small mb-2">@t.Beschreibung</p>

                            <span class="badge bg-@(GetStatusColor(t.Status)) me-2">@t.Status</span>
                            <span class="badge bg-secondary">@t.Prio</span>

                            @if (t.FaelligBis.HasValue)
                            {
                                <div class="text-muted small mt-2">
                                    <i class="bi bi-calendar-event me-1"></i>
                                    Fällig: @t.FaelligBis.Value.ToShortDateString()
                                </div>
                            }

                            <div class="d-flex justify-content-end mt-3">
                                <button class="btn btn-sm btn-outline-primary" @onclick="() => ToggleStatus(t)">
                                    <i class="bi bi-check2-circle me-1"></i> Status ändern
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            }
        }
    </div>
</div>

@code {
    private List<TaskModel> tasks = new();

    protected override void OnInitialized()
    {
        tasks = new()
        {
            new() { Id = 1, Title = "Filter prüfen Halle A", Beschreibung = "Sichtprüfung durchführen", ZugewiesenAn = "Marc Wieland", Status = "Offen", Prio = "Normal", FaelligBis = DateTime.Today.AddDays(1) },
            new() { Id = 2, Title = "Wartung Filter B2", Beschreibung = "Druck prüfen & Dokumentation", ZugewiesenAn = "Marc Wieland", Status = "In Arbeit", Prio = "Hoch" },
            new() { Id = 3, Title = "Filter austauschen", Beschreibung = "Alten Filter entfernen & neuen einsetzen", ZugewiesenAn = "Marc Wieland", Status = "Erledigt", Prio = "Normal", FaelligBis = DateTime.Today.AddDays(-2) }
        };
    }

    private void ToggleStatus(TaskModel t)
    {
        t.Status = t.Status switch
        {
            "Offen" => "In Arbeit",
            "In Arbeit" => "Erledigt",
            _ => "Offen"
        };
    }

    private string GetStatusColor(string status) => status switch
    {
        "Erledigt" => "success",
        "In Arbeit" => "warning",
        _ => "secondary"
    };
}