<div class="page-heading">
    <div>
        <span>Днес</span>
        <h1>Здравей, <?= e(explode(' ', $user['name'])[0]) ?>.</h1>
        <p>Следи сигналите, сроковете, институциите и готовите документи от едно място.</p>
    </div>
    <div class="score-card">
        <small>Citizen score</small>
        <strong><?= e($user['score']) ?></strong>
        <span><?= e($user['level']) ?></span>
    </div>
</div>

<section class="quick-grid">
    <a class="quick-card" href="/reports/new"><span class="tone-green"><i class="bi bi-megaphone"></i></span><strong>Подай сигнал</strong><small>Бързо и лесно</small></a>
    <a class="quick-card" href="/assistant"><span class="tone-blue"><i class="bi bi-robot"></i></span><strong>AI Асистент</strong><small>Опиши проблема</small></a>
    <a class="quick-card" href="/zdoi"><span class="tone-violet"><i class="bi bi-file-earmark-text"></i></span><strong>ЗДОИ заявление</strong><small>Поискай информация</small></a>
    <a class="quick-card" href="/petitions"><span class="tone-orange"><i class="bi bi-people"></i></span><strong>Петиция</strong><small>Събери подкрепа</small></a>
    <a class="quick-card" href="/generators"><span class="tone-blue-soft"><i class="bi bi-file-earmark-plus"></i></span><strong>Генератори</strong><small>Документи и образци</small></a>
</section>

<section class="dashboard-grid">
    <article class="panel span-4">
        <div class="panel-head">
            <h2>Моите сигнали</h2>
            <a href="/profile">Всички</a>
        </div>
        <div class="signal-list">
            <?php foreach (array_slice($reports, 0, 4) as $report): ?>
                <div class="signal-row">
                    <span class="status-dot <?= e(status_class($report['status'])) ?>"></span>
                    <div>
                        <strong>#<?= e($report['id']) ?> <?= e($report['title']) ?></strong>
                        <small><?= e($report['address']) ?></small>
                    </div>
                    <span class="status-pill <?= e(status_class($report['status'])) ?>"><?= e(status_label($report['status'])) ?></span>
                </div>
            <?php endforeach; ?>
        </div>
    </article>

    <article class="panel span-6">
        <div class="panel-head">
            <h2>Карта на проблемите</h2>
            <a href="/map">Виж всички на картата</a>
        </div>
        <div id="dashboardMap" class="map-surface" data-reports='<?= e(json_encode($reports, JSON_UNESCAPED_UNICODE)) ?>'></div>
        <div class="map-legend">
            <span><b class="danger"></b> Нерешени</span>
            <span><b class="warning"></b> В процес</span>
            <span><b class="success"></b> Решени</span>
        </div>
    </article>

    <article class="panel span-2">
        <div class="panel-head">
            <h2>Срокове</h2>
        </div>
        <div class="deadline-list">
            <?php foreach ($deadlines as $deadline): ?>
                <div>
                    <span><?= e($deadline['label']) ?></span>
                    <strong class="text-<?= e($deadline['tone']) ?>"><?= e($deadline['status']) ?></strong>
                </div>
            <?php endforeach; ?>
        </div>
        <a class="link-arrow" href="/tracking">Всички срокове <i class="bi bi-arrow-right"></i></a>
    </article>
</section>

<section class="stat-grid">
    <?php foreach ($overview as $stat): ?>
        <article class="metric-card">
            <span class="metric-icon <?= e($stat['tone']) ?>"><i class="bi <?= e($stat['icon']) ?>"></i></span>
            <div>
                <small><?= e($stat['label']) ?></small>
                <strong><?= e($stat['value']) ?></strong>
                <em><?= e($stat['trend']) ?></em>
            </div>
        </article>
    <?php endforeach; ?>
</section>

<section class="dashboard-grid">
    <article class="panel span-3">
        <div class="panel-head">
            <h2>AI Асистент</h2>
        </div>
        <div class="mini-chat">
            <p class="bubble user">Има огромна дупка пред блока ми.</p>
            <p class="bubble bot">Разбрах. Ще създам сигнал за дупка на пътя. Моля, потвърдете адреса.</p>
            <form class="chat-inline" action="/assistant">
                <input type="text" value="ул. Иван Вазов 15, Пловдив" aria-label="Адрес">
                <button type="submit"><i class="bi bi-send"></i></button>
            </form>
        </div>
    </article>

    <article class="panel span-3">
        <div class="panel-head">
            <h2>Статистика по общини</h2>
            <a href="/statistics">Всички</a>
        </div>
        <canvas id="municipalityChart" height="180" data-chart="bar" data-labels='<?= e(json_encode(array_column($municipalities, 'name'), JSON_UNESCAPED_UNICODE)) ?>' data-values='<?= e(json_encode(array_column($municipalities, 'count'))) ?>'></canvas>
    </article>

    <article class="panel span-3">
        <div class="panel-head">
            <h2>Топ институции</h2>
            <a href="/transparency">Всички</a>
        </div>
        <div class="ranking-list">
            <?php foreach (array_slice($transparency, 0, 5) as $index => $row): ?>
                <div>
                    <span><?= $index + 1 ?></span>
                    <strong><?= e($row['institution']) ?></strong>
                    <small><?= e($row['avg_response']) ?></small>
                </div>
            <?php endforeach; ?>
        </div>
    </article>

    <article class="panel span-3">
        <div class="panel-head">
            <h2>QR кодове</h2>
            <a href="/qr-library">Всички</a>
        </div>
        <div class="qr-mini-grid">
            <?php foreach (array_slice($qrLinks, 0, 4) as $link): ?>
                <div class="qr-faux">
                    <i class="bi bi-qr-code"></i>
                    <span><?= e($link['label']) ?></span>
                </div>
            <?php endforeach; ?>
        </div>
    </article>
</section>
