<?php

declare(strict_types=1);

ob_start();

$old = $old ?? [];

?>

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

    <div>

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

        <p class="text-muted mb-0">
            Create a new system user account.
        </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/create">

                    <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(
                                    (string) (
                                        $old['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(
                                    (string) (
                                        $old['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(
                                    (string) (
                                        $old['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>

                                <option value="">
                                    Select Role
                                </option>

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

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

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

                                    </option>

                                <?php endforeach; ?>

                            </select>

                        </div>


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

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

                            <input
                                type="password"
                                name="password"
                                class="form-control"
                                minlength="8"
                                required>

                        </div>


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

                            <label class="form-label">
                                Confirm Password
                            </label>

                            <input
                                type="password"
                                name="password_confirmation"
                                class="form-control"
                                minlength="8"
                                required>

                        </div>

                    </div>


                    <hr>


                    <div class="d-flex justify-content-end gap-2">

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

                            Cancel

                        </a>

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

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

                            Create User

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

</div>

<?php

$content = ob_get_clean();

$title = 'Create User';

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