<?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/adm/amsystem.js')
;

$this->headLink()
    ->appendStylesheet('/css/styles-amsystem.css')

;

$lang = $this->lang;
$groups = $this->groups;
$campaigns = $this->campaigns;
$groups_campaigns = $this->groups_campaigns;
$error = $this->error;

?>

<?php if($error): ?>

    <section class="b-content__work error">
        <div class="wrapp">
            <h1><?= $error?></h1>
        </div>
    </section>

<?php else: ?>

    <section class="b-content__work">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h1><?= _t('Список кампаний') ?></h1>
            </div>

            <div class="panel-body all">

                <table class="table campaigns" data-content="campaigns" data-action="/amsystem/campaigns/id/edit/">
                    <thead>
                        <tr>
                            <th>Название кампании</th>
                            <th width="1px">Показы</th>
                            <th width="1px">Клики</th>
                            <th width="1px">CTR</th>
                            <th width="1px">Расход</th>
                            <th width="1px">Охват</th>
                            <th width="1px">eCPM</th>
                            <th width="1px">eCPC</th>
                        </tr>
                    </thead>
                    <tbody>

                    <?php foreach ($groups as $group) : ?>
                        <?php
                            $group_id = $group['id'];
                            $stat = null;
                            $target_class = ($group['target_action'] == 'load') ? 'load-target' : '';
                        ?>
                        <tr class="group-item <?php if (!$group['active']): ?>disabled<?php endif; ?>" data-id="<?= $group_id ?>">
                            <td>
                                <?php
                                   /*
                                   $btn_link = $this->url('user.cabinet.campaign', [
                                       'lang' => $this->lang,
                                       'id' => $campaign_id,
                                   ]);*/
                                ?>
                                <span class="group"><b class="closed">+</b><b class="open">&ndash;</b></span>
                                <input class="is-active" type="checkbox" data-id="<?= $group_id ?>" <?php if ($group['active']): ?>checked<?php endif; ?> disabled>
                                <span class="item-title"><?= $group['title'] ?></span>
                            </td>
                            <td class="stat <?= $target_class ?>"><?= $stat['views'] ?></td>
                            <td class="stat"><?= $stat['clicks'] ?></td>
                            <td class="stat"><?= $stat['ctr'] ?></td>
                            <td class="stat"><?= $stat['money'] ?></td>
                            <td class="stat <?= $target_class ?>"><?= $stat['reach'] ?></td>
                            <td class="stat"><?= $stat['cpm'] ?></td>
                            <td class="stat"><?= $stat['cpc'] ?></td>
                        </tr>
                        
                        <?php if ($groups_campaigns[$group_id]): ?>
                        
                            <?php foreach ($groups_campaigns[$group_id] as $campaign) : ?>
                                <tr class="campaign-item <?php if (!$campaign['active']): ?>disabled<?php endif; ?>" data-parent="<?= $group_id ?>">
                                    <?php
                                        $campaign_id = $campaign['id'];
                                        $stat = $stats[$campaign_id];
                                        $target_class = ($campaign['target_action'] == 'load') ? 'load-target' : '';
                                    ?>
                                    <td class="campaign-item-title">
                                        <?php
                                           $btn_link = $this->url('user.cabinet.campaign', [
                                               'lang' => $this->lang,
                                               'id' => $campaign_id,
                                           ]);
                                        ?>
                                        <input class="is-active" type="checkbox" data-id="<?= $campaign_id ?>" <?php if ($campaign['active']): ?>checked<?php endif; ?> disabled>
                                        <span class="item-title"><a href="<?= $btn_link?>"><?= $campaign['title'] ?></a></span>
                                    </td>
                                    <td class="stat <?= $target_class ?>"><?= $stat['views'] ?></td>
                                    <td class="stat"><?= $stat['clicks'] ?></td>
                                    <td class="stat"><?= $stat['ctr'] ?></td>
                                    <td class="stat"><?= $stat['money'] ?></td>
                                    <td class="stat <?= $target_class ?>"><?= $stat['reach'] ?></td>
                                    <td class="stat"><?= $stat['cpm'] ?></td>
                                    <td class="stat"><?= $stat['cpc'] ?></td>
                                </tr>
                            <?php endforeach; ?>
                        
                        <?php endif; ?>
                        
                    <?php endforeach; ?>
                    
                    </tbody>
                </table>

            </div>
            <div class="panel-footer">
            </div>    
        </div>
    </section>

<?php endif; ?>