<?php $pageTitle = t('dashboard'); ?>
<div class="page-header">
    <h1 class="page-title"><?= t('welcome_back') ?>, <?= Helper::e(Auth::user()['username']) ?></h1>
</div>

<!-- Stats — each card links to the matching list view -->
<div class="stats-grid">
    <a href="/projects" class="stat-card">
        <div class="stat-icon stat-icon-blue">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><path d="M22 19a2 2 0 01-2 2H4a2 2 0 01-2-2V5a2 2 0 012-2h5l2 3h9a2 2 0 012 2z"/></svg>
        </div>
        <div class="stat-body">
            <div class="stat-value"><?= $projectCount ?></div>
            <div class="stat-label"><?= t('active_projects') ?></div>
        </div>
    </a>
    <a href="/my-tasks?scope=mine&amp;status=open" class="stat-card">
        <div class="stat-icon stat-icon-indigo">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2h11"/></svg>
        </div>
        <div class="stat-body">
            <div class="stat-value"><?= $openTasks ?></div>
            <div class="stat-label"><?= t('my_open_tasks') ?></div>
        </div>
    </a>
    <a href="/my-tasks?scope=mine&amp;status=open&amp;due=today" class="stat-card">
        <div class="stat-icon stat-icon-amber">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></svg>
        </div>
        <div class="stat-body">
            <div class="stat-value"><?= $dueToday ?></div>
            <div class="stat-label"><?= t('due_today') ?></div>
        </div>
    </a>
    <a href="/my-tasks?scope=mine&amp;status=open&amp;due=overdue" class="stat-card">
        <div class="stat-icon stat-icon-red">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><path d="M12 9v4M12 17h.01"/></svg>
        </div>
        <div class="stat-body">
            <div class="stat-value"><?= $overdue ?></div>
            <div class="stat-label"><?= t('overdue') ?></div>
        </div>
    </a>
</div>
<style>
a.stat-card { text-decoration: none; color: inherit; cursor: pointer; transition: transform 120ms, box-shadow 120ms; }
a.stat-card:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,.08); color: inherit; }
</style>

<div class="dashboard-grid">
    <!-- My tasks -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title"><?= t('my_open_tasks') ?></h2>
        </div>
        <div class="card-body p-0">
            <?php if (empty($myTasks)): ?>
            <div class="empty-state-small"><?= t('no_tasks') ?></div>
            <?php else: ?>
            <div class="task-list scrollable">
                <?php foreach ($myTasks as $task): ?>
                <a href="/task/<?= $task['id'] ?>" class="task-list-item">
                    <span class="task-status-dot" style="background:<?= Helper::e($task['status_color']) ?>"></span>
                    <div class="task-list-body">
                        <div class="task-list-title"><?= Helper::e($task['title']) ?></div>
                        <div class="task-list-meta">
                            <span class="project-badge" style="--project-color:<?= Helper::e($task['project_color']) ?>">
                                <?= Helper::e($task['project_name']) ?>
                            </span>
                            <?php if ($task['due_date']): ?>
                            <span class="task-due <?= $task['due_date'] < date('Y-m-d') ? 'overdue' : ($task['due_date'] === date('Y-m-d') ? 'due-today' : '') ?>">
                                <?= Helper::formatDate($task['due_date']) ?>
                            </span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?= Helper::priorityBadge($task['priority']) ?>
                </a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Projects -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title"><?= t('projects') ?></h2>
            <a href="/project/create" class="btn btn-primary btn-sm">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M12 5v14M5 12h14"/></svg>
                <?= t('new_project') ?>
            </a>
        </div>
        <div class="card-body p-0">
            <?php if (empty($projects)): ?>
            <div class="empty-state-small"><?= t('no_projects') ?></div>
            <?php else: ?>
            <div class="task-list scrollable">
                <?php foreach ($projects as $p): ?>
                <a href="/project/<?= $p['id'] ?>" class="task-list-item">
                    <span class="task-status-dot" style="background:<?= Helper::e($p['color']) ?>"></span>
                    <div class="task-list-body">
                        <div class="task-list-title"><?= Helper::e($p['name']) ?></div>
                        <div class="task-list-meta">
                            <span><?= $p['task_count'] ?> <?= t('tasks') ?></span>
                            <span>·</span>
                            <span><?= $p['member_count'] ?> <?= t('members') ?></span>
                        </div>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Recent Activity -->
<?php if (!empty($activity)): ?>
<div class="card" style="margin-top:24px">
    <div class="card-header">
        <h2 class="card-title"><?= t('recent_activity') ?></h2>
    </div>
    <div class="card-body p-0">
        <div class="activity-list">
            <?php foreach ($activity as $act): ?>
            <div class="activity-item">
                <div class="activity-avatar">
                    <?= Helper::avatar(['username' => $act['username'], 'avatar' => $act['avatar'], 'id' => $act['user_id'] ?? 0], 28) ?>
                </div>
                <div class="activity-body">
                    <span class="activity-user"><?= Helper::e($act['username']) ?></span>
                    <span class="activity-action"><?= Helper::e($act['action']) ?></span>
                    <?php if ($act['task_title']): ?>
                    <span class="activity-task"><?= Helper::e($act['task_title']) ?></span>
                    <?php endif; ?>
                    <span class="activity-time"><?= Helper::timeAgo($act['created_at']) ?></span>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>
