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

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


/** @var $layoutInfo \App\Entity\LayoutInfo */
$layoutInfo = $this->layoutInfo;

$lang = $this->lang;
$feed = $this->feed;
$currencies = $this->currencies;
$formats = $this->formats;
$users = $this->users;

$link_feeds_list = $this->url('adm.feeds', ['lang' => $lang]);
$link_cancel = $this->url('adm.feeds', ['lang' => $lang]);

if($feed)
{
    $feed_id = $feed->getId();
    $feed_title = $feed->getTitle();
    $feed_active = $feed->getActive() ? 'checked' : '';
    $feed_clientid = $feed->getClientid();
    $feed_url = $feed->getUrl();
    $feed_format = $feed->getFormat();
    $feed_status = $feed->getStatus();
    $feed_defaultcur = $feed->getDefaultcur();
    $feed_goodscount = $feed->getGoodscount();
    
    
    $form_link = $this->url('adm.feed.edit', ['lang' => $lang, 'id' => $feed_id]);
}
else
{
    $feed_active = 'checked';
    
    $form_link = $this->url('adm.feed.create',['lang' => $lang]);
}

?>


<section class="b-content__work inner">
    
    <form id="feed-create-form" method="post" action="<?= $form_link ?>">
        <div class="panel panel-info">
            <!-- Header -->
            <div class="panel-heading">
                <h1>
                    <a href="<?=$link_feeds_list ?>">Фиды</a> /
                    <?php if ($feed): ?>
                        <span class="offer-id" data-id="<?= $feed_id ?>"><?= $feed_id ?></span> /
                    <?php else: ?>
                        <span class="offer-id" data-id="0">Новый</span> /
                    <?php endif; ?>
                </h1>
            </div>
            <div class="panel-body">
                
                <!-- Активный фид -->
                <div class="form-group feed-enable">
                    <input type="checkbox" id="feed-active" <?=$feed_active ?>>
                    <label for="feed-active"><?= _t('Активный')?></label>
                </div>
                <div class="b-left">
                    
                    <!-- Название -->
                    <div class="form-group">
                        <label for="feed-title"><?= _t('Название')?></label>
                        <input type="text" class="form-control" id="feed-title" name="title" value="<?= $feed_title?>" placeholder="<?= _t('Название') ?>">
                    </div>
                    <!-- URL -->
                    <div class="form-group">
                        <label for="feed-url"><?= _t('URL')?></label>
                        <input type="text" class="form-control" id="feed-url" name="url" value="<?= $feed_url?>" placeholder="<?= _t('url') ?>">
                    </div>
                </div>
                
                <div class="b-right">
                    <!-- Пользователь -->
                    <div class="form-group">
                        <label for="feed-clientid"><?= _t('Пользователь')?></label>
                        <select class="form-control" id="feed-clientid" name="clientid" style="width: 250px;">
                            <option value="0" ><?= _t('Выберите пользователя') ?></option>
                            <?php foreach($users as $userId => $userName): ?>
                            <option value="<?= $userId?>" <?= ($userId == $feed_clientid ? 'selected' : '')?> ><?= $userName ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <!-- Format -->
                    <div class="form-group">
                        <label for="feed-format"><?= _t('Формат')?></label>
                        <select class="form-control" id="feed-format" name="format" style="width: 250px;">
                            <option value="0"><?= _t('не указано') ?></option>
                            <?php foreach($formats as $format): ?>
                            <option value="<?= $format?>" <?= ($format == $feed_format ? 'selected' : '')?> ><?= $format ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <!-- DefaultCUR -->
                    <div class="form-group">
                        <label for="feed-defaultcur"><?= _t('Основная валюта')?></label>
                        <select class="form-control" id="feed-defaultcur" name="defaultcur" style="width: 250px;">
                            <option value="0" ><?= _t('не указано') ?></option>
                            <?php foreach($currencies as $currencyCode => $name): ?>
                            <option value="<?= $currencyCode?>" <?= ($currencyCode == $feed_defaultcur ? 'selected' : '')?> ><?= $currencyCode?> (<?= $name?>)</option>
                            <?php endforeach ?>
                        </select>                    
                    </div>
                </div>
                    
                <div class="b-clear"></div>
                
                <!-- Сохранить / Отмена -->
                <button type="submit" class="btn btn-primary"><?= _t('Сохранить')?></button>
                <a class="btn btn-default cancel-action" href="<?= $link_cancel ?>"><?= _t('Отмена')?></a>
                
            </div>
        </div>
    </form>
    
</section>
