<?= $this->include('partials/html') ?>

<head>
    <?php echo view("partials/title-meta", array("title" => $title)) ?>
    <?= $this->include('partials/head-css') ?>
</head>

<body>
    <div class="wrapper">
        <?= $this->include('partials/menu') ?>

        <div class="page-content">
            <div class="page-container">
                <div class="row">
                    <div class="col-12">
                        <div class="page-title-box">
                            <h4 class="page-title"><?= esc($title) ?></h4>
                        </div>
                    </div>
                </div>

                <?php if (session()->has('errors')): ?>
                    <?php foreach (session('errors') as $error): ?>
                        <div class="alert alert-danger"><?= $error ?></div>
                    <?php endforeach; ?>
                <?php endif; ?>

                <div class="row">
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <form action="/admin/roles/store" method="post">
                                    <?= csrf_field() ?>

                                    <div class="mb-3">
                                        <label class="form-label">Nom</label>
                                        <input type="text" name="name" class="form-control" value="<?= old('name') ?>" required>
                                    </div>

                                    <div class="mb-3">
                                        <label class="form-label">Slug (identifiant unique)</label>
                                        <input type="text" name="slug" class="form-control" value="<?= old('slug') ?>" required>
                                    </div>

                                    <div class="mb-3">
                                        <label class="form-label">Description</label>
                                        <textarea name="description" class="form-control" rows="3"><?= old('description') ?></textarea>
                                    </div>

                                    <div class="mb-3">
                                        <label class="form-label">Permissions</label>
                                        <?php foreach ($permissions as $permission): ?>
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" name="permissions[]" value="<?= $permission['id'] ?>" id="perm_<?= $permission['id'] ?>" <?= in_array($permission['id'], old('permissions', [])) ? 'checked' : '' ?>>
                                                <label class="form-check-label" for="perm_<?= $permission['id'] ?>">
                                                    <?= esc($permission['name']) ?> <code>(<?= esc($permission['slug']) ?>)</code>
                                                </label>
                                            </div>
                                        <?php endforeach; ?>
                                    </div>

                                    <button type="submit" class="btn btn-primary">Créer</button>
                                    <a href="/admin/roles" class="btn btn-secondary">Annuler</a>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <?= $this->include('partials/footer-scripts') ?>
</body>

</html>
