<?php

/**
 * @var int                                                           $id
 * @var bool                                                          $saved
 * @var \Johncms\Modules\Library\Domain\Models\LibraryCategory|null  $category
 * @var \Illuminate\Database\Eloquent\Collection|null                 $parentSections
 * @var bool|null                                                     $isEmpty
 * @var \Johncms\Users\User                                           $user
 */

$this->layout('system::layout/default');
?>

<?php if ($saved) : ?>
    <div class="alert alert-success"><?= __('Changes saved successfully.') ?></div>
    <div class="mt-2"><a href="<?= $category_url ?>"><?= __('Back to Section') ?></a></div>
<?php else : ?>
    <form action="/library/section/<?= $id ?>/edit" method="post">
        <div class="form-group">
            <label for="name"><?= __('Title') ?></label>
            <input type="text" name="name" class="form-control" id="name"
                   value="<?= $this->e($category->name) ?>"
                   placeholder="<?= __('Title') ?>" required>
        </div>

        <div class="form-group">
            <label for="description"><?= __('Section description') ?></label>
            <textarea class="form-control" rows="<?= $user->config->fieldHeight ?>"
                      name="description" id="description"><?= $this->e($category->description ?? '') ?></textarea>
        </div>

        <?php if ($isEmpty) : ?>
            <h3 class="mt-3"><?= __('Section type') ?></h3>
            <div class="custom-control custom-radio">
                <input type="radio" id="section_type1" name="dir" value="0" class="custom-control-input"
                    <?= (int) $category->dir === 0 ? 'checked="checked"' : '' ?>>
                <label class="custom-control-label" for="section_type1"><?= __('Articles') ?></label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" id="section_type2" name="dir" value="1" class="custom-control-input"
                    <?= (int) $category->dir === 1 ? 'checked="checked"' : '' ?>>
                <label class="custom-control-label" for="section_type2"><?= __('Sections') ?></label>
            </div>
        <?php endif; ?>

        <?php if ((int) $category->dir === 0) : ?>
            <h3 class="mt-3"><?= __('Allow users to add their Articles?') ?></h3>
            <div class="custom-control custom-radio">
                <input type="radio" id="user_add1" name="user_add" value="1" class="custom-control-input"
                    <?= (int) $category->user_add === 1 ? 'checked="checked"' : '' ?>>
                <label class="custom-control-label" for="user_add1"><?= __('Yes') ?></label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" id="user_add2" name="user_add" value="0" class="custom-control-input"
                    <?= (int) $category->user_add === 0 ? 'checked="checked"' : '' ?>>
                <label class="custom-control-label" for="user_add2"><?= __('No') ?></label>
            </div>
        <?php endif; ?>

        <?php if ($parentSections->isNotEmpty()) : ?>
            <div class="mt-2">
                <label for="move"><?= __('Move to Section') ?></label>
                <select name="move" class="form-control" id="move">
                    <option value="0" <?= (int) $category->parent === 0 ? 'selected="selected"' : '' ?>>
                        <?= __('The ROOT') ?>
                    </option>
                    <?php foreach ($parentSections as $section) : ?>
                        <option value="<?= $section->id ?>"
                            <?= (int) $category->parent === (int) $section->id ? 'selected="selected"' : '' ?>>
                            <?= $this->e($section->name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
        <?php endif; ?>

        <div class="mt-3">
            <input type="submit" name="submit" value="<?= __('Save') ?>" class="btn btn-primary">
            <a href="<?= $category_url ?>" class="btn btn-secondary"><?= __('Cancel') ?></a>
        </div>
    </form>
<?php endif; ?>
