<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION["role"]) || $_SESSION["role"] !== "superadmin") {
   header("Location: ../html/superadmin.php?page=applicants");
    exit();
}

require_once "../php/db_connect.php";

$sql = "SELECT * FROM users WHERE role = 'admin' AND status = 'pending' ORDER BY id DESC";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Applicant Requests</title>
    <link rel="stylesheet" href="../css/applicant-request.css?v=1">
</head>
<body>

    <div class="page-container">
        <h1>Applicant Requests</h1>
        <p class="subtitle">Review pending shop owner applications.</p>

        <div class="table-wrapper">
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Shop Name</th>
                        <th>Shop Type</th>
                        <th>Email</th>
                        <th>Mobile</th>
                        <th>Permit No.</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($result && $result->num_rows > 0): ?>
                        <?php while ($row = $result->fetch_assoc()): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($row["id"]); ?></td>
                                <td><?php echo htmlspecialchars(($row["first_name"] ?? '') . ' ' . ($row["last_name"] ?? '')); ?></td>
                                <td><?php echo htmlspecialchars($row["shop_name"] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row["shop_type"] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row["email"] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row["mobile_number"] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row["business_permit_number"] ?? ''); ?></td>
                                <td class="actions">
                                    <a class="view-btn" href="view-applicant.php?id=<?php echo $row["id"]; ?>">View</a>

                                    <a class="approve-btn" href="../php/update-applicant-status.php?id=<?php echo $row["id"]; ?>&action=approve"
                                       onclick="return confirm('Approve this applicant?');">Approve</a>

                                    <a class="reject-btn" href="../php/update-applicant-status.php?id=<?php echo $row["id"]; ?>&action=reject"
                                       onclick="return confirm('Reject this applicant?');">Reject</a>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="8" class="no-data">No pending applicant requests found.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

       
    </div>

</body>
</html>