<?php $__env->startSection('title', 'Invoices & Payments'); ?>
<?php $__env->startSection('page-title', 'Invoices & Payments'); ?>
<?php $__env->startSection('sidebar-nav'); ?>
<div class="nav-item"><a href="<?php echo e(route('admin.dashboard')); ?>"><i class="bi bi-grid"></i> Dashboard</a></div>
<div class="nav-item"><a href="<?php echo e(route('admin.fees.structure')); ?>"><i class="bi bi-cash-stack"></i> Fee Structure</a></div>
<div class="nav-item"><a href="<?php echo e(route('admin.fees.invoices')); ?>"><i class="bi bi-receipt"></i> Invoices</a></div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
<div class="page-header">
    <div><h1>Invoices & Payments</h1></div>
</div>

<div class="grid grid-3" style="margin-bottom:24px;">
    <div class="stat-card">
        <div class="stat-icon amber"><i class="bi bi-cash-stack"></i></div>
        <div class="stat-info">
            <h3>KES <?php echo e(number_format($summary['expected'])); ?></h3>
            <p>Expected</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon green"><i class="bi bi-check-circle"></i></div>
        <div class="stat-info">
            <h3>KES <?php echo e(number_format($summary['collected'])); ?></h3>
            <p>Collected (<?php echo e($summary['collection_rate']); ?>%)</p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon red"><i class="bi bi-exclamation-circle"></i></div>
        <div class="stat-info">
            <h3>KES <?php echo e(number_format($summary['balance'])); ?></h3>
            <p>Outstanding</p>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h5>Student Invoices</h5>
        <form method="GET" style="display:flex;gap:8px;">
            <select name="status" style="width:140px;">
                <option value="">All Status</option>
                <option value="unpaid" <?php echo e(request('status') == 'unpaid' ? 'selected' : ''); ?>>Unpaid</option>
                <option value="partial" <?php echo e(request('status') == 'partial' ? 'selected' : ''); ?>>Partial</option>
                <option value="paid" <?php echo e(request('status') == 'paid' ? 'selected' : ''); ?>>Paid</option>
            </select>
            <button type="submit" class="btn btn-sm btn-secondary">Filter</button>
        </form>
    </div>
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Student</th><th>Stream</th><th>Term</th><th>Total</th><th>Paid</th><th>Balance</th><th>Status</th><th>Action</th></tr>
            </thead>
            <tbody>
                <?php $__empty_1 = true; $__currentLoopData = $invoices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $invoice): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <tr>
                    <td>
                        <div style="font-weight:500;"><?php echo e($invoice->student->user->name); ?></div>
                        <div style="font-size:12px;color:var(--gray);"><?php echo e($invoice->student->admission_no); ?></div>
                    </td>
                    <td><?php echo e($invoice->student->stream?->schoolClass->name); ?> <?php echo e($invoice->student->stream?->name); ?></td>
                    <td><?php echo e($invoice->term->name); ?></td>
                    <td><?php echo e(number_format($invoice->total_amount, 2)); ?></td>
                    <td><?php echo e(number_format($invoice->amount_paid, 2)); ?></td>
                    <td style="color:<?php echo e($invoice->balance > 0 ? 'var(--danger)' : 'var(--success)'); ?>;">
                        <?php echo e(number_format($invoice->balance, 2)); ?>

                    </td>
                    <td>
                        <span class="badge <?php echo e($invoice->status === 'paid' ? 'badge-success' : ($invoice->status === 'partial' ? 'badge-warning' : 'badge-danger')); ?>">
                            <?php echo e(ucfirst($invoice->status)); ?>

                        </span>
                    </td>
                    <td>
                        <button onclick="openPayment(<?php echo e($invoice->id); ?>)" class="btn btn-sm btn-primary">
                            <i class="bi bi-plus"></i> Pay
                        </button>
                    </td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <tr><td colspan="8" style="text-align:center;padding:40px;color:var(--gray);">No invoices found</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <div style="padding:16px;"><?php echo e($invoices->links()); ?></div>
</div>

<!-- Payment Modal -->
<div id="paymentModal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:2000;align-items:center;justify-content:center;">
    <div class="card" style="width:100%;max-width:420px;margin:20px;">
        <div class="card-header">
            <h5>Record Payment</h5>
            <button onclick="document.getElementById('paymentModal').style.display='none'" style="background:none;border:none;cursor:pointer;font-size:20px;">&times;</button>
        </div>
        <div class="card-body">
            <form method="POST" action="<?php echo e(route('admin.fees.payment')); ?>">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="invoice_id" id="payInvoiceId">
                <div class="form-group">
                    <label>Amount (KES) *</label>
                    <input type="number" name="amount" min="1" step="0.01" required>
                </div>
                <div class="form-group">
                    <label>Payment Method *</label>
                    <select name="method" required>
                        <option value="cash">Cash</option>
                        <option value="mpesa">M-PESA</option>
                        <option value="bank">Bank Transfer</option>
                        <option value="cheque">Cheque</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Transaction Reference</label>
                    <input type="text" name="transaction_ref" placeholder="e.g. MPESA code">
                </div>
                <button type="submit" class="btn btn-primary">Record Payment</button>
            </form>
        </div>
    </div>
</div>

<?php $__env->startPush('scripts'); ?>
<script>
function openPayment(invoiceId) {
    document.getElementById('payInvoiceId').value = invoiceId;
    document.getElementById('paymentModal').style.display = 'flex';
}
</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:\xampp\htdocs\cbe\resources\views\admin\fees\invoices.blade.php ENDPATH**/ ?>