<?php

declare(strict_types=1);

ob_start();

?>

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

    <div>
        <h2 class="mb-1">Edit User</h2>
        <p class="text-muted mb-0">
            Update account information and assigned role.
        </p>
    </div>

    <a
        href="/public/users"
        class="btn btn-outline-secondary">

        <i class="bi bi-arrow-left me-1"></i>
        Back

    </a>

</div>

<div class="row">

    <div class="col-xl-8">

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

            <div class="card-body p-4">

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

                    <div class="alert alert-danger">

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

                    </div>

                <?php endif; ?>

                <form
                    method="POST"
                    action="/public/users/edit">

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

                    <div class="row">

                        <div class="col-md-6 mb-3">

                            <label class="form-label">
                                Full Name
                            </label>

                            <input
                                type="text"
                                name="full_name"
                                class="form-control"
                                value="<?= htmlspecialchars(
                                    $user['full_name'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>"
                                required>

                        </div>

                        <div class="col-md-6 mb-3">

                            <label class="form-label">
                                Username
                            </label>

                            <input
                                type="text"
                                name="username"
                                class="form-control"
                                value="<?= htmlspecialchars(
                                    $user['username'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>"
                                required>

                        </div>

                        <div class="col-md-6 mb-3">

                            <label class="form-label">
                                Email
                            </label>

                            <input
                                type="email"
                                name="email"
                                class="form-control"
                                value="<?= htmlspecialchars(
                                    $user['email'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>"
                                required>

                        </div>

                        <div class="col-md-6 mb-3">

                            <label class="form-label">
                                Role
                            </label>

                            <select
                                name="role_id"
                                class="form-select"
                                required>

                                <?php foreach ($roles as $role): ?>

                                    <option
                                        value="<?= (int) $role['id'] ?>"
                                        <?= (int) $userRoleId ===
                                            (int) $role['id']
                                            ? 'selected'
                                            : '' ?>>

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

                                    </option>

                                <?php endforeach; ?>

                            </select>

                        </div>

                        <div class="col-12 mb-3">

                            <div class="form-check form-switch">

                                <input
                                    class="form-check-input"
                                    type="checkbox"
                                    name="active"
                                    value="1"
                                    id="active"
                                    <?= (int) $user['active'] === 1
                                        ? 'checked'
                                        : '' ?>>

                                <label
                                    class="form-check-label"
                                    for="active">

                                    Active Account

                                </label>

                            </div>

                        </div>

                    </div>

                    <hr>

                    <div class="d-flex justify-content-between">

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

                            <i class="bi bi-key me-1"></i>
                            Reset Password

                        </a>

                        <button
                            type="submit"
                            class="btn btn-primary">

                            <i class="bi bi-check-lg me-1"></i>
                            Save Changes

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

</div>

<?php

$content = ob_get_clean();

$title = 'Edit User';

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