
<!DOCTYPE html>
<html>

<head>
    <title>Manage Users</title>
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/staff_panel.css">
    <link rel="stylesheet" type="text/css" href="css/background.css">
    <link href="css/dark.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link rel="stylesheet" type="text/css" href="css/sweetalert.css">
    <style>
        .users-table input[type="text"],
        .users-table input[type="password"] {
            padding: 5px;
            border: 1px solid #444;
            border-radius: 5px;
            background-color: rgba(20, 20, 20, 0.5);
            color: #fff;
        }

        .users-table input[type="checkbox"] {
            margin-right: 5px;
        }

        .users-table button {
            padding: 5px;
            font-size: 12px;
            border: none;
            border-radius: 5px;
            background-color: #02a0af;
            color: #fff;
            cursor: pointer;
            transition: background-color 0.3s ease-in-out;
        }

        .users-table button:hover {
            background-color: #018c94;
        }
    </style>
</head>

<body>
    <div class="menu-container">
        <img src="images/frozerlogo.webp" alt="logo" class="logo dash-logo" onclick="goHome()" />

        <div class="dashboard-buttons">
            <a class="dashboard-button" href="/home.php"><img src="/images/icons/home.svg">Home</a>
            <a class="dashboard-button" href="/account.php"><img src="/images/icons/account.svg">Account</a>
            <a class="dashboard-button" href="/subscriptions.php"><img src="/images/icons/subscriptions.svg">Subscriptions</a>
            <a class="dashboard-button" href="/logs.php"><img src="/images/icons/logs.svg">Logs</a>
            <a class="dashboard-button" href="/settings.php"><img src="/images/icons/settings.svg">Settings</a>

            <a class="dashboard-button" href="/staff_panel.php"><img src="/images/icons/staff_panel.svg">Staff Panel</a>
        </div>

        <div class="bottom-menu">
            <div class="menu-user">
                <div class="user-avatar-container">
                    <img class="user-avatar" src="/images/frozerlogo.webp">
                </div>
                <div class="user-info-container">
                    <div class="user-name"><?php echo getUsername(); ?></div>
                    <div class="user-uid">UID: <?php echo getUserId(); ?></div>
                </div>
            </div>
            <img class="logout-button" src="/images/icons/logout.svg" onclick="logout()"></i>
        </div>
    </div>

    <div class="content-container">
        <div class="title-container">
            <h1>Manage Users</h1>
        </div>

        <div class="manage-users-container">
            <form method="post" action="manage_users.php" class="search-form">
                <input type="text" name="search_query" placeholder="Search by username" value="<?php echo htmlspecialchars($searchQuery); ?>">
                <button type="submit" name="search" class="button">Search</button>
            </form>

            <table class="users-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Username</th>
                        <th>Password</th>
                        <th>Staff</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>******</td> 
                        <td><?php echo $user['is_staff'] == 1 ? 'Yes' : 'No'; ?></td>
                        <td>
                            <form method="post" action="manage_users.php" style="display:inline;">
                                <input type="hidden" name="id" value="<?php echo htmlspecialchars($user['id']); ?>">
                                <input type="text" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required>
                                <button type="submit" name="update" class="button">Change Username</button>
                            </form>

                            <form method="post" action="manage_users.php" style="display:inline;">
                                <input type="hidden" name="id" value="<?php echo htmlspecialchars($user['id']); ?>">
                                <input type="password" name="password" placeholder="New password" required>
                                <button type="submit" name="update_password" class="button">Change Password</button>
                            </form>

                            <form method="post" action="manage_users.php" style="display:inline;">
                                <input type="hidden" name="id" value="<?php echo htmlspecialchars($user['id']); ?>">
                                <input type="hidden" name="current_status" value="<?php echo htmlspecialchars($user['is_staff']); ?>">
                                <button type="submit" name="toggle_staff" class="button">
                                    <?php echo $user['is_staff'] == 1 ? 'Remove Staff' : 'Add Staff'; ?>
                                </button>
                            </form>

                            <a href="manage_users.php?delete=<?php echo htmlspecialchars($user['id']); ?>" onclick="return confirm('Are you sure you want to delete this user?')" class="button">Delete</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <script src="js/menu.js"></script>
    <script src="js/redirects.js"></script>
    <script>
        function logout() {
            window.location.href = '/staff_panel.php';
        }

        function goHome() {
            window.location.href = '/home.php';
        }
    </script>
</body>

</html>

<?php
$conn->close();
?>
