<?php 
session_start();
require_once '../db_connect.php'; 
if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit; }

include 'includes/header.php'; 
include 'includes/sidebar.php'; 

// Calculations
$total_income = $pdo->query("SELECT SUM(amount) FROM students_payments WHERE status='approved'")->fetchColumn() ?? 0;
$this_month = $pdo->query("SELECT SUM(amount) FROM students_payments WHERE status='approved' AND MONTH(created_at) = MONTH(CURRENT_DATE())")->fetchColumn() ?? 0;
$method_stats = $pdo->query("SELECT method, SUM(amount) as total FROM students_payments WHERE status='approved' GROUP BY method")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="main-content p-4">
    <h4 class="fw-bold mb-4">Financial Revenue Report</h4>
    
    <div class="row g-4 mb-5">
        <div class="col-md-6">
            <div class="card p-4 border-0 shadow-sm bg-success text-white rounded-4">
                <small class="fw-bold text-uppercase opacity-75">Life Time Total Revenue</small>
                <h1 class="fw-bold mb-0">৳<?= number_format($total_income) ?></h1>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card p-4 border-0 shadow-sm bg-dark text-white rounded-4">
                <small class="fw-bold text-uppercase opacity-75">This Month Earnings</small>
                <h1 class="fw-bold mb-0">৳<?= number_format($this_month) ?></h1>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-7">
            <div class="card border-0 shadow-sm p-4 rounded-4">
                <h5 class="fw-bold mb-4">Earnings by Payment Method</h5>
                <div class="table-responsive">
                    <table class="table">
                        <tbody>
                            <?php foreach($method_stats as $ms): ?>
                            <tr>
                                <td class="fw-bold"><?= $ms['method'] ?></td>
                                <td class="text-end fw-bold text-success">৳<?= number_format($ms['total']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include 'includes/footer.php'; ?>