<?php $pageTitle = Helper::e($project['name']) . ' – Kanban'; ?>

<div class="project-header">
    <div class="project-header-left">
        <a href="/projects" 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('projects') ?>
        </a>
        <span class="project-dot" style="background:<?= Helper::e($project['color']) ?>"></span>
        <h1 class="page-title"><?= Helper::e($project['name']) ?></h1>
    </div>
    <div class="project-header-actions">
        <?php if (in_array($myRole, ['owner','manager','member'])): ?>
        <a href="/task/create/<?= $project['id'] ?>" 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_task') ?>
        </a>
        <?php endif; ?>
        <?php if (in_array($myRole, ['owner','manager'])): ?>
        <a href="/project/<?= $project['id'] ?>/settings" class="btn btn-ghost btn-sm"><?= t('project_settings') ?></a>
        <a href="/project/<?= $project['id'] ?>/members" class="btn btn-ghost btn-sm"><?= t('members') ?></a>
        <?php endif; ?>
    </div>
</div>

<!-- View switcher -->
<div class="view-switcher">
    <a href="/project/<?= $project['id'] ?>?view=kanban" class="view-tab active">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><rect x="3" y="3" width="5" height="18"/><rect x="10" y="3" width="5" height="18"/><rect x="17" y="3" width="5" height="18"/></svg>
        Kanban
    </a>
    <a href="/project/<?= $project['id'] ?>?view=list" class="view-tab">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01"/></svg>
        <?= t('list') ?>
    </a>
    <a href="/project/<?= $project['id'] ?>?view=gantt" class="view-tab">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M8 6h8M6 12h10M9 18h6"/></svg>
        Gantt
    </a>
    <?php if (AI::isEnabled() && !Auth::isAiBlockedForUser() && in_array($myRole, ['owner','manager','member'])): ?>
    <a href="/project/<?= $project['id'] ?>/ai" class="view-tab view-tab--ai">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"/></svg>
        <?= t('ai_assistant') ?>
    </a>
    <?php endif; ?>
</div>

<!-- Kanban Board -->
<div class="kanban-board" id="kanbanBoard">
    <?php foreach ($statuses as $status): ?>
    <div class="kanban-column" data-status-id="<?= $status['id'] ?>">
        <div class="kanban-column-header">
            <div class="kanban-column-title">
                <span class="status-dot" style="background:<?= Helper::e($status['color']) ?>"></span>
                <?= Helper::e($status['name']) ?>
                <span class="kanban-count"><?= count($tasksByStatus[$status['id']] ?? []) ?></span>
            </div>
            <?php if (in_array($myRole, ['owner','manager','member'])): ?>
            <a href="/task/create/<?= $project['id'] ?>?status_id=<?= $status['id'] ?>" class="kanban-add-btn" title="<?= t('new_task') ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M12 5v14M5 12h14"/></svg>
            </a>
            <?php endif; ?>
        </div>
        <div class="kanban-column-body" data-status-id="<?= $status['id'] ?>">
            <?php foreach ($tasksByStatus[$status['id']] ?? [] as $task): ?>
            <div class="kanban-card" draggable="true"
                 data-task-id="<?= $task['id'] ?>"
                 data-status-id="<?= $task['status_id'] ?>">
                <div class="kanban-card-header">
                    <?= Helper::priorityBadge($task['priority']) ?>
                    <div class="kanban-card-header-right">
                        <?php if ($task['due_date']): ?>
                        <span class="task-due-sm <?= $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; ?>
                        <?php if (in_array($myRole, ['owner','manager','member'])): ?>
                        <form method="POST" action="/task/<?= $task['id'] ?>/edit"
                              class="kanban-card-delete-form" draggable="false" data-confirm="<?= Helper::e(t('confirm_delete')) ?>">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="action" value="delete">
                            <button type="submit" class="kanban-card-delete" title="<?= t('delete') ?>" aria-label="<?= t('delete') ?>">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="13" height="13"><path d="M3 6h18M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2M19 6l-1 14a2 2 0 01-2 2H8a2 2 0 01-2-2L5 6"/></svg>
                            </button>
                        </form>
                        <?php endif; ?>
                    </div>
                </div>
                <a href="/task/<?= $task['id'] ?>" class="kanban-card-title"><?= Helper::e($task['title']) ?></a>
                <div class="kanban-card-footer">
                    <div class="kanban-card-meta">
                        <?php if ($task['comment_count'] > 0): ?>
                        <span class="meta-badge" title="<?= t('comments') ?>">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="12" height="12"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
                            <?= $task['comment_count'] ?>
                        </span>
                        <?php endif; ?>
                        <?php if ($task['attachment_count'] > 0): ?>
                        <span class="meta-badge" title="<?= t('attachments') ?>">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="12" height="12"><path d="M21.44 11.05l-9.19 9.19a6 6 0 01-8.49-8.49l9.19-9.19a4 4 0 015.66 5.66l-9.2 9.19a2 2 0 01-2.83-2.83l8.49-8.48"/></svg>
                            <?= $task['attachment_count'] ?>
                        </span>
                        <?php endif; ?>
                    </div>
                    <?php if (!empty($task['assignees'])): ?>
                    <div class="kanban-assignees">
                        <?php foreach (array_slice($task['assignees'], 0, 3) as $a): ?>
                        <?php if (!empty($a['user_id'])): ?>
                        <?= Helper::avatar(['username' => $a['username'], 'avatar' => $a['avatar'], 'id' => $a['user_id']], 22) ?>
                        <?php else: ?>
                        <span class="avatar-initials avatar-ext" style="width:22px;height:22px;font-size:9px"><?= Helper::e(mb_strtoupper(mb_substr((string)$a['external_name'],0,1))) ?></span>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <?php if (count($task['assignees']) > 3): ?>
                        <span class="assignee-more">+<?= count($task['assignees']) - 3 ?></span>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
            <?php if (empty($tasksByStatus[$status['id']])): ?>
            <div class="kanban-empty"><?= t('drag_here') ?></div>
            <?php endif; ?>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<script nonce="<?= csp_nonce() ?>" src="/assets/js/kanban.js"></script>
<script nonce="<?= csp_nonce() ?>">
initKanban(<?= json_encode(array_map(fn($s) => $s['id'], $statuses)) ?>);
</script>
