
<?php
    $tlRows = collect($rows ?? []);

    $statusBadge = [
        'not_started' => 'secondary',
        'planning'    => 'info',
        'in_progress' => 'primary',
        'validation'  => 'warning',
        'completed'   => 'success',
        'suspended'   => 'secondary',
        'cancelled'   => 'dark',
        'overdue'     => 'danger',
    ];

    // Agrupa por mês/ano da data de início (ou 'sem_data') de forma sequencial:
    // um novo grupo inicia quando o mês muda em relação ao item anterior. Isso
    // preserva a ordenação escolhida (código, título, status, etc.) e mantém o
    // visual cronológico idêntico quando a ordem já é por data.
    $grouped = [];
    $currentMonthKey = null;
    foreach ($tlRows as $row) {
        $monthKey = empty($row['planned_start_date'])
            ? 'sem_data'
            : \Carbon\Carbon::parse($row['planned_start_date'])->format('Y-m');

        if ($monthKey !== $currentMonthKey) {
            $grouped[] = ['key' => $monthKey, 'rows' => []];
            $currentMonthKey = $monthKey;
        }

        $grouped[array_key_last($grouped)]['rows'][] = $row;
    }
?>

<div class="cr-timeline" id="cr-timeline">
    <?php $__currentLoopData = $grouped; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $group): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <?php $monthKey = $group['key']; $monthRows = $group['rows']; ?>
        <div class="cr-timeline-month">
            <span class="cr-timeline-month-label">
                <?php if($monthKey === 'sem_data'): ?>
                    <?php echo e(__('Sem data')); ?>

                <?php else: ?>
                    <?php echo e(\Carbon\Carbon::createFromFormat('Y-m', $monthKey)->translatedFormat('F \d\e Y')); ?>

                <?php endif; ?>
            </span>

            <?php $__currentLoopData = $monthRows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php
                    $dotStatus = $row['is_overdue'] ? 'overdue' : $row['status'];
                    $badge     = $statusBadge[$dotStatus] ?? 'secondary';
                    $startFmt  = $row['planned_start_date'] ? \Carbon\Carbon::parse($row['planned_start_date'])->format('d/m/Y') : null;
                    $endFmt    = $row['planned_end_date']   ? \Carbon\Carbon::parse($row['planned_end_date'])->format('d/m/Y')   : null;
                    $isChild   = ! empty($row['parent_id']);
                ?>
                <div class="cr-timeline-item status-<?php echo e($dotStatus); ?> <?php echo e($isChild ? 'ms-4' : ''); ?>" data-activity-id="<?php echo e($row['id']); ?>">
                    <div class="cr-timeline-dot"></div>
                    <div class="card cr-timeline-card shadow-sm">
                        <div class="card-body py-2 px-3">
                            <div class="d-flex justify-content-between align-items-start gap-2 flex-wrap">
                                <div class="fw-semibold text-truncate cr-kanban-title-max" title="<?php echo e($row['title']); ?>">
                                    <?php if($isChild): ?><i class="fa-solid fa-turn-up fa-rotate-90 text-muted me-1 small"></i><?php endif; ?>
                                    <code class="small"><?php echo e($row['code']); ?></code> <?php echo e($row['title']); ?>

                                </div>
                                <span class="badge bg-<?php echo e($badge); ?> flex-shrink-0"><?php echo e($row['status_label']); ?></span>
                            </div>
                            <div class="d-flex align-items-center gap-3 mt-1 flex-wrap">
                                <?php if($startFmt || $endFmt): ?>
                                    <small class="text-muted text-nowrap">
                                        <i class="fa-regular fa-calendar me-1"></i>
                                        <?php echo e($startFmt ?? '-'); ?> - <?php echo e($endFmt ?? '-'); ?>

                                    </small>
                                <?php endif; ?>
                                <?php if(! empty($row['responsible_labels'])): ?>
                                    <div class="mt-1">
                                        <?php if (isset($component)) { $__componentOriginala0dea79f0b80f4bdbefb79367f67c23c = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginala0dea79f0b80f4bdbefb79367f67c23c = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.cronogramas.responsible-badges','data' => ['labels' => $row['responsible_labels']]] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('cronogramas.responsible-badges'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['labels' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($row['responsible_labels'])]); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginala0dea79f0b80f4bdbefb79367f67c23c)): ?>
<?php $attributes = $__attributesOriginala0dea79f0b80f4bdbefb79367f67c23c; ?>
<?php unset($__attributesOriginala0dea79f0b80f4bdbefb79367f67c23c); ?>
<?php endif; ?>
<?php if (isset($__componentOriginala0dea79f0b80f4bdbefb79367f67c23c)): ?>
<?php $component = $__componentOriginala0dea79f0b80f4bdbefb79367f67c23c; ?>
<?php unset($__componentOriginala0dea79f0b80f4bdbefb79367f67c23c); ?>
<?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                            <div class="d-flex align-items-center gap-2 mt-2">
                                <div class="progress flex-grow-1 cr-progress-h-kanban">
                                    <div class="progress-bar bg-<?php echo e($badge); ?> cr-progress-bar-dynamic" style="--cr-w: <?php echo e($row['completion_percent']); ?>%;"></div>
                                </div>
                                <small class="text-muted text-nowrap cr-kanban-percent-min"><?php echo e((int) $row['completion_percent']); ?>%</small>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
</div>
<?php /**PATH /var/www/html/portal/resources/views/components/cronogramas/timeline.blade.php ENDPATH**/ ?>