{{--
    Tabela read-only de atividades para a visualização pública executiva.

    @var list<array<string, mixed>> $rows
--}}
@php
    $statusBadge = [
        'not_started' => 'secondary',
        'planning'    => 'info',
        'in_progress' => 'primary',
        'validation'  => 'warning',
        'completed'   => 'success',
        'suspended'   => 'secondary',
        'cancelled'   => 'dark',
        'overdue'     => 'danger',
    ];

    $orderedRows = collect($rows ?? [])->map(fn ($row) => [
        'row' => $row,
        'depth' => empty($row['parent_id']) ? 0 : 1,
    ]);
@endphp

<div class="table-responsive">
    <table class="table table-sm table-hover align-middle cr-table">
        <thead class="table-light">
            <tr>
                <th scope="col">{{ __('Item') }}</th>
                <th scope="col">{{ __('Atividades') }}</th>
                <th scope="col">{{ __('Predecessora') }}</th>
                <th scope="col" class="cr-col-progress">{{ __('%') }}</th>
                <th scope="col" class="cr-col-status">{{ __('Status') }}</th>
                <th scope="col">{{ __('Responsável') }}</th>
                <th scope="col">{{ __('Início') }}</th>
                <th scope="col">{{ __('Término') }}</th>
                <th scope="col">{{ __('Descrição') }}</th>
                <th scope="col">{{ __('Observação') }}</th>
            </tr>
        </thead>
        <tbody>
            @forelse($orderedRows as $item)
                @php
                    $row = $item['row'];
                    $depth = $item['depth'];
                    $dotStatus = $row['is_overdue'] ? 'overdue' : $row['status'];
                    $badge = $statusBadge[$dotStatus] ?? 'secondary';
                @endphp
                <tr @if($row['is_overdue']) class="table-danger" @endif>
                    <td class="text-nowrap"><code>{{ $row['code'] }}</code></td>
                    <td>
                        <span class="@if($depth > 0) cr-depth-1 @endif">
                            @if($depth > 0)<i class="fa-solid fa-turn-up fa-rotate-90 text-muted me-1 small"></i>@endif
                            {{ $row['title'] }}
                        </span>
                    </td>
                    <td class="small text-muted">{{ $row['predecessors'] ?: '-' }}</td>
                    <td>
                        <div class="d-flex align-items-center gap-2 cr-row-progress-wrap">
                            <div class="progress flex-grow-1 cr-row-progress" role="progressbar" aria-label="{{ __('Progresso (%)') }}" aria-valuenow="{{ (int) $row['completion_percent'] }}" aria-valuemin="0" aria-valuemax="100">
                                <div class="progress-bar cr-progress-bar-dynamic" style="--cr-w: {{ $row['completion_percent'] }}%;"></div>
                            </div>
                            <small class="text-muted text-nowrap cr-row-progress-label">{{ (int) $row['completion_percent'] }}%</small>
                        </div>
                    </td>
                    <td>
                        <span class="badge bg-{{ $badge }} cr-status-badge">{{ $row['status_label'] }}</span>
                        @if($row['is_overdue'])
                            <span class="badge bg-danger mt-1">{{ __('Atrasado') }}</span>
                        @endif
                    </td>
                    <td class="small">
                        <x-cronogramas.responsible-badges :labels="$row['responsible_labels'] ?? []" />
                    </td>
                    <td class="small text-nowrap">{{ $row['planned_start_date'] ? \Carbon\Carbon::parse($row['planned_start_date'])->format('d/m/Y') : '-' }}</td>
                    <td class="small text-nowrap">{{ $row['planned_end_date'] ? \Carbon\Carbon::parse($row['planned_end_date'])->format('d/m/Y') : '-' }}</td>
                    <td class="small text-start">{{ $row['description'] ?: '-' }}</td>
                    <td class="small text-start">{{ $row['notes'] ?: '-' }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="10" class="text-center text-muted py-4">{{ __('Nenhuma atividade cadastrada.') }}</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>
