<?php
include('includes/session1.php');
include('includes/dbconnection.php');
include('includes/session_term_helper.php');

if (!isset($_SESSION['sid'])) {
    header('Location: logoutteacher.php');
    exit();
}

// Fetch active session ordered by earliest start_date

// Resolve current session and term (with fallback support)
$sessionTerm = resolveCurrentSessionTerm($con);
$currentSessionId   = $sessionTerm['session_id']   ?: null;
$currentSessionName = $sessionTerm['session_name'];
$currentTermId      = $sessionTerm['term_id']       ?: null;
$currentTermName    = $sessionTerm['term_name'];

$class = $_GET['class'] ?? '';
$section = $_GET['section'] ?? '';

$query = "
    SELECT 
        s.id, s.studentName,
        p.fluency, p.handwriting, p.lab_workshops, p.musical_skills, p.sports,
        a.punctuality, a.reliability, a.politeness_honesty, a.charisma, a.organizational_ability, a.self_control
    FROM students s
    LEFT JOIN psychomotor p ON s.id = p.student_id AND p.term = ? AND p.session = ?
    LEFT JOIN affective a ON s.id = a.student_id AND a.term = ? AND a.session = ?
    WHERE s.class = ? AND s.section = ? AND status < 3
    ORDER BY s.studentName ASC
";

$stmt = $con->prepare($query);
$stmt->bind_param("ssssss", $currentTermName, $currentSessionName, $currentTermName, $currentSessionName, $class, $section);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html>
<?php @include("includes/head.php"); ?>


<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <?php @include("includes/headert.php"); ?>
        <?php @include("includes/sidebart.php"); ?>

        <div class="content-wrapper">
            <!-- Content Header -->
                         <section class="content-header premium-header">
    <div class="container-fluid">
        <div class="header-glass-card">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <div class="title-section">
                        <div class="icon-box bg-primary-gradient shadow-sm">
                            <i class="fas fa-clipboard-list"></i>
                        </div>
                        <div>
                            <h1 class="premium-title text-primary">
                                Result Management
                                <span class="premium-subtitle text-secondary">Rating Review -<?= htmlspecialchars($class) ?> (<?= htmlspecialchars($section) ?>) </span>
                            </h1>
                                        <div class="mt-1">
                                            <?php if ($currentSessionName): ?>
                                                <span class="badge badge-primary mr-1">
                                                    <i class="fas fa-calendar-alt"></i> <?= htmlspecialchars($currentSessionName) ?>
                                                </span>
                                            <?php endif; ?>
                                            <?php if ($currentTermName): ?>
                                                <span class="badge badge-success">
                                                    <i class="fas fa-bookmark"></i> <?= htmlspecialchars($currentTermName) ?>
                                                </span>
                                            <?php else: ?>
                                                <span class="badge badge-warning">
                                                    <i class="fas fa-exclamation-triangle"></i> No active term
                                                </span>
                                            <?php endif; ?>
                                        </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb premium-breadcrumb">
                            <li class="breadcrumb-item"><a href="dashboard.php" class="text-primary">Home</a></li>
                            <li class="breadcrumb-item active" aria-current="page">View Rating</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</section>

            <section class="content">
                <div class="container-fluid">
                    <div class="card border-primary">
                        <div class="card-header bg-primary text-white font-weight-bold">
                            <h5 class="mb-0"><i class="fas fa-user-graduate"></i> Psychomotor & Affective Rating - <?= htmlspecialchars($class . " (" . $section . ")") ?></h5>
                        </div>
                        <div class="card-body">
                            <table id="example1" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>Student Name</th>
                                        <th colspan="5">Psychomotor Skills</th>
                                        <th colspan="6">Affective Domain</th>
                                    </tr>
                                    <tr>
                                        <th></th>
                                        <th>Fluency</th>
                                        <th>Handwriting</th>
                                        <th>Lab/Workshops</th>
                                        <th>Musical Skills</th>
                                        <th>Sports</th>
                                        <th>Punctuality</th>
                                        <th>Reliability</th>
                                        <th>Politeness/Honesty</th>
                                        <th>Charisma</th>
                                        <th>Organizational Ability</th>
                                        <th>Self-Control</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $result->fetch_assoc()) { ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($row['studentName']); ?></td>
                                            <td><?php echo $row['fluency'] ?? '-'; ?></td>
                                            <td><?php echo $row['handwriting'] ?? '-'; ?></td>
                                            <td><?php echo $row['lab_workshops'] ?? '-'; ?></td>
                                            <td><?php echo $row['musical_skills'] ?? '-'; ?></td>
                                            <td><?php echo $row['sports'] ?? '-'; ?></td>
                                            <td><?php echo $row['punctuality'] ?? '-'; ?></td>
                                            <td><?php echo $row['reliability'] ?? '-'; ?></td>
                                            <td><?php echo $row['politeness_honesty'] ?? '-'; ?></td>
                                            <td><?php echo $row['charisma'] ?? '-'; ?></td>
                                            <td><?php echo $row['organizational_ability'] ?? '-'; ?></td>
                                            <td><?php echo $row['self_control'] ?? '-'; ?></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </section>

        </div>
        <?php @include("includes/footer.php"); ?>
    </div>
    <?php @include("includes/foot.php"); ?>


</body>

</html>