@extends('layouts.app')
@section('title', 'Enterprise Project Roadmap')

@push('styles')
<link rel="stylesheet" href="{{ asset('assets/css/dashboard-final.css') }}">
<style>
    .timeline-container {
        display: flex;
        flex-direction: column;
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: 24px;
        overflow: hidden;
        margin-top: 24px;
    }
    .timeline-header {
        display: flex;
        background: rgba(255,255,255,0.03);
        border-bottom: 1px solid var(--border-color);
        position: sticky;
        top: 0;
        z-index: 100;
    }
    .timeline-sidebar-header {
        width: 350px;
        padding: 20px 32px;
        border-right: 1px solid var(--border-color);
        font-size: 11px;
        font-weight: 900;
        text-transform: uppercase;
        color: var(--text-secondary);
        letter-spacing: 0.1em;
        flex-shrink: 0;
        background: var(--bg-card);
    }
    .timeline-calendar-header {
        display: flex;
        flex-grow: 1;
    }
    .timeline-day {
        min-width: 60px;
        flex: 1;
        padding: 16px 0;
        text-align: center;
        border-right: 1px solid rgba(255,255,255,0.05);
    }
    .timeline-day.today { background: rgba(181, 255, 107, 0.05); }
    .timeline-body {
        max-height: 700px;
        overflow-y: auto;
        position: relative;
    }
    .timeline-row {
        display: flex;
        height: 70px;
        border-bottom: 1px solid rgba(255,255,255,0.03);
        transition: background 0.2s ease;
        align-items: center;
    }
    .timeline-row:hover { background: rgba(255,255,255,0.01); }
    .timeline-task-info {
        width: 350px;
        padding: 0 32px;
        border-right: 1px solid var(--border-color);
        flex-shrink: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 4px;
        background: var(--bg-card);
        z-index: 10;
        height: 100%;
    }
    .timeline-grid {
        position: relative;
        flex-grow: 1;
        display: flex;
        height: 100%;
    }
    .timeline-bar-container {
        position: absolute;
        height: 32px;
        top: 50%;
        transform: translateY(-50%);
        border-radius: 100px;
        overflow: hidden;
        display: flex;
        align-items: center;
        padding: 0 16px;
        font-size: 10px;
        font-weight: 900;
        color: #000;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        cursor: pointer;
        border: 1px solid rgba(0,0,0,0.1);
    }
    .timeline-bar-container:hover {
        height: 40px;
        filter: brightness(1.1);
        box-shadow: 0 10px 30px rgba(0,0,0,0.4);
    }
    .timeline-progress-fill {
        position: absolute;
        inset: 0;
        background: linear-gradient(90deg, var(--accent-color), #96ff3d);
        z-index: -1;
    }
    .timeline-progress-bg {
        position: absolute;
        inset: 0;
        background: rgba(255,255,255,0.1);
        z-index: -2;
    }
    .timeline-scroll-wrapper { overflow-x: auto; position: relative; }
</style>
@endpush

@section('content')

@include('components.page-header', [
    'title' => 'Advanced Project Roadmap',
    'description' => 'Structural engineering roadmap with zero-collision geometry and real-time workforce alignment.',
    'breadcrumbs' => ['Home' => route('admin.dashboard'), 'Timeline' => '#'],
    'actions' => '<button onclick="window.location.href=\''.route('tasks.create').'\'" class="final-btn final-btn-primary"><i class="bi bi-plus-lg"></i> Plan Milestone</button>'
])

<div class="final-card">
    <div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 16px;">
        <div style="display: flex; gap: 12px; flex-grow: 1;">
            <select class="final-select" style="width: 200px;" onchange="updateFilter('project_id', this.value)">
                <option value="">All Projects</option>
                @foreach($projects as $p) <option value="{{ $p->id }}" @selected(request('project_id') == $p->id)>{{ $p->name }}</option> @endforeach
            </select>
            <select class="final-select" style="width: 200px;" onchange="updateFilter('employee_id', this.value)">
                <option value="">All Engineers</option>
                @foreach($employees as $e) <option value="{{ $e->id }}" @selected(request('employee_id') == $e->id)>{{ $e->name }}</option> @endforeach
            </select>
        </div>
        <div class="flex items-center gap-2">
            <a href="{{ route('tasks.index') }}" class="final-btn final-btn-secondary" style="padding: 8px 12px;"><i class="bi bi-list-ul"></i></a>
            <a href="{{ route('tasks.kanban') }}" class="final-btn final-btn-secondary" style="padding: 8px 12px;"><i class="bi bi-kanban"></i></a>
            <a href="{{ route('tasks.timeline') }}" class="final-btn final-btn-primary" style="padding: 8px 12px;"><i class="bi bi-calendar-range"></i></a>
        </div>
    </div>
</div>

<div class="timeline-container">
    @php
        $start = $windowStart;
        $end = $windowEnd;
        $totalDays = $start->diffInDays($end) + 1;
        $dayWidth = 60;
    @endphp

    <div class="timeline-scroll-wrapper scrollbar-modern">
        <div class="timeline-header" style="width: {{ ($totalDays * $dayWidth) }}px; margin-left: 350px;">
            <div class="timeline-calendar-header">
                @for($i = 0; $i < $totalDays; $i++)
                    @php $date = $start->copy()->addDays($i); @endphp
                    <div class="timeline-day {{ $date->isToday() ? 'today' : '' }}" style="width: {{ $dayWidth }}px; flex: 0 0 {{ $dayWidth }}px;">
                        <div style="font-size: 9px; color: var(--text-secondary); margin-bottom: 2px; font-weight: 800;">{{ $date->format('D') }}</div>
                        <div style="font-size: 14px; font-weight: 900; color: {{ $date->isToday() ? 'var(--accent-color)' : 'var(--text-primary)' }}">{{ $date->format('d') }}</div>
                    </div>
                @endfor
            </div>
        </div>

        <div class="timeline-body scrollbar-modern" style="width: {{ 350 + ($totalDays * $dayWidth) }}px;">
            @forelse($lanes as $laneIndex => $laneTasks)
                <div class="timeline-row">
                    <div class="timeline-task-info">
                        <div style="font-size: 11px; font-weight: 900; color: var(--text-secondary); letter-spacing: 0.1em;">LANE {{ $laneIndex + 1 }}</div>
                        <div style="font-size: 9px; color: var(--text-secondary);">{{ count($laneTasks) }} Active Modules</div>
                    </div>
                    <div class="timeline-grid" style="width: {{ ($totalDays * $dayWidth) }}px;">
                        @for($i = 0; $i < $totalDays; $i++)
                            <div style="position: absolute; left: {{ $i * $dayWidth }}px; top: 0; bottom: 0; width: 1px; background: rgba(255,255,255,0.03);"></div>
                        @endfor

                        @foreach($laneTasks as $t)
                            @php
                                $leftOffset = $start->diffInDays($t['start']) * $dayWidth;
                                $width = ($t['start']->diffInDays($t['end']) + 1) * $dayWidth;
                                $isOverdue = $t['end']->isPast() && $t['status']->slug != 'completed';
                                $task = $t['model'];
                            @endphp
                            <div class="timeline-bar-container" 
                                 style="left: {{ $leftOffset }}px; width: {{ $width }}px; {{ $isOverdue ? 'background: #ff4b4b;' : '' }}"
                                 onclick="window.location.href='{{ route('tasks.kanban') }}?task_id={{ $task->id }}'">
                                @if(!$isOverdue)
                                    <div class="timeline-progress-bg"></div>
                                    <div class="timeline-progress-fill" style="width: {{ $task->progress_percent }}%"></div>
                                @endif
                                <div style="display: flex; align-items: center; gap: 8px; position: relative; z-index: 10; width: 100%; padding: 0 4px;">
                                    <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex-grow: 1;">{{ $task->title }}</span>
                                    <span style="font-size: 8px; opacity: 0.8;">{{ $task->progress_percent }}%</span>
                                </div>
                            </div>
                        @endforeach
                    </div>
                </div>
            @empty
                <div style="padding: 100px; text-align: center; width: 100%;">
                    <x-empty-state title="No Active Roadmap" subtitle="Initialize task deadlines to generate the structural timeline." icon="bi-calendar-x" />
                </div>
            @endforelse
        </div>

        @php $todayLeft = $start->diffInDays(now()) * $dayWidth + ($dayWidth / 2); @endphp
        <div style="position: absolute; top: 0; bottom: 0; left: {{ 350 + $todayLeft }}px; width: 2px; background: var(--accent-color); z-index: 200; pointer-events: none; opacity: 0.6;">
            <div style="position: sticky; top: 12px; background: var(--accent-color); color: #000; font-size: 9px; font-weight: 900; padding: 2px 8px; border-radius: 4px; transform: translateX(-50%); box-shadow: 0 0 20px var(--accent-soft);">NOW</div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    function updateFilter(key, val) {
        const url = new URL(window.location.href);
        if (val) url.searchParams.set(key, val);
        else url.searchParams.delete(key);
        window.location.href = url.toString();
    }
</script>
@endpush

@endsection


