<?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/jquery.cookie.js')
    ->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;

?>

<?= $this->partial('app::user/_format'); ?>

<?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">
                
                <div class="table-filters">
                    Статус кампании:
                    <select class="form-control campaign-status">
                        <option value="all"><?= _t('все') ?></option>
                        <option value="active"><?= _t('активные') ?></option>
                        <option value="not-active"><?= _t('не активные') ?></option>
                        <option value="archive"><?= _t('в архиве') ?></option>
                        <option value="not-archive"><?= _t('не в архиве') ?></option>
                    </select>
                </div>

                <table class="table campaigns" data-content="campaigns" data-action="/amsystem/campaigns/id/edit/">
                    <thead>
                        <tr>
                            <th>Название кампании</th>
                            <th class="stat">Срок действия</th>
                            <th class="stat">Показы</th>
                            <th class="stat">Клики</th>
                            <th class="stat">CTR</th>
                            <th class="stat">Расход</th>
                            <th class="stat">Охват</th>
                            <th class="stat">eCPM</th>
                            <th class="stat">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></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) : ?>
                                <?php
                                    $tr_class = '';
                                    if (!$campaign['active']) {
                                        $tr_class.= 'not-active disabled ';
                                    } else {
                                        $tr_class.= 'active ';
                                    }
                                    if ($campaign['archive']) {
                                        $tr_class.= 'archive ';
                                    } else {
                                        $tr_class.= 'not-archive ';
                                    }
                                ?>
                                <tr class="campaign-item <?= $tr_class ?>" 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>
                                    <?php
                                    $begin = ($campaign['date_begin']) ? date('d-m-Y', strtotime($campaign['date_begin'])) : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                    $end = ($campaign['date_end']) ? date('d-m-Y', strtotime($campaign['date_end'])) : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                    $today = date('Y-m-d');
                                    $date_begin_class = '';
                                    $date_end_class = '';
                                    if ($campaign['date_begin'] && $today < $campaign['date_begin']) {
                                        $date_begin_class = 'not-active';
                                    }
                                    if ($campaign['date_end'] && $today > $campaign['date_end']) {
                                        $date_end_class = 'not-active';
                                    }

                                    ?>
                                    <td class="stat date">
                                        <span class="<?= $date_begin_class ?>"><?= $begin ?></span> &ndash; <span class="<?= $date_end_class ?>"><?= $end ?></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"><?= formatMoney($stat['money']) ?></td>
                                    <td class="stat <?= $target_class ?>"><?= $stat['reach'] ?></td>
                                    <td class="stat"><?= $stat['cpm'] ?> &#8372;</td>
                                    <td class="stat"><?= $stat['cpc'] ?> &#8372;</td>
                                </tr>
                            <?php endforeach; ?>
                        
                        <?php endif; ?>
                        
                    <?php endforeach; ?>
                    
                    </tbody>
                </table>

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

<?php endif; ?>