<?php
session_start();

// Periksa apakah pengguna sudah login
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    header("Location: login.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User List</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Poppins', sans-serif;
            background: #141414;
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .center {
            width: 80%;
            padding: 20px;
            background: #191919;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #fff;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background: #03e9f4;
            color: #000;
        }
        .btn {
            padding: 10px 20px;
            background: #03e9f4;
            border: none;
            border-radius: 5px;
            color: #000;
            cursor: pointer;
            transition: .3s;
            text-decoration: none;
        }
        .btn:hover {
            background: #039bc4;
        }
        .btn-small {
            padding: 5px 10px;
            margin: 0 5px;
            background: #03e9f4;
            border: none;
            border-radius: 5px;
            color: #000;
            cursor: pointer;
            transition: .3s;
            text-decoration: none;
        }
        .btn-small:hover {
            background: #039bc4;
        }
    </style>
</head>
<body>
    <div class="center">
        <h1>User List</h1>
        <table>
            <thead>
                <tr>
                    <th>Username</th>
                    <th>Password</th>
                    <th>No Handphone</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody id="userTableBody">
                <!-- User data will be inserted here -->
            </tbody>
        </table>
        <a href="signup.html" class="btn">Add New User</a>
        <a href="logout.php" class="btn">Logout</a>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            function loadUserData() {
                const userData = JSON.parse(localStorage.getItem('userData'));
                const userTableBody = document.getElementById('userTableBody');
                userTableBody.innerHTML = '';

                if (userData) {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>${userData.username}</td>
                        <td>${userData.password}</td>
                        <td>${userData.nohp}</td>
                        <td>
                            <button class="btn-small" onclick="editUser()">Edit</button>
                            <button class="btn-small" onclick="deleteUser()">Delete</button>
                        </td>
                    `;
                    userTableBody.appendChild(row);
                } else {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td colspan="4" style="text-align: center;">No user data found</td>
                    `;
                    userTableBody.appendChild(row);
                }
            }

            window.editUser = function() {
                const userData = JSON.parse(localStorage.getItem('userData'));
                if (userData) {
                    const newUsername = prompt("Enter new username:", userData.username);
                    const newPassword = prompt("Enter new password:", userData.password);
                    const newNohp = prompt("Enter new No Handphone:", userData.nohp);

                    if (newUsername !== null && newPassword !== null && newNohp !== null) {
                        userData.username = newUsername;
                        userData.password = newPassword;
                        userData.nohp = newNohp;
                        localStorage.setItem('userData', JSON.stringify(userData));
                        loadUserData();
                    }
                }
            }

            window.deleteUser = function() {
                if (confirm("Are you sure you want to delete this user?")) {
                    localStorage.removeItem('userData');
                    loadUserData();
                }
            }

            loadUserData();
        });
    </script>
</body>
</html>
