<?php /** @var array $tasks @var array $grouped @var string $subjectKind @var string $subjectName @var string $subjectMeta @var ?array $subjectAvatar */ ?>
<?php $pageTitle = $subjectName . ' – ' . t('managed_tasks_title'); ?>

<div class="page-header">
    <div class="page-header-left">
        <a href="javascript:history.back()" class="btn btn-ghost btn-sm">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
            <?= t('back') ?>
        </a>
        <div style="display:flex;align-items:center;gap:10px">
            <?php if ($subjectKind === 'user' && $subjectAvatar): ?>
                <?= Helper::avatar($subjectAvatar, 32) ?>
            <?php else: ?>
                <span class="avatar-initials avatar-ext" style="width:32px;height:32px;font-size:13px;display:inline-flex;align-items:center;justify-content:center"><?= Helper::e(mb_strtoupper(mb_substr($subjectName, 0, 1))) ?></span>
            <?php endif; ?>
            <div style="display:flex;flex-direction:column;gap:2px">
                <h1 class="page-title" style="margin:0;font-size:1.15rem">
                    <?= Helper::e($subjectName) ?>
                    <?php if ($subjectKind === 'external'): ?>
                    <span class="badge badge-neutral" style="font-weight:500;margin-left:6px"><?= t('external') ?></span>
                    <?php endif; ?>
                </h1>
                <?php if ($subjectMeta !== ''): ?>
                <span class="text-muted" style="font-size:.78rem"><?= Helper::e($subjectMeta) ?></span>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<p class="text-muted" style="margin:0 0 20px;font-size:.88rem"><?= t('managed_tasks_subtitle', ['name' => Helper::e($subjectName)]) ?></p>

<?php if (empty($tasks)): ?>
<div class="empty-state">
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" width="64" height="64" opacity=".3">
        <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>
    <h3><?= t('managed_tasks_empty') ?></h3>
</div>
<?php else: ?>

<?php foreach ($grouped as $group): ?>
<?php $proj = $group['project']; $groupTasks = $group['tasks']; ?>
<div class="my-tasks-group">
    <div class="my-tasks-group-header">
        <a href="/project/<?= $proj['id'] ?>" class="my-tasks-project-link">
            <span class="project-dot" style="background:<?= Helper::e($proj['color']) ?>"></span>
            <?= Helper::e($proj['name']) ?>
        </a>
        <span class="task-count-badge"><?= count($groupTasks) ?></span>
    </div>
    <div class="table-card" style="margin-top:0;border-radius:0 0 var(--border-radius) var(--border-radius)">
        <table class="data-table">
            <thead>
                <tr>
                    <th><?= t('task_title') ?></th>
                    <th><?= t('task_status') ?></th>
                    <th><?= t('priority') ?></th>
                    <th><?= t('due_date') ?></th>
                    <th><?= t('assigned_to') ?></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($groupTasks as $task): ?>
            <tr class="table-row-link" data-href="/task/<?= $task['id'] ?>">
                <td>
                    <a href="/task/<?= $task['id'] ?>" class="task-link"><?= Helper::e($task['title']) ?></a>
                </td>
                <td>
                    <?php if ($task['status_name']): ?>
                    <span class="status-badge" style="--status-color:<?= Helper::e($task['status_color']) ?>">
                        <?= Helper::e($task['status_name']) ?>
                    </span>
                    <?php endif; ?>
                </td>
                <td><?= Helper::priorityBadge($task['priority']) ?></td>
                <td>
                    <?php if ($task['due_date']): ?>
                    <?php
                    $dueClass = '';
                    if ($task['due_date'] < date('Y-m-d'))       $dueClass = 'overdue';
                    elseif ($task['due_date'] === date('Y-m-d')) $dueClass = 'due-today';
                    ?>
                    <span class="task-due <?= $dueClass ?>"><?= Helper::formatDate($task['due_date']) ?></span>
                    <?php else: ?>—<?php endif; ?>
                </td>
                <td><?= Helper::assigneeChips($task['assignees'] ?? [], 20) ?></td>
                <td>
                    <a href="/task/<?= $task['id'] ?>" class="btn btn-ghost btn-xs" data-stop-propagation>→</a>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endforeach; ?>

<?php endif; ?>
