<?php $__env->startSection('title', 'Antrean Ambil Daging - Sistem Distribusi Qurban'); ?>

<?php $__env->startSection('header_title', 'Antrean Ambil Daging'); ?>
<?php $__env->startSection('header_subtitle', 'Urutan antrean peserta yang menunggu penyerahan paket daging qurban'); ?>

<?php $__env->startSection('content'); ?>
<div class="panel" style="min-height: 500px;">
    <div class="panel-header">
        <h2 class="panel-title"><span>📋</span> Antrean Aktif (<?php echo e($queue->count()); ?> orang)</h2>
        <div style="display: flex; align-items: center; gap: 1rem;">
            <span class="badge badge-info" style="font-size: 0.75rem; text-transform: none; display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.4rem 0.75rem;">
                🔄 Auto-refresh: <strong id="seconds-left" style="font-size: 0.85rem; color: var(--accent); margin-left: 0.15rem;">10</strong>s
            </span>
            <button onclick="window.location.reload()" class="btn btn-secondary btn-sm">🔄 Refresh Manual</button>
        </div>
    </div>

    <?php if($queue->isEmpty()): ?>
        <div style="text-align: center; padding: 5rem 0; color: var(--text-muted); display: flex; flex-direction: column; align-items: center; justify-content: center; flex-grow: 1;">
            <span style="font-size: 4.5rem; margin-bottom: 1.5rem; animation: pulse 2s infinite;">⏳</span>
            <strong style="font-size: 1.25rem; color: var(--primary-dark);">Belum Ada Antrean</strong>
            <p style="font-size: 0.9rem; margin-top: 0.5rem; max-width: 350px;">
                Saat ini antrean kosong. Silakan scan kupon di menu <a href="<?php echo e(route('distribution.index')); ?>" style="color: var(--primary); font-weight: 700; text-decoration: underline;">Cari & Scan Kupon</a> untuk memasukkan peserta ke antrean.
            </p>
        </div>
    <?php else: ?>
        <div class="table-wrapper">
            <table class="table-custom">
                <thead>
                    <tr>
                        <th style="width: 8%; text-align: center;">No Antrean</th>
                        <th style="width: 20%;">Nama Penerima (Kupon)</th>
                        <th style="width: 10%;">Tipe</th>
                        <th style="width: 18%;">Status & Jatah Daging</th>
                        <th style="width: 20%;">List Titipan (Pemilik & Tipe)</th>
                        <th style="width: 12%;">Total Paket</th>
                        <th style="width: 12%; text-align: center;">Aksi Serah Terima</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $queue; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $p = $item->participant;
                            $alloc = $p->getAllocation();
                            $ownStatus = $p->distribution ? $p->distribution->status : 'Belum';
                            $pendingTitips = $p->entrustedToMe->where('status', '!=', 'Selesai');
                            $hasTitipans = $p->entrustedToMe->count() > 0;
                            $hasPendingItems = ($ownStatus !== 'Sudah') || ($pendingTitips->count() > 0);
                        ?>
                        <tr>
                            <!-- Queue number -->
                            <td style="text-align: center; font-weight: 800; font-size: 1.25rem; color: var(--primary-dark); vertical-align: middle;">
                                <span style="display: inline-flex; width: 34px; height: 34px; border-radius: 50%; background: var(--primary-light); align-items: center; justify-content: center; border: 2px solid var(--primary);">
                                    <?php echo e($index + 1); ?>

                                </span>
                                <div style="font-size: 0.65rem; color: var(--text-muted); font-weight: 500; margin-top: 0.25rem;">
                                    <?php echo e($item->created_at->format('H:i:s')); ?>

                                </div>
                            </td>

                            <!-- Participant details -->
                            <td>
                                <strong style="font-size: 1.05rem; color: var(--primary-dark); display: block;"><?php echo e($p->nama); ?></strong>
                                <div style="display: flex; gap: 0.5rem; align-items: center; margin-top: 0.15rem;">
                                    <span style="font-size: 0.75rem; color: var(--accent); font-weight: 700;">Kupon: <?php echo e($p->no_kupon); ?></span>
                                    <span style="font-size: 0.75rem; color: var(--text-muted);">Regu <?php echo e($p->regu ?? '-'); ?></span>
                                </div>
                            </td>

                            <!-- Type -->
                            <td style="vertical-align: middle;">
                                <span style="font-size: 0.85rem; background: var(--primary-light); color: var(--primary-dark); padding: 0.25rem 0.5rem; border-radius: 4px; font-weight: 700;">
                                    <?php echo e($p->jenis); ?>

                                </span>
                            </td>

                            <!-- Own Allocation Details -->
                            <td>
                                <div style="font-size: 0.8rem; line-height: 1.35; background: #fafafa; padding: 0.5rem; border-radius: 6px; border: 1px solid #f0f0f0;">
                                    <?php if($ownStatus === 'Titip'): ?>
                                        <div style="margin-bottom: 0.25rem; font-weight: 600; color: var(--text-muted);">
                                            Paket dititipkan ke:<br>
                                            <strong style="color: var(--primary-dark);"><?php echo e($p->distribution->entrustee->nama ?? '-'); ?></strong>
                                        </div>
                                        <span class="badge badge-info" style="font-size: 0.65rem; padding: 0.1rem 0.35rem;">Dititip</span>
                                    <?php else: ?>
                                        <?php if($alloc->daging_sapi > 0): ?> 🐮 Sapi: <?php echo e($alloc->daging_sapi); ?> Porsi<br> <?php endif; ?>
                                        <?php if($alloc->daging_kambing > 0): ?> 🐐 Kambing: <?php echo e($alloc->daging_kambing); ?> Porsi<br> <?php endif; ?>
                                        <?php if($alloc->hak_2kg > 0): ?> ⭐ Hak 2Kg: <?php echo e($alloc->hak_2kg); ?><br> <?php endif; ?>
                                        <?php if($alloc->hak_paha > 0): ?> 🍗 Paha: <?php echo e($alloc->hak_paha); ?> <?php endif; ?>
                                        <div style="margin-top: 0.25rem;">
                                            <?php if($ownStatus === 'Sudah'): ?>
                                                <span class="badge badge-success" style="font-size: 0.65rem; padding: 0.1rem 0.35rem;">Diambil</span>
                                            <?php else: ?>
                                                <span class="badge badge-danger" style="font-size: 0.65rem; padding: 0.1rem 0.35rem;">Belum</span>
                                            <?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </td>

                            <!-- Entrusted List -->
                            <td>
                                <?php if(!$hasTitipans): ?>
                                    <span style="font-size: 0.8rem; color: var(--text-muted); font-style: italic;">Tidak ada titipan paket</span>
                                <?php else: ?>
                                    <ul style="list-style-type: none; font-size: 0.8rem; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.35rem;">
                                        <?php $__currentLoopData = $p->entrustedToMe; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $t): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <?php
                                                $tAlloc = $t->participant->getAllocation();
                                            ?>
                                            <li style="border-left: 2px solid var(--primary); padding-left: 0.5rem; line-height: 1.35;">
                                                <strong style="color: var(--text-main);"><?php echo e($t->participant->nama); ?></strong>
                                                <span style="font-size: 0.7rem; color: var(--accent); font-weight: 700; margin-left: 0.25rem;">(<?php echo e($t->participant->no_kupon); ?>)</span>
                                                <span style="font-size: 0.7rem; font-family: monospace; font-weight: 600; background: #e2e8f0; padding: 0.1rem 0.25rem; border-radius: 3px; margin-left: 0.25rem;">
                                                    <?php echo e($t->participant->jenis); ?>

                                                </span>
                                                <div style="font-size: 0.75rem; color: var(--text-muted);">
                                                    Jatah:
                                                    <?php if($tAlloc->daging_sapi > 0): ?> 🐮S:<?php echo e($tAlloc->daging_sapi); ?> <?php endif; ?>
                                                    <?php if($tAlloc->daging_kambing > 0): ?> 🐐K:<?php echo e($tAlloc->daging_kambing); ?> <?php endif; ?>
                                                    <?php if($tAlloc->hak_2kg > 0): ?> ⭐2K:<?php echo e($tAlloc->hak_2kg); ?> <?php endif; ?>
                                                </div>
                                                <div style="margin-top: 0.15rem;">
                                                    <?php if($t->status === 'Pending'): ?>
                                                        <span class="badge badge-warning" style="font-size: 0.6rem; padding: 0 0.3rem;">Pending</span>
                                                    <?php elseif($t->status === 'Diproses'): ?>
                                                        <span class="badge badge-info" style="font-size: 0.6rem; padding: 0 0.3rem;">Diproses</span>
                                                    <?php else: ?>
                                                        <span class="badge badge-success" style="font-size: 0.6rem; padding: 0 0.3rem;">Selesai</span>
                                                    <?php endif; ?>
                                                </div>
                                            </li>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </ul>
                                <?php endif; ?>
                            </td>

                            <!-- Total Paket Tally -->
                            <td style="vertical-align: middle;">
                                <?php
                                    $tally = [];
                                    if ($ownStatus !== 'Titip') {
                                        $tally[$p->jenis] = ($tally[$p->jenis] ?? 0) + 1;
                                    }
                                    foreach ($p->entrustedToMe as $t) {
                                        $tally[$t->participant->jenis] = ($tally[$t->participant->jenis] ?? 0) + 1;
                                    }
                                ?>
                                <?php if(empty($tally)): ?>
                                    <span style="font-size: 0.8rem; color: var(--text-muted); font-style: italic;">0 Paket</span>
                                <?php else: ?>
                                    <div style="display: flex; flex-direction: column; gap: 0.3rem;">
                                        <?php $__currentLoopData = $tally; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $type => $count): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <span style="font-size: 0.8rem; font-weight: 700; color: var(--primary-dark); display: inline-flex; align-items: center; gap: 0.25rem;">
                                                <span style="background: #e2e8f0; color: #334155; font-size: 0.7rem; font-weight: 800; padding: 0.1rem 0.35rem; border-radius: 4px; line-height: 1;">
                                                    <?php echo e($count); ?>x
                                                </span>
                                                <?php echo e($type); ?>

                                            </span>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </div>
                                <?php endif; ?>
                            </td>

                            <!-- Actions -->
                            <td style="text-align: center; vertical-align: middle;">
                                <div style="display: flex; flex-direction: column; gap: 0.45rem; align-items: stretch;">
                                    <?php if($ownStatus === 'Titip'): ?>
                                        <span class="badge badge-info" style="font-size: 0.75rem; padding: 0.35rem 0.6rem; display: inline-flex; width: 100%; justify-content: center;">
                                            Dititipkan
                                        </span>
                                    <?php elseif($pendingTitips->count() > 0): ?>
                                        <!-- Has entrusted packages -->
                                        <form action="<?php echo e(route('distribution.distribute-all', $p->id)); ?>" method="POST" style="display: block;">
                                            <?php echo csrf_field(); ?>
                                            <button type="submit" class="btn btn-primary btn-sm" style="width: 100%; justify-content: center; font-size: 0.75rem; padding: 0.5rem; font-weight: 700;">
                                                🎁 Serahkan Semua
                                            </button>
                                        </form>
                                        <div style="font-size: 0.65rem; color: var(--text-muted); font-weight: 500;">
                                            Total: <strong><?php echo e(($ownStatus !== 'Titip' ? 1 : 0) + $pendingTitips->count()); ?> Paket</strong>
                                        </div>
                                    <?php elseif($ownStatus !== 'Sudah'): ?>
                                        <!-- Single package handover -->
                                        <form action="<?php echo e(route('distribution.distribute', $p->id)); ?>" method="POST" style="display: block;">
                                            <?php echo csrf_field(); ?>
                                            <button type="submit" class="btn btn-primary btn-sm" style="width: 100%; justify-content: center; font-size: 0.75rem; padding: 0.5rem; font-weight: 700;">
                                                ✅ Serahkan
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <span class="badge badge-success" style="font-size: 0.75rem; padding: 0.35rem 0.6rem; display: inline-flex; width: 100%; justify-content: center;">
                                            Selesai
                                        </span>
                                    <?php endif; ?>

                                    <!-- Cancel queue check-in -->
                                    <form action="<?php echo e(route('distribution.cancel-queue', $p->id)); ?>" method="POST" style="display: block;">
                                        <?php echo csrf_field(); ?>
                                        <button type="submit" class="btn btn-danger btn-sm" style="width: 100%; justify-content: center; padding: 0.3rem 0.5rem; font-size: 0.7rem;" onclick="return confirm('Keluarkan <?php echo e($p->nama); ?> dari antrean?')">
                                            ❌ Batal Antre
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    document.addEventListener('DOMContentLoaded', () => {
        let timeLeft = 10;
        const secondsSpan = document.getElementById('seconds-left');
        
        const countdownInterval = setInterval(() => {
            timeLeft--;
            if (secondsSpan) {
                secondsSpan.textContent = timeLeft;
            }
            if (timeLeft <= 0) {
                clearInterval(countdownInterval);
                window.location.reload();
            }
        }, 1000);
    });
</script>

<style>
    @keyframes pulse {
        0% { transform: scale(1); opacity: 0.7; }
        50% { transform: scale(1.1); opacity: 1; }
        100% { transform: scale(1); opacity: 0.7; }
    }

    /* Premium display styles for TV screens */
    .table-custom th {
        font-size: 1.05rem !important;
        padding: 1.25rem 1rem !important;
        background-color: var(--primary-dark) !important;
        color: white !important;
        border-bottom: 3px solid var(--accent) !important;
    }
    
    .table-custom td {
        font-size: 1.05rem !important;
        padding: 1.25rem 1rem !important;
        vertical-align: middle !important;
    }

    .table-custom tr:hover {
        background-color: #f0faf5 !important;
    }

    .panel-title {
        font-size: 1.4rem !important;
    }
</style>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.full', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Users/sagara/Projects/distribusi-apps/resources/views/distribution/antrian.blade.php ENDPATH**/ ?>