<?php
require_once('../config.php');
include('../header/header.php');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header("Location: dashboard.php");
    exit();
}

// Summary statistics
$total_students = $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'student'")->fetchColumn();
$total_courses = $pdo->query("SELECT COUNT(*) FROM courses")->fetchColumn();
$total_paid = $pdo->query("SELECT SUM(amount_paid) FROM payments")->fetchColumn();
$total_arrears = $pdo->query("SELECT SUM(total_fee - amount_paid) FROM payments")->fetchColumn();

// Fetch performance report (student results)
$stmt = $pdo->query("SELECT r.*, u.username as student_name, c.course_name FROM results r JOIN users u ON r.student_id = u.id JOIN courses c ON r.course_id = c.id ORDER BY r.created_at DESC");
$results = $stmt->fetchAll();

// Fetch payment report summary
$stmt = $pdo->query("SELECT p.*, u.username as student_name FROM payments p JOIN users u ON p.student_id = u.id ORDER BY p.payment_date DESC");
$payments = $stmt->fetchAll();
?>
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">System Performance & Fee Reports</h2>
        <div class="d-flex gap-2">
            <button onclick="window.print()" class="btn btn-outline-primary btn-sm">
                <i class="fas fa-print me-2"></i>Print Report
            </button>
            <span class="badge bg-dark px-3 py-2 rounded-pill">Admin View</span>
        </div>
    </div>
    
    <!-- Statistics Summary Cards -->
    <div class="row g-4 mb-5">
        <div class="col-md-3">
            <div class="dash-card bg-white p-3 shadow-sm border-start border-4 border-primary h-100">
                <div class="d-flex align-items-center">
                    <div class="icon-box bg-primary bg-opacity-10 text-primary me-3">
                        <i class="fas fa-users-graduate"></i>
                    </div>
                    <div>
                        <p class="text-muted small text-uppercase mb-0 fw-bold">Total Students</p>
                        <h4 class="fw-bold mb-0"><?php echo $total_students; ?></h4>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="dash-card bg-white p-3 shadow-sm border-start border-4 border-success h-100">
                <div class="d-flex align-items-center">
                    <div class="icon-box bg-success bg-opacity-10 text-success me-3">
                        <i class="fas fa-money-bill-wave"></i>
                    </div>
                    <div>
                        <p class="text-muted small text-uppercase mb-0 fw-bold">Fees Collected</p>
                        <h4 class="fw-bold mb-0">KES <?php echo number_format($total_paid, 0); ?></h4>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="dash-card bg-white p-3 shadow-sm border-start border-4 border-danger h-100">
                <div class="d-flex align-items-center">
                    <div class="icon-box bg-danger bg-opacity-10 text-danger me-3">
                        <i class="fas fa-hand-holding-usd"></i>
                    </div>
                    <div>
                        <p class="text-muted small text-uppercase mb-0 fw-bold">Total Arrears</p>
                        <h4 class="fw-bold mb-0">KES <?php echo number_format($total_arrears, 0); ?></h4>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="dash-card bg-white p-3 shadow-sm border-start border-4 border-info h-100">
                <div class="d-flex align-items-center">
                    <div class="icon-box bg-info bg-opacity-10 text-info me-3">
                        <i class="fas fa-book-reader"></i>
                    </div>
                    <div>
                        <p class="text-muted small text-uppercase mb-0 fw-bold">Active Courses</p>
                        <h4 class="fw-bold mb-0"><?php echo $total_courses; ?></h4>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Performance Report -->
        <div class="col-lg-6 mb-4">
            <div class="card glass-card border-0 shadow-sm h-100">
                <div class="card-header bg-white py-3 border-0 d-flex justify-content-between align-items-center">
                    <h5 class="fw-bold mb-0">Student Performance</h5>
                    <i class="fas fa-chart-line text-muted"></i>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="ps-4">Student</th>
                                    <th>Course</th>
                                    <th>Score</th>
                                    <th class="pe-4 text-end">Grade</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($results as $result): ?>
                                    <tr>
                                        <td class="ps-4 fw-bold text-dark"><?php echo htmlspecialchars($result['student_name']); ?></td>
                                        <td><?php echo htmlspecialchars($result['course_name']); ?></td>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <div class="progress flex-grow-1 me-2" style="height: 6px;">
                                                    <div class="progress-bar" style="width: <?php echo $result['score']; ?>%"></div>
                                                </div>
                                                <small><?php echo $result['score']; ?>%</small>
                                            </div>
                                        </td>
                                        <td class="pe-4 text-end">
                                            <span class="badge bg-primary px-3 py-2 rounded-pill"><?php echo $result['grade']; ?></span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Detailed Fee Payment Report -->
        <div class="col-lg-6 mb-4">
            <div class="card glass-card border-0 shadow-sm h-100">
                <div class="card-header bg-white py-3 border-0 d-flex justify-content-between align-items-center">
                    <h5 class="fw-bold mb-0">Fee Payment Audit</h5>
                    <i class="fas fa-file-invoice-dollar text-muted"></i>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="ps-4">Student Name</th>
                                    <th>Total Fee</th>
                                    <th>Paid</th>
                                    <th class="pe-4 text-end">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($payments as $payment): 
                                    $arrears = $payment['total_fee'] - $payment['amount_paid'];
                                ?>
                                    <tr>
                                        <td class="ps-4 fw-bold text-dark"><?php echo htmlspecialchars($payment['student_name']); ?></td>
                                        <td>KES <?php echo number_format($payment['total_fee'], 0); ?></td>
                                        <td class="text-success fw-bold">KES <?php echo number_format($payment['amount_paid'], 0); ?></td>
                                        <td class="pe-4 text-end">
                                            <span class="badge <?php 
                                                echo $payment['status'] == 'paid' ? 'bg-success' : ($payment['status'] == 'partial' ? 'bg-warning' : 'bg-danger'); 
                                            ?> px-3 py-2 rounded-pill text-capitalize">
                                                <?php echo $payment['status']; ?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>
