<?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 */

/** @var $userSiteStatistic \App\Entity\Statistic\Site */

$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')
;

$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;
$periodStats = $this->periodStats;
$companyStats = $this->companyStats;
$current_period = $this->current_period;
$dates = $this->dates;
$cats = $this->cats;
$report = $this->report;

$periods_conf = array(
    'today' => 'сегодня',
    'yesterday' => 'вчера',
    '7days' => '7 дней',
    '30days' => '30 дней',
    'current_month' => 'текущий месяц',
    'prev_month' => 'прошлый месяц',
    'year' => 'год',
);

$colors_active_lines = array(
    'shows' => '#2CA02C',
    'clicks' => '#D62728',
    'transactions' => '#1F77B4',
    'salemoney' => '#FF7F0E',
    'admoney' => '#9467BD',
);


?>

<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 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) : '')?></textarea>
    <textarea id="company-stats"><?= ($companyStats ? json_encode($companyStats) : '')?></textarea>

    <section class="b-content__work">
        <div class="wrapp">
            
            <div class="b-feeds-stat_controls form-group">
                
                <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>
                
                <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">
                        <option value="" disabled>--</option>
                        <?php foreach($periods_conf 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;"></div>
            <div class="chart-graph-stat-loading"></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>
                                <b class="main-cat date selected"><?= _t('Дата')?></b> / <b class="main-cat company"><?= _t('Компания')?></b>
                            </th>
                            <?php foreach($cats as $name): ?>
                            <th>
                                <?php if ($report == 'common'): ?>
                                <span style="border-color: <?= $colors_active_lines[$name] ?>;">
                                    <input type="checkbox" data-field="<?= $name ?>" />
                                </span>
                                <?php endif; ?>
                                <b id="<?= $name ?>"><?= _t($name)?><!--<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 ?>