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

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

$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'] ?? 'Parent';

/* 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'];

/* CHILDREN */
$stmt = $conn->prepare("
SELECT s.*, c.class_name
FROM students s
JOIN classes c ON s.class_id = c.class_id
WHERE s.parent_id=?
");
$stmt->bind_param("i", $parent_id);
$stmt->execute();
$children = $stmt->get_result();
?>

<!DOCTYPE html>
<html>
<head>
<title>Parent Dashboard</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<style>
body{
    background:#eef2f7;
    font-family:Segoe UI;
}

/* SIDEBAR */
.sidebar{
    width:250px;
    height:100vh;
    position:fixed;
    background:#0f172a;
    color:#fff;
    padding:20px;
}

.sidebar a{
    display:block;
    padding:10px;
    color:#cbd5e1;
    text-decoration:none;
    border-radius:8px;
    margin-bottom:5px;
}

.sidebar a:hover{
    background:#1e293b;
    color:#fff;
}

/* CONTENT */
.content{
    margin-left:270px;
    padding:25px;
}

/* TOPBAR */
.topbar{
    background:#fff;
    padding:15px;
    border-radius:12px;
    box-shadow:0 5px 15px rgba(0,0,0,0.05);
    margin-bottom:25px;
}

/* MAIN CARD WRAPPER */
.child-wrapper{
    margin-bottom:30px;
}

/* CHILD HEADER CARD */
.child-header{
    background:#ffffff;
    border-radius:16px;
    padding:15px 20px;
    box-shadow:0 5px 15px rgba(0,0,0,0.05);
    margin-bottom:15px;
    display:flex;
    justify-content:space-between;
    align-items:center;
}

/* SMALL CARDS */
.info-card{
    background:#fff;
    border-radius:14px;
    padding:15px;
    box-shadow:0 4px 12px rgba(0,0,0,0.04);
    height:100%;
}

.info-title{
    font-size:12px;
    color:#64748b;
}

.info-value{
    font-size:18px;
    font-weight:bold;
}

/* BADGES */
.badge-paid{
    background:#16a34a;
    color:#fff;
    padding:5px 10px;
    border-radius:20px;
}

.badge-due{
    background:#dc2626;
    color:#fff;
    padding:5px 10px;
    border-radius:20px;
}

.badge-att{
    background:#2563eb;
    color:#fff;
    padding:5px 10px;
    border-radius:20px;
}

/* ACTION BUTTONS */
.action-btn{
    width:100%;
    margin-top:8px;
    border-radius:10px;
}
</style>
</head>

<body>

<!-- SIDEBAR -->
<div class="sidebar">
    <h4>Parent Portal</h4>

    <a href="parent_dashboard.php">Dashboard</a>
    <a href="transcript.php">Transcript</a>
    <a href="notifications.php">Notifications</a>
    <a href="enrollments.php">Subjects</a>
    <a href="timetable.php">Timetable</a>
    <a href="payment.php">Payments</a>
</div>

<!-- CONTENT -->
<div class="content">

<div class="topbar d-flex justify-content-between">
    <h4>Welcome, <?= htmlspecialchars($username) ?></h4>
    <i class="fa fa-user-circle fa-2x"></i>
</div>

<!-- CHILD LOOP -->
<?php while($c = $children->fetch_assoc()): ?>

<?php
$student_id = $c['student_id'];

/* ATTENDANCE */
$att = $conn->prepare("
SELECT 
SUM(CASE WHEN status='Present' THEN 1 ELSE 0 END) present,
SUM(CASE WHEN status='Absent' THEN 1 ELSE 0 END) absent
FROM student_attendance
WHERE student_id=?
");
$att->bind_param("i", $student_id);
$att->execute();
$a = $att->get_result()->fetch_assoc();

$present = $a['present'] ?? 0;
$absent  = $a['absent'] ?? 0;

$badge = $c['fees_outstanding'] > 0
    ? "<span class='badge-due'>Owing</span>"
    : "<span class='badge-paid'>Paid</span>";
?>

<div class="child-wrapper">

<!-- CHILD HEADER -->
<div class="child-header">
    <div>
        <h5 class="mb-0"><?= htmlspecialchars($c['name']) ?></h5>
        <small class="text-muted"><?= $c['class_name'] ?></small>
    </div>

    <div>
        <?= $badge ?>
    </div>
</div>

<!-- GRID CARDS -->
<div class="row g-3">

<!-- FEES CARD -->
<div class="col-md-4">
<div class="info-card">
    <div class="info-title">Fees Paid</div>
    <div class="info-value">₦<?= number_format($c['fees_paid']) ?></div>

    <hr>

    <div class="info-title">Outstanding</div>
    <div class="info-value text-danger">₦<?= number_format($c['fees_outstanding']) ?></div>
</div>
</div>

<!-- ATTENDANCE CARD -->
<div class="col-md-4">
<div class="info-card">
    <div class="info-title">Attendance</div><br>

    <p class="mb-1">
        <span class="badge-att">Present: <?= $present ?></span>
    </p>
<hr>
    <p>
        <span class="badge-due">Absent: <?= $absent ?></span>
    </p>
</div>
</div>

<!-- ACTION CARD -->
<div class="col-md-4">
<div class="info-card">

<a href="payment.php?student_id=<?= $student_id ?>" class="btn btn-success action-btn">
Pay Fees
</a>

<a href="transcript.php?student_id=<?= $student_id ?>" class="btn btn-primary action-btn">
Transcript
</a>



</div>
</div>

</div>

</div>

<?php endwhile; ?>

</div>

</body>
</html>