<?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'];

/* GET CHILDREN */
$stmt = $conn->prepare("
SELECT student_id, name 
FROM students 
WHERE parent_id=?
");
$stmt->bind_param("i", $parent_id);
$stmt->execute();
$children = $stmt->get_result();

/* SELECTED CHILD */
$student_id = $_GET['student_id'] ?? null;

/* IF NOT SELECTED → AUTO FIRST CHILD */
if (!$student_id) {
    $first = $children->fetch_assoc();
    $student_id = $first['student_id'] ?? 0;
    $children->data_seek(0);
}

/* GET CLASS OF SELECTED CHILD */
$stmt = $conn->prepare("
SELECT class_id, name FROM students WHERE student_id=?
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$child = $stmt->get_result()->fetch_assoc();

$class_id = $child['class_id'] ?? 0;

/* TIMETABLE */
$stmt = $conn->prepare("
SELECT 
    t.day,
    t.period,
    t.start_time,
    t.end_time,
    s.subject_name,
    c.class_name,
    t.location
FROM timetable t
JOIN subject s ON t.subject_id = s.subject_id
JOIN classes c ON t.class_id = c.class_id
WHERE t.class_id = ?
ORDER BY 
FIELD(t.day,'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),
t.period
");

$stmt->bind_param("i", $class_id);
$stmt->execute();
$result = $stmt->get_result();

/* ORGANIZE */
$timetable = [];

while ($row = $result->fetch_assoc()) {
    $timetable[$row['period']][$row['day']] = $row;
}

$days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
?>

<!DOCTYPE html>
<html>
<head>
<title>Timetable</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

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

/* HEADER */
.header{
    background:#3a57e8;
    color:#fff;
    padding:20px;
    text-align:center;
}

/* CHILD SELECTOR */
.child-box{
    display:inline-block;
    padding:8px 14px;
    margin:5px;
    background:#fff;
    border-radius:20px;
    box-shadow:0 3px 10px rgba(0,0,0,0.05);
    text-decoration:none;
    color:#333;
}

.child-active{
    background:#3a57e8;
    color:#fff;
}

/* TABLE */
.table-timetable{
    background:#fff;
    border-radius:12px;
    overflow:hidden;
}

.table-timetable th{
    background:#111827;
    color:#fff;
    text-align:center;
}

.table-timetable td{
    text-align:center;
    padding:12px;
    border:1px solid #eee;
}

.subject{
    background:#3a57e8;
    color:#fff;
    padding:5px 10px;
    border-radius:8px;
    font-size:13px;
    display:inline-block;
}
</style>
</head>

<body>

<div class="header">
    <h3>📅 Timetable - <?= htmlspecialchars($child['name']) ?></h3>
</div>

<div class="container mt-3">

<!-- CHILD SELECTOR -->
<div class="mb-3">

<?php while($c = $children->fetch_assoc()): ?>
    <a class="child-box <?= ($c['student_id']==$student_id?'child-active':'') ?>"
       href="?student_id=<?= $c['student_id'] ?>">
        👤 <?= htmlspecialchars($c['name']) ?>
    </a>
<?php endwhile; ?>

</div>

<!-- TIMETABLE GRID -->
<div class="table-responsive">

<table class="table table-timetable">

<thead>
<tr>
    <th>Period</th>
    <?php foreach($days as $day): ?>
        <th><?= $day ?></th>
    <?php endforeach; ?>
</tr>
</thead>

<tbody>

<?php for($p=1;$p<=8;$p++): ?>
<tr>
<td><b>Period <?= $p ?></b></td>

<?php foreach($days as $day): ?>
<td>

<?php if(isset($timetable[$p][$day])): ?>
    <div class="subject">
        <?= $timetable[$p][$day]['subject_name'] ?>
    </div><br>
    <small>
        <?= $timetable[$p][$day]['start_time'] ?> - 
        <?= $timetable[$p][$day]['end_time'] ?>
    </small>
<?php else: ?>
    <span class="text-muted">-</span>
<?php endif; ?>

</td>
<?php endforeach; ?>

</tr>
<?php endfor; ?>

</tbody>

</table>

</div>

</div>

</body>
</html>