<?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;
    $cats = $this->cats;
    $report = $this->report;
    $periods_list = $this->periods_list;
    $colors_active_lines = $this->colors_active_lines;

    $data_request_link = $this->url('user.cabinet', ['lang' => $this->lang, 'report' => $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">
                            <?php if($report == 'common'): ?>
                                <?= _t('Сводный отчет') ?>
                            <?php else: ?>
                                <?= _t('Транзакции') ?>
                            <?php endif; ?>
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu" aria-labelledby="dropdownMenu">
                            <li><a href="<?= $this->url('user.cabinet', ['lang' => $this->lang, 'report' => 'common']) ?>" <?php if($report == 'common'): ?>class="current"<?php endif; ?> report="common"><?= _t('Сводный отчет') ?></a></li>
                            <li><a href="<?= $this->url('user.cabinet', ['lang' => $this->lang, 'report' => 'transaction']) ?>" <?php if($report == 'transaction'): ?>class="current"<?php endif; ?> report="transaction"><?= _t('Транзакции') ?></a></li>
                        </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 ($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">
                    <thead class="metric sortable">
                        <tr>
                            <th>
                                <?php if($report == 'common'): ?>
                                    <b class="main-cat date selected"><?= _t('Дата')?></b>
                                    <?php /* ?>/ <b class="main-cat campaign"><?= _t('Кампания')?></b><?php */ ?>
                                <?php else: ?>
                                    <b class="main-cat date selected"><?= _t('Транзакция')?></b>
                                <?php endif; ?>
                            </th>
                            <?php foreach($cats as $name => $title): ?>
                            <th>
                                <?php if ($report == 'common'): ?>
                                <span style="border-color: <?= $colors_active_lines[$name] ?>;">
                                    <input type="checkbox" data-field="<?= $name ?>" checked />
                                </span>
                                <?php endif; ?>
                                <b class="cat" id="<?= $name ?>"><?= _t($title)?><!--<b class="arrow asc">&and;</b><b class="arrow desc">&or;</b>--></b>
                            </th>
                            <?php endforeach; ?>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
            
        </div>
    </section>

<?php endif ?>