<?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/jquery.table2excel.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;
    $current_period = $this->current_period;
    $dates = $this->dates;
    $current_report = $this->current_report;
    $report_conf = $this->report_conf;
    $report_title = $report_conf['title'];
    $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;
    $shop_title = $this->shop_title;

    $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>
    <textarea id="report-cats"><?= ($report_cats ? json_encode($report_cats, JSON_UNESCAPED_UNICODE) : '')?></textarea>

    <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 /* Переключатель страниц */ ?>
                <?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 */ ?>
                
                <?php /* Переключатель отчеты категории */ ?>
                <div class="b-filter__switch display">
                    <?php foreach($reports_list as $report_name => $report_title): ?>
                        <span class="b-filter__switch-item text <?php if($current_report == $report_name): ?>m-filter__switch-item_active<?php endif; ?>"><a href="<?= $this->url('user.cabinet', ['lang' => $this->lang, 'report' => $report_name]) ?>"><?= _t($report_title) ?></a></span>
                    <?php endforeach; ?>
                        <span class="b-filter__switch-item text"><a href="<?= $this->url('user.cabinet.cats', ['lang' => $this->lang]) ?>"><?= _t('Категории') ?></a></span>
                </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 /* Таблица */ ?>
            
            <?php if ($current_report == 'transactions'): ?>
            <?php /* Переключатель статусов заказов */ ?>
            <div class="b-filter__switch category-orders">
                <span class="b-filter__switch-item text all m-filter__switch-item_active" data-order-category="all"><?= _t('Все')?></span>
                <?php /* ?>
                <span class="b-filter__switch-item text approved" data-order-category="approved"><?= _t('Согласованны')?></span>
                <?php */ ?>
                <span class="b-filter__switch-item text confirmed" data-order-category="confirmed"><?= _t('Подтверждены')?></span>
                <span class="b-filter__switch-item text rejected" data-order-category="rejected"><?= _t('Отклонены')?></span>
            </div>
            <?php endif ?>
            
            <div id="table-stat" class="b-table_wrapp report">
                <div class="b-content__loading"></div>
                <table id="table-line" class="table table-striped admin <?= $current_report ?> all" data-link="<?=$data_request_link ?>">
                    <thead class="metric sortable">
                        <tr>
                            <?php if($current_report == 'transactions'): ?>
                            <th class="status">
                                <b class="not-sorted">Подтвержден</b>
                            </th>
                            <?php endif; ?>
                            <th class="period" data-id="period">
                                <b class="main-cat selected <?php if(!$main_cat['isSorted']): ?>not-sorted<?php endif; ?>" data-cat="<?= $main_cat['name'] ?>"><?= _t($main_cat['title'])?></b>
                            </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 == 'transactions'): ?>
                                    <?php $not_sorted = (in_array($name, ['Basket', 'ViewID'])) ? 'not-sorted' : ''; ?>
                                    <?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 <?= $not_sorted ?>" 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>
                
                <?php /* if($current_report == 'transactions'): ?>
                    <div class="b-orders_controls">
                        <a class="btn btn-primary approve" href="<?= $system_link ?>">Утвердить заказы</a>
                    </div>
                <?php endif; */ ?>
                
                <?php if($current_report == 'transactions'): ?>
                    <table id="table2excel" data-content="shop-<?= $shop_title ?>" width="100%">
                        <thead>
                            <tr class="orders-title all"><th colspan="4">Все заказы</th></tr>
                            <tr class="orders-title confirmed"><th colspan="4">Подтвержденные заказы</th></tr>
                            <tr class="orders-title rejected"><th colspan="4">Отклонённые заказы</th></tr>
                            <tr>
                                <th>
                                    <b><?= _t($main_cat['title'])?></b>
                                </th>
                                <?php foreach($report_cats as $name => $title): ?>
                                    <?php if ($name == 'Timestamp'): ?>
                                        <th><b><?= _t('Дата время')?></b></th>
                                    <?php else: ?>
                                        <?php if (!in_array($name, ['ViewID', 'Basket'])): ?>
                                        <th><b><?= _t($title)?></b></th>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                    
                    <div class="b-export-xls">
                        <button id="btnExport"> EXPORT to Excel </button>
                    </div>
                <?php endif; ?>
                
            </div>
            
        </div>
    </section>

<?php endif ?>