<?php
/**
 * Fee Reports — Course-wise breakdown with filter + PDF export
 *
 * Upload to: /gmind/branch/Views/fees/reports.php
 */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$csrf = htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8');

$grandPercent = $grandFee > 0 ? round(($grandPaid / $grandFee) * 100, 1) : 0;
?>
<style>
    .report-stats-card { background:#fff; border-radius:12px; padding:18px 22px; box-shadow:0 2px 8px rgba(0,0,0,0.04); display:flex; align-items:center; gap:14px; margin-bottom:16px; }
    .report-stats-card .stats-icon { width:48px; height:48px; border-radius:12px; display:flex; align-items:center; justify-content:center; font-size:1.3rem; color:#fff; flex-shrink:0; }
    .report-stats-card .stats-num { font-size:1.4rem; font-weight:700; color:#2c1e4a; line-height:1; }
    .report-stats-card .stats-label { font-size:0.82rem; color:#667085; margin-top:4px; }
    .report-table-card { background:#fff; border-radius:12px; padding:22px; box-shadow:0 2px 8px rgba(0,0,0,0.04); }
    .report-table { width:100%; border-collapse:collapse; }
    .report-table thead th { background:#f8f4ff; color:#6c3fe8; font-weight:700; font-size:0.78rem; text-transform:uppercase; letter-spacing:0.5px; padding:12px 14px; border-bottom:2px solid #6c3fe8; }
    .report-table tbody td { padding:12px 14px; border-bottom:1px solid #f1f1f1; font-size:0.9rem; }
    .report-table tbody tr:hover { background:#faf8ff; }
    .report-table tfoot td { padding:14px; background:#fef3c7; color:#92400e; font-weight:700; border-top:2px solid #d97706; }
    .progress-wrap { display:flex; align-items:center; gap:8px; }
    .progress-bar { flex:1; height:8px; background:#e5e7eb; border-radius:4px; overflow:hidden; min-width:80px; }
    .progress-fill { height:100%; background:linear-gradient(90deg,#16a34a,#15803d); border-radius:4px; }
    .progress-text { font-size:0.78rem; font-weight:700; color:#16a34a; min-width:42px; }
    .btn-pdf { background:linear-gradient(135deg,#dc2626,#b91c1c); color:#fff; padding:10px 22px; border-radius:8px; font-weight:600; text-decoration:none; display:inline-flex; align-items:center; gap:8px; }
    .btn-pdf:hover { color:#fff; transform:translateY(-1px); }
    .btn-back { background:#f3f4f6; color:#6b7280; padding:10px 18px; border-radius:8px; text-decoration:none; font-weight:600; display:inline-flex; align-items:center; gap:6px; }
    .filter-form { background:#fff; border-radius:12px; padding:18px; box-shadow:0 2px 8px rgba(0,0,0,0.04); margin-bottom:18px; }
    .filter-form .form-label { font-weight:600; color:#2c1e4a; font-size:0.85rem; margin-bottom:4px; }
    .filter-form .form-control, .filter-form .form-select { border-radius:8px; padding:8px 12px; }
</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-chart-bar"></i> Fee Reports</h2>
            <p class="text-muted mb-0" style="font-size:0.88rem;">Course-wise fee collection breakdown with totals.</p>
        </div>
        <div class="d-flex gap-2 flex-wrap">
            <a href="<?= $bp ?>/branch/fees" class="btn-back"><i class="fas fa-arrow-left"></i> Back</a>
            <a href="<?= $bp ?>/branch/fees/export-pdf?course_id=<?= urlencode($courseFilter ?? '') ?>" target="_blank" class="btn-pdf">
                <i class="fas fa-file-pdf"></i> Download PDF
            </a>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row">
        <div class="col-md-3 col-6">
            <div class="report-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#2563eb,#1d4ed8);"><i class="fas fa-users"></i></div>
                <div>
                    <div class="stats-num"><?= (int)$grandStudents ?></div>
                    <div class="stats-label">Total Students</div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-6">
            <div class="report-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#6c3fe8,#4a2db8);"><i class="fas fa-wallet"></i></div>
                <div>
                    <div class="stats-num">&#8377; <?= number_format((float)$grandFee, 0) ?></div>
                    <div class="stats-label">Total Expected</div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-6">
            <div class="report-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#16a34a,#15803d);"><i class="fas fa-check-circle"></i></div>
                <div>
                    <div class="stats-num">&#8377; <?= number_format((float)$grandPaid, 0) ?></div>
                    <div class="stats-label">Total Collected</div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-6">
            <div class="report-stats-card">
                <div class="stats-icon" style="background:linear-gradient(135deg,#dc2626,#b91c1c);"><i class="fas fa-exclamation-circle"></i></div>
                <div>
                    <div class="stats-num">&#8377; <?= number_format((float)$grandDue, 0) ?></div>
                    <div class="stats-label">Total Due</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter Form -->
    <form method="GET" action="<?= $bp ?>/branch/fees/reports" class="filter-form">
        <div class="row align-items-end">
            <div class="col-md-4">
                <label class="form-label">Filter by Course</label>
                <select name="course_id" class="form-select">
                    <option value="">All Courses</option>
                    <?php foreach ($courses as $c): ?>
                    <option value="<?= (int)$c['id'] ?>" <?= ($courseFilter == (string)$c['id']) ? 'selected' : '' ?>>
                        <?= htmlspecialchars($c['name'], ENT_QUOTES) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-4">
                <button type="submit" class="btn btn-primary" style="background:#6c3fe8; border-color:#6c3fe8; padding:8px 22px;">
                    <i class="fas fa-filter"></i> Apply Filter
                </button>
                <?php if (!empty($courseFilter)): ?>
                <a href="<?= $bp ?>/branch/fees/reports" class="btn btn-secondary" style="padding:8px 18px;">Clear</a>
                <?php endif; ?>
            </div>
        </div>
    </form>

    <!-- Course-wise Table -->
    <div class="report-table-card">
        <h3 style="color:#2c1e4a; font-weight:700; margin-bottom:14px; font-size:1.05rem;">
            <i class="fas fa-book"></i> Course-wise Fee Collection
        </h3>
        <?php if (!empty($feeByCourse)): ?>
        <div class="table-responsive">
            <table class="report-table">
                <thead>
                    <tr>
                        <th>Course Name</th>
                        <th>Students</th>
                        <th>Total Fee</th>
                        <th>Total Paid</th>
                        <th>Total Due</th>
                        <th style="width:200px;">Collection %</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($feeByCourse as $row):
                        $totalFee = (float)($row['total_fee'] ?? 0);
                        $totalPaid = (float)($row['total_paid'] ?? 0);
                        $totalDue = (float)($row['total_due'] ?? 0);
                        $collectionPercent = $totalFee > 0 ? round(($totalPaid / $totalFee) * 100, 1) : 0;
                    ?>
                    <tr>
                        <td style="font-weight:600; color:#2c1e4a;"><?= htmlspecialchars($row['course_name'] ?? '', ENT_QUOTES) ?></td>
                        <td><?= (int)($row['student_count'] ?? 0) ?></td>
                        <td>&#8377; <?= number_format($totalFee, 2) ?></td>
                        <td style="color:#16a34a; font-weight:600;">&#8377; <?= number_format($totalPaid, 2) ?></td>
                        <td style="color: <?= $totalDue > 0 ? '#dc2626' : '#16a34a' ?>; font-weight:600;">&#8377; <?= number_format($totalDue, 2) ?></td>
                        <td>
                            <div class="progress-wrap">
                                <div class="progress-bar">
                                    <div class="progress-fill" style="width: <?= min($collectionPercent, 100) ?>%"></div>
                                </div>
                                <span class="progress-text"><?= $collectionPercent ?>%</span>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td>GRAND TOTAL</td>
                        <td><?= (int)$grandStudents ?></td>
                        <td>&#8377; <?= number_format((float)$grandFee, 2) ?></td>
                        <td>&#8377; <?= number_format((float)$grandPaid, 2) ?></td>
                        <td>&#8377; <?= number_format((float)$grandDue, 2) ?></td>
                        <td><strong style="font-size:1.05rem;"><?= $grandPercent ?>%</strong></td>
                    </tr>
                </tfoot>
            </table>
        </div>
        <?php else: ?>
        <div style="text-align:center; padding:50px 20px; color:#9ca3af;">
            <i class="fas fa-inbox" style="font-size:3rem; opacity:0.3;"></i>
            <p class="mt-3 mb-0">No fee data available. Add students and collect fees to see reports.</p>
        </div>
        <?php endif; ?>
    </div>
</div>
