<?php
slot('title', 'Каталог - ' . __(sfConfig::get('app_www_title')));
use_javascript('fotorama.js');
use_stylesheet('fotorama.css');
/** @var Product $product */
$directPrice = $product->getDirectPrice();
$priceM2 = 0;
$priceList = 0;
/*
if(!$directPrice)
{
    if($product->getM2Price())
    {
        $priceM2 = $product->getM2Price();
    }
    if($product->getListPrice())
    {
        $priceList = $product->getListPrice();
    }
}*/

if($priceM2 == 0 && $priceList == 0)
{

    $offers = array();

    foreach($product->getOffer() as $offer)
    {
        $offers[] = array(
            'title' => $offer->getTranslation()->$culture->title,
            'price' => $offer->getPrice(),
            'img' => $offer->getImg()
        );
    }

}

$view = false;

if($priceM2 != 0 && $priceList != 0)
{
    $view = 'default_offer';
}
elseif($directPrice)
{
    $view = 'simple';
}
else
{
    $view = 'custom_offer';
}




?>

<style>
    .content__main {padding-bottom: 40px;}
</style>


<div class="content__main">
    <div class="max_width">

        <div class="breadcrumbs">
            <a href="/" title="Главная">Главная</a>
            <i class="arrow"></i>
            <a href="<?php echo url_for("@catalog"); ?>" title="Каталог">Каталог</a>
            <?php

            $cat = $product->getCatProducts()->getFirst();
            $breadcrumbs = $cat->getBreadcrumbs();

            if($breadcrumbs)
            {
                foreach($breadcrumbs as $crumb)
                {
                    $url = url_for('@catalog_category?alias=' . $crumb['alias']);

                    echo '<i class="arrow"></i>';
                    echo '<a href="' . $url . '" title="' . $crumb['title'] . '">' . $crumb['title'] . '</a>';
                }


            }

            $url = url_for('@catalog_category?alias=' . $cat['alias']);

            echo '<i class="arrow"></i>';
            echo '<a href="' . $url . '" title="' . $cat['title'] . '">' . $cat['title'] . '</a>';

            ?>
        </div>

        <h1 class="content_title"><?php echo $product->getTitle() ?></h1>
        
        <div class="catalog_product_wrap">
            <form action="<?php echo url_for('@basket_append') ?>" method="post">
                
                <?php if($product->getOffer()->count() > 0) {?>
                    <input type="hidden" name="instance" value="offer">
                    <div class="form_table">
                <?php foreach ($product->getOffer() as $productOffer) {?>
                        <label class="form_table_row">
                            <div class="form_table_cell">
                            <input type="radio" name="id" value="<?php echo $productOffer->getIncremented()?>">
                            <?php echo $productOffer->getTitle();?>
                            </div>
                            <div class="form_table_cell">
                            <input type="text" name="count_<?php echo $productOffer->getIncremented();?>" value="1">
                            </div>
                            <div class="form_table_cell">
                            <?php echo $productOffer->getPrice();?>
                            </div>
                            </label>
                <?php }?>
                    </div>
                        <?} else {?>
                    <input type="hidden" name="id" value="<?php echo $product->getIncremented()?>">
                    <input type="hidden" name="instance" value="product">
                    <input type="text" name="count_<?php echo $product->getIncremented()?>" value="1">
                <?php }?>
                <input type="submit" class="btn green_b_btn product_btn" value="В корзину">
            </form>
            <table cellspacing="0" cellpadding="0" width="100%">
                <tbody valign="top">
                <tr class="product_main">
                    <td width="48%">
                        <div class="fotorama"
                             width="100%"
                             height="400px"
                             data-nav="thumbs"
                             style="display: inline-block;">

                            <?php

                            $productImgs = explode(';', $product->getProductImg());

                            foreach($productImgs as $img)
                            {
                                echo '<img src="/u/i/' . str_replace('.', '-XXL.', $img) . '">';
                            }

                            ?>
                        </div>
                    </td>
                    <td width="50"></td>
                    <td>


                        <?php



                        if($view == 'default_offer')
                        {
                            ?>
                            <span class="product_title"><?php echo $product->getTitle() ?></span>

                            <i class="br25"></i>

                            <table cellpadding="0" cellspacing="0" width="200">
                                <tbody valign="top">
                                <tr>
                                    <td align="left"><b>Цена за м²:</b><i class="br10"></td>
                                    <td align="right"><?php echo $priceM2 ?> <span class="rub fs_11em">i</span><i class="br10"></td>
                                </tr>
                                <tr>
                                    <td align="left"><b>Цена за лист:</b></td>
                                    <td align="right"><?php echo $priceList ?> <span class="rub fs_11em">i</span></td>
                                </tr>
                                </tbody>
                            </table>

                            <i class="br30"></i>

                            <?php
                        }

                        ?>


                        <table cellpadding="0" cellspacing="0" width="100%">
                            <tbody valign="middle" align="left">
                            <tr>

                                <?php

                                if($view == 'simple') { ?>

                                    <td width="1" style="white-space: nowrap; padding-left: 20px;">
                                        <div class="product_counter">
                                            <input type="button" class="product_counter_minus" data-symbol="-" onclick="counterLite.update($(this))">
                                            <input type="text" class="product_counter_val" value="1" data-symbol="false" onkeyup="counterLite.update($(this))">
                                            <input type="button" class="product_counter_plus" data-symbol="+" onclick="counterLite.update($(this))">
                                        </div>
                                    </td>
                                    <td align="center">
                                        <span class="product_price">
                                            <span class="product_price_value" data-price="<?php echo $directPrice; ?>"><?php echo $directPrice ?></span>
                                            <span class="rub fs_10em">i</span>
                                        </span>
                                    </td>
                                    <td width="1">
                                        <button  class="btn green_b_btn product_btn">В корзину</button>
                                    </td>

                                    <?php
                                } elseif($view == 'default_offer') { ?>

                                    <td width="1" style="white-space: nowrap;">
                                        <div class="product_counter" data-id="<?php echo $product->getId(); ?>">
                                            <input type="button" class="product_counter_minus" data-symbol="-" onclick="counterLite.update($(this))">
                                            <input type="text" class="product_counter_val" value="1" data-symbol="false" onkeyup="counterLite.update($(this))">
                                            <input type="button" class="product_counter_plus" data-symbol="+" onclick="counterLite.update($(this))">
                                        </div>
                                        <ul class="product_price_trigger">
                                            <?php

                                            $defaultShowPrice = 0;

                                            if($priceM2 != 0) {
                                                echo '<li>';
                                                    echo '<label>
                                                            <input class="selectPriceOffer" type="radio" name="price" data-price="' . $priceM2 . '" onchange="counterLite.change($(this))" checked>
                                                            <span></span> м²
                                                          </label>';
                                                echo '</li>';

                                                $defaultShowPrice = $priceM2;

                                            } if($priceList != 0) {
                                                echo '<li>';
                                                    echo '<label>
                                                            <input class="selectPriceOffer" type="radio" name="price" data-price="' . $priceList . '" onchange="counterLite.change($(this))">
                                                            <span></span> лист
                                                          </label>';
                                                echo '</li>';

                                                $defaultShowPrice = $defaultShowPrice == 0 ? $priceList : $defaultShowPrice;

                                            }
                                            ?>
                                        </ul>
                                    </td>
                                    <td align="center">
                                        <span class="product_price">
                                            <span class="product_price_value"><?php echo $defaultShowPrice ?></span>
                                            <span class="rub fs_10em">i</span>
                                        </span>
                                    </td>
                                    <td width="1">
                                        <button class="btn green_b_btn product_btn">В корзину</button>
                                    </td>

                                    <?php
                                } elseif($view == 'custom_offer') { ?>

                                    <td>
                                        <?php

                                        $first_cost = 0;

                                        if(count($offers) > 0) {
                                            $i = 1;
                                            foreach($offers as $offer)
                                            {
                                                if($i == 1) {
                                                    $first_cost = $offer['price'];
                                                }
                                                ?>

                                                <label class="product_offer">
                                                    <input type="radio" name="offer" onchange="counterLite.change($(this))" <?php echo $i==1 ? 'checked' : '';?>>
                                                    <table width="100%" cellpadding="0" cellspacing="0">
                                                        <tr>
                                                            <td><?php echo $offer['title']; ?></td>
                                                            <td align="right">
                                                                <div class="product_counter min" data-id="<?php echo $product->getId(); ?>" data-price="<?php echo $offer['price']; ?>">
                                                                    <input type="button" class="product_counter_minus" data-symbol="-" onclick="counterLite.update($(this))">
                                                                    <input type="text" class="product_counter_val" value="1" data-symbol="false" onkeyup="counterLite.update($(this))">
                                                                    <input type="button" class="product_counter_plus" data-symbol="+" onclick="counterLite.update($(this))">
                                                                </div>
                                                            </td>
                                                            <td width="1" align="right">
                                                                <?php echo $offer['price']; ?>
                                                                <span class="rub">i</span>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </label>

                                                <?php
                                                $i++;
                                            }

                                        }

                                        ?>

                                        <i class="br5"></i>

                                        <hr class="product_hr">

                                        <i class="br20"></i>

                                        <table cellspacing="0" cellpadding="0" width="100%">
                                            <tbody align="right">
                                            <tr>
                                                <td style="padding-right: 25px;">
                                                    <span class="product_price">
                                                        <span class="product_price_value" data-price="<?php echo $first_cost; ?>"><?php echo $first_cost; ?></span>
                                                        <span class="rub fs_10em">i</span></span>
                                                </td>
                                                <td width="1" style="padding-right: 20px;"><button href="" class="btn green_b_btn product_btn">В корзину</button></td>
                                            </tr>
                                            </tbody>
                                        </table>

                                    </td>

                                    <?php
                                }
                                ?>


                            </tr>
                            <tr>
                                <td class="product__advantages__info" colspan="3">

                                    <?php

                                    $productDescription = $product->getDescription();
                                    $productPropValue = $product->getPropertyArray();


                                    ?>

                                    <i class="br40"></i>

                                    <?php

                                    if($productDescription)
                                    {
                                        echo '<a href="" class="fs_18 bbs_d green_link" data-tab="description">Описание</a>';
                                    }

                                    if(count($productPropValue) > 0)
                                    {
                                        echo '<a href="" class="fs_18 bbs_d green_link active" data-tab="features">Характеристики</a>';
                                    }

                                    ?>
                                    <i class="br20"></i>
                                    <div class="product__advantages__info__tab" data-tab="description"><?php echo $productDescription ?></div>
                                    <div class="product__advantages__info__tab active" data-tab="features">
                                        <table cellspacing="0" cellpadding="0" width="100%" style="max-width: 450px;">
                                            <tbody valign="top" align="left">
                                            <?php

                                            foreach($productPropValue as $prop)
                                            {
                                                echo '<tr>';
                                                    echo '<td>' . $prop['prop'] . '</td>';
                                                    echo '<td></td>';
                                                    echo '<td>' . $prop['value'] . '</td>';
                                                echo '</tr>';
                                            }

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

                                    <script type="text/javascript">
                                        $('.product__advantages__info a').click(function(e){
                                            $('.product__advantages__info a, .product__advantages__info__tab').removeClass('active');
                                            $(this).addClass('active');
                                            $('.product__advantages__info__tab[data-tab='+$(this).data('tab')+']').addClass('active');
                                            $('.product__advantages__info__tab[data-tab='+$(this).data('tab')+']').addClass('active');
                                            return false;
                                        })
                                    </script>
                                </td>
                            </tr>
                            </tbody>
                        </table>

                    </td>
                </tr>
                </tbody>
            </table>
        </div>

    </div>
</div>


<div class="product_section product__calculator">
    <div class="max_width">

        <?php include_partial("catalog/product_calculator"); ?>

    </div>
</div>


<?php include_partial("pages/landingArea2", array("class" => "product_section background_grey")); ?>


<div class="product_section product__advantages">
    <div class="caption">С этим товаром покупают</div>
    <div class="max_width">
        <table cellpadding="0" cellspacing="0" width="100%">
            <tbody valign="top" align="left">
            <tr>
                <td>
                    <?php include_partial("catalog/product", array("count" => 4)); ?>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>


<div class="product_section product__video">
    <div class="caption">Видео</div>
    <div class="max_width">

        <div class="video_item">
            <iframe src="https://www.youtube.com/embed/JMkHr68zO6Q?ecver=2" width="640" height="360" frameborder="0" allowfullscreen></iframe>
        </div>

        <div class="video_item">
            <iframe src="https://www.youtube.com/embed/JMkHr68zO6Q?ecver=2" width="640" height="360" frameborder="0" allowfullscreen></iframe>
        </div>

    </div>
</div>


<div class="product_section product__articles">
    <div class="caption">Статьи</div>
    <div class="max_width">

        <?php include_partial("article/article", array("count" => 4)); ?>

    </div>
</div>


<div class="product_section product__reviews background_lightgrey">

    <div class="caption">Отзывы клиентов</div>
    <div class="max_width">

        <?php include_partial("review/review"); ?>

    </div>
</div>