<?php
$table = "news";
$fields = [];
$fields["content"] = "json";
$fields["admin_preview"] = "bool";

if (is_post('update')) {
    if ($settings_show_img_news) {
        if (isset($_FILES['img']['tmp_name']) && !empty($_FILES['img']['tmp_name'])) {
            $fields["img"] = "file";
        } else {
            $fields["img"] = "string";
            $_POST['img'] = $_POST['img_link'];
        }
    }
} elseif ($settings_show_img_news) {
    $fields["img"] = "file";
}

if (!DEMO) {
    $report = form_update($table, $fields);
}

if ($settings_show_img_news) {
    $_POST['img_link'] = post('img');
}

$list_languages = get_list_languages();
$count_list_languages = count($list_languages);
if ($count_list_languages > 0) {
    $width = floor(100 / $count_list_languages);
    if ($width > 25) {
        $width = 25;
    } elseif ($width < 5) {
        $width = 5;
    }
    $width = 'width: '.$width.'%;';
}

$first_item = true;
?>

<div id="page-wrapper">
    <div class="container-fluid">
        <!-- Flot Charts -->
        <div class="row">
            <div class="col-lg-12">
                <h2 class="page-header">Изменить новость</h2>
            </div>
        </div>
        <!-- /.row -->

        <?php echo flash_show('adg_news'); ?>

        <!-- Редактирование новостей -->
        <div class="row">
            <div class="col-lg-12">
                <?php if (isset($report) && $report['message'] != '') { ?>
                    <div class="alert alert-<?php echo $report['error'] ? 'danger' : 'success'; ?> alert-dismissable">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                        <?php echo $report['message']; ?>
                    </div>
                <?php } ?>

                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> Изменить</h3>
                    </div>
                    <div class="panel-body">
                        <div class="flot-chart">
                            <div class="flot-chart-content" id="flot-line-chart">
                                <div>
                                    <form enctype="multipart/form-data" action = "" method = "POST">
                                        <div class="bs-example bs-example-tabs">
                                            <ul id="myTab_news" class="nav nav-tabs">
                                                <li style="<?php echo escape($width); ?>" class="active">
                                                    <a href="#news_ru" data-toggle="tab">
                                                        Русский
                                                    </a>
                                                </li>
                                                <?php foreach ($list_languages as $key => $value) {
                                                    if ($key != 'ru') { ?>
                                                        <li style="<?php echo escape($width); ?>">
                                                            <a href="#news_<?php echo strtolower(escape($key)); ?>"
                                                                    data-toggle="tab">
                                                                <?php foreach ($value as $item) {
                                                                    echo escape($item);
                                                                } ?>
                                                            </a>
                                                        </li>
                                                    <?php }
                                                }?>
                                            </ul>
                                            <br>
                                            <div id="myTabContent_news" class="tab-content">

                                                <?php $key = 'ru'; ?>
                                                <div
                                                        class="tab-pane fade <?php if ($key == 'ru') { echo 'active in'; } ?>"
                                                        id="news_<?php echo strtolower(escape($key)); ?>">

                                                    <label><?php echo T('Название'); ?></label><br>
                                                    <?php if (!isset(post('content')[$key]['title'])) {
                                                        $_POST['content'][$key]['title'] = '';
                                                    } ?>
                                                    <input class="form-control" style="width: 100%;" type="text" name="content[<?php echo $key; ?>][title]" title="<?php echo T('Название'); ?>" value="<?php echo escape(post('content')[$key]['title']); ?>">
                                                    <br>
                                                    <label>Описание</label>
                                                    <?php if (!isset(post('content')[$key]['description'])) {
                                                        $_POST['content'][$key]['description'] = '';
                                                    } ?>
                                                    <textarea name="content[<?php echo $key; ?>][description]" rows="8" cols="40" class="ckeditor" style="width: 100%;"><?php echo html(post('content')[$key]['description']); ?></textarea>
                                                    <br>
                                                    <label>Текст новости</label>
                                                    <?php if (!isset(post('content')[$key]['text'])) {
                                                        $_POST['content'][$key]['text'] = '';
                                                    } ?>
                                                    <textarea name="content[<?php echo $key; ?>][text]" rows="8" cols="40" class="ckeditor" style="width: 100%;"><?php echo html(post('content')[$key]['text']); ?></textarea>
                                                    <br>

                                                </div>

                                                <?php
                                                foreach ($list_languages as $key => $value) {
                                                    if ($key != 'ru') {
                                                        foreach ($value as $item) {
                                                            $title = $item;
                                                        } ?>

                                                        <div
                                                                class="tab-pane fade"
                                                                id="news_<?php echo strtolower(escape($key)); ?>">

                                                            <label><?php echo T('Название'); ?></label><br>
                                                            <?php if (!isset(post('content')[$key]['title'])) {
                                                                $_POST['content'][$key]['title'] = '';
                                                            } ?>
                                                            <input class="form-control" style="width: 100%;" type="text"
                                                                    name="content[<?php echo $key; ?>][title]"
                                                                    title="<?php echo T('Название'); ?>"
                                                                    value="<?php echo escape(post('content')[$key]['title']); ?>">
                                                            <br>
                                                            <label>Превью</label>
                                                            <?php if (!isset(post('content')[$key]['description'])) {
                                                                $_POST['content'][$key]['description'] = '';
                                                            } ?>
                                                            <textarea name="content[<?php echo $key; ?>][description]" rows="8" cols="40" class="ckeditor" style="width: 100%;"><?php echo html(post('content')[$key]['description']); ?></textarea>
                                                            <br>
                                                            <label>Контент</label>
                                                            <?php if (!isset(post('content')[$key]['text'])) {
                                                                $_POST['content'][$key]['text'] = '';
                                                            } ?>
                                                            <textarea name="content[<?php echo $key; ?>][text]" rows="8" cols="40" class="ckeditor" style="width: 100%;"><?php echo html(post('content')[$key]['text']); ?></textarea>

                                                            <br>
                                                        </div>
                                                    <?php }
                                                } ?>

                                                <br>
                                                <?php if ($settings_show_img_news) { ?>
                                                    <?php include (__DIR__."/demo.php"); ?>
                                                    <?php if (!empty(post('img'))) { ?>
                                                        <img src="<?php echo images_thumbnail_resize(post("img"), 300, 200); ?>" width=300 height=200><br>
                                                    <?php } ?>
                                                    <input style="width: 400px; " type = "file" name = "img" placeholder = "Прикрепить фото">
                                                    <?php echo T('или'); ?>
                                                    <label><?php echo T('Введите ссылку на картинку'); ?></label>
                                                    <input type="url" name="img_link" value="<?php echo post('img_link'); ?>" class="form-control">
                                                    <br>
                                                <?php } ?>

                                                <b><?php input_checkbox('admin_preview', ''); echo T('Предпросмотр'); ?></b>
                                                <br><br>

                                                <!-- скрытое поле -->
                                                <?php input("id", "", "hidden", "", ""); ?>
                                                <?php button_update(); ?>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>