<?php
session_name('super_admin_session');
if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');
session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);
session_start();
error_reporting(0);
include('includes/dbconnection.php');
require_once('vendor/autoload.php'); // Include mPDF

use Mpdf\Mpdf;
use Dotenv\Dotenv;

// Load environment variables
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$school_name = $_ENV['SCHOOL_NAME'];
$school_address = $_ENV['SCHOOL_ADDRESS'];
$logo =$_ENV['WEBSITE_LOGO'];

// Get and validate the filtered data
$class = isset($_GET['class']) ? filter_var($_GET['class'], FILTER_SANITIZE_STRING) : '';
$arm = isset($_GET['arm']) ? filter_var($_GET['arm'], FILTER_SANITIZE_STRING) : '';

if (empty($class)) {
    die("Invalid request. Please select a class.");
}

// Prepare the query dynamically based on user input
$query = "SELECT * FROM students WHERE class = ?";
$params = [$class];
$types = "s";

if (!empty($arm)) {
    $query .= " AND section = ?";
    $params[] = $arm;
    $types .= "s";
}

// Prepare and execute the statement
$stmt = $con->prepare($query);
if (!$stmt) {
    die("Query preparation failed: " . $con->error);
}

$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();

// Generate the HTML content for the PDF
$html = "
<style>
    table { width: 100%; border-collapse: collapse; font-size: 12px; }
    th, td { padding: 8px; text-align: left; }
    th { background-color: #007bff; color: white; }
    tr:nth-child(even) { background-color: #f9f9f9; }
    .text-center { text-align: center; }
</style>
<div class='text-center'>
    <img src=$logo alt='logo' width='70' height='70'><br>
    <h1>$school_name</h1>
    <b><i>$school_address</i></b><br>
    <hr>
    <strong>STUDENT LIST - $class " . (!empty($arm) ? "($arm)" : "") . "</strong><br>
</div>
<hr>
<table border='1'>
    <thead>
        <tr>
            <th>#</th>
            <th>Admission No.</th>
            <th>Student Name</th>
            <th>Class</th>
            <th>Arm</th>
            <th>School Type</th>
            <th>Scholarship Status</th>
        </tr>
    </thead>
    <tbody>";

$cnt = 1;

while ($row = $result->fetch_assoc()) {
    $html .= "<tr>
                <td>" . $cnt . "</td>
                <td>" . htmlentities($row['studentno']) . "</td>
                <td>" . htmlentities($row['studentName']) . "</td>
                <td>" . htmlentities($row['class']) . "</td>
                <td>" . htmlentities($row['section']) . "</td>
                <td>" . htmlentities($row['stream']) . "</td>
                <td>" . htmlentities($row['scholarship']) . "</td>
              </tr>";
    $cnt++;
}

if ($cnt == 1) {
    $html .= "<tr><td colspan='7' style='text-align: center;'>No records found.</td></tr>";
} 
$html .= "</tbody>
</table>";

// Initialize mPDF and generate the PDF
$mpdf = new Mpdf();
$mpdf->SetHeader($school_name.'||Student List');
$mpdf->SetFooter('Generated on ' . date("M d, Y H:i A") . '||Page {PAGENO}');
$mpdf->WriteHTML($html);
$mpdf->Output('student_list.pdf', 'D');
?>
