<?php
/**
 * @var $lots array
 * @var $loadMoreBtn boolean
 * @var string $hiddenPrice
 * @var string $showEstimatedPriceInItemsList
 * @var string $displayStartPriceInCatalog
 */

use auctionforge\inc\abs\ImageModel;
use auctionforge\inc\controllers\BidspiritLots;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\helpers\View;
use auctionforge\inc\model\LotImage;

$auctionBg = "#ffffff";
$cdnUse = SettingsHelper::getOption('cdn_images', 'on');
$limitWords = apply_filters('auctionforge_lot_title_words', 8);
if (!empty($lots) && is_array($lots)) { ?>
    <div class="bt-wi-lots_wrp" data-bg-color="<?= $auctionBg ?>">
        <div class="bp-lots bp-d-flex bp-f-wrap bp-f-center bp-md-f-around bt-wi-lots">
            <?php /** @var WP_Post $lot */
            foreach ($lots as $lot) { ?>
                <?php
                $lotHelper = new LotHelper($lot->ID);
                $featureImage = $lotHelper->getFeatureImage();

                $auctions = get_the_terms($lot->ID, BidspiritLots::TAX_AUCTION_TYPE);
                $auction = 0;
                if (!empty($auctions)) {
                    $auctions[0] = apply_filters('auctionforge_prepare_auction_object', $auctions[0]);
                    $auction = $auctions[0];
                    $auctionId = SettingsHelper::getTermOption($auction->term_id, 'auction_id');
                } else {
                    $auctionId = 0;
                }

                $title = $lotHelper->getOptionValue('lot_name', '');
                $content = $lot->post_content;
                $title = LotHelper::getLotTitle($title, $content);
                $lotIndex = $lotHelper->getOptionValue('lot_itemIndex', 0);

                $isRemovedItem = $lotHelper->getOptionValue('removedFromAuction', 0);

                if ($showEstimatedPriceInItemsList == '1') {
                    $estimatedPrice = $lotHelper->getOptionValue('lot_estimatedPrice', 0);
                } else {
                    $estimatedPrice = null;
                }

                $itemData = new stdClass();
                $itemData->title = $title;
                $itemData->content = $content;
                ?>
                <div id="single-lot-<?= $auctionId ?>-<?= $lotIndex ?>"
                     class="bp-lot"
                     data-lot-index="<?= $lotIndex ?>"
                     data-auction-id="<?= $auctionId ?>"
                     data-content="<?= urlencode(json_encode($itemData)) ?>"
                >
                    <a href="<?= get_the_permalink($lot->ID) ?>" class="bp-lot__wrp-image">
                        <div class="bp-lot__image" style="background-image:url(<?= $featureImage ?>);"></div>
                    </a>
                    <div id="single-lot-<?= $auctionId ?>-<?= $lotIndex ?>-content"
                         class="bp-lot__content lot-<?= ($isRemovedItem === "1") ? "removed" : "isset" ?>">
                        <div class="bp-lot__title">
                            <a href="<?= get_the_permalink($lot->ID) ?>"><span class="bp-title">
                                    <?= LotHelper::printTranslateLot($lotIndex) ?>:</span><?= $title ?>
                            </a>
                        </div>
                        <div class="bp-lot__message"></div>
                        <div class="bp-lot__tool" data-entry="0.1">
                            <?php if (!$hiddenPrice) { ?>
                                <?php if ($showEstimatedPriceInItemsList == '1' && !empty($estimatedPrice) && $estimatedPrice != '') { ?>
                                    <p class="bp-subtitle"><?= __('Estimated price', 'auctionforge') ?>:</p>
                                    <p class="bp-price"><?= $estimatedPrice ?></p>
                                <?php } elseif ($showEstimatedPriceInItemsList == '0') { ?>
                                    <?php if ($displayStartPriceInCatalog == '1') { ?>
                                        <p class="bp-subtitle"><?= __('Start price', 'auctionforge') ?>:</p>
                                        <p class="bp-price"><?= $lotHelper->getOptionValue('lot_start_price', 0) ?></p>
                                    <?php } ?>
                                <?php } ?>
                            <?php } else { ?>
                                <?php if ($showEstimatedPriceInItemsList == '1' && !empty($estimatedPrice)) { ?>
                                    <p class="bp-subtitle"><?= __('Estimated price', 'auctionforge') ?>:</p>
                                    <p class="bp-price"><?= $estimatedPrice ?></p>
                                <?php } else { ?>
                                    <p class="bp-subtitle"><?= __('Not available', 'auctionforge') ?></p>
                                <?php } ?>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </div>
        <?php if ($loadMoreBtn) { ?>
            <div class="bt-wi-lots_more">
                <button class="btn"><?= __('Load more', 'auctionforge') ?></button>
            </div>
        <?php } ?>
    </div>
    <?php
    echo View::render('templates.parts.shortcodes-templates', array());
}
