<?php

/**
 * This file is part of JohnCMS Content Management System.
 *
 * @copyright JohnCMS Community
 * @license   https://opensource.org/licenses/GPL-3.0 GPL-3.0
 * @link      https://johncms.com JohnCMS Project
 */

/**
 * @var $title
 * @var $page_title
 * @var $data
 */

$this->layout(
    'system::layout/default',
    [
        'title'      => $title,
        'page_title' => $page_title ?? $title,
    ]
);
?>

<?php if (isset($_POST['submit'])) : ?>
    <?= $this->fetch(
        'system::app/alert',
        [
            'alert_type' => 'alert-success',
            'alert'      => __('Changed'),
        ]
    ) ?>
<?php else : ?>
    <form action="?act=moder&amp;type=<?= $type ?>&amp;id=<?= $id ?>" method="post" enctype="multipart/form-data">
        <?php if ($type === 'article' && $res['cover']) : ?>
            <div class="mb-3">
                <div><img src="../upload/library/images/big/<?= $id ?>.png" alt="screen"/></div>
                <div class="alarm"><a href="?act=del&amp;type=image&amp;id=<?= $id ?>">Удалить обложку</a></div>
                <div class="custom-file mt-2">
                    <input type="file" class="custom-file-input" id="image" name="image" aria-describedby="file_label">
                    <label class="custom-file-label" for="image"><?= __('To upload a photo') ?></label>
                </div>
            </div>
        <?php endif; ?>
        <div class="form-group">
            <label for="name"><?= __('Title') ?></label>
            <input type="text" name="name" class="form-control" id="name" value="<?= $res['name'] ?>" placeholder="<?= __('Title') ?>" required>
        </div>

        <?php if ($type === 'dir') : ?>
            <div class="form-group">
                <label for="description"><?= __('Section description') ?></label>
                <textarea class="form-control" rows="<?= $user->config->fieldHeight ?>" name="description" id="description"><?= $res['description'] ?></textarea>
            </div>
        <?php else: ?>
            <div class="form-group">
                <label for="announce"><?= __('Announce') ?></label>
                <textarea class="form-control" rows="<?= $user->config->fieldHeight ?>" name="announce" id="announce"><?= $res['announce'] ?></textarea>
            </div>
        <?php endif; ?>

        <?php if ($type === 'article') : ?>
            <?php if (mb_strlen($res['text']) < 500000) : ?>
                <div class="form-group">
                    <label for="text"><?= __('Text') ?></label>
                    <div><?= $bbcode ?></div>
                    <textarea class="form-control" rows="<?= $user->config->fieldHeight ?>" name="text" id="text"><?= $res['text'] ?></textarea>
                </div>
            <?php else: ?>
                <div class="alert alert-info"><?= __('The text of the Article can not be edited, a large amount of data !!!') ?></div>
                <input type="hidden" name="text" value="do_not_change">
            <?php endif; ?>
            <div class="form-group">
                <label for="tags"><?= __('Tags') ?></label>
                <input name="tags" class="form-control" type="text" id="tags" value="<?= $res['tags'] ?>">
            </div>
        <?php endif; ?>

        <?php if ($adm) : ?>
            <?php if ($empty) : ?>
                <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" <?= ($res['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" <?= ($res['dir'] === 1 ? 'checked="checked"' : '') ?>>
                    <label class="custom-control-label" for="section_type2"><?= __('Sections') ?></label>
                </div>
            <?php endif; ?>

            <?php if ($type === 'dir' && $res['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" <?= ($res['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" <?= ($res['user_add'] === 0 ? 'checked="checked"' : '') ?>>
                    <label class="custom-control-label" for="user_add2"><?= __('No') ?></label>
                </div>
            <?php endif; ?>
            <?php if ($select) : ?>
                <div class="mt-2">
                    <label for="move"><?= __('Move to Section') ?></label>
                    <select name="move" class="form-control" id="move">
                        <?php foreach ($select as $option) : ?>
                            <?= $option ?>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>
        <?php endif; ?>

        <?php if ($adm) : ?>
            <?php if ($type === 'article') : ?>
                <div class="custom-control custom-checkbox mt-3">
                    <input type="checkbox" class="custom-control-input" name="premod" value="1" id="premod" <?= ($res['premod'] > 0 ? 'checked="checked"' : '') ?>>
                    <label class="custom-control-label" for="premod"><?= __('Verified') ?></label>
                </div>
                <div class="custom-control custom-checkbox">
                    <input type="checkbox" class="custom-control-input" name="comments" value="1" id="comments" <?= ($res['comments'] > 0 ? 'checked="checked"' : '') ?>>
                    <label class="custom-control-label" for="comments"><?= __('Commenting on the Article') ?></label>
                </div>
                <div class="form-group mt-3">
                    <label for="count_views"><?= __('Number of readings') ?></label>
                    <input type="text" name="count_views" id="count_views" class="form-control" value="<?= $res['count_views'] ?>"/>
                </div>
            <?php endif; ?>
        <?php endif; ?>
        <div class="mt-3">
            <input type="submit" name="submit" value="<?= __('Save') ?>" class="btn btn-primary">
            <a href="?do=<?= ($type === 'dir' ? 'dir' : 'text') ?>&amp;id=<?= $id ?>" class="btn btn-secondary"><?= __('Cancel') ?></a>
        </div>
    </form>
<?php endif; ?>
<div class="mt-2"><a href="?do=<?= ($type === 'dir' ? 'dir' : 'text') ?>&amp;id=<?= $id ?>"><?= __('Back') ?></a></div>
