<?php $__env->startSection('title', 'Laporan Kasir'); ?>
<?php $__env->startSection('page-title', 'Laporan Performa Kasir'); ?>

<?php $__env->startSection('content'); ?>
<div class="space-y-5">

    <?php if($selectedClerk): ?>
    
    <div class="flex items-center justify-between mb-2">
        <a href="<?php echo e(route('reports.clerk', request()->except('clerk_id'))); ?>" 
           class="px-4 py-2 bg-slate-100 hover:bg-slate-200 text-slate-700 font-bold rounded-xl text-sm transition flex items-center gap-2 border border-slate-200">
            ← Kembali ke Daftar Kasir
        </a>
        <div class="text-right">
            <h2 class="text-xl font-black text-slate-800"><?php echo e($selectedClerk->name); ?></h2>
            <p class="text-xs text-slate-500 uppercase tracking-widest font-bold">Detail Riwayat Sesi</p>
        </div>
    </div>

    <div class="bg-white rounded-2xl border border-slate-200 shadow-sm overflow-hidden">
        <div class="px-5 py-4 border-b border-slate-200 flex items-center justify-between bg-slate-50">
            <h3 class="font-bold text-slate-800">📋 Riwayat Sesi: <?php echo e($start->format('d M')); ?> — <?php echo e($end->format('d M Y')); ?></h3>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead class="bg-slate-100">
                    <tr class="text-[10px] text-slate-500 uppercase tracking-wider">
                        <th class="text-left px-4 py-4">Waktu Buka</th>
                        <th class="text-left px-4 py-4">Waktu Tutup</th>
                        <th class="text-right px-4 py-4">Modal Awal</th>
                        <th class="text-right px-4 py-4">Total Jual</th>
                        <th class="text-right px-4 py-4">Kas Aktual</th>
                        <th class="text-right px-4 py-4">Selisih</th>
                        <th class="text-center px-4 py-4">Status</th>
                        <th class="text-center px-4 py-4">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    <?php $__empty_1 = true; $__currentLoopData = $clerkSessions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sess): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr class="hover:bg-slate-50 transition">
                        <td class="px-4 py-4 font-medium text-slate-700">
                            <?php echo e($sess->opened_at->format('d/m/Y')); ?>

                            <div class="text-[10px] text-slate-400"><?php echo e($sess->opened_at->format('H:i')); ?></div>
                        </td>
                        <td class="px-4 py-4 text-slate-500">
                            <?php echo e($sess->closed_at ? $sess->closed_at->format('d/m/Y') : '—'); ?>

                            <?php if($sess->closed_at): ?>
                            <div class="text-[10px] text-slate-400"><?php echo e($sess->closed_at->format('H:i')); ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="px-4 py-4 text-right text-slate-600 font-mono text-xs">Rp <?php echo e(number_format($sess->opening_cash,0,',','.')); ?></td>
                        <td class="px-4 py-4 text-right text-blue-600 font-bold font-mono text-xs">Rp <?php echo e(number_format($sess->total_sales,0,',','.')); ?></td>
                        <td class="px-4 py-4 text-right font-mono text-slate-800 text-xs"><?php echo e($sess->closing_cash !== null ? 'Rp '.number_format($sess->closing_cash,0,',','.') : '—'); ?></td>
                        <td class="px-4 py-4 text-right">
                            <?php if($sess->cash_difference !== null): ?>
                            <span class="font-black px-2 py-1 rounded-lg text-[10px] <?php echo e($sess->cash_difference >= 0 ? 'bg-emerald-50 text-emerald-600' : 'bg-red-50 text-red-600'); ?>">
                                <?php echo e($sess->cash_difference >= 0 ? '+' : ''); ?>Rp <?php echo e(number_format($sess->cash_difference,0,',','.')); ?>

                            </span>
                            <?php else: ?> —
                            <?php endif; ?>
                        </td>
                        <td class="px-4 py-4 text-center">
                            <?php if($sess->status == 'open'): ?>
                            <span class="px-2 py-0.5 rounded-full bg-blue-100 text-blue-700 text-[10px] font-bold uppercase tracking-widest animate-pulse">Aktif</span>
                            <?php else: ?>
                            <span class="px-2 py-0.5 rounded-full bg-slate-100 text-slate-500 text-[10px] font-bold uppercase tracking-widest">Selesai</span>
                            <?php endif; ?>
                        </td>
                        <td class="px-4 py-4 text-center">
                            <?php
                                $sessionJson = [
                                    'id'       => $sess->id,
                                    'opened'   => $sess->opened_at->format('d/m/Y H:i'),
                                    'closed'   => $sess->closed_at ? $sess->closed_at->format('d/m/Y H:i') : '-',
                                    'opening'  => (float)$sess->opening_cash,
                                    'total'    => (float)$sess->total_sales,
                                    'cash'     => (float)$sess->total_cash,
                                    'qris'     => (float)$sess->total_qris,
                                    'transfer' => (float)$sess->total_transfer,
                                    'expected' => (float)$sess->expected_cash,
                                    'actual'   => (float)$sess->closing_cash,
                                    'diff'     => (float)$sess->cash_difference,
                                    'notes'    => $sess->closing_notes,
                                    'expenses' => $sess->expenses,
                                    'purchases' => $sess->purchases->where('payment_status', 'paid')->map(function($p){ 
                                        return [
                                            'total_amount' => $p->total_amount, 
                                            'supplier' => ['name' => $p->supplier->name ?? '-'], 
                                            'invoice_number' => $p->invoice_number
                                        ]; 
                                    })->values()
                                ];
                            ?>
                            <button onclick='showDetailModal(<?php echo json_encode($sessionJson, 15, 512) ?>)' 
                                class="p-2 hover:bg-blue-50 text-blue-500 rounded-lg transition" title="Lihat Detail">
                                🔍
                            </button>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr><td colspan="9" class="px-4 py-12 text-center text-slate-400">Tidak ada riwayat sesi pada periode ini</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    
    <div class="bg-white rounded-2xl border border-slate-200 shadow-sm overflow-hidden border-t-4 border-t-red-500">
        <div class="px-5 py-4 border-b border-slate-200 flex items-center justify-between bg-red-50/50">
            <h3 class="font-bold text-red-700 flex items-center gap-2">
                <span>🚫</span> Log Aktivitas Void (Pembatalan Item)
            </h3>
            <span class="px-2 py-1 bg-red-100 text-red-600 rounded-lg text-[10px] font-black uppercase"><?php echo e($voidLogs->count()); ?> Aktivitas</span>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead class="bg-slate-50">
                    <tr class="text-[10px] text-slate-400 uppercase tracking-widest">
                        <th class="text-left px-4 py-3">Waktu</th>
                        <th class="text-left px-4 py-3">Jenis Tindakan</th>
                        <th class="text-left px-4 py-3">Produk</th>
                        <th class="text-right px-4 py-3">Qty / Nilai</th>
                        <th class="text-left px-4 py-3">Otoritas</th>
                        <th class="text-left px-4 py-3">Alasan</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    <?php $__empty_1 = true; $__currentLoopData = $voidLogs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $log): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr class="hover:bg-red-50/20 transition">
                        <td class="px-4 py-3 text-xs text-slate-500">
                            <?php echo e($log->created_at->format('d/m/Y H:i')); ?>

                        </td>
                        <td class="px-4 py-3">
                            <span class="text-[9px] font-bold uppercase px-1.5 py-0.5 rounded bg-slate-100 text-slate-600">
                                <?php echo e(str_replace('_', ' ', $log->type)); ?>

                            </span>
                        </td>
                        <td class="px-4 py-3 font-medium text-slate-700">
                            <?php echo e($log->product_name); ?>

                        </td>
                        <td class="px-4 py-3 text-right">
                            <div class="text-xs font-bold text-slate-800"><?php echo e($log->quantity); ?>x</div>
                            <div class="text-[10px] text-slate-400 line-through">Rp <?php echo e(number_format($log->amount, 0, ',', '.')); ?></div>
                        </td>
                        <td class="px-4 py-3 text-xs">
                            <span class="font-bold text-blue-600"><?php echo e($log->authorizedBy?->name ?? 'Admin'); ?></span>
                        </td>
                        <td class="px-4 py-3">
                            <div class="text-[10px] text-slate-500 italic max-w-[150px] truncate" title="<?php echo e($log->reason); ?>">
                                "<?php echo e($log->reason); ?>"
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="6" class="px-4 py-8 text-center text-slate-300 italic">
                            Tidak ada aktivitas void yang terekam.
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    
    <div id="detailSesiModal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-start justify-center p-4 overflow-y-auto hidden">
        <div class="bg-white rounded-3xl shadow-2xl w-full max-w-md my-auto overflow-hidden animate-in fade-in zoom-in duration-200">
            <div class="bg-slate-900 px-6 py-6 text-white flex items-center justify-between">
                <div>
                    <h3 class="text-lg font-black tracking-tight">DETAIL SESI KASIR</h3>
                    <p class="text-[10px] text-slate-400 uppercase tracking-widest font-bold" id="modalSessId"></p>
                </div>
                <button onclick="hideDetailModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-white/10 text-white transition">✕</button>
            </div>
            <div class="p-6 space-y-6">
                
                <div class="grid grid-cols-2 gap-4 text-xs">
                    <div>
                        <div class="text-slate-400 font-bold uppercase tracking-tighter">Buka Sesi</div>
                        <div id="modalOpened" class="font-bold text-slate-700"></div>
                    </div>
                    <div>
                        <div class="text-slate-400 font-bold uppercase tracking-tighter text-right">Tutup Sesi</div>
                        <div id="modalClosed" class="font-bold text-slate-700 text-right"></div>
                    </div>
                </div>

                
                <div class="bg-slate-50 rounded-2xl p-5 border border-slate-100 space-y-3">
                    <div class="flex justify-between items-center pb-2 border-b border-slate-200">
                        <span class="text-xs font-black text-slate-800 uppercase tracking-widest">Total Penjualan</span>
                        <span id="modalTotal" class="text-lg font-black text-blue-600"></span>
                    </div>
                    <div class="space-y-1.5">
                        <div class="flex justify-between text-xs">
                            <span class="text-slate-500">💵 Tunai (Sales)</span>
                            <span id="modalCash" class="font-bold text-slate-700"></span>
                        </div>
                        <div class="flex justify-between text-xs">
                            <span class="text-slate-500">📱 QRIS</span>
                            <span id="modalQris" class="font-bold text-slate-700"></span>
                        </div>
                        <div class="flex justify-between text-xs">
                            <span class="text-slate-500">🏦 Transfer</span>
                            <span id="modalTransfer" class="font-bold text-slate-700"></span>
                        </div>
                    </div>
                </div>

                
                <div class="grid grid-cols-2 gap-4">
                    <div class="space-y-1">
                        <div class="text-[9px] font-black text-slate-400 uppercase tracking-widest">Modal Awal</div>
                        <div id="modalOpening" class="text-sm font-bold text-slate-700"></div>
                    </div>
                    <div class="space-y-1 text-right">
                        <div class="text-[9px] font-black text-slate-400 uppercase tracking-widest">Kas Diharapkan</div>
                        <div id="modalExpected" class="text-sm font-bold text-slate-700"></div>
                    </div>
                </div>

                <div id="modalDiffContainer" class="rounded-2xl p-5 text-center">
                    <div class="text-[9px] font-black uppercase tracking-widest mb-1 opacity-70">Selisih Kas (Aktual)</div>
                    <div id="modalDiff" class="text-2xl font-black"></div>
                    <div id="modalActual" class="text-xs font-bold opacity-60 mt-1"></div>
                </div>

                <div id="modalNotesContainer" class="text-xs">
                    <div class="text-slate-400 font-bold uppercase tracking-tighter mb-1">Catatan Penutupan:</div>
                    <div id="modalNotes" class="p-3 bg-slate-50 rounded-xl italic text-slate-600 border border-slate-100"></div>
                </div>

                
                <div class="pt-4 border-t border-slate-100">
                    <div class="text-[10px] font-black text-slate-400 uppercase tracking-widest mb-3">Detail Pengeluaran & Stok (Tunai)</div>
                    <div id="modalDetailsList" class="space-y-2 max-h-48 overflow-y-auto pr-1"></div>
                </div>

                <button onclick="hideDetailModal()" class="w-full py-4 bg-slate-900 text-white font-black rounded-2xl text-sm uppercase tracking-widest shadow-xl shadow-slate-900/20 active:scale-95 transition">Tutup Detail</button>
            </div>
        </div>
    </div>

    <script>
        function rp(num) {
            return 'Rp ' + new Intl.NumberFormat('id-ID').format(num || 0);
        }
        function showDetailModal(data) {
            document.getElementById('modalSessId').textContent = 'ID Sesi: #' + data.id;
            document.getElementById('modalOpened').textContent = data.opened;
            document.getElementById('modalClosed').textContent = data.closed;
            document.getElementById('modalOpening').textContent = rp(data.opening);
            document.getElementById('modalTotal').textContent = rp(data.total);
            document.getElementById('modalCash').textContent = rp(data.cash);
            document.getElementById('modalQris').textContent = rp(data.qris);
            document.getElementById('modalTransfer').textContent = rp(data.transfer);
            document.getElementById('modalExpected').textContent = rp(data.expected);
            document.getElementById('modalActual').textContent = 'Kas Aktual: ' + rp(data.actual);
            
            const diff = data.diff;
            const resDiff = document.getElementById('modalDiff');
            const resContainer = document.getElementById('modalDiffContainer');
            const sign = diff > 0 ? '+' : '';
            resDiff.textContent = sign + rp(diff);
            
            if (diff === 0) {
                resDiff.className = 'text-2xl font-black text-blue-600';
                resContainer.className = 'rounded-2xl p-5 text-center bg-blue-50 border border-blue-100';
            } else if (diff > 0) {
                resDiff.className = 'text-2xl font-black text-emerald-600';
                resContainer.className = 'rounded-2xl p-5 text-center bg-emerald-50 border border-emerald-100';
            } else {
                resDiff.className = 'text-2xl font-black text-red-600';
                resContainer.className = 'rounded-2xl p-5 text-center bg-red-50 border border-red-100';
            }

            const notesEl = document.getElementById('modalNotes');
            if (data.notes && data.notes !== 'null') {
                notesEl.textContent = data.notes;
                document.getElementById('modalNotesContainer').classList.remove('hidden');
            } else {
                document.getElementById('modalNotesContainer').classList.add('hidden');
            }

            // Render Details
            let detailHtml = '';
            if (data.expenses && data.expenses.length > 0) {
                data.expenses.forEach(e => {
                    detailHtml += `
                        <div class="flex justify-between text-[10px] bg-rose-50 p-2 rounded-lg border border-rose-100">
                            <div class="flex flex-col">
                                <span class="font-bold text-rose-700">${e.category}</span>
                                <span class="text-rose-400 truncate w-32">${e.description}</span>
                            </div>
                            <span class="font-black text-rose-600">-${rp(e.amount)}</span>
                        </div>
                    `;
                });
            }

            if (data.purchases && data.purchases.length > 0) {
                data.purchases.forEach(p => {
                    detailHtml += `
                        <div class="flex justify-between text-[10px] bg-amber-50 p-2 rounded-lg border border-amber-100">
                            <div class="flex flex-col">
                                <span class="font-bold text-amber-700">📦 Stok: ${p.supplier.name}</span>
                                <span class="text-amber-400 truncate w-40">${p.invoice_number}</span>
                            </div>
                            <span class="font-black text-amber-600">-${rp(p.total_amount)}</span>
                        </div>
                    `;
                });
            }

            if (!detailHtml) {
                detailHtml = '<p class="text-center text-[10px] text-slate-400 py-4 italic">Tidak ada transaksi kas khusus pada sesi ini.</p>';
            }
            document.getElementById('modalDetailsList').innerHTML = detailHtml;

            document.getElementById('detailSesiModal').classList.remove('hidden');
        }
        function hideDetailModal() {
            document.getElementById('detailSesiModal').classList.add('hidden');
        }
    </script>

    <?php else: ?>
    
    
    <div class="bg-white rounded-2xl border border-slate-200 p-4 shadow-sm">
        <form method="GET" class="flex flex-wrap items-end gap-3">
            <div>
                <label class="block text-xs font-semibold text-slate-600 mb-1">Dari Tanggal</label>
                <input type="date" name="start" value="<?php echo e($start->format('Y-m-d')); ?>"
                    class="px-3 py-2 border border-slate-200 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-blue-400">
            </div>
            <div>
                <label class="block text-xs font-semibold text-slate-600 mb-1">Sampai Tanggal</label>
                <input type="date" name="end" value="<?php echo e($end->format('Y-m-d')); ?>"
                    class="px-3 py-2 border border-slate-200 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-blue-400">
            </div>
            <button type="submit" class="px-5 py-2 bg-slate-900 hover:bg-slate-800 text-white font-bold rounded-xl text-sm transition shadow-lg shadow-slate-900/20 flex items-center gap-2">
                <span>🔍</span> Terapkan Filter
            </button>
        </form>
    </div>

    
    <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
        <?php $__empty_1 = true; $__currentLoopData = $clerkStats; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $clerk): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
        <div class="bg-white rounded-3xl border border-slate-200 shadow-sm overflow-hidden hover:shadow-xl transition-all duration-300 group">
            <div class="px-6 py-5 flex items-center gap-4 border-b border-slate-100 bg-gradient-to-r from-white to-slate-50/50">
                <div class="w-12 h-12 rounded-2xl bg-slate-900 flex items-center justify-center text-white font-black text-xl shadow-lg shadow-slate-900/20 group-hover:scale-110 transition">
                    <?php echo e(strtoupper(substr($clerk['name'], 0, 1))); ?>

                </div>
                <div>
                    <div class="font-black text-slate-800 text-lg leading-tight"><?php echo e($clerk['name']); ?></div>
                    <div class="text-[10px] text-slate-400 uppercase tracking-widest font-bold">KLEREK / <?php echo e(strtoupper($clerk['username'])); ?></div>
                </div>
                
                <?php if($clerk['total_sessions'] > 0): ?>
                <div class="ml-auto text-right">
                    <?php if($clerk['cash_difference'] > 0): ?>
                    <span class="text-emerald-600 font-black text-sm">+<?php echo e(number_format($clerk['cash_difference'], 0, ',', '.')); ?></span>
                    <?php elseif($clerk['cash_difference'] < 0): ?>
                    <span class="text-red-600 font-black text-sm"><?php echo e(number_format($clerk['cash_difference'], 0, ',', '.')); ?></span>
                    <?php else: ?>
                    <span class="text-slate-400 font-black text-sm">±0</span>
                    <?php endif; ?>
                    <div class="text-[8px] text-slate-400 uppercase tracking-tighter font-bold">selisih kas</div>
                </div>
                <?php endif; ?>
            </div>
            <div class="p-6 grid grid-cols-2 gap-4">
                <div class="bg-blue-50/50 rounded-2xl p-4 text-center border border-blue-100/50">
                    <div class="text-2xl font-black text-blue-600"><?php echo e($clerk['total_trx']); ?></div>
                    <div class="text-[10px] text-blue-400 font-bold uppercase">Transaksi</div>
                </div>
                <div class="bg-emerald-50/50 rounded-2xl p-4 text-center border border-emerald-100/50">
                    <div class="text-sm font-black text-emerald-600">Rp <?php echo e(number_format($clerk['total_sales'], 0, ',', '.')); ?></div>
                    <div class="text-[10px] text-emerald-400 font-bold uppercase">Total Jual</div>
                </div>
                <div class="col-span-2 grid grid-cols-2 gap-2 pt-2 border-t border-slate-50">
                    <div>
                        <div class="text-[9px] text-slate-400 font-bold uppercase">Rata-rata</div>
                        <div class="text-xs font-bold text-slate-700">Rp <?php echo e(number_format($clerk['avg_trx'], 0, ',', '.')); ?></div>
                    </div>
                    <div>
                        <div class="text-[9px] text-slate-400 font-bold uppercase">Sesi Kasir</div>
                        <div class="text-xs font-bold text-slate-700"><?php echo e($clerk['total_sessions']); ?> sesi</div>
                    </div>
                </div>
            </div>
            <div class="px-6 pb-6">
                <a href="<?php echo e(route('reports.clerk', array_merge(request()->all(), ['clerk_id' => $clerk['id']]))); ?>"
                   class="block w-full py-3 text-center text-[11px] font-black uppercase tracking-widest bg-slate-50 hover:bg-slate-900 hover:text-white rounded-2xl transition-all border border-slate-200">
                    📋 Detail Sesi Kasir
                </a>
            </div>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
        <div class="col-span-full bg-white rounded-3xl border border-slate-200 p-16 text-center text-slate-400">
            <div class="text-6xl mb-4">👤</div>
            <div class="font-bold text-lg">Tidak ada data klerek</div>
            <p class="text-sm">Silakan sesuaikan periode filter Anda.</p>
        </div>
        <?php endif; ?>
    </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 C:\laragon\www\pos-umkm\resources\views/reports/clerk.blade.php ENDPATH**/ ?>