<?php
session_start();
require 'db_connection.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: parent_login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

/* GET PARENT */
$stmt = $conn->prepare("SELECT parent_id FROM parents WHERE user_id=?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$parent_id = $stmt->get_result()->fetch_assoc()['parent_id'];

$student_id = $_GET['student_id'] ?? 0;

/* GET STUDENT */
$stmt = $conn->prepare("
SELECT * FROM students 
WHERE student_id=? AND parent_id=?
");
$stmt->bind_param("ii", $student_id, $parent_id);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();

if (!$student) {
    die("Invalid student selected");
}
?>

<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Make Payment</title>
</head>

<body class="bg-light">

<div class="container mt-5">

<div class="card p-4 shadow">

<h3>Pay School Fees</h3>

<hr>

<h5><?= htmlspecialchars($student['name']) ?></h5>
<p><b>Outstanding:</b> ₦<?= number_format($student['fees_outstanding']) ?></p>

<form method="POST" action="flutterwave_init.php">

<input type="hidden" name="student_id" value="<?= $student_id ?>">

<label>Enter Amount to Pay</label>
<input type="number"
       name="amount"
       class="form-control"
       min="5"
       max="<?= $student['fees_outstanding'] ?>"
       required>

<small class="text-muted">
You can pay part or full amount.
</small>

<br><br>

<button class="btn btn-success w-100">
Pay with Flutterwave
</button>

</form>

</div>

</div>

</body>
</html>