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

<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"><?= t('kanban') ?></a>
    <a href="/project/<?= $project['id'] ?>?view=list" class="view-tab"><?= t('list') ?></a>
    <a href="/project/<?= $project['id'] ?>?view=gantt" class="view-tab active">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>

<!-- Gantt controls -->
<div class="gantt-controls">
    <button class="btn btn-ghost btn-sm" id="ganttPrev">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
    </button>
    <span class="gantt-range-label" id="ganttRangeLabel"></span>
    <button class="btn btn-ghost btn-sm" id="ganttNext">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M5 12h14M12 5l7 7-7 7"/></svg>
    </button>
    <button class="btn btn-ghost btn-sm" id="ganttToday"><?= t('today') ?></button>
    <div class="gantt-zoom">
        <button class="btn btn-ghost btn-sm" data-zoom="weeks"><?= t('gantt_weeks') ?></button>
        <button class="btn btn-ghost btn-sm active" data-zoom="months"><?= t('gantt_months') ?></button>
    </div>
    <?php if (in_array($myRole, ['owner','manager','member'])): ?>
    <button class="btn btn-ghost btn-sm" id="ganttAutoDeps" title="<?= t('gantt_auto_deps_title') ?>">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M10 13a5 5 0 007.54.54l3-3a5 5 0 00-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 00-7.54-.54l-3 3a5 5 0 007.07 7.07l1.71-1.71"/></svg>
        <?= t('gantt_auto_deps') ?>
    </button>
    <button class="btn btn-ghost btn-sm" id="ganttClearDeps" title="<?= t('gantt_clear_deps_title') ?>">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><path d="M10 13a5 5 0 007.54.54l3-3a5 5 0 00-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 00-7.54-.54l-3 3a5 5 0 007.07 7.07l1.71-1.71"/><line x1="4" y1="4" x2="20" y2="20"/></svg>
        <?= t('gantt_clear_deps') ?>
    </button>
    <button class="btn btn-ghost btn-sm" id="ganttClearDates" title="<?= t('gantt_clear_dates_title') ?>">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><rect x="3" y="4" width="18" height="18" rx="2"/><path d="M16 2v4M8 2v4M3 10h18"/><line x1="4" y1="4" x2="20" y2="20"/></svg>
        <?= t('gantt_clear_dates') ?>
    </button>
    <?php endif; ?>
</div>

<?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="M8 6h8M6 12h10M9 18h6"/></svg>
    <h3><?= t('no_tasks') ?></h3>
    <?php if (in_array($myRole, ['owner','manager','member'])): ?>
    <a href="/task/create/<?= $project['id'] ?>" class="btn btn-primary"><?= t('new_task') ?></a>
    <?php endif; ?>
</div>
<?php else: ?>
<div class="gantt-wrapper" id="ganttWrapper">
    <div class="gantt-sidebar" id="ganttSidebar">
        <div class="gantt-header-cell"><?= t('task_title') ?></div>
        <?php foreach ($tasks as $task): ?>
        <a href="/task/<?= $task['id'] ?>" class="gantt-task-label" title="<?= Helper::e($task['title']) ?>">
            <span class="gantt-task-dot" style="background:<?= Helper::e($task['status_color'] ?? '#6b7280') ?>"></span>
            <span class="gantt-task-title"><?= Helper::e($task['title']) ?></span>
        </a>
        <?php endforeach; ?>
        <div class="gantt-resize-handle" id="ganttResizeHandle"></div>
    </div>
    <div class="gantt-chart" id="ganttChart">
        <!-- Rendered by JS -->
    </div>
</div>

<script nonce="<?= csp_nonce() ?>">
const ganttCanEdit      = <?= in_array($myRole, ['owner','manager','member']) ? 'true' : 'false' ?>;
const GANTT_PROJECT_ID  = <?= (int)$project['id'] ?>;
const GANTT_CONFIRM_CLEAR_DEPS  = <?= json_encode(t('gantt_confirm_clear_deps')) ?>;
const GANTT_CONFIRM_CLEAR_DATES = <?= json_encode(t('gantt_confirm_clear_dates')) ?>;
const ganttDeps   = <?= json_encode(array_values($deps ?? [])) ?>;
const ganttTasks = <?= json_encode(array_map(fn($t) => [
    'id'         => $t['id'],
    'title'      => $t['title'],
    'start_date' => $t['start_date'] ?? null,
    'due_date'   => $t['due_date'] ?? null,
    'status'     => $t['status_name'] ?? '',
    'color'      => $t['status_color'] ?? '#6b7280',
    'is_done'    => (bool)($t['is_done'] ?? false),
    'priority'   => $t['priority'],
    'assignee'   => $t['assignee_name'] ?? '',
], $tasks), JSON_UNESCAPED_UNICODE) ?>;
</script>
<script nonce="<?= csp_nonce() ?>" src="/assets/js/gantt.js?v=<?= filemtime(__DIR__ . '/../../assets/js/gantt.js') ?>"></script>
<?php endif; ?>
