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

<?php $__env->startSection('content'); ?>
<div class="space-y-5">
    
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-5">
        <div class="lg:col-span-2 bg-white rounded-3xl border border-slate-200 p-6 shadow-sm">
            <form method="GET" class="flex flex-wrap items-end gap-4">
                <div>
                    <label class="text-[10px] font-bold text-slate-400 uppercase tracking-widest block mb-1">Mulai</label>
                    <input type="date" name="start" value="<?php echo e($start->format('Y-m-d')); ?>" 
                        class="px-4 py-2 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                </div>
                <div>
                    <label class="text-[10px] font-bold text-slate-400 uppercase tracking-widest block mb-1">Selesai</label>
                    <input type="date" name="end" value="<?php echo e($end->format('Y-m-d')); ?>" 
                        class="px-4 py-2 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                </div>
                <button type="submit" class="px-6 py-2 bg-slate-900 text-white font-bold rounded-xl text-sm hover:bg-slate-800 transition">Filter</button>
            </form>
        </div>
        
        <div class="bg-indigo-600 rounded-3xl p-6 text-white shadow-xl shadow-indigo-600/20 flex flex-col justify-center">
            <div class="text-[10px] font-bold uppercase tracking-widest opacity-80 mb-1">Total Pembelian</div>
            <div class="text-3xl font-black">Rp <?php echo e(number_format($summary->total_amount, 0, ',', '.')); ?></div>
            <div class="text-xs mt-1 opacity-70"><?php echo e($summary->total_count); ?> faktur pembelian</div>
        </div>
    </div>

    
    <div class="bg-white rounded-3xl border border-slate-200 shadow-sm overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-slate-50 text-slate-500 border-b border-slate-200">
                        <th class="px-4 py-4 text-left font-bold uppercase tracking-tighter">Waktu</th>
                        <th class="px-4 py-4 text-left font-bold uppercase tracking-tighter">No. Faktur</th>
                        <th class="px-4 py-4 text-left font-bold uppercase tracking-tighter">Suplier</th>
                        <th class="px-4 py-4 text-left font-bold uppercase tracking-tighter text-rose-500">Jatuh Tempo</th>
                        <th class="px-4 py-4 text-right font-bold uppercase tracking-tighter">Total</th>
                        <th class="px-4 py-4 text-center font-bold uppercase tracking-tighter">Status</th>
                        <th class="px-4 py-4 text-center font-bold uppercase tracking-tighter">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    <?php $__empty_1 = true; $__currentLoopData = $purchases; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr class="hover:bg-slate-50/50 transition">
                        <td class="px-4 py-4">
                            <div class="font-bold text-slate-700"><?php echo e($p->purchase_date->format('d/m/Y')); ?></div>
                            <div class="text-[10px] text-slate-400"><?php echo e($p->purchase_date->format('H:i')); ?></div>
                        </td>
                        <td class="px-4 py-4 font-mono text-xs text-slate-500"><?php echo e($p->invoice_number); ?></td>
                        <td class="px-4 py-4 font-bold text-slate-700"><?php echo e($p->supplier->name); ?></td>
                        <td class="px-4 py-4 text-rose-500 font-bold">
                            <?php echo e($p->due_date ? $p->due_date->format('d/m/Y') : '-'); ?>

                        </td>
                        <td class="px-4 py-4 text-right font-black text-indigo-600">Rp <?php echo e(number_format($p->total_amount, 0, ',', '.')); ?></td>
                        <td class="px-4 py-4 text-center">
                            <?php if($p->payment_status === 'paid'): ?>
                                <span class="px-2 py-1 rounded-lg text-[10px] font-bold bg-emerald-100 text-emerald-600 uppercase">Lunas</span>
                            <?php else: ?>
                                <span class="px-2 py-1 rounded-lg text-[10px] font-bold bg-rose-100 text-rose-600 uppercase">Hutang</span>
                            <?php endif; ?>
                        </td>
                        <td class="px-4 py-4 text-center">
                            <?php if($p->payment_status === 'debt'): ?>
                            <button onclick="payPurchase(<?php echo e($p->id); ?>, '<?php echo e($p->invoice_number); ?>')" 
                                class="px-3 py-1.5 bg-emerald-500 text-white text-[10px] font-black uppercase tracking-widest rounded-lg hover:bg-emerald-600 transition shadow-lg shadow-emerald-500/20">
                                ✅ Lunaskan
                            </button>
                            <?php else: ?>
                            <span class="text-slate-300">Selesai</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr><td colspan="6" class="px-4 py-16 text-center text-slate-400">Belum ada data pembelian</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <div class="px-6 py-4 border-t border-slate-100">
            <?php echo e($purchases->links()); ?>

        </div>
    </div>
</div>
<?php $__env->startPush('scripts'); ?>
<script>
async function payPurchase(id, invoice) {
    if (!confirm(`Lunaskan pembayaran untuk faktur ${invoice}?`)) return;

    try {
        const res = await fetch(`/reports/purchases/${id}/pay`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': '<?php echo e(csrf_token()); ?>' }
        });
        const data = await res.json();
        if (data.success) {
            location.reload();
        } else {
            alert(data.message || 'Gagal melunasi pembayaran.');
        }
    } catch (e) {
        alert('Gagal menghubungi server.');
    }
}
</script>
<?php $__env->stopPush(); ?>
<?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/purchases.blade.php ENDPATH**/ ?>