<?php
/**
 * Wide layout template for a Bidspirit auction card in the bp_upcoming_wide shortcode.
 *
 * @var $index string
 * @var $featureImage string
 * @var $auction WP_Term
 * @var $auctionState string
 * @var $auctionDay object
 * @var $linkToAuction string
 * @var $dateTimeRaw string
 * @var $dateTime string
 * @var $auctionTitle string
 * @var $registerUrl string
 */

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

$limitWords = apply_filters('auctionforge_lot_title_words', 80);
$dateTimeStr = date_i18n('F jS, Y g:i A', strtotime($dateTimeRaw));
// BidSpirit times are in the auction house's local timezone.
// Convert to UTC using the AF time_zone_name setting for the $isLive check.
$afTzName = SettingsHelper::getOption(Settings::OPTIONS_TIMEZONES, '');
try {
    $tz = !empty($afTzName) ? new \DateTimeZone($afTzName) : new \DateTimeZone('UTC');
    $dtObj = new \DateTime($dateTimeRaw, $tz);
    $auctionStartUtc = $dtObj->getTimestamp();
} catch (\Exception $e) {
    $auctionStartUtc = strtotime($dateTimeRaw);
}
$isLive = ($auctionStartUtc - 3600) <= time();
?>
<div class="bpw-auction-card">
    <div class="bpw-auction-img">
        <?php if ($auctionState != Auction::STATE_PENDING) { ?>
            <a href="<?= esc_url($linkToAuction) ?>">
                <img src="<?= esc_url($featureImage) ?>" alt="<?= esc_attr($auctionTitle) ?>" />
            </a>
        <?php } else { ?>
            <div class="bpw-img-wrap">
                <img src="<?= esc_url($featureImage) ?>" alt="<?= esc_attr($auctionTitle) ?>" />
            </div>
        <?php } ?>
    </div>
    <div class="bpw-auction-body">
        <h3 class="bpw-title">
            <?php if ($auctionState != Auction::STATE_PENDING) { ?>
                <a href="<?= esc_url($linkToAuction) ?>"><?= $auctionTitle ?></a>
            <?php } else { ?>
                <?= $auctionTitle ?>
            <?php } ?>
        </h3>

        <div class="bpw-desc">
            <?php if (!empty($auctionDay->details) && $auctionDay->details != "") { ?>
                <?= wp_trim_words($auctionDay->details, $limitWords, '...') ?>
            <?php } elseif (!empty($auctionDay->name) && $auctionDay->name != "") { ?>
                <?= wp_trim_words($auctionDay->name, $limitWords, '...') ?>
            <?php } else { ?>
                <?= wp_trim_words($auction->description, $limitWords, '...') ?>
            <?php } ?>
        </div>

        <div class="bpw-date"><?= $dateTimeStr ?></div>

        <div class="bpw-countdown-row">
            <?php if ($isLive) { ?>
                <a href="<?= esc_url($linkToAuction) ?>" class="bpw-live-btn"><?= __('Enter Live Auction', 'auctionforge') ?></a>
            <?php } else { ?>
                <span class="bpw-countdown-label"><?= __('Live bidding starts in:', 'auctionforge') ?></span>
                <div id="countdown-wide-<?= $index ?>" class="bp-countdown" data-time="<?= preg_replace('/\s/', 'T', $dateTime) ?>" style="display:inline-flex;gap:4px;align-items:center;">
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-days bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Days", "auctionforge") ?></span>
                    </div>
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-hours bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Hrs", "auctionforge") ?></span>
                    </div>
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-minutes bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Min", "auctionforge") ?></span>
                    </div>
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-seconds bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Sec", "auctionforge") ?></span>
                    </div>
                </div>
            <?php } ?>
        </div>

        <?php $hasRegister = !empty($registerUrl); ?>
        <div class="bpw-buttons<?= $hasRegister ? '' : ' bpw-buttons-single' ?>">
            <?php if ($auctionState == Auction::STATE_READY || $auctionState == Auction::STATE_RUNNING || $auctionState == Auction::STATE_PRE_AUCTION) { ?>
                <a href="<?= esc_url($linkToAuction) ?>" class="bpw-btn-primary"><?= __('View Catalog', 'auctionforge') ?></a>
                <?php if (!empty($registerUrl)) { ?>
                    <a href="#" class="bpw-btn-secondary" data-micromodal-trigger="modal-register"><?= __('Register', 'auctionforge') ?></a>
                <?php } ?>
            <?php } elseif ($auctionState == Auction::STATE_PENDING) { ?>
                <span class="bpw-btn-disabled"><?= __('Catalog Coming Soon', 'auctionforge') ?></span>
                <?php if (!empty($registerUrl)) { ?>
                    <a href="#" class="bpw-btn-secondary" data-micromodal-trigger="modal-register"><?= __('Register', 'auctionforge') ?></a>
                <?php } ?>
            <?php } elseif ($auctionState == Auction::STATE_ENDING || $auctionState == Auction::STATE_ARCHIVED) { ?>
                <span class="bpw-status-text"><?= __('The auction has ended', 'auctionforge') ?></span>
                <a href="<?= esc_url($linkToAuction) ?>" class="bpw-btn-primary"><?= __('View Results', 'auctionforge') ?></a>
            <?php } ?>
        </div>
    </div>
</div>
