<?php
/**
 * Fee Dashboard — shows totals, recent payments, students with due fees
 *
 * Upload to: /gmind/branch/Views/fees/index.php
 */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$csrf = htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8');
?>
<style>
    .fee-stat-card {
        background: #fff; border-radius: 12px; padding: 22px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.04);
        display: flex; align-items: center; gap: 16px;
        margin-bottom: 18px; transition: transform 0.15s ease;
    }
    .fee-stat-card:hover { transform: translateY(-2px); box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
    .fee-stat-card .stat-icon {
        width: 56px; height: 56px; border-radius: 14px;
        display: flex; align-items: center; justify-content: center;
        font-size: 1.6rem; color: #fff; flex-shrink: 0;
    }
    .fee-stat-card .stat-num { font-size: 1.55rem; font-weight: 700; color: #2c1e4a; line-height: 1; }
    .fee-stat-card .stat-label { font-size: 0.85rem; color: #667085; margin-top: 4px; }
    .fee-table-card { background: #fff; border-radius: 12px; padding: 22px; box-shadow: 0 2px 8px rgba(0,0,0,0.04); margin-bottom: 18px; }
    .fee-table-card h3 { color: #2c1e4a; font-weight: 700; margin-bottom: 14px; font-size: 1.05rem; }
    .fee-table { width: 100%; border-collapse: collapse; }
    .fee-table thead th { background: #f8f4ff; color: #6c3fe8; font-weight: 700; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.5px; padding: 10px 12px; border-bottom: 2px solid #6c3fe8; text-align: left; }
    .fee-table tbody td { padding: 11px 12px; border-bottom: 1px solid #f1f1f1; font-size: 0.88rem; }
    .fee-table tbody tr:hover { background: #faf8ff; }
    .btn-fee-action { padding: 7px 14px; border-radius: 6px; font-size: 0.8rem; font-weight: 600; text-decoration: none; display: inline-flex; align-items: center; gap: 5px; }
    .btn-collect { background: #dcfce7; color: #16a34a; }
    .btn-collect:hover { background: #bbf7d0; }
    .btn-receipt { background: #dbeafe; color: #2563eb; }
    .btn-receipt:hover { background: #bfdbfe; }
    .btn-report { background: linear-gradient(135deg,#6c3fe8,#4a2db8); color: #fff; padding: 10px 22px; border-radius: 8px; font-weight: 600; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; }
    .btn-report:hover { color: #fff; transform: translateY(-1px); }
    .badge-mode { padding: 3px 10px; border-radius: 12px; font-size: 0.72rem; font-weight: 700; text-transform: uppercase; }
    .badge-cash    { background: #f3f4f6; color: #4b5563; }
    .badge-online  { background: #dbeafe; color: #2563eb; }
    .badge-upi     { background: #dcfce7; color: #16a34a; }
    .badge-wallet  { background: #fef3c7; color: #d97706; }
    .empty-state { text-align: center; padding: 40px 20px; color: #9ca3af; }
</style>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
        <div>
            <h2 style="color:#2c1e4a; font-weight:700; margin:0;"><i class="fas fa-rupee-sign"></i> Fee Management</h2>
            <p class="text-muted mb-0" style="font-size:0.88rem;">Track fee collections, generate receipts, and view reports.</p>
        </div>
        <a href="<?= $bp ?>/branch/fees/reports" class="btn-report">
            <i class="fas fa-chart-bar"></i> Fee Reports
        </a>
    </div>

    <!-- Stats Cards -->
    <div class="row">
        <div class="col-md-4">
            <div class="fee-stat-card">
                <div class="stat-icon" style="background:linear-gradient(135deg,#2563eb,#1d4ed8);"><i class="fas fa-wallet"></i></div>
                <div>
                    <div class="stat-num">&#8377; <?= number_format((float)$totalExpected, 2) ?></div>
                    <div class="stat-label">Total Expected</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="fee-stat-card">
                <div class="stat-icon" style="background:linear-gradient(135deg,#16a34a,#15803d);"><i class="fas fa-check-circle"></i></div>
                <div>
                    <div class="stat-num">&#8377; <?= number_format((float)$totalCollected, 2) ?></div>
                    <div class="stat-label">Total Collected</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="fee-stat-card">
                <div class="stat-icon" style="background:linear-gradient(135deg,#dc2626,#b91c1c);"><i class="fas fa-exclamation-circle"></i></div>
                <div>
                    <div class="stat-num">&#8377; <?= number_format((float)$totalDue, 2) ?></div>
                    <div class="stat-label">Total Due</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Students with Due Fees -->
    <div class="fee-table-card">
        <h3><i class="fas fa-user-clock" style="color:#dc2626;"></i> Students with Due Fees</h3>
        <?php if (!empty($studentsWithDue)): ?>
        <div class="table-responsive">
            <table class="fee-table">
                <thead>
                    <tr>
                        <th>Enrollment</th>
                        <th>Student</th>
                        <th>Course</th>
                        <th>Total Fee</th>
                        <th>Paid</th>
                        <th>Due</th>
                        <th style="text-align:right;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($studentsWithDue as $s): ?>
                    <tr>
                        <td><?= htmlspecialchars($s['enrollment_no'] ?? '', ENT_QUOTES) ?></td>
                        <td><?= htmlspecialchars($s['name'] ?? '', ENT_QUOTES) ?></td>
                        <td><?= htmlspecialchars($s['course_name'] ?? '—', ENT_QUOTES) ?></td>
                        <td>&#8377; <?= number_format((float)($s['course_fee'] ?? 0), 2) ?></td>
                        <td style="color:#16a34a; font-weight:600;">&#8377; <?= number_format((float)($s['fee_paid'] ?? 0), 2) ?></td>
                        <td style="color:#dc2626; font-weight:600;">&#8377; <?= number_format((float)($s['fee_due'] ?? 0), 2) ?></td>
                        <td style="text-align:right;">
                            <a href="<?= $bp ?>/branch/fees/collect/<?= (int)$s['id'] ?>" class="btn-fee-action btn-collect">
                                <i class="fas fa-hand-holding-usd"></i> Collect
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-check-circle" style="font-size:2.5rem; color:#16a34a;"></i>
            <p class="mt-2 mb-0" style="color:#16a34a; font-weight:600;">All fees paid! No dues.</p>
        </div>
        <?php endif; ?>
    </div>

    <!-- Recent Payments -->
    <div class="fee-table-card">
        <h3><i class="fas fa-history" style="color:#6c3fe8;"></i> Recent Payments (Last 10)</h3>
        <?php if (!empty($recentPayments)): ?>
        <div class="table-responsive">
            <table class="fee-table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Receipt No</th>
                        <th>Student</th>
                        <th>Course</th>
                        <th>Mode</th>
                        <th>Amount</th>
                        <th style="text-align:right;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recentPayments as $p): ?>
                    <tr>
                        <td><?= !empty($p['payment_date']) ? date('d M Y', strtotime($p['payment_date'])) : '—' ?></td>
                        <td><?= htmlspecialchars($p['receipt_no'] ?? ('RCP-'.$p['id']), ENT_QUOTES) ?></td>
                        <td><?= htmlspecialchars($p['student_name'] ?? '', ENT_QUOTES) ?></td>
                        <td><?= htmlspecialchars($p['course_name'] ?? '—', ENT_QUOTES) ?></td>
                        <td>
                            <?php
                            $mode = $p['payment_mode'] ?? 'cash';
                            $modeCls = 'badge-' . $mode;
                            ?>
                            <span class="badge-mode <?= $modeCls ?>"><?= htmlspecialchars(ucfirst($mode), ENT_QUOTES) ?></span>
                        </td>
                        <td style="font-weight:600; color:#16a34a;">&#8377; <?= number_format((float)$p['amount'], 2) ?></td>
                        <td style="text-align:right;">
                            <a href="<?= $bp ?>/branch/fees/receipt/<?= (int)$p['id'] ?>" target="_blank" class="btn-fee-action btn-receipt">
                                <i class="fas fa-file-pdf"></i> Receipt
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-inbox" style="font-size:2.5rem; opacity:0.3;"></i>
            <p class="mt-2 mb-0">No payments collected yet.</p>
        </div>
        <?php endif; ?>
    </div>
</div>
