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

// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: parent_login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

// Fetch parent_id from the parents table based on user_id
$parent_stmt = $conn->prepare("SELECT parent_id FROM parents WHERE user_id = ?");
$parent_stmt->bind_param("i", $user_id);
$parent_stmt->execute();
$parent_result = $parent_stmt->get_result();

if ($parent_result->num_rows > 0) {
    $parent = $parent_result->fetch_assoc();
    $parent_id = $parent['parent_id'];
} else {
    die("Parent details not found.");
}

// Fetch fee details for children
$fees_stmt = $conn->prepare("
    SELECT students.*, classes.class_name 
    FROM students 
    JOIN classes ON students.class_id = classes.class_id 
    WHERE students.parent_id = ?
");
$fees_stmt->bind_param("i", $parent_id);
$fees_stmt->execute();
$fees_result = $fees_stmt->get_result();

$conn->close();
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fee Details</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            margin: 0;
        }
        .sidebar {
            width: 250px;
            background-color: #343a40;
            color: white;
            display: flex;
            flex-direction: column;
            padding: 1rem;
            height: 100vh;
            position: fixed;
        }
        .main-content {
            margin-left: 250px;
            display: flex;
            flex-direction: column;
            flex: 1;
        }
        header, footer {
            background-color: #007bff;
            color: white;
            text-align: center;
            padding: 1rem;
        }
        footer {
            margin-top: auto;
        }
        .card {
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <div class="sidebar">
        <h2>Parent Dashboard</h2>
          <ul class="nav flex-column">
            <li class="nav-item"><a class="nav-link text-white" href="parent_dashboard.php">Dashboard</a></li>
            <li class="nav-item"><a class="nav-link text-white" href="fee_details.php">Fee Details</a></li>
            <li class="nav-item"><a class="nav-link text-white" href="performance_reports.php">Performance Reports</a></li>
            <li class="nav-item"><a class="nav-link text-white" href="notifications.php">Notifications</a></li>
            <li class="nav-item"><a class="nav-link text-white" href="logout.php">Logout</a></li>
        </ul>
    </div>
    <div class="main-content">
        <header>
            <h1>Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!</h1>
        </header>
        <div class="container mt-4">
            <h2>Fee Details</h2>
            <div class="row">
                <?php while ($child = $fees_result->fetch_assoc()): ?>
                    <div class="col-md-4">
                        <div class="card">
                            <div class="card-header">
                                <h5><?php echo htmlspecialchars($child['name']); ?></h5>
                            </div>
                            <div class="card-body">
                                <p>Class: <?php echo htmlspecialchars($child['class_name']); ?></p>
                                <h6>Fee Details:</h6>
                                <p>Total Fees: <?php echo htmlspecialchars($child['fees_paid'] + $child['fees_outstanding']); ?></p>
                                <p>Amount Paid: <?php echo htmlspecialchars($child['fees_paid']); ?></p>
                                <p>Outstanding: <?php echo htmlspecialchars($child['fees_outstanding']); ?></p>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
        <footer>
            <p>&copy; 2026 School Management System</p>
        </footer>
    </div>
</body>
</html>
