<div class="section-title">{{ __('Tareas') }}</div>

@if(empty($tasks))
    <div class="text-center text-gray-400 py-8">
        <p>{{ __('No hay tareas en el rango seleccionado.') }}</p>
    </div>
@else
    <div class="overflow-x-auto">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Tarea</th>
                    <th>Fase</th>
                    <th>Estado</th>
                    <th>Prioridad</th>
                    <th>Asignado</th>
                    <th>Creador</th>
                    <th>Inicio</th>
                    <th>Fin</th>
                    <th>Completada</th>
                    <th>Horas Est.</th>
                    <th>Horas Real</th>
                    <th>Progreso</th>
                    <th>Vencida</th>
                    <th>Subtareas</th>
                </tr>
            </thead>
            <tbody>
                @foreach($tasks as $task)
                    @php
                        $statusBadge = match($task['status']) {
                            'completed' => 'badge-success',
                            'in_progress' => 'badge-info',
                            'pending' => 'badge-warning',
                            'cancelled' => 'badge-error',
                            default => 'badge-ghost',
                        };
                        $priorityBadge = match($task['priority']) {
                            'critical' => 'badge-error',
                            'high' => 'badge-warning',
                            'medium' => 'badge-info',
                            'low' => 'badge-ghost',
                            default => 'badge-ghost',
                        };
                        $subtaskTitles = $task['subtasks'] ? implode('; ', array_column($task['subtasks'], 'title')) : '—';
                    @endphp
                <tr>
                    <td>{{ $task['id'] }}</td>
                    <td class="font-medium">{{ $task['title'] }}</td>
                    <td>{{ $task['phase'] }}</td>
                    <td>
                        <span class="badge {{ $statusBadge }}">{{ $task['status_label'] }}</span>
                    </td>
                    <td>
                        <span class="badge {{ $priorityBadge }}">{{ $task['priority_label'] }}</span>
                    </td>
                    <td>{{ $task['assignee'] }}</td>
                    <td>{{ $task['creator'] }}</td>
                    <td>{{ $task['start_date'] ?? '—' }}</td>
                    <td>{{ $task['due_date'] ?? '—' }}</td>
                    <td>{{ $task['completed_at'] ?? '—' }}</td>
                    <td>{{ $task['estimated_hours'] ?? '—' }}</td>
                    <td>{{ $task['actual_hours'] ?? '—' }}</td>
                    <td>
                        <div style="display:flex;align-items:center;gap:6px;">
                            <div style="flex:1;background:#e5e7eb;border-radius:4px;height:6px;min-width:60px;">
                                <div style="height:6px;border-radius:4px;background:#3b82f6;width:{{ min(100, $task['progress']) }}%;"></div>
                            </div>
                            <span style="font-size:11px;color:#6b7280;white-space:nowrap;">{{ $task['progress'] }}%</span>
                        </div>
                    </td>
                    <td>
                        @if($task['is_overdue'])
                            <span class="badge badge-error">{{ __('Sí') }}</span>
                        @else
                            <span class="badge badge-success">{{ __('No') }}</span>
                        @endif
                    </td>
                    <td class="max-w-xs truncate">{{ $subtaskTitles }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
@endif