<?php

session_start();

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

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

// allow_super_admin();

$page_title = 'Expired Subscriptions';

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

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

/*
|--------------------------------------------------------------------------
| FETCH EXPIRED 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

    WHERE sss.status = 'Expired'

    ORDER BY sss.expiry_date ASC

");

$stmt->execute();

$subscriptions = $stmt->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">

                    Expired Subscriptions

                </h1>

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

                    Schools requiring renewal

                </p>

            </div>

            <div>

                <a
                    href="subscriptions.php"
                    class="btn btn-secondary"
                >

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

                    Back

                </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>Expiry Date</th>
                                <th>Amount</th>
                                <th>Payment</th>
                                <th>Status</th>
                                <th width="140">Action</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>

                                            <span class="text-danger font-weight-bold">

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

                                            </span>

                                        </td>

                                        <td>

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

                                        </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>

                                            <span class="badge badge-danger">

                                                Expired

                                            </span>

                                        </td>

                                        <td>

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

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

                                                Renew

                                            </a>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php else: ?>

                                <tr>

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

                                        <i class="fas fa-check-circle fa-3x text-success mb-3"></i>

                                        <h5>

                                            No Expired Subscriptions

                                        </h5>

                                    </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;
    padding: 6px 12px;
    font-size: 12px;
}

.btn {

    border-radius: 10px;
}

</style>

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