@model ApplicationListViewModel
@using System.Text.Json
@{
    ViewData["Title"] = "Meine Bewerbungen";
}

<section class="page-header">
    <div>
        <h1>Bewerbungen</h1>
        <p class="lead">Verwalte deine Bewerbungen, halte Fortschritte fest und behalte alle Termine im Blick.</p>
    </div>
    <a class="btn btn-primary" asp-action="Create">Neue Bewerbung</a>
</section>

<section class="stats-grid compact">
    <article class="stat-card">
        <p class="stat-label">Gesamt</p>
        <p class="stat-value">@Model.TotalApplications</p>
        <p class="stat-caption">Gespeicherte Bewerbungen</p>
    </article>
    <article class="stat-card">
        <p class="stat-label">Beworben</p>
        <p class="stat-value">@Model.AppliedCount</p>
        <p class="stat-caption">Offene Bewerbungen</p>
    </article>
    <article class="stat-card">
        <p class="stat-label">Interviews</p>
        <p class="stat-value">@Model.InterviewCount</p>
        <p class="stat-caption">Anstehende Gespräche</p>
    </article>
    <article class="stat-card">
        <p class="stat-label">Angebote</p>
        <p class="stat-value">@Model.OfferCount</p>
        <p class="stat-caption">Positive Rückmeldungen</p>
    </article>
    <article class="stat-card">
        <p class="stat-label">Absagen</p>
        <p class="stat-value">@Model.RejectedCount</p>
        <p class="stat-caption">Abgeschlossene Bewerbungen</p>
    </article>
</section>

@if (Model.TotalApplications > 0)
{
    var statusChartConfig = JsonSerializer.Serialize(new
    {
        type = "doughnut",
        labels = new[] { "Beworben", "Interview", "Angebot", "Abgelehnt" },
        values = new[] { Model.AppliedCount, Model.InterviewCount, Model.OfferCount, Model.RejectedCount },
        colors = new[] { "#4460f7", "#3ac0a0", "#ff9f43", "#ef476f" },
        borderColor = "#ffffff",
        datasetLabel = "Bewerbungen",
        legend = true
    });

        <section class="visual-grid compact">
            <article class="chart-card">
                <div class="chart-card-header">
                    <h2>Statusübersicht</h2>
                    <p>Verteilung deiner Bewerbungen nach aktuellem Status.</p>
                </div>
                <div class="chart-wrapper">
                    <canvas id="applications-status-chart" data-chart='@Html.Raw(statusChartConfig)'></canvas>
                </div>
            </article>
        </section>
}

<section class="filter-card">
    <form method="get" class="filter-grid">
        <div class="form-group">
            <label for="searchTerm">Suche</label>
            <input id="searchTerm" name="searchTerm" value="@Model.SearchTerm" placeholder="Nach Unternehmen, Rolle oder Notizen suchen" />
        </div>
        <div class="form-group">
            <label for="status">Status</label>
            <select id="status" name="status">
                @foreach (var option in Model.StatusOptions)
                {
                    <option value="@option.Value" selected="@(option.Selected ? "selected" : null)">@option.Text</option>
                }
            </select>
        </div>
        <div class="filter-actions">
            <button type="submit" class="btn btn-primary">Filter anwenden</button>
            <a class="btn btn-outline" asp-action="Index">Zurücksetzen</a>
        </div>
    </form>
</section>

@if (!Model.Applications.Any())
{
    <div class="empty-state">
        <p>Noch keine Bewerbungen erfasst.</p>
        <a class="btn btn-primary" asp-action="Create">Jetzt Bewerbung hinzufügen</a>
    </div>
}
else
{
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Unternehmen</th>
                    <th>Rolle</th>
                    <th>Quelle</th>
                    <th>Status</th>
                    <th>Beworben am</th>
                    <th>Letzte Aktualisierung</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
            @foreach (var application in Model.Applications)
            {
                <tr>
                    <td>@application.Company</td>
                    <td>@application.Role</td>
                    <td>@(string.IsNullOrWhiteSpace(application.Source) ? "-" : application.Source)</td>
                    <td>
                        <span class="status-badge status-@application.Status.ToString().ToLowerInvariant()">
                            @application.Status.GetDisplayName()
                        </span>
                    </td>
                    <td>@application.AppliedOn.ToString("dd.MM.yyyy")</td>
                    <td>@application.UpdatedAt.ToLocalTime().ToString("dd.MM.yyyy HH:mm")</td>
                    <td>
                        <div class="table-actions">
                            <a class="link" asp-action="Details" asp-route-id="@application.Id">Details</a>
                            <a class="link" asp-action="Edit" asp-route-id="@application.Id">Bearbeiten</a>
                            <form asp-action="Delete" asp-route-id="@application.Id" method="post" class="inline-form" onsubmit="return confirm('Möchtest du diese Bewerbung wirklich löschen?');">
                                @Html.AntiForgeryToken()
                                <button type="submit" class="btn-link">Löschen</button>
                            </form>
                        </div>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    </div>
}
