<?php
/**
 * Template for displaying a manual auction in the upcoming auctions shortcode.
 *
 * @var $index int
 * @var $manualAuction object
 */

use auctionforge\inc\helpers\LotHelper;

$limitWords = apply_filters('auctionforge_lot_title_words', 8);

$featureImage = !empty($manualAuction->feature_image) ? $manualAuction->feature_image : LotHelper::getPlaceholderImage();
$auctionTitle = esc_html($manualAuction->title);
$auctionLink = !empty($manualAuction->auction_link) ? esc_url($manualAuction->auction_link) : '';
$isExternal = !empty($manualAuction->has_external_link);
$target = $isExternal ? 'target="_blank"' : '';
$description = !empty($manualAuction->description) ? wp_trim_words(wp_strip_all_tags($manualAuction->description), $limitWords, '...') : '';

$dateTimeRaw = $manualAuction->date . ' ' . $manualAuction->time;
$dateTimeStr = date_i18n('M. j, Y', strtotime($manualAuction->date));
$dateTimeFormatted = date("Y-m-d H:i:s", strtotime($dateTimeRaw));
?>
<div class="bp-lot bp-lot_auction bp-lot_countdown manual_auction_<?= $manualAuction->post_id ?>">
    <?php if (!empty($auctionLink)) { ?>
        <a href="<?= $auctionLink ?>" <?= $target ?> class="bp-lot__wrp-image">
            <p class="bp-lot__date"><?= $dateTimeStr ?></p>
            <div class="bp-lot__image" style="background-image:url('<?= esc_url($featureImage) ?>');"></div>
        </a>
    <?php } else { ?>
        <div class="bp-lot__wrp-image">
            <p class="bp-lot__date"><?= $dateTimeStr ?></p>
            <div class="bp-lot__image" style="background-image:url('<?= esc_url($featureImage) ?>');"></div>
        </div>
    <?php } ?>
    <div id="countdown-manual-<?= $index ?>" class="bp-countdown" data-time="<?= preg_replace('/\s/', "T", $dateTimeFormatted) ?>">
        <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 (!empty($auctionLink)) { ?>
                <a href="<?= $auctionLink ?>" <?= $target ?> class="bp-title"><?= $auctionTitle ?></a>
            <?php } else { ?>
                <p class="bp-title"><?= $auctionTitle ?></p>
            <?php } ?>
            <?php if (!empty($description)) { ?>
                <div class="bp-lot__text"><?= $description ?></div>
            <?php } ?>
        </div>
        <div class="bp-lot__tool bp-d-flex bp-f-center bp-f-wrap">
            <?php if (!empty($auctionLink)) { ?>
                <a href="<?= $auctionLink ?>" <?= $target ?> class="bp-lot__tool__item bp-btn bp-btn-default"><?= __('View auction', 'auctionforge') ?></a>
            <?php } ?>
        </div>
    </div>
</div>
