<?php
session_start();
include('includes/dbconnection.php');

$response = [];

// Get total students
$query = mysqli_query($con, "SELECT COUNT(*) AS total FROM students where status =1");
$row = mysqli_fetch_assoc($query);
$response['total_students'] = $row['total'];

// Get new students (registered this month)
$query = mysqli_query($con, "SELECT COUNT(*) AS total FROM students WHERE status = 1 and MONTH(postingDate) = MONTH(CURDATE())");
$row = mysqli_fetch_assoc($query);
$response['new_students'] = $row['total'];

// Get payment stats
$query = mysqli_query($con, "SELECT 
    (SELECT COUNT(*) FROM payment WHERE status = 'approved') AS approved,
    (SELECT COUNT(*) FROM message WHERE status = 0) AS pending,
    (SELECT COUNT(*) FROM message WHERE status = 2) AS disapproved
");
$row = mysqli_fetch_assoc($query);
$response['approved'] = $row['approved'];
$response['pending'] = $row['pending'];
$response['disapproved'] = $row['disapproved'];

// Get total payment amount per month
$months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
$payment_totals = array_fill(0, 12, 0); // Default value for each month

$query = mysqli_query($con, "SELECT MONTH(payment_date) AS month_number, SUM(amount) AS total_amount 
                            FROM payment 
                            WHERE YEAR(payment_date) = YEAR(CURDATE()) 
                            GROUP BY MONTH(payment_date)");

while ($row = mysqli_fetch_assoc($query)) {
    $monthIndex = (int)$row['month_number'] - 1; // Convert month number to array index
    $payment_totals[$monthIndex] = (float)$row['total_amount']; // Store total payment amount for that month
}

$response['months'] = $months;
$response['payment_totals'] = $payment_totals;

header('Content-Type: application/json');
echo json_encode($response);
?>
