<?php $__env->startSection('title', 'Paket Distribusi Perwakilan - Sistem Distribusi Qurban'); ?>

<?php $__env->startSection('header_title', 'Paket Distribusi Perwakilan'); ?>
<?php $__env->startSection('header_subtitle', 'Daftar penanggung jawab pengambilan paket titipan beserta rincian jatahnya'); ?>

<?php $__env->startSection('content'); ?>
<div class="panel">
    <div class="panel-header">
        <h2 class="panel-title"><span>📦</span> Rekapitulasi Paket Distribusi & Titipan</h2>
    </div>

    <!-- Filters -->
    <form action="<?php echo e(route('distribution.paket')); ?>" method="GET" class="filter-bar">
        <input type="text" name="search" class="form-control" placeholder="Cari Nama Perwakilan atau Nomor Kupon..." value="<?php echo e(request('search')); ?>">

        <select name="status" class="form-control" onchange="this.form.submit()">
            <option value="">-- Semua Status Pengambilan --</option>
            <option value="belum" <?php echo e($status === 'belum' ? 'selected' : ''); ?>>Belum Diambil</option>
            <option value="sudah" <?php echo e($status === 'sudah' ? 'selected' : ''); ?>>Sudah Diambil (Selesai)</option>
        </select>

        <div style="display: flex; align-items: center; gap: 0.5rem; margin-right: 0.5rem;">
            <label style="font-size: 0.85rem; font-weight: 600; color: var(--text-muted); cursor: pointer; display: flex; align-items: center; gap: 0.25rem;">
                <input type="checkbox" name="only_representatives" value="1" <?php echo e($onlyRepresentatives ? 'checked' : ''); ?> onchange="this.form.submit()" style="accent-color: var(--primary);"> 
                Hanya Tampilkan Perwakilan (Memiliki Titipan)
            </label>
        </div>

        <button type="submit" class="btn btn-primary btn-sm">Filter</button>
        <?php if(request()->anyFilled(['search', 'only_representatives', 'status'])): ?>
            <a href="<?php echo e(route('distribution.paket')); ?>" class="btn btn-secondary btn-sm">Reset</a>
        <?php endif; ?>
    </form>

    <!-- Table of representatives -->
    <?php if($participants->isEmpty()): ?>
        <div style="text-align: center; padding: 3rem 0; color: var(--text-muted);">
            <span style="font-size: 3rem; display: block; margin-bottom: 1rem;">👥</span>
            Tidak ditemukan data perwakilan paket qurban.
        </div>
    <?php else: ?>
        <div class="table-wrapper">
            <table class="table-custom">
                <thead>
                    <tr>
                        <th style="width: 20%;">Nama Perwakilan</th>
                        <th style="width: 10%;">Tipe</th>
                        <th style="width: 15%;">Status</th>
                        <th style="width: 22%;">List Titipan (Pemilik & Tipe)</th>
                        <th style="width: 18%;">Total Paket</th>
                        <th style="width: 15%; text-align: center;">Aksi Serah Terima</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $participants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $alloc = $p->getAllocation();
                            $ownStatus = $p->distribution ? $p->distribution->status : 'Belum';
                            $pendingTitips = $p->entrustedToMe->where('status', '!=', 'Selesai');
                            $hasPendingItems = ($ownStatus !== 'Sudah') || ($pendingTitips->count() > 0);
                        ?>
                        <tr>
                            <!-- Representative Name -->
                            <td>
                                <strong style="color: var(--primary-dark); font-size: 1.05rem; 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>

                            <!-- Tipe -->
                            <td>
                                <span style="font-weight: 700; color: var(--text-main); font-size: 0.85rem; background: var(--primary-light); padding: 0.2rem 0.5rem; border-radius: 4px;">
                                    <?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($p->entrustedToMe->isEmpty()): ?>
                                    <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 -->
                            <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>

                            <!-- Bulk release button -->
                            <td style="text-align: center; vertical-align: middle;">
                                <?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($hasPendingItems): ?>
                                    <form action="<?php echo e(route('distribution.distribute-all', $p->id)); ?>" method="POST">
                                        <?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;">
                                            🎁 Serahkan Semua
                                        </button>
                                    </form>
                                    <?php if($p->entrustedToMe->count() > 0): ?>
                                        <div style="font-size: 0.7rem; color: var(--text-muted); font-weight: 500; margin-top: 0.35rem;">
                                            Total: <strong><?php echo e(1 + $p->entrustedToMe->count()); ?> Paket</strong>
                                        </div>
                                    <?php endif; ?>
                                <?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;">
                                        ✅ Semua Selesai
                                    </span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>

        <div class="pagination-wrapper">
            <div style="font-size: 0.85rem; color: var(--text-muted);">
                Menampilkan <?php echo e($participants->firstItem()); ?> - <?php echo e($participants->lastItem()); ?> dari <?php echo e($participants->total()); ?> Data
            </div>
            <div>
                <?php echo e($participants->links()); ?>

            </div>
        </div>
    <?php endif; ?>
</div>
<?php $__env->stopSection(); ?>

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