<?php


if (count($searchResult) > 0)
{


    ?>


    <span style="padding-left:20px;" id="search_div_qs_count">Найдено: <?php echo $searchCount ?></span>

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

    <table class="std_table js_cat_view" width="100%" cellspacing="0" cellpadding="0">
        <thead>
        <tr valign="bottom" align="left">
            <th colspan="2"
                data-sort="title" style="text-align: left">Название
            </th>
            <th data-sort="article" style="text-align: left">Артикул</th>
            <th data-sort="min">Мин.&nbsp;партия</th>
            <th data-sort="quantity">В&nbsp;наличии</th>
            <th data-sort="price" style="text-align: right">Цена</th>
            <th></th>
        </tr>
        </thead>
        <tbody>


        <?php

        $productsIdsInBasket = $this->context->getUser()->getProductsIdsInBasket();

        foreach ($searchResult as $product)
        {
            echo "<tr class='js_cat_view__ct__tr'>";
            echo "<td style='position: relative; text-align: center'>";
            if ($product['sell_out'])
            {
                echo '<div class="sale_out">Распродажа</div>';
            }


            echo "<a href='" . url_for('@catalog_show_prod?alias=' . $product['alias']) . "' style='border-bottom:none;'>";
            echo '<img class="js_cat_view__img image_view" src="' . url_for('@get_image?id=' . $product['id']) . '?small">';
            echo "</a>";
            echo "</td>";
            echo "<td class='js_cat_view_product_name__td'>";
            echo "<a href='" . url_for('@catalog_show_prod?alias=' . $product['alias']) . "'>" . $product['title'] . "</a>";
            echo "</td>";
            echo "<td>";
            echo $product['product_code'];
            echo "</td>";

            echo "<td>";
            echo $product['min_order'];
            echo "</td>";

            $stockCount = round($product['stockCount']);

            if($stockCount == 0)
            {
                echo "<td align='center'>";
                echo "Нет в наличии";
                echo "</td><td></td>";
                echo "<td><b>";
                if ($product['price'])
                {
                    echo number_format($product['price'],2,',',' ') . '<span class="rub">a</span>';
                }
                echo "</b></td>";
            }
            else
            {
                echo "<td>";
                echo round($product['stockCount']);
                echo "</td>";
                echo "<td><b>";
                if ($product['price'])
                {
                    echo number_format($product['price'],2,',',' ') . '<span class="rub">a</span>';
                }
                echo "</b></td>";


                $value = 0;
                $dopBtnClass = '';
                $canAction = 'false';
                if (key_exists($product['id'], $productsIdsInBasket))
                {
                    $value = $productsIdsInBasket[$product['id']];
                    $dopBtnClass = 'disabledBtn';
                    $canAction = 'true';
                }


                ?>
                <td>
                    <ul class="counter"
                        data-product-count="<?php echo $stockCount ?>"
                        data-product-min="<?php echo $product['min_order'] ?>"
                        data-product-id="<?php echo $product['id'] ?>"
                        data-product-price="<?php echo $product['price'] ?>" style="height: 36px; padding-top: 2px; border: 1px solid #d8d8d8;">

                        <li onclick="basketSender.singleDownCount($(this))" action="true" class="btn_all counter__button counter_button_minus red_button2" ></li>
                        <li class="counter__num">
                            <input onfocus="basketSender.onfocusCheck($(this))"
                                   onkeyup="basketSender.keyUpCheck($(this))"
                                   onchange="basketSender.singleByInput($(this))"
                                   name="" value="<?php echo $value ?>" autocomplete="off" type="text">
                        </li>
                        <li onclick="basketSender.singleUpCount($(this))" class="btn_all counter__button counter_button_plus green_button2"></li>
                    </ul>
                </td>
                <?php
            }

            echo "</tr>";
        }

        ?>

        </tbody>
    </table>

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

    <div class="postNavigation">


        <?php
        $href_previous = 'href="' . url_for('@search') . "?query=" . $searchString . '&page=' . ($page - 1) . '"';
        $href_first = 'href="' . url_for('@search') . "?query=" . $searchString . '&page=1"';
        if ($page != 1)
        {

            if ($page == $countPage)
            {
                ?>

                <a class="page_link page_button" style="width: auto; padding: 4px;" <?php echo $href_first ?>>В начало</a>

                <?php
            }

            ?>

            <a class="page_link page_button" <?php echo $href_previous ?>>←</a>

            <?php
        }
        ?>


        <?php
        if ($page - 2 >= 0)
        {
            for ($i = $page - 2; $i < $page; $i++)
            {
                if ($i == 0)
                {
                    continue;
                }

                ?>

                <a class="page_link" href="<?php echo url_for('@search') . "?query=" . $searchString ?>&page=<?php echo $i ?>"><?php echo $i ?></a>

                <?php
            }
        }
        ?>

        <b class="page_active"><?php echo $page ?></b>

        <?php
        if ($page + 2 <= $countPage)
        {
            $maxPage = $page + 2;
        }
        else
        {
            $maxPage = $countPage;
        }
        for ($i = $page + 1; $i <= $maxPage; $i++)
        {
            ?>

            <a class="page_link" href="<?php echo url_for('@search') . "?query=" . $searchString ?>&page=<?php echo $i ?>"><?php echo $i ?></a>

            <?php
        }
        ?>


        <?php
        $href_next = 'href="' . url_for('@search') . "?query=" . $searchString . '&page=' . ($page + 1) . '"';
        if ($page != $countPage)
        {
            ?>

            <a class="page_link page_button" <?php echo $href_next ?>>→</a>

            <?php
        }
        ?>


    </div>


    <?php
}
else
{
    ?>
    <div style="padding:6px 20px;color:#969696;">Ничего не найдено</div>
    <?php
}
?>