<?php
/**
 * @var $title string
 * @var $lots array
 * @var $args array
 */

use auctionforge\inc\controllers\BidspiritLots;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\model\Auction;

$hash = "-" . rand(1000, 9999);

if (!empty($args['before_widget'])) {
    echo $args['before_widget'];
}
echo "<div class=\"bp-widget-block\">";

if ((!empty($lots) && is_array($lots)) && !empty($title)) {
    echo "<div class=\"bt-main__title\">{$title}</div>";
}

if (!empty($lots) && is_array($lots)) {
    echo "<div class=\"bt-wi-slider bt-wi-slider_swiper\">";
    echo "<div class=\"swiper-container bt-wi-lots_wrp\">
            <div class=\"swiper-wrapper bp-lots bt-wi-lots\" data-hash=\"" . $hash . "\">";
    /** @var WP_Post $lot */
    $limitWords = apply_filters('auctionforge_lot_title_words', 8);
    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);
        $estimatedPrice = $lotHelper->getOptionValue('estimated_price', 0);
        $itemData = new stdClass();
        $itemData->title = $title;
        $itemData->content = $content;
        ?>
        <div id="single-lot-<?= $auctionId ?>-<?= $lotIndex ?><?= $hash ?>"
             class="bp-lot swiper-slide"
             data-lot-index="<?= $lotIndex ?>"
             data-auction-id="<?= $auctionId ?>"
             data-estimated-price="<?= $estimatedPrice ?>"
             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 ?><?= $hash ?>-content" class="bp-lot__content">
                <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">
                    <p class="bp-subtitle"><?= __('Start price', 'auctionforge') ?>:</p>
                    <p class="bp-price"><?= LotHelper::formattedPrice($lotHelper->getOptionValue('lot_start_price', 0)) ?></p>
                </div>
            </div>
        </div>
    <?php }
    echo "</div>";
    echo "</div>";
    echo "</div>";
}
echo "</div>";
if (!empty($args['after_widget'])) {
    echo $args['after_widget'];
}
