<?php
if (is_post('preview_onoff')) {
    csrf_check_token();
    $news_id = post('id');
    $admin_preview = get_value("SELECT `admin_preview` FROM `news` WHERE `id` = ".escape_db($news_id));
    $new_admin_preview = !$admin_preview;
    $sql = "UPDATE `news` SET `admin_preview` = ".escape_db($new_admin_preview)." WHERE `id` = ".escape_db($news_id);
    if ($mysqli->query($sql)) {
        $report_add = message_success('Предпросмотр успешно ' . ($new_admin_preview ? 'включен' : 'выключен'));
    } else {
        $report_add = message_error(T("Ошибка смены статуса!"));
    }
}


$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.'%;';
} ?>

<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_add) && $report_add['message'] != '') { ?>
                <div class="alert alert-<?php echo $report_add['error'] ? 'danger' : 'success'; ?> alert-dismissable">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <?php echo $report_add['message']; ?>
                </div>
                <?php } ?>

    			<div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> <?php echo T('Добавить'); ?></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="/handlers/adg/news.php" method = "POST">
                                        <?php echo form_init(); ?>
                                        <div class="bs-example bs-example-tabs">
                                            <ul id="myTab_news" class="nav nav-tabs">
                                                <?php /* Русский выводим отдельно */ ?>
                                                <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 <?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]" class="ckeditor"><?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]" class="ckeditor"><?php echo html(post('content')[$key]['text']); ?></textarea>
                                                            <br>

                                                        </div>
                                                    <?php }
                                                } ?>

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

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

                                                <?php button_create(); ?>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                  </div>
                </div>
    		</div>
        </div>
    	<!-- /Добавление новостей -->

    	<!-- Flot Charts -->
        <div class="row">
            <div class="col-lg-12">
                <h2 class="page-header"><?php echo T('Список Новостей'); ?></h2>
            </div>
        </div>
        <!-- /.row -->

        <!-- Список новостей -->
        <div class="row">
            <div class="col-lg-12">
                <?php include(ROOT."/adg/pages/show_report.php"); ?>

                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> <?php echo T('Новости'); ?></h3>
                    </div>
                    <div class="panel-body">
                        <div class="flot-chart">
                            <div class="flot-chart-content" id="flot-line-chart">
                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th width="200"><?php echo T('Дата'); ?></th>
                                                <th><?php echo T('Название'); ?></th>
                                                <th><?php echo T('Превью'); ?></th>
                                                <th><?php echo T('Текст'); ?></th>
                                                <?php if ($settings_show_img_news) { ?>
                                                <th><?php echo T('Изображение (url)'); ?></th>
                                                <?php } ?>
                                                <th colspan="2"><?php echo T('Предпросмотр'); ?></th>
                                                <th><?php echo T('Изменить'); ?></th>
                                                <th><?php echo T('Удалить'); ?></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach (get_news() as $item) { ?>
                                            <tr>
                                                <td><?php echo format_date($item['date']); ?></td>
                                                <td><?php echo escape($item['title']); ?></td>
                                                <td><?php echo html($item['description']); ?></td>
                                                <td><?php echo html($item['text']); ?></td>
                                                <?php if ($settings_show_img_news && $item['img']) { ?>
                                                    <td>
                                                        <a href="<?php echo images_thumbnail_resize($item["img"], 300, 0); ?>" target="_blank">
                                                            <img width="150px" src="<?php echo images_thumbnail_resize($item["img"], 300, 0); ?>" alt="Картинка" />
                                                        </a>
                                                    </td>
                                                <?php } else { ?>
                                                    <td>
                                                        <?php echo $class_error; ?>
                                                    </td>
                                                <?php } ?>
                                                <td>
                                                    <?php echo $item['admin_preview'] === '1' ? $class_success : $class_error; ?>
                                                </td>
                                                <td>
                                                    <form action = "" method = "post">
                                                        <?php echo csrf_install(); ?>
                                                        <button class = "btn btn-default" name = "preview_onoff"><?php echo T('Вкл/Выкл'); ?></button>
                                                        <input name="id" value="<?php echo (int)($item['id']); ?>" hidden />
                                                    </form>
                                                </td>
                                                <td>
                                                    <?php if (!DEMO) { ?>
                                                    <form action="?page=update_news" method="post">
                                                        <input type="hidden" name="id" value="<?php echo (int)$item['id']; ?>">
                                                        <button type="submit" class="btn btn-default edit"><?php echo T('Изменить'); ?></button>
                                                    </form>
                                                    <?php } else { echo $class_error;} ?>
                                                </td>
                                                <td><?php echo button_delete($item['id'], "delete", '/handlers/adg/news.php'); ?></td>
                                            </tr>
                                            <?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /Список новостей -->
            </div>
        </div>
    </div>
  <!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
