<?php

declare(strict_types=1);

ob_start();

?>

<div class="d-flex justify-content-between align-items-center mb-4">

    <div>

        <h2 class="mb-1">
            User Management
        </h2>

        <p class="text-muted mb-0">
            Manage system user accounts.
        </p>

    </div>

    <a
        href="/public/users/create"
        class="btn btn-primary">

        <i class="bi bi-person-plus me-1"></i>

        Add User

    </a>

</div>
<?php if (!empty($created)): ?>

    <div class="alert alert-success alert-dismissible fade show">

        User created successfully.

        <button
            type="button"
            class="btn-close"
            data-bs-dismiss="alert">
        </button>

    </div>

<?php endif; ?>

<div class="card shadow-sm border-0">

    <div class="card-header bg-white py-3">

        <h5 class="mb-0">
            Users
        </h5>

    </div>

    <div class="card-body">

        <div class="table-responsive">

            <table class="table table-hover align-middle">

                <thead>

                <tr>

                    <th>ID</th>

                    <th>Full Name</th>

                    <th>Username</th>

                    <th>Email</th>

                    <th>Status</th>

                    <th>Created</th>



                </tr>

                </thead>

                <tbody>
<?php if (!empty($updated)): ?>

    <div class="alert alert-success">
        User updated successfully.
    </div>

<?php endif; ?>

<?php if (!empty($statusChanged)): ?>

    <div class="alert alert-success">
        User status updated successfully.
    </div>

<?php endif; ?>

<?php if (!empty($passwordReset)): ?>

    <div class="alert alert-success">
        Password reset successfully.
    </div>

<?php endif; ?>

<?php if (!empty($error)): ?>

    <div class="alert alert-danger">

        <?= htmlspecialchars(
            $error,
            ENT_QUOTES,
            'UTF-8'
        ) ?>

    </div>

<?php endif; ?>
                <?php if (empty($users)): ?>

                    <tr>

                        <td
                            colspan="7"
                            class="text-center text-muted py-4">

                            No users found.

                        </td>

                    </tr>

                <?php else: ?>

                    <?php foreach ($users as $user): ?>

                        <tr>

                            <td>
                                <?= (int) $user['id'] ?>
                            </td>
<td class="text-end">

    <div class="btn-group">

        <a
            href="/public/users/edit?id=<?= (int) $user['id'] ?>"
            class="btn btn-sm btn-outline-primary"
            title="Edit">

            <i class="bi bi-pencil"></i>

        </a>

        <a
            href="/public/users/reset-password?id=<?= (int) $user['id'] ?>"
            class="btn btn-sm btn-outline-warning"
            title="Reset Password">

            <i class="bi bi-key"></i>

        </a>

        <?php if (
            (int) $user['id'] !==
            (int) ($_SESSION['user_id'] ?? 0)
        ): ?>

            <form
                method="POST"
                action="/public/users/toggle-status"
                class="d-inline">

                <input
                    type="hidden"
                    name="id"
                    value="<?= (int) $user['id'] ?>">

                <button
                    type="submit"
                    class="btn btn-sm <?= (int) $user['active'] === 1
                        ? 'btn-outline-danger'
                        : 'btn-outline-success' ?>">

                    <i class="bi <?= (int) $user['active'] === 1
                        ? 'bi-person-x'
                        : 'bi-person-check' ?>">
                    </i>

                </button>

            </form>

        <?php endif; ?>

    </div>

</td>
                            <td>

                                <?= htmlspecialchars(
                                    $user['full_name'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    $user['username'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    $user['email'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?php if ((int) $user['active'] === 1): ?>

                                    <span class="badge text-bg-success">
                                        Active
                                    </span>

                                <?php else: ?>

                                    <span class="badge text-bg-secondary">
                                        Inactive
                                    </span>

                                <?php endif; ?>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    (string) $user['created_at'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td class="text-end">

                                <a
                                    href="/public/users/edit?id=<?= (int) $user['id'] ?>"
                                    class="btn btn-sm btn-outline-primary">

                                    <i class="bi bi-pencil"></i>

                                </a>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

<?php

$content = ob_get_clean();

$title = 'User Management';

include APP_PATH .
    '/Modules/Administration/Views/layouts/admin.php';