<?php
include('includes/config.php');

$id = $_GET['id'] ?? '';

$sql = "SELECT s.*, 
        d.department_name, 
        g.degree_name 
        FROM tblstudents s
        LEFT JOIN tbldepartment d ON s.department_id = d.department_id
        LEFT JOIN tbldegree g ON s.degree_id = g.degree_id
        WHERE s.id = :id LIMIT 1";

$q = $dbh->prepare($sql);
$q->bindParam(':id', $id);
$q->execute();
$data = $q->fetch(PDO::FETCH_ASSOC);

if ($data) {

    // Calculate Year using passedout column (Ex: 2023 - 2024)
    $years = explode('-', $data['passed_out_year']);
    $passedOutYear = intval(trim($years[1]));

    $current = date('Y');
    $diff = $passedOutYear - $current;

    if ($diff == 3)
        $yearName = "I Year";
    else if ($diff == 2)
        $yearName = "II Year";
    else if ($diff == 1)
        $yearName = "III Year";
    else
        $yearName = "Completed";

    $shift = $data["shift"];
    if ($shift == 1):
        $shift = "Morning";
    else:
        $shift = "Evening";
    endif;
    echo json_encode([
        "department" => $data["department_name"],
        "degree" => $data["degree_name"],
        "shift" => $shift,
        "mobile" => $data["MobileNumber"],
        "year" => $yearName
    ]);
}
?>