<?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();
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
$start_date = isset($_GET['start_date']) ? filter_var($_GET['start_date'], FILTER_SANITIZE_STRING) : '';
$end_date = isset($_GET['end_date']) ? filter_var($_GET['end_date'], FILTER_SANITIZE_STRING) : '';
$section = isset($_GET['section']) ? filter_var($_GET['section'], FILTER_SANITIZE_STRING) : '';
$pay_for = isset($_GET['pay_for']) ? filter_var($_GET['pay_for'], FILTER_SANITIZE_STRING) : '';
$Term = isset($_GET['Term']) ? filter_var($_GET['Term'], FILTER_SANITIZE_STRING) : '';
$session = isset($_GET['session']) ? filter_var($_GET['session'], FILTER_SANITIZE_STRING) : '';
$class = isset($_GET['class']) ? filter_var($_GET['class'], FILTER_SANITIZE_STRING) : '';
$school_type = isset($_GET['school_type']) ? filter_var($_GET['school_type'], FILTER_SANITIZE_STRING) : '';




if (empty($start_date) || empty($end_date)) {
    die("Start date and end date are required.");
}

// Prepare the query
$query = "SELECT * FROM payment WHERE DATE(payment_date) BETWEEN ? AND ?";
$params = [$start_date, $end_date];
$types = "ss";

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

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

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

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


$query .= " ORDER BY payment_date DESC";

// 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>
    Payment History from $start_date to $end_date
   
</div>
<div class='row text-center'>
    <div class='col-md-4'>
        <strong>Term:</strong> $Term  || <strong>Session:</strong> $session 
    </div>
    <div class='col-md-4'>
        <strong>Class:</strong> $class  || <strong>Arm:</strong> $section 
    </div>
    <div class='col-md-4'>
        <strong>School Type:</strong> $school_type  || <strong>Payment Name:</strong> $pay_for 
    </div>
</div>





<hr>
<table border='1'>
    <thead>
        <tr>
            <th>#</th>
            <th>Admission No.</th>
            <th>Student Name</th>
            <th>Receipt No.</th>
            <th>Class</th>
            <th>Payment For</th>
            <th>Amount</th>
            <th>Payment Date</th>
        </tr>
    </thead>
    <tbody>";

$cnt = 1;
$total_amount = 0;

while ($row = $result->fetch_assoc()) {
    $total_amount += $row['amount'];
    $html .= "<tr>
                <td>" . $cnt . "</td>
                <td>" . htmlentities($row['studentno']) . "</td>
                <td>" . htmlentities($row['studentName']) . "</td>
                <td>R0000" . htmlentities($row['id']) . "</td>
                <td>" . htmlentities($row['class']) . "</td>
                <td>" . htmlentities($row['pay_for']) . "</td>
                <td>" . number_format($row['amount'], 2) . "</td>
                <td>" . date("M d, Y H:i A", strtotime($row['payment_date'])) . "</td>
              </tr>";
    $cnt++;
}

if ($cnt == 1) {
    $html .= "<tr><td colspan='8' style='text-align: center;'>No records found for the selected date range.</td></tr>";
} else {
    $html .= "<tr>
                <td colspan='6' style='text-align: right;'><strong>Total</strong></td>
                <td colspan='2'>" . number_format($total_amount, 2) . "</td>
              </tr>";
}
$html .= "</tbody>
</table>";

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

?>