<?php
if (session_status() === PHP_SESSION_NONE) session_start();

// Sanitize student_reg_number
$student_reg_number = $connection->real_escape_string($student_reg_number);

// Fetch payment history using prepared statement
$stmt = $connection->prepare("SELECT * FROM `fee_payment` WHERE `reg_number` = ?");
$stmt->bind_param("s", $student_reg_number);
$stmt->execute();
$result = $stmt->get_result();
?>
<div class='page-title'>
    <div>
        <h1><i class='fa fa-laptop'></i> Payment History</h1>
        <p>View your fee payment records</p>
    </div>   
</div>

<div class="card">
    <div class='panel panel-primary'>
        <div class='panel-heading'>
            <h4 class='text-center'>Your Payment History</h4>
        </div>
        <div class='panel-body'>
            <?php
            if ($result && $result->num_rows > 0) {
                echo "
                <div class='table-responsive'>
                    <table class='table table-bordered'>
                        <thead>
                            <tr class='bg-primary'>
                                <th>S/N</th>
                                <th>Term</th>
                                <th>Session</th>
                                <th>Class</th>
                                <th>Amount Paid (NGN)</th>
                                <th>Balance (NGN)</th>
                                <th>Teller No</th>
                                <th>Date</th>
                                <th>Status</th>
                                <th>Receipt</th>
                            </tr>
                        </thead>
                        <tbody>
                ";

                $i = 0;
                while ($row = $result->fetch_assoc()) {
                    $i++;
                    $amount_paid = number_format($row['amount_paid']);
                    $balance = number_format($row['student_balance']);
                    $status = $row['payment_status'] === "Cleared" 
                        ? "<p class='text-success'><b>Cleared</b></p>" 
                        : "<p class='text-danger'><b>Not Cleared</b></p>";

                    echo "
                    <tr>
                        <td>{$i}</td>
                        <td>{$row['fee_term']}</td>
                        <td>{$row['fee_session']}</td>
                        <td>{$row['student_class']}</td>
                        <td class='text-success'><b>{$amount_paid}</b></td>
                        <td class='text-danger'><b>{$balance}</b></td>
                        <td>{$row['teller_no']}</td>
                        <td>{$row['date_of_upload']}</td>
                        <td>{$status}</td>
                        <td>
                            <form action='print_receipt.php' method='POST' target='_blank'>
                                <input type='hidden' name='receipt_id' value='{$row['id']}' />
                                <button type='submit' class='btn btn-success btn-xs'>Print Receipt</button>
                            </form>
                        </td>
                    </tr>
                    ";
                }

                echo "</tbody></table></div>";
            } else {
                $_SESSION['msg'] = "No payment records found.";
                $_SESSION['type'] = "warning";
            }
            ?>
        </div>
    </div>
</div>
