<?php
/**
 * @var $index int
 * @var $featureImage string
 * @var $auction WP_Term
 * @var $auctionState string
 * @var $auctionDay array
 * @var $linkToAuction string
 * @var $target string|null
 * @var $dateTimeRaw string
 * @var $dateTime string
 * @var $auctionTitle bool
 */

use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\model\Auction;

$limitWords = apply_filters('auctionforge_lot_title_words', 8);
$dateTimeStr = date_i18n('M. j, Y', strtotime($dateTimeRaw));
?>
<div class="bp-lot bp-lot_auction bp-lot_countdown term_<?= $auction->term_id ?>">
    <?php if ($auctionState == Auction::STATE_PENDING) { ?>
        <div href="<?= $linkToAuction ?>" class="bp-lot__wrp-image">
            <p class="bp-lot__date"><?= $dateTimeStr ?></p>
            <div class="bp-lot__image" style="background-image:url('<?= $featureImage ?>');"></div>
        </div>
    <?php } else { ?>
        <a href="<?= $linkToAuction ?>" class="bp-lot__wrp-image">
            <p class="bp-lot__date"><?= $dateTimeStr ?></p>
            <div class="bp-lot__image" style="background-image:url('<?= $featureImage ?>');"></div>
        </a>
    <?php } ?>
    <div id="countdown-<?= $index ?>" class="bp-countdown" data-time="<?= preg_replace('/\s/', "T", $dateTime) ?>">
        <div class="bp-countdown-number">
            <span class="bp-days bp-countdown-time"></span>
            <span class="bp-countdown-text"><?= __("Days", "auctionforge") ?></span>
        </div>
        <div class="bp-countdown-number">
            <span class="bp-hours bp-countdown-time"></span>
            <span class="bp-countdown-text"><?= __("Hrs", "auctionforge") ?></span>
        </div>
        <div class="bp-countdown-number">
            <span class="bp-minutes bp-countdown-time"></span>
            <span class="bp-countdown-text"><?= __("Min", "auctionforge") ?></span>
        </div>
        <div class="bp-countdown-number">
            <span class="bp-seconds bp-countdown-time"></span>
            <span class="bp-countdown-text"><?= __("Sec", "auctionforge") ?></span>
        </div>
    </div>
    <div class="bp-lot__content">
        <div class="bp-lot__title">
            <?php if ($auctionState == Auction::STATE_PENDING) { ?>
                <p class="bp-title"><?= $auctionTitle ?></p>
            <?php } else { ?>
                <a href="<?= $linkToAuction ?>" class="bp-title"><?= $auctionTitle ?></a>
            <?php } ?>
            <?php if (!empty($auctionDay->details) && $auctionDay->details != "") { ?>
                <div class="bp-lot__text" data-text="auctionName"><?= wp_trim_words($auctionDay->details, $limitWords, '...') ?></div>
            <?php } elseif (!empty($auctionDay->name) && $auctionDay->name != "") { ?>
                <div class="bp-lot__text"><?= wp_trim_words($auctionDay->name, $limitWords, '...') ?></div>
            <?php } else { ?>
                <div class="bp-lot__text"><?= wp_trim_words($auction->description, $limitWords, '...') ?></div>
            <?php } ?>
        </div>
        <div class="bp-lot__tool bp-d-flex bp-f-center bp-f-wrap">
            <?php if ($auctionState == Auction::STATE_READY || $auctionState == Auction::STATE_RUNNING || $auctionState == Auction::STATE_PRE_AUCTION) { ?>
                <a href="<?= $linkToAuction ?>" class="bp-lot__tool__item bp-btn bp-btn-default"><?= __('View catalog', 'auctionforge') ?></a>
            <?php } elseif ($auctionState == Auction::STATE_PENDING) { ?>
                <div class="bp-lot__tool__item bp-text-red"><?= __('Catalog will be available soon', 'auctionforge') ?></div>
            <?php } elseif ($auctionState == Auction::STATE_ENDING &&
                (isset($auctionDay->postAuctionSaleOpenedUntil) && ($auctionDay->postAuctionSaleOpenedUntil/1000 > time()))) { ?>
                <div class="bp-lot__tool__item bp-text-red"><?= __('The after-sale has started', 'auctionforge') ?></div>
                <a href="<?= $linkToAuction ?>" <?= (!empty($target)) ? $target : '' ?> class="bp-lot__tool__item bp-btn bp-btn-default"><?= __('View results', 'auctionforge') ?></a>
            <?php } elseif ($auctionState == Auction::STATE_ENDING || $auctionState == Auction::STATE_ARCHIVED) { ?>
                <div class="bp-lot__tool__item bp-text-red"><?= __('The auction has ended', 'auctionforge') ?></div>
                <a href="<?= $linkToAuction ?>" <?= (!empty($target)) ? $target : '' ?> class="bp-lot__tool__item bp-btn bp-btn-default"><?= __('View results', 'auctionforge') ?></a>
            <?php } ?>
        </div>
    </div>
</div>