<?php
// Include config file
require_once "../includes/config.php";
require_once "../includes/session.php";

// Check if the user is an admin
checkAdmin();

// Process user approval
if (isset($_GET["action"]) && $_GET["action"] == "approve" && isset($_GET["id"])) {
    $id = $_GET["id"];
    
    $sql = "UPDATE users SET is_approved = 1 WHERE id = ?";
    
    if ($stmt = mysqli_prepare($conn, $sql)) {
        mysqli_stmt_bind_param($stmt, "i", $id);
        
        if (mysqli_stmt_execute($stmt)) {
            header("location: index.php?approved=1");
            exit;
        } else {
            echo "Error: " . mysqli_error($conn);
        }
        
        mysqli_stmt_close($stmt);
    }
}

// Process user deletion
if (isset($_GET["action"]) && $_GET["action"] == "delete" && isset($_GET["id"])) {
    $id = $_GET["id"];
    
    // Prevent admin from deleting themselves
    if ($id == $_SESSION["id"]) {
        header("location: index.php?error=1");
        exit;
    }
    
    $sql = "DELETE FROM users WHERE id = ?";
    
    if ($stmt = mysqli_prepare($conn, $sql)) {
        mysqli_stmt_bind_param($stmt, "i", $id);
        
        if (mysqli_stmt_execute($stmt)) {
            header("location: index.php?deleted=1");
            exit;
        } else {
            echo "Error: " . mysqli_error($conn);
        }
        
        mysqli_stmt_close($stmt);
    }
}

// Fetch all users
$users = [];
$sql = "SELECT * FROM users ORDER BY created_at DESC";
$result = mysqli_query($conn, $sql);
if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        $users[] = $row;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Panel - Stednja App</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8f9fa;
            padding: 20px;
        }
        .wrapper {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
        }
        .table th {
            background-color: #f8f9fa;
        }
        .action-buttons {
            display: flex;
            gap: 5px;
        }
        .action-buttons a {
            padding: 2px 8px;
        }
        .badge-admin {
            background-color: #dc3545;
            color: white;
        }
        .badge-user {
            background-color: #28a745;
            color: white;
        }
        .badge-pending {
            background-color: #ffc107;
            color: #212529;
        }
        .badge-approved {
            background-color: #28a745;
            color: white;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="header">
            <h2>Admin Panel</h2>
            <div>
                <a href="../index.php" class="btn btn-primary">Back to App</a>
                <a href="../logout.php" class="btn btn-danger">Sign Out</a>
            </div>
        </div>
        
        <?php if (isset($_GET["approved"])) { ?>
            <div class="alert alert-success">User approved successfully!</div>
        <?php } ?>
        
        <?php if (isset($_GET["deleted"])) { ?>
            <div class="alert alert-success">User deleted successfully!</div>
        <?php } ?>
        
        <?php if (isset($_GET["error"])) { ?>
            <div class="alert alert-danger">You cannot delete your own account!</div>
        <?php } ?>
        
        <h4>User Management</h4>
        <div class="mb-3">
            <a href="../register.php" class="btn btn-success"><i class="fas fa-user-plus"></i> Add New User</a>
            <a href="#" class="btn btn-info" onclick="refreshTable(); return false;"><i class="fas fa-sync"></i> Refresh</a>
        </div>
        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Username</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th>Created At</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $user) { ?>
                    <tr>
                        <td><?php echo htmlspecialchars($user["id"]); ?></td>
                        <td><?php echo htmlspecialchars($user["username"]); ?></td>
                        <td>
                            <span class="badge <?php echo $user["role"] == "admin" ? "badge-admin" : "badge-user"; ?>">
                                <?php echo ucfirst($user["role"]); ?>
                            </span>
                        </td>
                        <td>
                            <span class="badge <?php echo $user["is_approved"] ? "badge-approved" : "badge-pending"; ?>">
                                <?php echo $user["is_approved"] ? "Approved" : "Pending"; ?>
                            </span>
                        </td>
                        <td><?php echo htmlspecialchars($user["created_at"]); ?></td>
                        <td class="action-buttons">
                            <?php if (!$user["is_approved"] && $user["role"] != "admin") { ?>
                                <a href="index.php?action=approve&id=<?php echo $user["id"]; ?>" class="btn btn-sm btn-success"><i class="fas fa-check"></i> Approve</a>
                            <?php } ?>
                            <?php if ($user["id"] != $_SESSION["id"]) { ?>
                                <a href="index.php?action=delete&id=<?php echo $user["id"]; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this user?');"><i class="fas fa-trash"></i> Delete</a>
                            <?php } ?>
                        </td>
                    </tr>
                <?php } ?>
                <?php if (empty($users)) { ?>
                    <tr>
                        <td colspan="5" class="text-center">No users found</td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
    <script>
        function refreshTable() {
            window.location.reload();
        }
    </script>
</body>
</html>
