<?php ob_start(); ?>

<!-- ── Welcome Banner + Quick Actions ──────────────────────────────────── -->
<div class="dash-welcome">
    <div class="dash-welcome-text">
        <h2><?= __('admin.dashboard.welcome', ['name' => e($auth->userName() ?? 'Admin')]) ?></h2>
        <p class="dash-welcome-date"><?= e($todayFormatted) ?></p>
    </div>
    <div class="dash-welcome-actions">
        <a href="/admin/pages/create" class="btn btn-primary btn-sm"><?= __('admin.dashboard.new_page') ?></a>
        <a href="/admin/blog/create" class="btn btn-primary btn-sm"><?= __('admin.dashboard.new_post') ?></a>
        <a href="/admin/events/create" class="btn btn-secondary btn-sm"><?= __('admin.dashboard.new_event') ?></a>
        <a href="/admin/media" class="btn btn-secondary btn-sm"><?= __('admin.dashboard.upload_media') ?></a>
    </div>
</div>

<!-- ── Primary Stats ───────────────────────────────────────────────────── -->
<div class="dash-stats-primary">
    <a href="/admin/pages" class="dash-stat-card dash-stat--pages">
        <div class="dash-stat-accent"></div>
        <div class="dash-stat-body">
            <div class="dash-stat-number"><?= (int) $pageCount ?></div>
            <div class="dash-stat-label"><?= __('admin.dashboard.pages') ?></div>
        </div>
    </a>
    <a href="/admin/blog" class="dash-stat-card dash-stat--blog">
        <div class="dash-stat-accent"></div>
        <div class="dash-stat-body">
            <div class="dash-stat-number"><?= (int) $postCount ?></div>
            <div class="dash-stat-label"><?= __('admin.dashboard.blog_posts') ?></div>
            <div class="dash-stat-sub">
                <span class="badge badge-success"><?= (int) $postPublished ?> <?= __('admin.dashboard.published') ?></span>
                <span class="badge badge-draft"><?= (int) $postDraft ?> <?= __('admin.dashboard.draft') ?></span>
            </div>
        </div>
    </a>
    <a href="/admin/gallery" class="dash-stat-card dash-stat--gallery">
        <div class="dash-stat-accent"></div>
        <div class="dash-stat-body">
            <div class="dash-stat-number"><?= (int) $galleryCount ?></div>
            <div class="dash-stat-label"><?= __('admin.dashboard.gallery_albums') ?></div>
        </div>
    </a>
    <a href="/admin/events" class="dash-stat-card dash-stat--events">
        <div class="dash-stat-accent"></div>
        <div class="dash-stat-body">
            <div class="dash-stat-number"><?= (int) $eventCount ?></div>
            <div class="dash-stat-label"><?= __('admin.dashboard.events') ?></div>
            <?php if ($upcomingEventCount > 0): ?>
            <div class="dash-stat-sub">
                <span class="badge badge-primary"><?= (int) $upcomingEventCount ?> <?= __('admin.dashboard.upcoming') ?></span>
            </div>
            <?php endif; ?>
        </div>
    </a>
</div>

<!-- ── Secondary Stats ─────────────────────────────────────────────────── -->
<div class="dash-stats-secondary">
    <a href="/admin/downloads" class="dash-stat-mini">
        <span class="dash-stat-mini-num"><?= (int) $downloadCount ?></span>
        <span class="dash-stat-mini-label"><?= __('admin.dashboard.downloads') ?></span>
    </a>
    <a href="/admin/media" class="dash-stat-mini">
        <span class="dash-stat-mini-num"><?= (int) $mediaCount ?></span>
        <span class="dash-stat-mini-label"><?= __('admin.dashboard.media_files') ?></span>
    </a>
    <a href="/admin/users" class="dash-stat-mini">
        <span class="dash-stat-mini-num"><?= (int) $userCount ?></span>
        <span class="dash-stat-mini-label"><?= __('admin.dashboard.users') ?></span>
    </a>
</div>

<!-- ── Content Grid (2 columns on desktop) ─────────────────────────────── -->
<div class="dash-content-grid">

    <!-- Left column: Recent Posts + Recent Pages -->
    <div class="dash-content-col">
        <div class="dash-panel">
            <div class="dash-panel-header">
                <h3><?= __('admin.dashboard.recent_posts') ?></h3>
                <a href="/admin/blog" class="dash-panel-link"><?= __('admin.dashboard.view_all_posts') ?></a>
            </div>
            <?php if (empty($recentPosts)): ?>
                <p class="dash-panel-empty"><?= __('admin.dashboard.recent_posts_empty') ?></p>
            <?php else: ?>
                <ul class="dash-list">
                    <?php foreach ($recentPosts as $post): ?>
                    <li class="dash-list-item">
                        <a href="/admin/blog/<?= (int) $post['id'] ?>/edit" class="dash-list-title"><?= e($post['title']) ?></a>
                        <span class="dash-list-meta">
                            <span class="badge <?= $post['status'] === 'published' ? 'badge-success' : 'badge-draft' ?>"><?= e($post['status']) ?></span>
                            <span class="dash-list-date"><?= date('d.m.Y', strtotime($post['created_at'])) ?></span>
                        </span>
                    </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>

        <div class="dash-panel">
            <div class="dash-panel-header">
                <h3><?= __('admin.dashboard.recent_pages') ?></h3>
                <a href="/admin/pages" class="dash-panel-link"><?= __('admin.dashboard.view_all_pages') ?></a>
            </div>
            <?php if (empty($recentPages)): ?>
                <p class="dash-panel-empty"><?= __('admin.dashboard.recent_pages_empty') ?></p>
            <?php else: ?>
                <ul class="dash-list">
                    <?php foreach ($recentPages as $page): ?>
                    <li class="dash-list-item">
                        <a href="/admin/pages/<?= (int) $page['id'] ?>/edit" class="dash-list-title"><?= e($page['title']) ?></a>
                        <span class="dash-list-meta">
                            <span class="badge <?= $page['status'] === 'published' ? 'badge-success' : 'badge-draft' ?>"><?= e($page['status']) ?></span>
                            <span class="dash-list-date"><?= date('d.m.Y', strtotime($page['updated_at'])) ?></span>
                        </span>
                    </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>
    </div>

    <!-- Right column: Upcoming Events + System Info -->
    <div class="dash-content-col">
        <div class="dash-panel">
            <div class="dash-panel-header">
                <h3><?= __('admin.dashboard.upcoming_events') ?></h3>
                <a href="/admin/events" class="dash-panel-link"><?= __('admin.dashboard.view_all_events') ?></a>
            </div>
            <?php if (empty($upcomingEvents)): ?>
                <p class="dash-panel-empty"><?= __('admin.dashboard.upcoming_events_empty') ?></p>
            <?php else: ?>
                <ul class="dash-events-list">
                    <?php foreach ($upcomingEvents as $ev): ?>
                    <li class="dash-event-item">
                        <div class="dash-event-date-block">
                            <span class="dash-event-day"><?= date('d', strtotime($ev['start_date'])) ?></span>
                            <span class="dash-event-month"><?= date('M', strtotime($ev['start_date'])) ?></span>
                        </div>
                        <div class="dash-event-info">
                            <a href="/admin/events/<?= (int) $ev['id'] ?>/edit" class="dash-event-title"><?= e($ev['title']) ?></a>
                            <?php if (!empty($ev['start_time'])): ?>
                                <span class="dash-event-time"><?= date('H:i', strtotime($ev['start_time'])) ?></span>
                            <?php endif; ?>
                            <?php if ($ev['event_status'] !== 'scheduled'): ?>
                                <span class="badge badge-warning"><?= e($ev['event_status']) ?></span>
                            <?php endif; ?>
                        </div>
                    </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>

        <div class="dash-panel dash-panel--system">
            <div class="dash-panel-header">
                <h3><?= __('admin.dashboard.system_info') ?></h3>
                <a href="/admin/system" class="dash-panel-link"><?= __('admin.dashboard.view_system_status') ?></a>
            </div>
            <dl class="dash-system-grid">
                <div class="dash-system-item">
                    <dt><?= __('admin.dashboard.cms_version') ?></dt>
                    <dd><strong><?= e($systemInfo['cms_version']) ?></strong></dd>
                </div>
                <div class="dash-system-item">
                    <dt><?= __('admin.dashboard.php_version') ?></dt>
                    <dd><?= e($systemInfo['php_version']) ?></dd>
                </div>
                <div class="dash-system-item">
                    <dt><?= __('admin.dashboard.active_theme') ?></dt>
                    <dd><a href="/admin/themes" class="dash-system-link"><?= e(ucfirst($systemInfo['active_theme'])) ?></a></dd>
                </div>
                <div class="dash-system-item">
                    <dt><?= __('admin.dashboard.maintenance_mode') ?></dt>
                    <dd>
                        <?php if ($systemInfo['maintenance']): ?>
                            <a href="/admin/maintenance" class="badge badge-warning"><?= __('admin.dashboard.maintenance_active') ?></a>
                        <?php else: ?>
                            <span class="badge badge-success"><?= __('admin.dashboard.maintenance_inactive') ?></span>
                        <?php endif; ?>
                    </dd>
                </div>
            </dl>
        </div>
    </div>
</div>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
