<?php
/**
 * Copyright (c) 2016 Serhii Borodai <clarifying@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *
 */


/** @var $this \Zend\View\Renderer\PhpRenderer */

$this->headScript()
    ->appendFile('/js/jquery.cookie.js')
    ->appendFile('/js/c3.min.js')
    ->appendFile('/js/d3.min.js')
    ->appendFile('/js/datepicker.min.js')
    ->appendFile('/js/adm/amsystem-table-sort.js')
    ->appendFile('/js/adm/amsystem-table-filter.js')
    ->appendFile('/js/adm/amsystem-group-edit.js')
    ->appendFile('/js/adm/amsystem-campaign-stats.js')
;

$this->headLink()
    ->appendStylesheet('/css/c3.css')
    ->appendStylesheet('/css/datepicker.min.css')
    ->appendStylesheet('/css/styles-amsystem.css')
;

$lang = $this->lang;
$countries = $this->countries;
$regions = $this->regions;
$cities = $this->cities;
$users = $this->users;

$error = $this->error;

if (!$error) {

    $group = $this->group;
    $campaigns = $this->campaigns;
    $campaigns_stat = $this->campaigns_stat;

    $group_id = $group->getId();
    $group_active = $group->getActive();
    $group_title = $group->getTitle();
    $group_user_id = $group->getUserId();
    $campaign_buy_type = $group->getBuyType();
    $campaign_target_action = $group->getTargetAction();
    $campaign_price = $group->getPrice()/10;
    $campaign_tracking_action = (bool)$group->getTrackingAction();
    $campaign_order_sum = $group->getOrderSum()/10;
    $campaign_visitor_limit = json_decode($group->getVisitorLimit(), true);
    $campaign_money_limit = json_decode($group->getMoneyLimit(), true);
    $campaign_max_speed = (bool)$group->getMaxSpeed();
    $campaign_geo = json_decode($group->getTargeting(), true)['geo'];


    $sites_stats = $this->sites_stats;
    $regions_stats = $this->regions_stats;

    $group_stat = $this->group_stat;
    $group_stat_total = $this->group_stat_total;
    $stat_cats = $this->stat_cats;

    $group_date_begin = $this->group_date_begin;
    $group_date_end = $this->group_date_end;

    $code_regions = $this->code_regions;
    $code_cities = $this->code_cities;

    $form_link = $this->url('user.cabinet.group', ['lang' => $lang, 'id' => $group_id]);

    $link_groups_list = $this->url('user.cabinet.amsystem', ['lang' => $lang]);
    $link_cancel = $this->url('user.cabinet.amsystem', ['lang' => $lang]);

    $stat_link = $this->url('user.cabinet.groups.stat', ['lang' => $lang, 'id' => $group_id]);

    $regexp_money = '^\d+([\.|,]\d+)?$';
    
}

?>

<?= $this->partial('app::user/amsystem/_format'); ?>

<?php if($error): ?>

    <section class="b-content__work error">
        <div class="wrapp">
            <h1><?= $error?></h1>
        </div>
    </section>

<?php else: ?>

    <section class="b-content__work inner">

            <div class="panel panel-info">

                <!-- Header -->
                <div class="panel-heading">
                    <h1>
                        <a class="group-id" data-id="<?= $group_id ?>" href="<?=$link_groups_list ?>">Группы кампаний</a> /
                        <span class="form-group title" data-type="title">
                            <label class="title-field">Назавние</label>
                            <span class="value-show"><?= $group_title ?></span>
                            <span class="value-edit">
                                <input class="form-control group-title" type="text" data-field="group-title" value="<?= $group_title ?>">
                            </span>
                        </span>
                    </h1>
                </div>

                <div class="panel-body">

                    <!-- Переключатель вкладок -->
                    <div class="switch-data" action="<?= $stat_link ?>">
                        <span class="switch-item" data-type="settings">Настройки группы кампаний</span>
                        <span class="switch-item" data-type="stat-common">Общая статистика</span>
                        <span class="switch-item" data-type="stat-sites">По сайтам</span>
                        <span class="switch-item" data-type="stat-regions">По регионам</span>
                    </div>

                    <!-- Настройки группы кампаний -->
                    <div class="form-group-data settings" data-type="settings">

                        <form id="group-create-form" method="post" action="<?= $form_link ?>">
                            <fieldset disabled="disabled">
                                <!-- Активность -->
                                <div class="form-group active"  data-type="active">
                                    <input type="checkbox" id="group-active" name="group-active" <?php if ($group_active): ?>checked<?php endif; ?>>
                                    <label class="title-field" for="group-active">Группа кампаний активна</label>
                                </div>

                                <div class="b-left">

                                    <!-- Тип кампании -->
                                    <div class="form-group">
                                        <label class="title-field"><?= _t('Тип кампании')?></label>
                                        <input type="radio" id="campaign-buy-type-auction" name="buy_type" value="auction" <?php if ($campaign_buy_type == 'auction'): ?>checked<?php endif; ?>>
                                        <label class="select-item" for="campaign-buy-type-auction">Аукцион</label>
                                        <input type="radio" id="campaign-buy-type-garantee" name="buy_type" value="garantee" <?php if ($campaign_buy_type == 'garantee'): ?>checked<?php endif; ?>>
                                        <label class="select-item disabled" for="group-buy-type-garantee">Гарантия</label>
                                    </div>

                                    <!-- Целевое действие -->
                                    <div class="form-group">
                                        <label class="title-field"><?= _t('Целевое действие')?></label>
                                        <input type="radio" id="campaign-view-load" name="target_action" value="load" <?php if ($campaign_target_action == 'load'): ?>checked<?php endif; ?>>
                                        <label class="select-item" for="campaign-view-load">Показ</label>
                                        <input type="radio" id="campaign-view-view" name="target_action" value="view" <?php if ($campaign_target_action == 'view'): ?>checked<?php endif; ?>>
                                        <label class="select-item" for="campaign-view-view">Реальный показ</label>
                                        <input type="radio" id="campaign-view-click" name="target_action" value="click" <?php if ($campaign_target_action == 'click'): ?>checked<?php endif; ?>>
                                        <label class="select-item" for="campaign-view-click">Клик</label>
                                    </div>

                                    <!-- Стоимость -->
                                    <div class="form-group">
                                        <label class="title-field" for="campaign-price"><?= _t('Стоимость')?></label>
                                        <input id="campaign-price" class="form-control price" name="price" value="<?= $campaign_price ?>" pattern="<?= $regexp_money ?>" />
                                        грн. за <span class="price-title-mark">1000 показов</span>
                                    </div>

                                    <!-- Ограничения на посетителя -->
                                    <div class="form-group campaign-visitor-limit__wrap" data-type="visitor-limit">
                                        <label class="title-field"><?= _t('Ограничения на посетителя')?></label>
                                        <br />
                                        <div class="value-show"></div>
                                        <div class="value-edit">
                                            <div class="campaign-visitor-limit view" data-name="view">
                                                <input type="number" min="0" id="campaign-visitor-limit-view-page" class="form-control limit" name="page" data-desc="на странице" value="<?= $campaign_visitor_limit['view']['page'] ?>" /> показов на странице <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-view-hour" class="form-control limit" name="hour" data-desc="в час" value="<?= $campaign_visitor_limit['view']['hour'] ?>" /> показов в час <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-view-day" class="form-control limit" name="day" data-desc="в сутки" value="<?= $campaign_visitor_limit['view']['day'] ?>" /> показов в сутки <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-view-week" class="form-control limit" name="week" data-desc="в неделю" value="<?= $campaign_visitor_limit['view']['week'] ?>" /> показов в неделю <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-view-month" class="form-control limit" name="month" data-desc="в месяц" value="<?= $campaign_visitor_limit['view']['month'] ?>" /> показов в месяц
                                            </div>
                                            <div class="campaign-visitor-limit click" data-name="click">
                                                <input type="number" min="0" id="campaign-visitor-limit-click-page" class="form-control limit" name="page" data-desc="на странице" value="<?= $campaign_visitor_limit['click']['page'] ?>" /> кликов на странице <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-click-hour" class="form-control limit" name="hour" data-desc="в час" value="<?= $campaign_visitor_limit['click']['hour'] ?>" /> кликов в час <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-click-day" class="form-control limit" name="day" data-desc="в сутки" value="<?= $campaign_visitor_limit['click']['day'] ?>" /> кликов в сутки <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-click-week" class="form-control limit" name="week" data-desc="в неделю" value="<?= $campaign_visitor_limit['click']['week'] ?>" /> кликов в неделю <br />
                                                <input type="number" min="0" id="campaign-visitor-limit-click-month" class="form-control limit" name="month" data-desc="в месяц" value="<?= $campaign_visitor_limit['click']['month'] ?>" /> кликов в месяц
                                            </div>
                                        </div>
                                        <div class="b-clear"></div>
                                    </div>

                                    <!-- Бюджет -->
                                    <div class="form-group campaign-money-limit" data-type="money-limit">
                                        <label class="title-field"><?= _t('Бюджет')?></label>
                                        <br />
                                        <div class="value-show"></div>
                                        <div class="value-edit">
                                            <div class="campaign-money-limit-values">
                                                <input id="campaign-money-limit-total" class="form-control limit" name="total" data-desc="всего" value="<?= $campaign_money_limit['total'] ?>" pattern="<?= $regexp_money ?>" /> грн. всего <br />
                                                <input id="campaign-money-limit-day" class="form-control limit" name="day" data-desc="в сутки" value="<?= $campaign_money_limit['day'] ?>" pattern="<?= $regexp_money ?>" /> грн. в сутки <br />
                                                <input id="campaign-money-limit-week" class="form-control limit" name="week" data-desc="в неделю" value="<?= $campaign_money_limit['week'] ?>" pattern="<?= $regexp_money ?>" /> грн. в неделю <br />
                                                <input id="campaign-money-limit-month" class="form-control limit" name="month" data-desc="в месяц" value="<?= $campaign_money_limit['month'] ?>" pattern="<?= $regexp_money ?>" /> грн. в месяц
                                            </div>
                                        </div>
                                    </div>

                                </div>

                                <div class="b-right">

                                    <!-- Скорость открута -->
                                    <div class="form-group">
                                        <label class="title-field"><?= _t('Скорость открута')?></label>
                                        <input type="radio" id="campaign-max-speed-true" name="max_speed" value="1" <?php if ($campaign_max_speed): ?>checked<?php endif; ?>>
                                        <label class="select-item" for="campaign-max-speed-true">Максимальная</label>
                                        <input type="radio" id="campaign-max-speed-false" name="max_speed" value="0" <?php if (!$campaign_max_speed): ?>checked<?php endif; ?>>
                                        <label class="select-item" for="campaign-max-speed-false">Равномерная</label>
                                    </div>

                                    <!-- Гео -->
                                    <div class="form-group geo" data-type="geo">
                                        <label class="title-field"><?= _t('Гео (таргетинг)')?></label>
                                        <div class="value-show"></div>
                                        <div class="value-edit">
                                            <div class="targeting_geo">
                                                <!-- country -->
                                                <div class="geo_country">
                                                    Страна:
                                                    <select class="form-control" id="targeting_geo_country" name="targeting_geo_country" disabled>
                                                        <?php foreach($countries as $code=>$title): ?>
                                                            <option value="<?= $code ?>" <?php if ($campaign_geo['country']['values'] && in_array($code, $campaign_geo['country']['values'])): ?>selected<?php endif; ?>><?= _t($title) ?></option>
                                                        <?php endforeach ?>
                                                    </select>
                                                    <span class="b-exclude">
                                                        <input type="checkbox" id="country-exclude" name="country-exclude" <?php if ($campaign_geo['special']['exclude'] || !$campaign_geo): ?>checked<?php endif; ?>>
                                                        <label for="country-exclude"><?= _t('исключить неподконтрольные территории')?></label>
                                                    </span>
                                                </div>

                                                <div class="geo_region-city">
                                                    <!-- region -->
                                                    <div class="geo_region-wrap">
                                                        <div class="geo_region">
                                                            <div class="form-group">
                                                                <label for="region-list"><?= _t('Области')?></label>
                                                                <div class="b-exclude">
                                                                    <input type="checkbox" id="region-exclude" name="region-exclude" <?php if ((bool)$campaign_geo['region']['exclude']): ?>checked<?php endif; ?>>
                                                                    <label for="region-exclude"><?= _t('исключить')?></label>
                                                                </div>
                                                                <div>
                                                                    <div class="filters-select">
                                                                        <div class="region-val-geo">
                                                                            <input type="text" class="form-control filter" id="region-filter" name="region-filter" value="" placeholder="фильтр">
                                                                        </div>
                                                                        <div class="list-filters-wrap">
                                                                            <ul class="list-filter">
                                                                                <?php foreach($regions as $id=>$data): ?>
                                                                                    <li>
                                                                                        <input type="checkbox" id="region-id-<?= $id ?>" data-id="<?= $id ?>" data-text="<?= $data['name'] ?>" <?php if ($campaign_geo['region']['values'] && in_array($id, $campaign_geo['region']['values'])): ?>checked<?php endif; ?>>
                                                                                        <label for="region-id-<?= $id ?>"><?= $data['name'] ?></label>
                                                                                    </li>
                                                                                <?php endforeach ?>
                                                                            </ul>
                                                                        </div>
                                                                    </div>
                                                                    <div class="filters-values">
                                                                        <ul class="values-string-list">
                                                                            <?php if($campaign_geo['region']['values']): ?>
                                                                                <?php foreach($campaign_geo['region']['values'] as $id): ?>
                                                                                    <li>
                                                                                        <input type="checkbox" checked id="l-region-id-<?= $id ?>" data-id="<?= $id ?>" data-text="<?= $regions[$id]['name'] ?>">
                                                                                        <label for="l-region-id-<?= $id ?>"><?= $regions[$id]['name'] ?></label>
                                                                                    </li>
                                                                                <?php endforeach ?>
                                                                            <?php endif; ?>
                                                                        </ul>    
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <!-- city -->
                                                    <div class="geo_city-wrap">
                                                        <div class="geo_city">
                                                            <div class="form-group">
                                                                <label for="city-list"><?= _t('Города')?></label>
                                                                <div class="b-exclude">
                                                                    <input type="checkbox" id="city-exclude" name="city-exclude" <?php if ((bool)$campaign_geo['city']['exclude']): ?>checked<?php endif; ?>>
                                                                    <label for="city-exclude"><?= _t('исключить')?></label>
                                                                </div>
                                                                <div>
                                                                    <div class="filters-select">
                                                                        <input type="text" class="form-control filter" id="city-filter" name="city-filter" value="" placeholder="фильтр">
                                                                        <div class="list-filters-wrap">
                                                                            <ul class="list-filter">
                                                                                <?php foreach($cities as $id=>$data): ?>
                                                                                    <li>
                                                                                        <input type="checkbox" id="city-id-<?= $id ?>" data-id="<?= $id ?>" data-text="<?= $data['name'] ?>" <?php if ($campaign_geo['city']['values'] && in_array($id, $campaign_geo['city']['values'])): ?>checked<?php endif; ?>>
                                                                                        <label for="city-id-<?= $id ?>"><?= $data['name'] ?></label>
                                                                                    </li>
                                                                                <?php endforeach ?>
                                                                            </ul>    
                                                                        </div>
                                                                    </div>
                                                                    <div class="filters-values">
                                                                        <ul class="values-string-list">
                                                                            <?php if($campaign_geo['city']['values']): ?>
                                                                            <?php foreach($campaign_geo['city']['values'] as $id): ?>
                                                                                <li>
                                                                                    <input type="checkbox" checked id="l-city-id-<?= $id ?>" data-id="<?= $id ?>" data-text="<?= $cities[$id]['name'] ?>">
                                                                                    <label for="l-region-id-<?= $id ?>"><?= $cities[$id]['name'] ?></label>
                                                                                </li>
                                                                            <?php endforeach ?>
                                                                            <?php endif; ?>
                                                                        </ul>    
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>

                                            </div>
                                        </div>
                                    </div>

                                </div>

                                <div class="b-clear"></div>

                                <!-- Трекинг действий -->
                                <div class="form-group tracking-action"  data-type="tracking-action" >
                                    <input type="checkbox" id="group-tracking-action" name="group-tracking-action" <?php if ($campaign_tracking_action): ?>checked<?php endif; ?>>
                                    <label class="title-field" for="group-active">Трекинг действий</label>
                                    <div class="tracking-action_content">
                                        <div class="tracking-action_link">
                                            http://order.azbne.net/b/order?&campaign_id={CAMPAIGN_ID}&click_id={CLICK_ID}&order_sum={ORDER_SUM}&order_id={ORDER_ID}
                                        </div>
                                        <!-- Цена действия по-умолчанию -->
                                        <div class="form-group order-sum"  data-type="order-sum">
                                            <label for="group-order-sum"><?= _t('Цена действия по-умолчанию')?></label>
                                            <input id="campaign-order-sum" class="form-control order-sum" name="order_sum" value="<?= ($campaign_order_sum) ? : 1 ?>" pattern="<?= $regexp_money ?>" />
                                            грн.
                                        </div>
                                    </div>
                                </div>

                            </fieldset>
                        </form>

                        <?php if($campaigns): ?>
                            <div class="group-campaigns">
                                <label><?= _t('Кампании')?></label>
                                    <div class="table-filters">
                                        Статус кампании:
                                        <select class="form-control campaign-status">
                                            <option value="all"><?= _t('все') ?></option>
                                            <option value="active"><?= _t('активные') ?></option>
                                            <option value="not-active"><?= _t('не активные') ?></option>
                                            <option value="archive"><?= _t('в архиве') ?></option>
                                            <option value="not-archive"><?= _t('не в архиве') ?></option>
                                        </select>
                                    </div>
                                    <table class="table table-striped campaigns" data-content="campaigns" data-action="/amsystem/campaigns/id/edit/">
                                        <thead>
                                            <tr>
                                                <th class="stat"></th>                        
                                                <th class="stat">#ID</th>
                                                <th>Название кампании</th>
                                                <th class="stat">Срок действия</th>
                                                <?php foreach ($stat_cats as $cat_name => $cat) : ?>
                                                <th class="stat"><span class="sort" data-sort="<?= $cat_name ?>" data-sortasc="1"><?= $cat['title'] ?></span></th>
                                                <?php endforeach; ?>
                                            </tr>
                                        </thead>
                                        <tbody>

                                        <?php foreach ($campaigns as $campaign) : ?>
                                            <?php
                                                $tr_class = '';
                                                if (!$campaign['active']) {
                                                    $tr_class.= 'not-active disabled ';
                                                } else {
                                                    $tr_class.= 'active ';
                                                }
                                                if ($campaign['archive']) {
                                                    $tr_class.= 'archive ';
                                                } else {
                                                    $tr_class.= 'not-archive ';
                                                }
                                            ?>
                                            <tr class="<?= $tr_class ?>">
                                                <?php
                                                    $campaign_id = $campaign['id'];
                                                    $stat = $campaigns_stat[$campaign_id];
                                                    $target_class = ($campaign['target_action'] == 'load') ? 'load-target' : '';
                                                ?>
                                                <td>
                                                    <input class="is-active" type="checkbox" data-id="<?= $campaign_id ?>" <?php if ($campaign['active']): ?>checked<?php endif; ?>>
                                                </td>
                                                <td>
                                                    <span class="item-id"><?= $campaign_id ?></span>
                                                </td>
                                                <td>
                                                     <?php
                                                        $btn_link = $this->url('user.cabinet.campaign', [
                                                            'lang' => $this->lang,
                                                            'id' => $campaign_id,
                                                        ]);
                                                     ?>
                                                    <span class="item-title"><a href="<?= $btn_link?>"><?= $campaign['title'] ?></a></span>
                                                </td>
                                                <?php
                                                $begin = ($campaign['date_begin']) ? date('d-m-Y', strtotime($campaign['date_begin'])) : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                                $end = ($campaign['date_end']) ? date('d-m-Y', strtotime($campaign['date_end'])) : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                                $today = date('Y-m-d');
                                                $date_begin_class = '';
                                                $date_end_class = '';
                                                if ($campaign['date_begin'] && $today < $campaign['date_begin']) {
                                                    $date_begin_class = 'not-active';
                                                }
                                                if ($campaign['date_end'] && $today > $campaign['date_end']) {
                                                    $date_end_class = 'not-active';
                                                }

                                                ?>
                                                <td class="stat date">
                                                    <span class="<?= $date_begin_class ?>"><?= $begin ?></span> &ndash; <span class="<?= $date_end_class ?>"><?= $end ?></span>
                                                </td>
                                                <?php foreach ($stat_cats as $cat => $cat_data) : ?>
                                                    <?php
                                                    $target_class_item = (in_array($cat, ['views', 'reach'])) ? $target_class : '';
                                                    ?>
                                                    <td class="stat <?= $target_class_item ?>" data-cat="<?= $cat ?>" data-value="<?= $stat[$cat] ?>"><?= formatField($stat[$cat], $cat) ?></td>
                                                <?php endforeach; ?>
                                            </tr>
                                        <?php endforeach; ?>


                                        </tbody>
                                    </table>
                            </div>
                        <?php endif; ?>

                    </div>

                    <!-- Общая статистика -->
                    <div class="form-group-data stat-common" data-type="stat-common">
                        <div class="b-content__loading"></div>

                        <div class="campaign-stat">

                            <textarea id="campaign-stat"><?= ($group_stat ? json_encode($group_stat) : '')?></textarea>
                            <textarea id="campaign-stat-total"><?= ($group_stat_total ? json_encode($group_stat_total) : '')?></textarea>
                            <textarea id="campaign-stat-cats"><?= ($stat_cats ? json_encode($stat_cats) : '')?></textarea>

                            <!-- Дата -->
                            <div class="form-group">
                                <label for="campaign-date-begin"><?= _t('Дата')?></label>
                                c:
                                <input type='text' id="campaign-date-begin-common" class="campaign-date-begin form-control date-value" name="date_begin" value="<?= $group_date_begin ?>" autocomplete="off" />
                                по:
                                <input type='text' id="campaign-date-end-common" class="campaign-date-end form-control date-value" name="date_end" value="<?= $group_date_end ?>" autocomplete="off" />
                            </div>

                            <div class="chart-graph-stat-info">
                                <h2>По заданным параметрам статистика отсутствует</h2>
                            </div>
                            <div class="chart-graph-stat-error">
                                <h2>Ошибка в статистике</h2>
                                <div class="message"></div>
                            </div>

                            <!-- График -->
                            <div id="chart-graph-stat" data-colors=<?= ($colors_active_lines ? json_encode($colors_active_lines) : '')?>></div>

                            <!-- Таблица -->
                            <div id="table-stat" class="b-table_wrapp">
                                <table id="table-line-common" class="table table-striped common-stat sortable" data-type="common-stat">
                                    <thead>
                                        <tr>
                                            <th class="period" data-id="period">
                                                <b class="sort" id="period">Дата</b>
                                            </th>
                                            <?php foreach($stat_cats as $name => $cat): ?>
                                            <th class="<?= $name ?>" data-id="<?= $name ?>">
                                                <span style="border-color: <?= $cat['color'] ?>;">
                                                    <input type="checkbox" data-field="<?= $name ?>" checked />
                                                </span>
                                                <b class="sort" id="<?= $name ?>"><?= $cat['title'] ?></b>
                                            </th>
                                            <?php endforeach; ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>

                        </div>
                    </div>

                    <!-- По сайтам -->
                    <div class="form-group-data stat-sites" data-type="stat-sites">
                        <div class="b-content__loading"></div>
                        <!-- Дата -->
                        <div class="form-group">
                            <label for="campaign-date-begin"><?= _t('Дата')?></label>
                            c:
                            <input type='text' id="campaign-date-begin-sites" class="campaign-date-begin form-control date-value" name="date_begin" value="<?= $group_date_begin ?>" autocomplete="off" />
                            по:
                            <input type='text' id="campaign-date-end-sites" class="campaign-date-end form-control date-value" name="date_end" value="<?= $group_date_end ?>" autocomplete="off" />
                        </div>

                        <div class="sites-stats">
                            <textarea id="campaign-sites-stat"><?= ($sites_stats ? json_encode($sites_stats) : '')?></textarea>

                            <div class="sites-stats-info">
                                <h2>По заданным параметрам статистика отсутствует</h2>
                            </div>

                            <div class="sites-stats-error">
                                <h2>Ошибка в статистике</h2>
                                <div class="message"></div>
                            </div>

                            <div class="b-table_wrapp">
                                <table id="table-line-sites" class="table table-striped table-by-cats sortable" data-type="sites-stat">
                                    <thead>
                                        <tr>
                                            <th class="period" data-id="period">
                                                <b>Сайт</b>
                                            </th>
                                            <?php foreach($stat_cats as $name => $cat): ?>
                                            <th class="<?= $name ?>" data-id="<?= $name ?>">
                                                <b class="sort" id="<?= $name ?>"><?= $cat['title'] ?></b>
                                            </th>
                                            <?php endforeach; ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>


                        </div>
                    </div>

                    <!-- По регионам -->
                    <div class="form-group-data stat-regions" data-type="stat-regions">
                        <div class="b-content__loading"></div>
                        <!-- Дата -->
                        <div class="form-group">
                            <label for="campaign-date-begin"><?= _t('Дата')?></label>
                            c:
                            <input type='text' id="campaign-date-begin-regions" class="campaign-date-begin form-control date-value" name="date_begin" value="<?= $group_date_begin ?>" autocomplete="off" />
                            по:
                            <input type='text' id="campaign-date-end-regions" class="campaign-date-end form-control date-value" name="date_end" value="<?= $group_date_end ?>" autocomplete="off" />
                        </div>

                        <div class="regions-stats">
                            <textarea id="campaign-regions-stat"><?= ($regions_stats ? json_encode($regions_stats) : '')?></textarea>
                            <textarea id="regions-codes"><?= ($code_regions ? json_encode($code_regions) : '')?></textarea>
                            <textarea id="cities-codes"><?= ($code_cities ? json_encode($code_cities) : '')?></textarea>

                            <div class="regions-stats-info">
                                <h2>По заданным параметрам статистика отсутствует</h2>
                            </div>

                            <div class="regions-stats-error">
                                <h2>Ошибка в статистике</h2>
                                <div class="message"></div>
                            </div>

                            <div class="b-table_wrapp">
                                <table id="table-line-regions" class="table table-striped table-by-cats sortable" data-type="regions-stat">
                                    <thead>
                                        <tr>
                                            <th class="period" data-id="period">
                                                <b>Регион</b>
                                            </th>
                                            <?php foreach($stat_cats as $name => $cat): ?>
                                            <th class="<?= $name ?>" data-id="<?= $name ?>">
                                                <b class="sort" id="<?= $name ?>"><?= $cat['title'] ?></b>
                                            </th>
                                            <?php endforeach; ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>

                        </div>

                    </div>

                </div>

            </div>

    </section>

<?php endif; ?>