<?php
session_start();
include_once '../../config/database.php';
$database = new Database();
$db = $database->getConnection();

// Verificar si el docente está autenticado
if (!isset($_SESSION['docente_id'])) {
    header('Location: ../../login.php');
    exit;
}

// Obtener el ID de la materia
if (!isset($_GET['materia_id'])) {
    echo "Materia no especificada.";
    exit;
}

$materia_id = $_GET['materia_id'];
$docente_id = $_SESSION['docente_id'];

// Consulta para obtener los estudiantes de la materia
$query = "SELECT 
            s.idRegistro AS student_id, 
            s.nombres_varchar AS nombres, 
            s.paterno_varchar AS paterno, 
            s.materno_varchar AS materno, 
            s.email_varchar AS email 
          FROM 
            asignar_materias am 
          JOIN 
            student s ON am.student_id = s.idRegistro 
          WHERE 
            am.materia_id = :materia_id AND am.docente_id = :docente_id AND am.estado = 1";
$stmt = $db->prepare($query);
$stmt->bindParam(':materia_id', $materia_id);
$stmt->bindParam(':docente_id', $docente_id);
$stmt->execute();
$estudiantes = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Estudiantes de la Materia</title>
    <link rel="stylesheet" href="../../public/css/styles.css">
</head>
<body>
    <div class="container">
        <h1>Estudiantes Inscritos</h1>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nombre Completo</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($estudiantes as $estudiante): ?>
                    <tr>
                        <td><?php echo $estudiante['student_id']; ?></td>
                        <td><?php echo $estudiante['nombres'] . " " . $estudiante['paterno'] . " " . $estudiante['materno']; ?></td>
                        <td><?php echo $estudiante['email']; ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</body>
</html>
