<?php
require_once 'init.php';

// Only admin can access this page
if ($_SESSION['role'] !== 'admin') {
    Helper::redirect(APP_URL . '/dashboard.php');
}

// Get all users with their subscription info
$users = $database->select("
    SELECT
        u.id,
        u.name,
        u.email,
        u.company_name,
        u.created_at,
        s.status as sub_status,
        s.plan_type,
        s.trial_ends_at,
        s.end_date,
        s.created_at as sub_created_at
    FROM users u
    LEFT JOIN subscriptions s ON u.id = s.user_id
    WHERE u.role != 'admin'
    ORDER BY u.created_at DESC
");

// Group by user (since there might be multiple subscriptions)
$userList = [];
foreach ($users as $user) {
    if (!isset($userList[$user['id']])) {
        $userList[$user['id']] = $user;
    }
}

$pageTitle = 'Admin - Daftar User';
?>
<?php include '../layout/header.php'; ?>
    <?php include '../layout/navbar.php'; ?>

    <div class="container-fluid mt-4 px-3 px-md-4" style="max-width:1200px;margin-left:auto;margin-right:auto;">
        <div class="page-header mb-4">
            <h1><i class="fas fa-users me-2" style="color:var(--primary)"></i>Daftar User Terdaftar</h1>
        </div>

        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover table-mobile-card">
                        <thead class="table-light">
                            <tr>
                                <th>Nama User</th>
                                <th>Email</th>
                                <th>Perusahaan</th>
                                <th>Tanggal Daftar</th>
                                <th>Status Langganan</th>
                                <th>Paket</th>
                                <th>Tanggal Expired</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($userList)): ?>
                                <?php foreach ($userList as $user): ?>
                                <tr>
                                    <td data-label="Nama User">
                                        <strong><?php echo htmlspecialchars($user['name']); ?></strong>
                                    </td>
                                    <td data-label="Email">
                                        <?php echo htmlspecialchars($user['email']); ?>
                                    </td>
                                    <td data-label="Perusahaan">
                                        <?php echo htmlspecialchars($user['company_name'] ?? '-'); ?>
                                    </td>
                                    <td data-label="Tanggal Daftar">
                                        <?php echo date('d M Y', strtotime($user['created_at'])); ?>
                                    </td>
                                    <td data-label="Status Langganan">
                                        <?php if (!$user['sub_status']): ?>
                                            <span class="badge bg-secondary">Belum Langganan</span>
                                        <?php elseif ($user['sub_status'] === 'trial'): ?>
                                            <span class="badge bg-info">
                                                <i class="fas fa-crown me-1"></i>Trial
                                            </span>
                                        <?php elseif ($user['sub_status'] === 'active'): ?>
                                            <span class="badge bg-success">
                                                <i class="fas fa-check-circle me-1"></i>Aktif
                                            </span>
                                        <?php elseif ($user['sub_status'] === 'expired'): ?>
                                            <span class="badge bg-danger">
                                                <i class="fas fa-times-circle me-1"></i>Expired
                                            </span>
                                        <?php else: ?>
                                            <span class="badge bg-warning"><?php echo htmlspecialchars($user['sub_status']); ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td data-label="Paket">
                                        <?php if ($user['plan_type'] === 'yearly'): ?>
                                            <span class="badge bg-primary">Tahunan</span>
                                        <?php elseif ($user['plan_type'] === 'monthly'): ?>
                                            <span class="badge bg-primary">Bulanan</span>
                                        <?php else: ?>
                                            <span class="badge bg-secondary">-</span>
                                        <?php endif; ?>
                                    </td>
                                    <td data-label="Tanggal Expired">
                                        <?php
                                            if ($user['sub_status'] === 'trial' && $user['trial_ends_at']) {
                                                echo date('d M Y', strtotime($user['trial_ends_at']));
                                            } elseif ($user['sub_status'] === 'active' && $user['end_date']) {
                                                echo date('d M Y', strtotime($user['end_date']));
                                            } else {
                                                echo '-';
                                            }
                                        ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="7" class="text-center text-muted py-4">
                                        Belum ada user terdaftar
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <div class="mt-3 text-muted" style="font-size:13px">
                    <strong>Total User:</strong> <?php echo count($userList); ?>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
