<?php
require_once "config.php";
require_once "auth.php";

// Only admin can access this page
requireAdmin();

$success = false;
$error = null;

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    
    if ($id === $_SESSION['user_id']) {
        $error = "Ne možete obrisati sopstveni nalog.";
    } else {
        $stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
        $stmt->bind_param("i", $id);
        
        if ($stmt->execute()) {
            $success = true;
        } else {
            $error = "Greška prilikom brisanja korisnika.";
        }
    }
}

if (isset($_GET['toggle_status'])) {
    $id = (int)$_GET['toggle_status'];
    
    if ($id === $_SESSION['user_id']) {
        $error = "Ne možete deaktivirati sopstveni nalog.";
    } else {
        $stmt = $conn->prepare("UPDATE users SET active = NOT active WHERE id = ?");
        $stmt->bind_param("i", $id);
        
        if ($stmt->execute()) {
            $success = true;
        } else {
            $error = "Greška prilikom promene statusa korisnika.";
        }
    }
}

$stmt = $conn->prepare("
    SELECT u.*, k.naziv as komitent_naziv 
    FROM users u 
    LEFT JOIN komitenti k ON u.komitent_id = k.id 
    ORDER BY u.name
");
$stmt->execute();
$users = $stmt->get_result();
?>
    <?php include "header.php"; ?>

    <div class="container-fluid mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>Korisnici</h2>
            <a href="create_user.php" class="btn btn-primary"><i class="bi bi-person-plus me-1"></i>Novi korisnik</a>
        </div>

        <?php if ($success): ?>
            <div class="alert alert-success">
                Operacija je uspešno izvršena.
            </div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-danger">
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <div class="table-responsive">
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>Ime i prezime</th>
                        <th>Email</th>
                        <th>Telefon</th>
                        <th>Uloga</th>
                        <th>Komitent</th>
                        <th>Status</th>
                        <th>Poslednja prijava</th>
                        <th>Datum kreiranja</th>
                        <th class="text-center" width="250px">Akcije</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($users->num_rows === 0): ?>
                        <tr>
                            <td colspan="9" class="text-center">Nema korisnika</td>
                        </tr>
                    <?php else: ?>
                        <?php while ($user = $users->fetch_assoc()): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($user['name']); ?></td>
                                <td><?php echo htmlspecialchars($user['email']); ?></td>
                                <td><?php echo htmlspecialchars($user['telefon'] ?? '-'); ?></td>
                                <td>
                                    <span class="badge bg-<?php 
                                        echo $user['role'] === 'admin' ? 'danger' : 
                                            ($user['role'] === 'vozac' ? 'success' : 'primary'); 
                                    ?>">
                                        <?php 
                                        echo $user['role'] === 'admin' ? 'Administrator' : 
                                            ($user['role'] === 'vozac' ? 'Vozač' : 'Korisnik'); 
                                        ?>
                                    </span>
                                </td>
                                <td><?php echo htmlspecialchars($user['komitent_naziv'] ?? 'Nije dodeljen'); ?></td>
                                <td>
                                    <span class="badge bg-<?php echo $user['active'] ? 'success' : 'secondary'; ?>">
                                        <?php echo $user['active'] ? 'Aktivan' : 'Neaktivan'; ?>
                                    </span>
                                </td>
                                <td><?php echo $user['last_login'] ? date('d.m.Y H:i', strtotime($user['last_login'])) : 'Nikad'; ?></td>
                                <td><?php echo date('d.m.Y H:i', strtotime($user['created_at'])); ?></td>
                                <td class="text-center">
                                    <div class="btn-group">
                                        <a href="edit_user.php?id=<?php echo $user['id']; ?>" class="btn btn-dark"><i class="bi bi-pencil-square me-1"></i></a>
                                        <?php if ($user['id'] !== $_SESSION['user_id']): ?>
                                            <a href="?toggle_status=<?php echo $user['id']; ?>" class="btn btn-warning" onclick="return confirm('Da li ste sigurni da želite da <?php echo $user['active'] ? 'deaktivirate' : 'aktivirate'; ?> ovog korisnika?')">
                                                <i class="bi bi-<?php echo $user['active'] ? 'x-circle' : 'check-circle'; ?> me-1"></i><?php echo $user['active'] ? 'Deaktiviraj' : 'Aktiviraj'; ?>
                                            </a>
                                            <a href="?delete=<?php echo $user['id']; ?>" class="btn btn-danger" onclick="return confirm('Da li ste sigurni da želite da obrišete ovog korisnika?')"><i class="bi bi-trash me-1"></i></a>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
<?php include "footer.php"; ?>