<?php

session_start();

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

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

// allow_super_admin();

$page_title = 'School Subscriptions';

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

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

/*
|--------------------------------------------------------------------------
| FETCH SUBSCRIPTIONS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT

        sss.*,
        s.name AS school_name,
        sp.plan_name

    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

");

$stmt->execute();

$subscriptions = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!-- =========================================================
PAGE HEADER
========================================================= -->

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-3">

            <div class="col-sm-6">

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

                    School Subscriptions

                </h1>

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

                    Manage active and expired subscriptions

                </p>

            </div>

            <div class="col-sm-6 text-right">

                <a
                    href="assign_subscription.php"
                    class="btn btn-primary shadow-sm"
                >

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

                    Assign Subscription

                </a>

            </div>

        </div>

    </div>

</section>

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

<section class="content">

    <div class="container-fluid">

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

            <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>ID</th>
                                <th>School</th>
                                <th>Plan</th>
                                <th>Start Date</th>
                                <th>Expiry Date</th>
                                <th>Amount</th>
                                <th>Status</th>
                                <th>Payment</th>
                                <th width="180">Actions</th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php if($subscriptions): ?>

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

                                    <tr>

                                        <td>

                                            <?= $subscription['id'] ?>

                                        </td>

                                        <td>

                                            <strong>

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

                                            </strong>

                                        </td>

                                        <td>

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

                                        </td>

                                        <td>

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

                                        </td>

                                        <td>

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

                                        </td>

                                        <td>

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

                                        </td>

                                        <td>

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

                                                <span class="badge badge-success">

                                                    Active

                                                </span>

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

                                                <span class="badge badge-warning">

                                                    Trial

                                                </span>

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

                                                <span class="badge badge-danger">

                                                    Expired

                                                </span>

                                            <?php else: ?>

                                                <span class="badge badge-secondary">

                                                    Suspended

                                                </span>

                                            <?php endif; ?>

                                        </td>

                                        <td>

                                            <?php if($subscription['payment_status'] == 'Paid'): ?>

                                                <span class="badge badge-success">

                                                    Paid

                                                </span>

                                            <?php elseif($subscription['payment_status'] == 'Partial'): ?>

                                                <span class="badge badge-warning">

                                                    Partial

                                                </span>

                                            <?php else: ?>

                                                <span class="badge badge-danger">

                                                    Pending

                                                </span>

                                            <?php endif; ?>

                                        </td>

                                        <td>

                                            <a
                                                href="edit_subscription.php?id=<?= $subscription['id'] ?>"
                                                class="btn btn-sm btn-info"
                                            >

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

                                            </a>

                                            <a
                                                href="renew_subscription.php?id=<?= $subscription['id'] ?>"
                                                class="btn btn-sm btn-success"
                                            >

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

                                            </a>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php else: ?>

                                <tr>

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

                                        No subscriptions found.

                                    </td>

                                </tr>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

</section>

<style>

.card {

    border-radius: 18px;
}

.table {

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

.badge {

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

.btn {

    border-radius: 10px;
}

</style>

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