<?php

$this->headScript()
    ->appendFile('/js/bootstrap-tokenfield.js')
    ->appendFile('/js/bootstrap-datepicker.min.js')
    ->appendFile('/js/bootstrap-datepicker.ru.min.js')
    ->appendFile('/js/bootstrap-datepicker.en.min.js')
    ->appendFile('/js/adm/users_form.js')
; 

$page = $this->page;
$page_lang = $this->lang;

/** @var \App\Entity\User $user */
$user = $this->user;
$user_id = 0;
$user_date = date('Y-m-d');

$listAllSites = $this->listAllSites;
$sites = $this->sites;
$segments = $this->segments;
$categories = $this->categories;
$countries = $this->countries;
$managers = $this->managers;
$currencies = $this->currencies;
$providers_list = $this->providersList;

$page_title = '';

if($this->page == 'create') {
    $form_link = $this->url('adm.users.create', ['lang' => $this->lang]);
    $page_title = _t('Добавление пользователя');
    
} else {    
    $form_link = $this->url('adm.users.edit', ['lang' => $this->lang, 'id' => $user->getId()]);
    $page_title = _t('Редактирование пользователя');
    
    $user_id = $user->getId();
    
    $user_segment = $user->getSegment();
    $user_country = $user->getCountryCode();
    $user_currency = $user->getCurrencyCode();
    $user_date = $user->getDateCreate();
    $user_name = $user->getFullName();
    $user_email = $user->getEmail();
    $user_phone = $user->getPhone();
    $user_comment = $user->getComment();
    $user_sites = '';
    if($sites) {
        $user_sites = implode(', ', array_map(function($site) {
            return $site['domain'];            
        }, $sites));        
    }
    $user_categories = $user->getCategories();
    $user_treaffic = $user->getTraffic();
    $user_manager_id = $user->getSupportId();
    
    $accessSites = $this->accessSites;
    
    $codeProvider = $this->codeProvider;
    
}

$cancel_link = $this->url('adm.users', ['lang' => $this->lang]);
$site_link = $this->url('adm.users.status.site', ['lang' => $this->lang, 'id' => $user_id]);

?>


<?php /* Шаблон для code-provider */ ?>
<div class="code-provider-pattern">
    <div class="input-item">
        <select class="form-control code-provider">
            <option value="" disabled selected><?= _t('Выберите провайдера') ?></option>
            <?php foreach($providers_list as $provider_item): ?>
                <option value="<?= $provider_item['id'] ?>"><?= $provider_item['name'] ?></option>
            <?php endforeach ?>
        </select>
        <div class="delete-input">&#215;</div>
    </div>
</div>

<section class="b-content__work">
    <div class="panel panel-info">
        <div class="panel-heading">
            <h1 style="padding: 0 0 10px 0;"><?= $page_title ?></h1>
        </div>
        <div class="panel-body">
            <form id="user-form" method="POST" action="<?= $form_link?>">
                <div class="form-group">
                    <label for="user-date-create"><?= _t('Дата активации')?></label>                    
                    <input type="text" class="form-control" id="user-date-create" name="date_create" value="<?= $user_date?>" placeholder="<?= _t('Дата активации') ?>" data-language="<?= $page_lang?>" style="width: 105px;">                                                                    
                </div>                
                <div class="form-group">
                    <label for="user-manager"><?= _t('Менеджер')?></label>
                    <select class="form-control" id="user-manage" name="manager" style="width: 250px;">
                        <option value="0" ><?= _t('Выберите менеджера') ?></option>
                        <?php foreach($managers as $managerId => $managerName): ?>
                        <option value="<?= $managerId?>" <?= ($managerId == $user_manager_id ? 'selected' : '')?> ><?= $managerName ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div class="form-group">
                    <label for="user-name"><?= _t('Название')?></label>
                    <input type="text" class="form-control" id="user-name" name="fullname" value="<?= $user_name?>" placeholder="<?= _t('Название') ?>">
                </div>
                <div class="form-group">
                    <label for="user-segment"><?= _t('Сегмент')?></label>
                    <select class="form-control" id="user-segment" name="segment" style="width: 150px;">
                        <option value="" ><?= _t('Выберите сегмент') ?></option>                        
                        <?php foreach($segments as $segment): ?>
                        <option value="<?= $segment?>" <?= ($segment == $user_segment ? 'selected' : '')?> ><?= $segment?></option>
                        <?php endforeach ?>                        
                    </select>
                </div>
                <div class="form-group">
                    <label for="user-country"><?= _t('Страна')?></label>
                    <select class="form-control" id="user-country" name="country" style="width: 250px;">
                        <option value="" ><?= _t('Выберите страну') ?></option>
                        <?php foreach($countries as $countryCode => $name): ?>
                        <option value="<?= $countryCode?>" <?= ($countryCode == $user_country ? 'selected' : '')?> ><?= $name?></option>
                        <?php endforeach ?>
                    </select>                    
                </div>
                <div class="form-group">
                    <label for="user-currency"><?= _t('Валюта отображения')?></label>
                    <select class="form-control" id="user-currency" name="currency" style="width: 250px;">
                        <option value="USD" ><?= _t('Выберите валюту') ?></option>
                        <?php foreach($currencies as $currencyCode => $name): ?>
                        <option value="<?= $currencyCode?>" <?= ($currencyCode == $user_currency ? 'selected' : '')?> ><?= $currencyCode?> (<?= $name?>)</option>
                        <?php endforeach ?>
                    </select>                    
                </div>
                <div class="form-group">
                    <label for="user-email"><?= _t('Email')?></label>
                    <input type="email" class="form-control" id="user-email" name="email" value="<?= $user_email?>" placeholder="<?= _t('Email') ?>">
                </div>                
                <div class="form-group">
                    <label for="user-phone"><?= _t('Телефон')?></label>
                    <input type="text" class="form-control" id="user-phone" name="phone" value="<?= $user_phone?>" placeholder="<?= _t('Телефон') ?>">
                </div>                
                <div class="form-group">
                    <label for="user-email"><?= _t('Пароль')?></label>
                    <input type="text" class="form-control" id="user-password" name="password" placeholder="<?= _t('Пароль') ?>">
                </div>
                
                
                <?php if($this->page == 'edit'): ?>
                
                    <?php if($feeds): ?>
                        <label for="user-feeds"><?= _t('Фиды текущего пользователя')?></label>
                        <div class="tokenfield">
                        <?php foreach($feeds as $feed): ?>
                            <?php
                                $feed_link = $this->url('adm.feed.edit', [
                                    'lang' => $this->lang,
                                    'id' => $feed['id'],
                                ]);
                            ?>
                            <div class="token"><span class="token-label"><a href="<?= $feed_link ?>"><?= $feed['title'] ?></a></span></div>
                        <?php endforeach; ?>
                        </div>
                    <?php else: ?> 

                        <div class="form-group">
                            <label for="user-site"><?= _t('Сайты текущего пользователя')?></label>
                            <!--
                            <input type="hidden<?php /* text */?>" class="form-control" id="user-site" name="sites" value="<?= $user_sites?>" data-status-url="<?=$site_link?>">
                            -->

                            <?php if($sites): ?>
                                <div class="tokenfield">
                                    <?php foreach($sites as $site): ?>
                                        <?php
                                            $btn_link = $this->url('adm.sites.edit', [
                                                'lang' => $this->lang,
                                                'id' => $site['id'],
                                            ]);
                                        ?>
                                        <div class="token"><span class="token-label"><a href="<?= $btn_link ?>"><?= $site['domain'] ?></a></span></div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else : ?>
                            <div class="tokenfield">У пользователя пока нет закрепленных за ним сайтов.</div>
                            <?php endif; ?>

                            <div class="btn-group" role="group">
                                <?php                
                                    $btn_link = $this->url('adm.sites.create', [                    
                                        'lang' => $this->lang,
                                        'userId' => $user_id,
                                    ]);
                                ?>
                                <a class="btn btn-primary" href="<?= $btn_link ?>"><?= _t('Добавить сайт') ?></a>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="user-access-site"><?= _t('Другие доступные сайты')?></label>
                            <?php if($accessSites): ?>
                                <div class="tokenfield">
                                    <?php foreach($accessSites as $site): ?>
                                        <?php
                                            $btn_link = $this->url('adm.sites.edit', [
                                                'lang' => $this->lang,
                                                'id' => $site->getId(),
                                            ]);
                                        ?>
                                        <div class="token"><span class="token-label"><a href="<?= $btn_link ?>"><?= $site->getDomain() ?></a></span></div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else : ?>
                            <div class="tokenfield">У пользователя пока нет доступных сайтов.</div>
                            <?php endif; ?>
                        </div>

                        <?php /* ?>
                        <div class="form-group">
                            <div style="margin-bottom: 10px;">
                                <label for="user-access-site"><?= _t('Доступные сайты')?></label>
                                <input type="text" class="form-control" id="user-access-site" name="access_sites" value="<?= $list_access_sites ?>">
                            </div>
                            <select class="form-control access-site" style="width: 250px; display: inline-block;">
                                <option value="" ><?= _t('Выберите сайт') ?></option>
                                <?php foreach($listAllSites as $site): ?>
                                <option value="<?= $site['domain'] ?>"><?= $site['domain'] ?></option>
                                <?php endforeach ?>
                            </select>
                            <a class="btn btn-primary add-access-site" href="#" style="vertical-align: 0px;"><?= _t('Добавить сайт') ?></a>
                        </div>
                        <?php */ ?>

                    <?php endif; ?>
                        
                <?php endif; ?>

                <div class="form-group code-provider">
                    <label for="code-provider">Провайдеры, к кодам которых у пользователя есть доступ:</label>
                    <a class="btn btn-primary edit-item" href="#"><?= _t('Редактировать') ?></a>
                    <input class="form-control" type="hidden" id="code-provider" name="code_provider" value="">
                    <?php $providers = ($codeProvider) ? $codeProvider : []; ?>
                    <div class="values-string">
                        <?php foreach($providers as $provider): ?>
                            <span><?= $provider['name'] ?></span>
                        <?php endforeach ?>
                    </div>
                    <div class="values-edit">
                        <div class="no-values" style="width: 400px;">Нет значений в списке</div>
                        <div class="values-items">
                            <?php foreach($providers as $provider): ?>
                            <div class="input-item">
                                <select class="form-control code-provider">
                                <?php foreach($providers_list as $provider_item): ?>
                                    <option value="<?= $provider_item['id'] ?>" <?php if($provider_item['id'] == $provider['id']): ?>selected<?php endif; ?>><?= $provider_item['name'] ?></option>
                                <?php endforeach ?>
                                </select>
                                <div class="delete-input">&#215;</div>
                            </div>
                            <?php endforeach ?>
                        </div>
                        <div class="controls">
                            <div class="temp-state"></div>
                            <a class="btn btn-primary add-item" href="#"><?= _t('Добавить') ?></a>
                            <a class="btn btn-primary cancel-item" href="#"><?= _t('Отмена') ?></a>
                            <a class="btn btn-primary save-item" href="#"><?= _t('Сохранить') ?></a>
                        </div>
                    </div>
                </div>

                
                <?php /* ?>
                <div class="form-group">
                    <label><?= _t('Категория') ?></label>
                    <?php foreach($categories as $categoryId => $categoryName): ?>
                    <div class="checkbox">
                        <label><input type="checkbox" name="category[]" value="<?= $categoryId ?>" <?= (isset($user_categories[$categoryId]) ? 'checked' : '')?> ><?= _t($categoryName) ?></label>
                    </div>
                    <?php endforeach ?>
                </div>
                <?php */ ?>
                
                <div class="form-group">
                    <label for="user-site-traffic"><?= _t('Траффик') ?></label>
                    <input type="text" class="form-control" id="user-site-traffic" name="traffic" value="<?= $user_treaffic ?>" placeholder="<?= _t('Траффик') ?>">
                </div>
                <div class="form-group">
                    <label for="user-comment"><?= _t('Комментарий')?></label>
                    <textarea class="form-control" id="user-comment" name="comment" style="height: 100px;"><?= $user_comment?></textarea>
                </div>                
                <button type="submit" class="btn btn-primary"><?= _t('Сохранить') ?></button>                
                <a class="btn btn-default" href="<?= $cancel_link?>"><?= _t('Отмена') ?></a>
            </form>
        </div>
    </div>
</section>

