<div class="tracking-detail-content">
    <?php
        // Logic Status Progress Bar
        $stages = ['OPEN', 'PICKUP', 'PROCESS', 'DEPART', 'ON DESTINATION', 'ON COURIER', 'DELIVERED'];
        $currentStatus = strtoupper($status);
        $currentIndex = 0;
        
        // Cari status eksak
        $found = array_search($currentStatus, $stages);
        if ($found !== false) {
            $currentIndex = $found;
        } else {
            // Cari status partial (misal: 'PROCESS (HUB)')
            foreach ($stages as $key => $stage) {
                if (str_contains($currentStatus, $stage)) { $currentIndex = $key; break; }
            }
        }
    ?>

    <!-- Step Wizard (Horizontal Status) -->
    <div class="step-wizard">
        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $stages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $stage): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <?php
                // Tentukan class CSS berdasarkan urutan
                $stepClass = '';
                if ($key < $currentIndex) {
                    $stepClass = 'passed';
                } elseif ($key == $currentIndex) {
                    $stepClass = ($currentStatus == 'DELIVERED') ? 'success' : 'active';
                }
            ?>
            <div class="step-item <?php echo e($stepClass); ?>">
                <div class="step-circle">
                    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($key < $currentIndex): ?>
                        <i class="fas fa-check"></i>
                    <?php elseif($key == $currentIndex && $currentStatus == 'DELIVERED'): ?>
                        <i class="fas fa-check-double"></i>
                    <?php elseif($key == $currentIndex): ?>
                        <i class="fas fa-circle" style="font-size: 0.5rem;"></i>
                    <?php else: ?>
                        <span><?php echo e($key + 1); ?></span>
                    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                </div>
                <!-- Label hanya muncul di desktop agar tidak berantakan di HP -->
                <div class="step-label d-none d-md-block"><?php echo e($stage); ?></div>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    </div>

    <!-- Timeline History (Vertical) -->
    <div class="history-title">
        <i class="bi bi-clock-history me-2 text-danger"></i>Riwayat Perjalanan
    </div>
    
    <div class="timeline-v">
        <?php
            // Membalik urutan data riwayat agar status TERBARU selalu ada di PALING ATAS
            // Hal ini juga memperbaiki bug indikator warna yang salah sasaran
            $historyList = collect($history)->reverse()->values();
        ?>
        
        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $historyList; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $hist): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <?php
                // Karena data sudah dibalik, status terbaru pasti ada di index 0 ($loop->first)
                $isLatest = $loop->first;
                $isDelivered = ($isLatest && strtoupper($hist->status) == 'DELIVERED');
                
                // Tentukan class timeline history (Hijau untuk Delivered, Merah untuk Active saat ini)
                $historyClass = '';
                if ($isDelivered) {
                    $historyClass = 'success';
                } elseif ($isLatest) {
                    $historyClass = 'active';
                }
            ?>
            <div class="history-item <?php echo e($historyClass); ?>">
                <div class="history-datetime">
                    <!-- MENGGUNAKAN last_update AGAR JAMNYA VALID SESUAI DATABASE -->
                    <span class="history-date"><?php echo e($hist->last_update ? \Carbon\Carbon::parse($hist->last_update)->format('d M Y') : '-'); ?></span>
                    <span class="history-time"><?php echo e($hist->last_update ? \Carbon\Carbon::parse($hist->last_update)->format('H:i') : '-'); ?> WIB</span>
                </div>
                <div class="history-status"><?php echo e($hist->status); ?></div>
                <div class="history-desc"><?php echo e($hist->status_description); ?></div>
                
                <!-- TOMBOL BUKTI FOTO (Muncul Jika Status Delivered dan url_img tidak kosong) -->
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(strtoupper($hist->status) == 'DELIVERED' && !empty($hist->url_img)): ?>
                    <a href="<?php echo e(route('tracking.proof', ['secrets' => $hist->url_img])); ?>" target="_blank" class="btn-proof mt-2">
                        <i class="bi bi-camera-fill me-1"></i> Lihat Bukti Foto
                    </a>
                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(count($historyList) == 0): ?>
            <div class="text-center text-muted py-3">
                <small>Belum ada data riwayat tersedia.</small>
            </div>
        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    </div>
</div><?php /**PATH /var/www/html/resources/views/partials/tracking_detail.blade.php ENDPATH**/ ?>