<?php

session_start();

require_once __DIR__ . '/../library/config.php';

require_once __DIR__ . '/../library/super_admin_auth.php';

// allow_super_admin();

$page_title = 'Super Admin Dashboard';

require_once __DIR__ . '/includes/header.php';

require_once __DIR__ . '/includes/sidebar.php';

/*
|--------------------------------------------------------------------------
| TOTAL SCHOOLS
|--------------------------------------------------------------------------
*/

$schoolStmt = $pdo->query("

    SELECT COUNT(*) AS total_schools

    FROM schools

");

$totalSchools = $schoolStmt->fetch(PDO::FETCH_ASSOC)['total_schools'];

/*
|--------------------------------------------------------------------------
| ACTIVE SCHOOLS
|--------------------------------------------------------------------------
*/

$activeStmt = $pdo->query("

    SELECT COUNT(*) AS active_schools

    FROM schools

    WHERE status = 'active'

");

$activeSchools = $activeStmt->fetch(PDO::FETCH_ASSOC)['active_schools'];

/*
|--------------------------------------------------------------------------
| INACTIVE SCHOOLS
|--------------------------------------------------------------------------
*/

$inactiveStmt = $pdo->query("

    SELECT COUNT(*) AS inactive_schools

    FROM schools

    WHERE status = 'inactive'

");

$inactiveSchools = $inactiveStmt->fetch(PDO::FETCH_ASSOC)['inactive_schools'];

/*
|--------------------------------------------------------------------------
| ACTIVE SUBSCRIPTIONS
|--------------------------------------------------------------------------
*/

$activeSubscriptionStmt = $pdo->query("

    SELECT COUNT(*) AS active_subscriptions

    FROM super_admin_school_subscriptions

    WHERE status = 'Active'

");

$activeSubscriptions = $activeSubscriptionStmt
    ->fetch(PDO::FETCH_ASSOC)['active_subscriptions'];

/*
|--------------------------------------------------------------------------
| EXPIRED SUBSCRIPTIONS
|--------------------------------------------------------------------------
*/

$expiredSubscriptionStmt = $pdo->query("

    SELECT COUNT(*) AS expired_subscriptions

    FROM super_admin_school_subscriptions

    WHERE status = 'Expired'

");

$expiredSubscriptions = $expiredSubscriptionStmt
    ->fetch(PDO::FETCH_ASSOC)['expired_subscriptions'];

/*
|--------------------------------------------------------------------------
| TOTAL REVENUE
|--------------------------------------------------------------------------
*/

$revenueStmt = $pdo->query("

    SELECT SUM(amount) AS total_revenue

    FROM super_admin_school_subscriptions

    WHERE payment_status = 'Paid'

");

$totalRevenue = $revenueStmt
    ->fetch(PDO::FETCH_ASSOC)['total_revenue'];

$totalRevenue = $totalRevenue ?: 0;

/*
|--------------------------------------------------------------------------
| RECENT SUBSCRIPTIONS
|--------------------------------------------------------------------------
*/

$recentStmt = $pdo->query("

    SELECT

        s.name AS school_name,
        sp.plan_name,
        sss.amount,
        sss.expiry_date,
        sss.status

    FROM super_admin_school_subscriptions sss

    INNER JOIN schools s
    ON s.id = sss.school_id

    INNER JOIN super_admin_subscription_plans sp
    ON sp.id = sss.plan_id

    ORDER BY sss.id DESC

    LIMIT 8

");

$recentSubscriptions = $recentStmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!-- =========================================================
CONTENT HEADER
========================================================= -->

<section class="content-header">

    <div class="container-fluid">

        <div class="d-flex justify-content-between align-items-center mb-3">

            <div>

                <h1 class="font-weight-bold mb-1">

                    Super Admin Dashboard

                </h1>

                <p class="text-muted mb-0">

                    SaaS ERP Overview & Analytics

                </p>

            </div>

            <div>

                <a
                    href="schools/create.php"
                    class="btn btn-primary shadow-sm px-4"
                >

                    <i class="fas fa-plus mr-1"></i>

                    Create School

                </a>

            </div>

        </div>

    </div>

</section>

<!-- =========================================================
MAIN CONTENT
========================================================= -->

<section class="content">

    <div class="container-fluid">

        <!-- =================================================
        STATS ROW
        ================================================== -->

        <div class="row">

            <!-- TOTAL SCHOOLS -->

            <div class="col-lg-3 col-md-6">

                <div class="small-box dashboard-card bg-primary shadow-sm">

                    <div class="inner">

                        <h3>

                            <?= (int)$totalSchools; ?>

                        </h3>

                        <p>

                            Total Schools

                        </p>

                    </div>

                    <div class="icon">

                        <i class="fas fa-school"></i>

                    </div>

                </div>

            </div>

            <!-- ACTIVE SCHOOLS -->

            <div class="col-lg-3 col-md-6">

                <div class="small-box dashboard-card bg-success shadow-sm">

                    <div class="inner">

                        <h3>

                            <?= (int)$activeSchools; ?>

                        </h3>

                        <p>

                            Active Schools

                        </p>

                    </div>

                    <div class="icon">

                        <i class="fas fa-check-circle"></i>

                    </div>

                </div>

            </div>

            <!-- INACTIVE SCHOOLS -->

            <div class="col-lg-3 col-md-6">

                <div class="small-box dashboard-card bg-danger shadow-sm">

                    <div class="inner">

                        <h3>

                            <?= (int)$inactiveSchools; ?>

                        </h3>

                        <p>

                            Inactive Schools

                        </p>

                    </div>

                    <div class="icon">

                        <i class="fas fa-times-circle"></i>

                    </div>

                </div>

            </div>

            <!-- TOTAL REVENUE -->

            <div class="col-lg-3 col-md-6">

                <div class="small-box dashboard-card bg-warning shadow-sm">

                    <div class="inner">

                        <h3>

                            ₹<?= number_format($totalRevenue, 0); ?>

                        </h3>

                        <p>

                            Total Revenue

                        </p>

                    </div>

                    <div class="icon">

                        <i class="fas fa-rupee-sign"></i>

                    </div>

                </div>

            </div>

        </div>

        <!-- =================================================
        SECOND ROW
        ================================================== -->

        <div class="row">

            <!-- ACTIVE SUBSCRIPTIONS -->

            <div class="col-lg-6 col-md-6">

                <div class="info-box shadow-sm dashboard-info-box">

                    <span class="info-box-icon bg-success">

                        <i class="fas fa-crown"></i>

                    </span>

                    <div class="info-box-content">

                        <span class="info-box-text">

                            Active Subscriptions

                        </span>

                        <span class="info-box-number">

                            <?= (int)$activeSubscriptions ?>

                        </span>

                    </div>

                </div>

            </div>

            <!-- EXPIRED SUBSCRIPTIONS -->

            <div class="col-lg-6 col-md-6">

                <div class="info-box shadow-sm dashboard-info-box">

                    <span class="info-box-icon bg-danger">

                        <i class="fas fa-ban"></i>

                    </span>

                    <div class="info-box-content">

                        <span class="info-box-text">

                            Expired Subscriptions

                        </span>

                        <span class="info-box-number">

                            <?= (int)$expiredSubscriptions ?>

                        </span>

                    </div>

                </div>

            </div>

        </div>

        <!-- =================================================
        QUICK ACTIONS
        ================================================== -->

        <div class="card shadow-sm border-0">

            <div class="card-header bg-white border-0">

                <h3 class="card-title font-weight-bold">

                    Quick Actions

                </h3>

            </div>

            <div class="card-body">

                <div class="row">

                    <div class="col-md-3 mb-3">

                        <a
                            href="schools/index.php"
                            class="btn btn-primary btn-block action-btn"
                        >

                            <i class="fas fa-school mb-2 d-block"></i>

                            Manage Schools

                        </a>

                    </div>

                    <div class="col-md-3 mb-3">

                        <a
                            href="subscriptions/plans.php"
                            class="btn btn-success btn-block action-btn"
                        >

                            <i class="fas fa-crown mb-2 d-block"></i>

                            Subscription Plans

                        </a>

                    </div>

                    <div class="col-md-3 mb-3">

                        <a
                            href="subscriptions/subscriptions.php"
                            class="btn btn-warning btn-block action-btn"
                        >

                            <i class="fas fa-sync mb-2 d-block"></i>

                            Subscriptions

                        </a>

                    </div>

                    <div class="col-md-3 mb-3">

                        <a
                            href="notifications/index.php"
                            class="btn btn-danger btn-block action-btn"
                        >

                            <i class="fas fa-bell mb-2 d-block"></i>

                            Notifications

                        </a>

                    </div>

                </div>

            </div>

        </div>

        <!-- =================================================
        RECENT SUBSCRIPTIONS
        ================================================== -->

        <div class="card shadow-sm border-0">

            <div class="card-header bg-white border-0">

                <h3 class="card-title font-weight-bold">

                    Recent Subscriptions

                </h3>

            </div>

            <div class="card-body p-0">

                <div class="table-responsive">

                    <table class="table table-hover table-bordered mb-0">

                        <thead class="bg-light">

                            <tr>

                                <th>School</th>
                                <th>Plan</th>
                                <th>Amount</th>
                                <th>Expiry</th>
                                <th>Status</th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php if($recentSubscriptions): ?>

                                <?php foreach($recentSubscriptions as $subscription): ?>

                                    <tr>

                                        <td>

                                            <strong>

                                                <?= htmlspecialchars($subscription['school_name']) ?>

                                            </strong>

                                        </td>

                                        <td>

                                            <?= htmlspecialchars($subscription['plan_name']) ?>

                                        </td>

                                        <td>

                                            ₹<?= number_format($subscription['amount'], 2) ?>

                                        </td>

                                        <td>

                                            <?= date('d M Y', strtotime($subscription['expiry_date'])) ?>

                                        </td>

                                        <td>

                                            <?php if($subscription['status'] == 'Active'): ?>

                                                <span class="badge badge-success">

                                                    Active

                                                </span>

                                            <?php elseif($subscription['status'] == 'Expired'): ?>

                                                <span class="badge badge-danger">

                                                    Expired

                                                </span>

                                            <?php else: ?>

                                                <span class="badge badge-warning">

                                                    <?= htmlspecialchars($subscription['status']) ?>

                                                </span>

                                            <?php endif; ?>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php else: ?>

                                <tr>

                                    <td colspan="5" class="text-center py-4">

                                        No subscriptions found.

                                    </td>

                                </tr>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

</section>

<style>

.dashboard-card {

    border-radius: 18px;
}

.dashboard-info-box {

    border-radius: 18px;
}

.card {

    border-radius: 18px;
}

.table {

    border-radius: 14px;
    overflow: hidden;
}

.badge {

    border-radius: 30px;
    padding: 6px 12px;
    font-size: 12px;
}

.action-btn {

    border-radius: 14px;
    padding: 20px;
    font-size: 16px;
    font-weight: 600;
}

.action-btn i {

    font-size: 24px;
}

</style>

<?php require_once __DIR__ . '/includes/footer.php'; ?>