<?php
session_start();
if (!isset($_SESSION['admin_id']) && $_SESSION['role'] != 'admin') {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

$subscriptions = $conn->query("
    SELECT s.*, p.name as user_name, p.email as user_email
    FROM hps_subscriptions s
    LEFT JOIN patients p ON s.user_id = p.patient_id
    ORDER BY s.created_at DESC
");

$page_title = 'Subscription Management';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Subscription Management</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
    <div class="d-flex justify-content-between mb-4">
        <h2><i class="fas fa-file-contract me-2"></i> HPS Subscriptions</h2>
        <a href="dashboard.php" class="btn btn-secondary">← Back</a>
    </div>
    <div class="card shadow">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead><tr><th>ID</th><th>User</th><th>Plan</th><th>Premium</th><th>Start Date</th><th>End Date</th><th>Status</th></tr></thead>
                    <tbody>
                        <?php while($s = $subscriptions->fetch_assoc()): ?>
                        <tr>
                            <td><?= $s['id'] ?></td>
                            <td><?= htmlspecialchars($s['user_name'] ?? $s['user_id']) ?></td>
                            <td><?= $s['plan_name'] ?></td>
                            <td>₦<?= number_format($s['premium_amount'], 2) ?></td>
                            <td><?= date('M d, Y', strtotime($s['start_date'])) ?></td>
                            <td><?= date('M d, Y', strtotime($s['end_date'])) ?></td>
                            <td><span class="badge bg-<?= $s['status'] == 'active' ? 'success' : 'secondary' ?>"><?= ucfirst($s['status']) ?></span></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php include '../../include/footer.php'; ?>