<?php
/**
 * YAWIK
 *
 * @filesource
 * @license MIT
 * @copyright  2013 - 2015 Cross Solution <http://cross-solution.de>
 */

$options = $this->services('Core/Options');
$defaultCurrencyCode = $options->defaultCurrencyCode;
$defaultTaxRate = $options->defaultTaxRate;


if ($this->renderSummary):?>
    <div class="row">
        <div class="col-md-12">
            <?php
            /* @var $router \Laminas\Mvc\Router\Http\TreeRouteStack */
            $router = $this->services('Router');
            /* @var $provider \Jobs\Options\ProviderOptions */
            $provider = $this->services('Jobs/Options/Provider');
            $options = $this->element->getValueOptions();
            $valueKey= $this->element->getValue();

            $activePortals = array();
            if ($valueKey && !empty($valueKey)):

               $this->plugin('currencyformat')->setCurrencyCode($defaultCurrencyCode);

                $total = 0; $sum = 0;
                foreach ($options as $category => $categoryOptions):
                    foreach ($categoryOptions['options'] as $channelName => $channel):
                        if (in_array($channelName, $valueKey)):
                            $activePortals[$category][$channelName] = $channel;
                        endif;
                    endforeach;
                endforeach; ?>
                <div class="panel panel-default">
                    <div class="panel-body">
                    <?php foreach ($activePortals as $category => $channels):?>
                        <h5><?php echo $category ?></h5>
                        <div class="row">
                        <?php foreach ($channels as $name => $spec):
                            /* @var $channel \Jobs\Options\ChannelOptions */
                            $channel = $provider->getChannel($name);


                            ?>
                            <div class="col-md-6" style="height: 34px; padding:  5px 15px;">
                                <a data-target="#modal-info-<?php echo $name?>" type="button" data-toggle="modal" href="<?php
                                $link = $router->assemble($channel->getParams(), array('name' => $channel->getRoute()));echo $link ?>">

                                    <?php if ($logo = $channel->getLogo()):?>
                                    <img src="<?php echo $this->basepath($logo) ?>" alt="<?php echo $channel->getLabel() ?>" class="img-thumbnail" style="max-width: 60px; max-height: 24px;">
                                    <?php endif; ?> <div><?php echo $channel->getLinkText() ?></div></a></div>
                            <?php
                            /**
                            * @todo: move the modal container at the bottom of the page and refresh the modal content
                            */
                            ?>
                                <div id="modal-info-<?php echo $name?>" class="modal fade modal-scrollable" aria-hidden="true" aria-labelledby="portal help" role="dialog" tabindex="-1">
                                <div class="modal-dialog modal-lg">
                                    <div class="modal-content"> </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                        </div>
                    <?php endforeach; ?>
                        </div>
                    <table class="table table-condensed table-bordered">
                    <thead>
                        <tr>
                            <th><?php echo $this->translate('Price') ?></th>
                            <th class="text-right"><?php
                                /* @var $filter \Jobs\Filter\ChannelPrices */
                                $filter = $this->services('FilterManager.Jobs/ChannelPrices');
                                $total = $filter->filter($valueKey);
                                echo $this->currencyFormat($total); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><?php echo $this->translate('plus VAT') ?> (<?php echo $defaultTaxRate ?>%)</td>
                            <td class="text-right"><?php echo $this->currencyFormat($total * ( $defaultTaxRate /100)); ?></td>
                        </tr>
                    </tbody>
                    <tfoot>
                        <tr>
                            <th><?php echo $this->translate('Invoice amount') ?></th>
                            <th class="text-right"><?php echo $this->currencyFormat($total * (1 + $defaultTaxRate /100)); ?></th>
                        </tr>
                    </tfoot>
                </table>
                </div>
            <?php endif; ?>
        </div>
    </div>

<?php else:
    echo $this->formRow()->render($this->element, /* ignoreViewPartial */ true);
    ?>
    <div class="row">
        <div id="<?php echo $this->element->getAttribute('id') ?>-total" class="text-right col-md-9 col-md-offset-3">
            <div class="panel panel-default">
                <div class="panel-body"><strong><span></span> <?php echo $defaultCurrencyCode?></strong><br>
                <small><?php echo $this->translate('plus VAT') ?></small></div>
            </div>
        </div>
    </div>
    <?php
    // Element Templates
    $resultTmpl = '
    <div class="clearfix">
        <div class="pull-left">
            <strong>%name - %headline</strong><br>
            <small>%desc</small>
        </div>
        <div class="pull-right">
            <strong>%nicePrice</strong>
        </div>
    </div>
    ';

    $selectTmpl = '
        %name (  %duration ) [ %nicePrice (%minNicePrice) ]
    ';

    ?>
    <span id="<?php echo $this->element->getAttribute('id') ?>-result-tmpl" data-template="<?php echo $this->escapeHtmlAttr($resultTmpl) ?>"></span>
    <span id="<?php echo $this->element->getAttribute('id') ?>-select-tmpl" data-template="<?php echo $this->escapeHtmlAttr($selectTmpl) ?>"></span>
    <span id="<?php echo $this->element->getAttribute('id') ?>-currency-tmpl" data-template="<?php echo $this->numberFormat(1234.56) ?>"></span>
<?php endif; // (else > $this->renderSummary) ?>