<?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/underscore-min.js')
    ->appendFile('/js/jquery.cookie.js')
    ->appendFile('/js/bootstrap.min.js')
    ->appendFile('/js/c3.min.js')
    ->appendFile('/js/d3.min.js')
    ->appendFile('/js/datepicker.min.js')
    ->appendFile('/js/script-feeds-stat-charts.js')
    ->appendFile('/js/script-feeds-stat-table.js')
    ->appendFile('/js/script-feeds-stat.js')
    ->appendFile('/js/adm/service-actions.js')
        
;

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



/** @var \App\Entity\IdentityInterface $identity */
$identity = $this->identity();
$gravatar = $this->gravatar($identity->getEmail());
$gravatar->setDefaultImg(\Zend\View\Helper\Gravatar::DEFAULT_IDENTICON);
$sxml = new SimpleXMLElement(substr($gravatar, 0, -1) . '/>');
$src = $sxml['src'];

$error = $this->error;

if (!$error) {

    $periodStats = $this->periodStats;
    //$campaignStats = $this->campaignStats;
    $current_period = $this->current_period;
    $dates = $this->dates;
    $current_report = $this->current_report;
    $report_conf = $this->report_conf;
    $report_title = $report_conf['title'];
    $report_main_cat = $report_conf['main_cat'];
    $report_cats = $report_conf['cats'];
    $reports_list = $this->reports_list;
    $periods_list = $this->periods_list;
    $colors_active_lines = $this->colors_active_lines;

    $data_request_link = $this->url('user.cabinet', ['lang' => $this->lang, 'report' => $current_report]);
    $system_link = $this->url('adm.system.actions');
    
}

?>

<?php /* ?>
<section class="b-content__title">
    <div class="wrapp">
        <!-- Вход произведен -->
        <picture>
            <img src="/img/bg-img-logo.svg" alt="No logo">
        </picture>
        <p class="lead"><?= $identity->getEmail() ?></p>
        <p class="b-content__title-settings">
            <a href="<?= $this->url('user.cabinet.profile', ['lang' => $this->lang]) ?>" title="" class="annotation"><?= _t("Настройки профиля") ?></a>
            <a href="<?= $this->url('logout', ['lang' => $this->lang]) ?>" title="" class="annotation"><?= _t("Выход") ?></a>
        </p>
    </div>
</section>
<?php */ ?>

<?php if($error): ?>

    <section class="b-content__work">
        <div class="wrapp" style="margin: 100px auto; text-align: center;">
            <h1><?= $error?></h1>
        </div>
    </section>

<?php else: ?>

    <textarea id="period-stats"><?= ($periodStats ? json_encode($periodStats, JSON_UNESCAPED_UNICODE) : '')?></textarea>
    <?php /* ?>
    <textarea id="campaign-stats"><?= ($campaignStats ? json_encode($campaignStats) : '')?></textarea>
    <?php */ ?>

    <section class="b-content__work">
        <div class="wrapp" style="position: relative;">
            
            <div class="b-feeds-stat_data-loading"></div>
            <?php /* ?>
            <a class="btn btn-primary service-action feed-test-data" href="<?= $system_link ?>">Сформировать тестовые данные</a>
            <?php */ ?>
            <div class="b-feeds-stat_controls form-group">
                
                <?php /* Переключатель отчетов */ ?>
                <div class="b-feeds-stat_controls-report">
                    <div class="dropdown" style="display:inline-block;">
                        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                            <?= _t($report_title) ?>
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu" aria-labelledby="dropdownMenu">
                            <?php foreach($reports_list as $report_name => $report_title): ?>
                                <li><a href="<?= $this->url('user.cabinet', ['lang' => $this->lang, 'report' => $report_name]) ?>" <?php if($current_report == $report_name): ?>class="current"<?php endif; ?> report="<?= $report_name ?>"><?= _t($report_title) ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>

                <?php /* Выбор дат */ ?>
                <span class="b-feeds-stat_controls-period">
                    <div style="display:inline-block;">Дата: </div>
                    <input type='text' id="date-value-1" class="btn btn-default date-value" value="<?=$dates[0] ?>"/> &ndash; 
                    <input type='text' id="date-value-2" class="btn btn-default date-value" value="<?=$dates[1] ?>"/>

                    <span>период: </span>
                    <select class="form-control stat-period" name="period" data-link="<?=$data_request_link ?>">
                        <option value="" disabled>--</option>
                        <?php foreach($periods_list as $period => $title): ?>
                            <option value="<?=$period ?>" <?php if ($period == $current_period): ?>selected<?php endif; ?>><?=$title ?></option>
                        <?php endforeach; ?>
                    </select>
                </span>
                
            </div>
                
            <div class="chart-graph-stat-info">
                <h2>По заданным параметрам статистика отсутствует</h2>
            </div>
            <div class="chart-graph-stat-error">
                <h2>Ошибка в статистике</h2>
                <div class="message"></div>
            </div>
            
            <?php /* График */ ?>
            <?php if ($current_report == 'common'): ?>
            <div id="chart-graph-stat" style="height: 370px;" data-colors=<?= ($colors_active_lines ? json_encode($colors_active_lines) : '')?>></div>
            <?php endif ?>    
            
            <?php /* Таблица */ ?>
            <div id="table-stat" class="b-table_wrapp">
                <div class="b-content__loading"></div>
                <table id="table-line" class="table table-striped admin <?= $current_report ?>">
                    <thead class="metric sortable">
                        <tr>
                            <th class="period" data-id="period">
                                <?php foreach($report_main_cat as $main_cat): ?>
                                    <b class="main-cat selected <?php if(!$main_cat['isSorted']): ?>not-sorted<?php endif; ?>" data-cat="<?= $main_cat['name'] ?>"><?= _t($main_cat['title'])?></b>
                                <?php endforeach; ?>
                            </th>
                            <?php foreach($report_cats as $name => $title): ?>
                            <th class="<?= $name ?>" data-id="<?= $name ?>">
                                <?php if($current_report == 'common'): ?>
                                    <span style="border-color: <?= $colors_active_lines[$name] ?>;">
                                        <input type="checkbox" data-field="<?= $name ?>" checked />
                                    </span>
                                    <b class="cat" id="<?= $name ?>"><?= _t($title)?></b>
                                <?php endif; ?>
                                <?php if($current_report == 'transaction'): ?>
                                    <?php if ($name == 'Basket'): ?>
                                        <b class="cat not-sorted" id="<?= $name ?>"><?= _t($title)?></b><br />
                                        <b class="basket-link not-sorted">Товар</b> <b class="quantity not-sorted">Кол-во</b> <b class="price not-sorted">Цена</b>
                                    <?php else: ?>
                                        <b class="cat" id="<?= $name ?>"><?= _t($title)?><b class="arrow asc">&#9650;</b><b class="arrow desc">&#9660;</b></b>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </th>
                            <?php endforeach; ?>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
            
        </div>
    </section>

<?php endif ?>