<?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-table-sort.js')
    ->appendFile('/js/adm/amsystem-table-filter.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;
$stat_cats = $this->stat_cats;


$error = $this->error;

?>

<?= $this->partial('app::user/amsystem/_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="users-groups">
                    
                    <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 groups" data-content="groups">
                        <thead>
                            <tr>
                                <th>Название кампании</th>
                                <th class="stat">Срок действия</th>
                                <?php foreach ($stat_cats as $cat_name => $cat) : ?>
                                    <th class="stat"><span class="sort" data-sort="<?= $cat_name ?>" data-sortasc="1"><?= $cat['title'] ?></span></th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>

                        <?php foreach ($groups as $group) : ?>
                            <?php
                                $group_id = $group['id'];
                                $stat = $groups_stat[$group_id];
                                $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.group', [
                                           'lang' => $this->lang,
                                           'id' => $group_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; ?>>
                                    <span class="item-title"><a href="<?= $btn_link?>"><?= $group['title'] ?></a></span>
                                </td>
                                <td></td>
                                <?php foreach ($stat_cats as $cat => $cat_data) : ?>
                                    <?php
                                    $target_class_item = (in_array($cat, ['views', 'reach'])) ? $target_class : '';
                                    ?>
                                    <td class="stat <?= $target_class_item ?>" data-cat="<?= $cat ?>" data-value="<?= $stat[$cat] ?>"><?= formatField($stat[$cat], $cat) ?></td>
                                <?php endforeach; ?>
                            </tr>

                            <?php if($groups_campaigns[$group_id]): ?>
                                <?php foreach ($groups_campaigns[$group_id] as $campaign) : ?>

                                    <?php
                                        $tr_class = '';
                                        if (!$campaign['active']) {
                                            $tr_class.= 'disabled ';
                                        }
                                        if (!$group['active']) {
                                            $tr_class.= 'disabled-group ';
                                        }
                                        if (!$campaign['active']) {
                                            $tr_class.= 'not-active ';
                                        } 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 = $campaigns_stat[$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; ?>>
                                            <span><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>

                                        <?php foreach ($stat_cats as $cat => $cat_data) : ?>
                                            <?php
                                            $target_class_item = (in_array($cat, ['views', 'reach'])) ? $target_class : '';
                                            ?>
                                            <td class="stat <?= $target_class_item ?>" data-cat="<?= $cat ?>" data-value="<?= $stat[$cat] ?>"><?= formatField($stat[$cat], $cat) ?></td>
                                        <?php endforeach; ?>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>

                        <?php endforeach; ?>


                        </tbody>
                    </table>
                    
                </div>

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

<?php endif; ?>