@extends('layouts.window.theme')
@section('title', __('Cronograma') . ' - ' . $project->name)
@section('content')

<link rel="stylesheet" href="{{ asset('pmar/css/cronogramas.css') }}?v={{ filemtime(public_path('pmar/css/cronogramas.css')) }}">
<link rel="stylesheet" href="{{ asset('pmar/css/cronogramas-public.css') }}?v={{ filemtime(public_path('pmar/css/cronogramas-public.css')) }}">

@php
    $viewTab = $viewTab ?? 'timeline';
    $scopeParts = collect([
        ! empty($scopeLabels['structures']) ? __('Órgãos') . ': ' . implode(', ', $scopeLabels['structures']) : null,
        ! empty($scopeLabels['unidades']) ? __('Unidades') . ': ' . implode(', ', $scopeLabels['unidades']) : null,
        ! empty($scopeLabels['empresas']) ? __('Empresas') . ': ' . implode(', ', $scopeLabels['empresas']) : null,
    ])->filter()->values();
@endphp

<div class="cr-public-page">
    <header class="cr-public-header card shadow-sm border-0 mb-4">
        <div class="card-body">
            <div class="d-flex flex-wrap align-items-start gap-3 mb-3">
                <div class="cr-public-brand flex-shrink-0">
                    <img src="{{ asset('pmar/assets/img/brasao-2026.png') }}" alt="{{ __('Brasão da Prefeitura de Angra dos Reis') }}">
                </div>
                <div class="flex-grow-1 min-w-0">
                    <div class="d-flex flex-wrap align-items-center gap-2 mb-2">
                        <h1 class="h4 mb-0 text-primary">{{ $project->name }}</h1>
                        <span class="badge bg-secondary cr-public-readonly-badge">
                            <i class="fa-solid fa-eye" aria-hidden="true"></i> {{ __('Visualização executiva') }}
                        </span>
                    </div>
                    <div class="d-flex flex-wrap align-items-center gap-2 mb-2">
                        <span class="badge {{ $project->isReadOnly() ? 'bg-dark' : 'bg-primary' }}">
                            {{ \App\Enums\Accounts\Cronogramas\CronogramaProjectStatus::tryFrom($project->status)?->label() }}
                        </span>
                        <span class="small text-muted">
                            <i class="fa-regular fa-calendar" aria-hidden="true"></i>
                            {{ __('Início') }}: {{ $project->start_date?->format('d/m/Y') ?? '-' }}
                        </span>
                        <span class="small text-muted">
                            <i class="fa-regular fa-calendar-check" aria-hidden="true"></i>
                            {{ __('Término previsto') }}: {{ $project->planned_end_date?->format('d/m/Y') ?? '-' }}
                        </span>
                    </div>
                    @if($project->description)
                        <p class="text-muted mb-2">{{ $project->description }}</p>
                    @endif
                    @if($scopeParts->isNotEmpty())
                        <ul class="cr-public-scope-list small text-muted" aria-label="{{ __('Escopo organizacional') }}">
                            @foreach($scopeParts as $scopeLine)
                                <li>{{ $scopeLine }}</li>
                            @endforeach
                        </ul>
                    @endif
                </div>
            </div>

            <div class="row g-3 align-items-stretch">
                <div class="col-lg-7">
                    <div class="progress cr-progress-h-sm mb-1" role="progressbar" aria-label="{{ __('Progresso do projeto') }}" aria-valuenow="{{ $project->completion_percent }}" aria-valuemin="0" aria-valuemax="100">
                        <div class="progress-bar bg-success cr-progress-bar-dynamic" style="--cr-w: {{ $project->completion_percent }}%;"></div>
                    </div>
                    <small class="text-muted">{{ $project->completion_percent }}% {{ __('concluído') }}</small>
                </div>
                <div class="col-lg-5">
                    <div class="row g-2 text-center">
                        <div class="col-4">
                            <div class="border rounded py-2 h-100">
                                <div class="fw-bold">{{ $projectStats['total_count'] ?? 0 }}</div>
                                <div class="small text-muted">{{ __('Atividades') }}</div>
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="border rounded py-2 h-100">
                                <div class="fw-bold text-success">{{ $projectStats['completed_count'] ?? 0 }}</div>
                                <div class="small text-muted">{{ __('Concluídas') }}</div>
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="border rounded py-2 h-100">
                                <div class="fw-bold text-danger">{{ $projectStats['overdue_count'] ?? 0 }}</div>
                                <div class="small text-muted">{{ __('Atrasadas') }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <ul class="nav nav-tabs cr-view-tabs cr-public-tabs mb-3" role="tablist">
        @php
            $tabs = [
                'timeline' => ['icon' => 'fa-timeline', 'label' => __('Linha do tempo')],
                'table' => ['icon' => 'fa-table-list', 'label' => __('Tabela')],
            ];
        @endphp
        @foreach($tabs as $key => $tab)
            <li class="nav-item" role="presentation">
                <a class="nav-link @if($viewTab === $key) active @endif"
                   href="{{ route('cronogramas.public.show', ['token' => $project->public_share_token, 'tab' => $key]) }}">
                    <i class="fa-solid {{ $tab['icon'] }}" aria-hidden="true"></i> {{ $tab['label'] }}
                </a>
            </li>
        @endforeach
    </ul>

    <main class="card shadow-sm border-0">
        <div class="card-body">
            @if($viewTab === 'table')
                @include('components.cronogramas.public-activities-table', ['rows' => $tableRows])
            @else
                @if(empty($tableRows))
                    <div class="text-center text-muted py-5">
                        <i class="fa-solid fa-calendar-xmark fa-2x mb-2 d-block" aria-hidden="true"></i>
                        {{ __('Nenhuma atividade cadastrada.') }}
                    </div>
                @else
                    @include('components.cronogramas.timeline', ['rows' => $tableRows, 'dir' => 'asc'])
                @endif
            @endif
        </div>
    </main>

    <footer class="cr-public-footer text-center mt-4">
        {{ __('Prefeitura Municipal de Angra dos Reis') }} · {{ __('Atualizado em') }} {{ now()->format('d/m/Y H:i') }}
    </footer>
</div>

@endsection
