<?php
require_once __DIR__ . '/../functions.php';
ensure_role(['Admin']);
$users = pdo()->query('SELECT id,name,email,role,status,profile_picture,created_at FROM users ORDER BY created_at DESC')->fetchAll();
?>
<?php include __DIR__ . '/includes/header.php'; ?>
    <section class="panel">
        <header>
            <h2>Users</h2>
            <button class="btn btn-primary" onclick="showUserModal()">Create New User</button>
        </header>
        <div class="table-scroll">
            <table>
                <thead><tr><th></th><th>Name</th><th>Email</th><th>Role</th><th>Status</th><th>Joined</th><th>Actions</th></tr></thead>
                <tbody>
                <?php foreach ($users as $user): ?>
                    <tr data-user='<?= json_encode($user, JSON_HEX_APOS | JSON_HEX_QUOT) ?>'>
                        <td>
                            <?php if (!empty($user['profile_picture'])): ?>
                                <img src="<?= BASE_URL . UPLOAD_URL . '/' . clean($user['profile_picture']) ?>" alt="User pic" class="table-thumb">
                            <?php else: ?>
                                <span class="table-thumb placeholder">No</span>
                            <?php endif; ?>
                        </td>
                        <td><?= clean($user['name']) ?></td>
                        <td><?= clean($user['email']) ?></td>
                        <td><?= clean($user['role']) ?></td>
                        <td><?= clean($user['status']) ?></td>
                        <td><?= clean($user['created_at']) ?></td>
                        <td>
                            <button class="btn btn-secondary" onclick="editUser(this)">Edit</button>
                            <button class="btn btn-danger" onclick="deleteUser(<?= $user['id'] ?>)">Delete</button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
    <div id="user-modal" class="modal hidden">
        <div class="modal-panel">
            <header>
                <h3 id="user-modal-title">Create User</h3>
                <button class="close" onclick="closeModal('user-modal')">×</button>
            </header>
            <form id="user-form" class="ajax-form" method="post" enctype="multipart/form-data">
                <input type="hidden" name="csrf" value="<?= csrf_token() ?>">
                <input type="hidden" name="action" value="create_user">
                <input type="hidden" name="id" value="">
                <label>Name</label>
                <input type="text" name="name" required>
                <label>Email</label>
                <input type="email" name="email" required>
                <label>Profile Picture</label>
                <input type="file" name="profile_picture" accept="image/*,image/svg+xml">
                <label>Password</label>
                <input type="password" name="password" required>
                <label>Role</label>
                <select name="role">
                    <option>Admin</option>
                    <option>Manager</option>
                    <option>Normal User</option>
                </select>
                <label>Status</label>
                <select name="status">
                    <option>Active</option>
                    <option>Inactive</option>
                </select>
                <button type="submit" class="btn btn-primary">Save User</button>
            </form>
        </div>
    </div>
<?php include __DIR__ . '/includes/footer.php'; ?>
