<?php
include 'header.php';
require_once '../db/config.php';

// Check if the user is logged in as an admin
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

$pdo = db();

// Fetch all drivers with user information
$stmt = $pdo->query("
    SELECT d.id, d.full_name, d.phone_number, d.vehicle_details, d.approval_status, u.email
    FROM drivers d
    JOIN users u ON d.user_id = u.id
    ORDER BY d.created_at DESC
");
$drivers = $stmt->fetchAll();

$possible_statuses = ['pending', 'approved', 'rejected'];
?>

<div class="container mt-4">
    <h2>Driver Management</h2>

    <table class="table table-striped">
        <thead>
            <tr>
                <th>Driver ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Vehicle</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($drivers as $driver): ?>
                <tr>
                    <td><?php echo $driver['id']; ?></td>
                    <td><?php echo htmlspecialchars($driver['full_name']); ?></td>
                    <td><?php echo htmlspecialchars($driver['email']); ?></td>
                    <td><?php echo htmlspecialchars($driver['phone_number']); ?></td>
                    <td><?php echo htmlspecialchars($driver['vehicle_details']); ?></td>
                    <td><?php echo htmlspecialchars($driver['approval_status']); ?></td>
                    <td>
                        <?php if ($driver['approval_status'] == 'pending'): ?>
                            <a href="update_driver_status.php?id=<?php echo $driver['id']; ?>&status=approved" class="btn btn-success btn-sm">Approve</a>
                            <a href="update_driver_status.php?id=<?php echo $driver['id']; ?>&status=rejected" class="btn btn-danger btn-sm">Reject</a>
                        <?php else: ?>
                            N/A
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php include 'footer.php'; ?>
