<?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/bootstrap.min.js')
;

$this->headLink()
    ->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) {
    $viewIdStats = $this->viewIdStats;
    $cats = $this->cats;
}

?>

<?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: ?>


    <section class="b-content__work">
        <div class="wrapp" style="position: relative;">
            
            <?php //var_dump($viewIdStats) ?>
            
            <?php if(!$viewIdStats): ?>
            
                <div class="click-history-info">
                    <h2>Извините, история по клику недоступна</h2>
                </div>
            
            <?php else: ?>
            
                <?php /* Таблица */ ?>
                <div id="table-stat" class="b-table_wrapp">
                    <div class="b-content__loading"></div>
                    <table id="table-line" class="table table-striped admin transactions viewid">
                        <thead class="metric sortable">
                            <tr>
                                <?php foreach($cats as $name => $title): ?>
                                <th class="<?= $name ?>" data-id="<?= $name ?>">
                                    <?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>
                                    <?php endif; ?>
                                </th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($viewIdStats as $view_id): ?>
                            <tr>
                                <?php foreach($cats as $name => $title): ?>

                                <td class="<?= $name ?>" data-id="<?= $name ?>">
                                <?php if($name == 'StartTime' || $name == 'EndTime' || $name == 'Timestamp'): ?>
                                    <?php
                                    $date_time = explode(" ", $view_id[$name]);
                                    ?>
                                    <span style="white-space: nowrap;">
                                    <b><?= $date_time[1] ?></b>
                                    <span><?= $date_time[0] ?></span>
                                    </span>
                                <?php elseif($name == 'Basket'): ?>
                                    <div class="b-basket-content">
                                        <a class="view-id-url" href="<?=$view_id['URL'] ?>" target="_blank"><?= $view_id['URL'] ?></a>
                                        <?php if(count($view_id[$name])>0): ?>
                                        <table>
                                            <tbody>
                                                <?php foreach($view_id[$name] as $item): ?>
                                                <tr>
                                                    <td><a class="basket-link" href="<?=$item['URL'] ?>" target="_blank"><?=$item['Title'] ?></a></td>
                                                    <td><span class="quantity"><?=$item['Quantity'] ?></span></td>
                                                    <td><span class="price"><?=$item['Price'] ?> грн.</span></td>
                                                </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                        <?php endif; ?>
                                    </div>
                                <?php elseif($name == 'OrderSum'): ?>
                                    <span class="cat"><?= $view_id[$name] ?> грн.</span>
                                <?php else: ?>
                                    <span class="cat"><?= $view_id[$name] ?></span>
                                <?php endif; ?>
                                </td>

                                <?php endforeach; ?>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            
            <?php endif; ?>
            
        </div>
    </section>

<?php endif ?>