<?php
session_start();
require_once('../config.php');

// ✅ Restrict access to logged-in students only
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'student') {
    header("Location: login.php");
    exit();
}

include('../header/header.php');

// ✅ Correct variable
$student_id = $_SESSION['user_id'];

// ✅ Fetch payment records
$stmt = $pdo->prepare("
    SELECT * FROM payments 
    WHERE student_id = ? 
    ORDER BY payment_date DESC
");
$stmt->execute([$student_id]);
$payments = $stmt->fetchAll(PDO::FETCH_ASSOC);

// ✅ Initialize totals
$total_fee = 0;
$total_paid = 0;
$total_arrears = 0;

// ✅ Calculate totals
foreach ($payments as $payment) {
    $total_fee += $payment['total_fee'];
    $total_paid += $payment['amount_paid'];
    $total_arrears += ($payment['total_fee'] - $payment['amount_paid']);
}
?>

<div class="container-fluid py-4">

    <!-- Summary Cards -->
    <div class="row g-4 mb-5">
        <div class="col-md-6 col-lg-4">
            <div class="card shadow-sm border-0 bg-success text-white p-4">
                <h5 class="fw-bold">Total Paid Fees</h5>
                <h2>Ksh<?php echo number_format($total_paid, 2); ?></h2>
            </div>
        </div>

        <div class="col-md-6 col-lg-4">
            <div class="card shadow-sm border-0 bg-danger text-white p-4">
                <h5 class="fw-bold">Total Arrears</h5>
                <h2>Ksh<?php echo number_format($total_arrears, 2); ?></h2>
            </div>
        </div>
    </div>

    <!-- Payment Table -->
    <div class="card shadow-sm border-0">
        <div class="card-header bg-white">
            <h4 class="fw-bold mb-0">Payment History</h4>
        </div>

        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Date</th>
                            <th>Total Fee</th>
                            <th>Paid</th>
                            <th>Arrears</th>
                            <th class="text-end pe-4">Status</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php if (empty($payments)): ?>
                            <tr>
                                <td colspan="5" class="text-center py-4 text-muted">
                                    No payment records found.
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($payments as $payment): 
                                $arrears = $payment['total_fee'] - $payment['amount_paid'];
                            ?>
                                <tr>
                                    <td class="ps-4">
                                        <?php echo date('M d, Y', strtotime($payment['payment_date'])); ?>
                                    </td>

                                    <td>
                                        $<?php echo number_format($payment['total_fee'], 2); ?>
                                    </td>

                                    <td class="text-success fw-bold">
                                        $<?php echo number_format($payment['amount_paid'], 2); ?>
                                    </td>

                                    <td class="text-danger fw-bold">
                                        $<?php echo number_format($arrears, 2); ?>
                                    </td>

                                    <td class="text-end pe-4">
                                        <span class="badge <?php 
                                            echo $payment['status'] == 'paid' ? 'bg-success' : 
                                                ($payment['status'] == 'partial' ? 'bg-warning' : 'bg-danger'); 
                                        ?>">
                                            <?php echo htmlspecialchars($payment['status']); ?>
                                        </span>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>

                </table>
            </div>
        </div>
    </div>

</div>

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