<?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/feed-cats.js')
;

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

$error = $this->error;

if (!$error) {
    $lang = $this->lang;
    $feeds_cats = $this->feeds_cats;
    $reports_list = $this->reports_list;
}

?>


<?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; margin-bottom: 10px;">
            
            <div class="b-feeds-stat_controls form-group">
                <?php /* Переключатель страниц */ ?>
                <div class="b-filter__switch display">
                    <?php foreach($reports_list as $report_name => $report_title): ?>
                        <span class="b-filter__switch-item text"><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 m-filter__switch-item_active"><a href="<?= $this->url('user.cabinet.cats', ['lang' => $this->lang]) ?>"><?= _t('Категории') ?></a></span>
                </div>
            </div>
            
            <?php
            function render_cat($i, $cat)
            {
                $i++;
                $symb = (count($cat['sub_cats']) > 0) ? '<span class="group"><b class="closed">+</b><b class="open">&ndash;</b></span>' : '<span class="group"></span>';
            ?>

                <tr class="feed-cat" style="font-size: <?= 16-$i+1 ?>px;" data-id="<?= $cat['id'] ?>" data-parent="<?= $cat['parentid'] ?>">
                    <td>
                        <span class="cat-title" style="padding-left: <?= ($i-1)*40 ?>px;"><?= $symb ?><?= $cat['title'] ?> <span class="foreign-id" data-id="<?= $cat['foreignid'] ?>">[<?= $cat['foreignid'] ?>]</span></span>
                    </td>
                    <td class="percent <?= ($cat['own']) ? 'own': 'inherited' ?>" data-old-type="<?= ($cat['own']) ? 'own': 'inherited' ?>">
                        <span class="edited" data-old-value="<?= $cat['percent'] ?>"><span class="percent-value"><?= $cat['percent'] ?></span>&nbsp;%</span>
                        <span class="editing-wrap"><input class="percent-input editing" value="<?= $cat['percent'] ?>">&nbsp;%</span>
                        <br /><span class="old-value"><?= $cat['percent'] ?>&nbsp;%</span>
                    </td>
                    <?php /*?>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <?php */?>
                </tr>

                <?php if (count($cat['sub_cats']) > 0): ?>
                    <?php foreach ($cat['sub_cats'] as $sub_cat) : ?>
                        <?= render_cat($i, $sub_cat) ?>
                    <?php endforeach; ?>
                <?php endif; ?>

            <?php 
            }
            ?>    


            <?php foreach ($feeds_cats as $feeds_cat) : ?>

                <div class="feed-title">фид: <b><?= $feeds_cat['title'] ?></b></div>

                <table class="table table-striped feeds-cats open" data-content="cats" data-feed-id="<?= $feeds_cat['id'] ?>" data-feed-percent="<?= $feeds_cat['percent'] ?>">

                    <thead>
                        <tr>                        
                            <th>
                                <span class="cat-title-main">
                                    <span class="group"><b class="closed">+</b><b class="open">&ndash;</b></span>Название категории</span>
                                    <!-- Сохранить / Отмена -->
                                    <span class="form-actions">
                                        <a class="btn btn-primary submit-action" href="#" action="<?= $form_link ?>" method="post"><?= _t('Сохранить') ?></a>
                                        <a class="btn btn-default cancel-action" href="<?= $link_cancel ?>"><?= _t('Отмена')?></a>
                                    </span>
                            </th>
                            <th class="data"><span>Процент</span></th>
                            <?php /*?>
                            <th class="data"><span>Показы</span></th>
                            <th class="data"><span>Клики</span></th>
                            <th class="data"><span>Заказы</span></th>
                            <th class="data"><span>Стоимость</span></th>
                            <th class="data"><span>Комиссия</span></th>
                            <?php */?>
                        </tr>
                    </thead>

                    <tbody>

                        <?php foreach ($feeds_cat['cats'] as $cat) : ?>
                            <?= render_cat(0, $cat) ?>
                        <?php endforeach; ?>    

                    </tbody>
                </table>

            <?php endforeach; ?>

            <!-- Сохранить / Отмена -->
            <div class="form-actions">
                <a class="btn btn-primary submit-action" href="#" action="<?= $form_link ?>" method="post"><?= _t('Сохранить') ?></a>
                <a class="btn btn-default cancel-action" href="<?= $link_cancel ?>"><?= _t('Отмена')?></a>
            </div>
            
        </div>
    </section>

<?php endif ?>