<?php
session_start(); 
include '../admin/includes/database.php';
include '../admin/includes/functions.php';
$conn= new Functions();

if(isset($_SESSION['teacher'])){

    $sql = "SELECT * FROM teachers WHERE username =:username";
    $conn->query($sql);
    $conn->bind(":username", $_SESSION['teacher']);

    $result = $conn->fetchSingle();
    $teacher_name = $result->name;

    //fetch teacher section details 
    $sql = 'SELECT * FROM sections WHERE teacher_name =:teacher_name'; 
    $conn->query($sql);
    $conn->bind(":teacher_name", $teacher_name);
    $result_details = $conn->fetchSingle();

    $class_id = $result_details->class;
    $section_name = $result_details->name;
    $section_id = $result_details->id;


    $conn->query("SELECT id, name FROM classes WHERE id =:class_id");
    $conn->bind(":class_id", $class_id);    
    $classes = $conn->fetchMultiple();
    
    $conn->query("SELECT id, subject_name FROM subjects");
    $subjects = $conn->fetchMultiple();
    
    $conn->query("SELECT id, name FROM terms");
    $terms = $conn->fetchMultiple();
    
    echo json_encode([
        'classes' => $classes,
        'subjects' => $subjects,
        'terms' => $terms
    ]);

}else{
    $array = ["classes" => ['Invalid Request'], "subjects" => ['Invalid request'], "terms" => ['Invalid request']];
    echo json_encode($array); 
}


