<?php
/**
 * Wide layout template for a manual auction card in the bp_upcoming_wide shortcode.
 *
 * @var $index string
 * @var $manualAuction object
 * @var $registerUrl string
 */

use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\controllers\Settings;
use auctionforge\inc\helpers\SettingsHelper;

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

$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('F jS, Y g:i A', strtotime($dateTimeRaw));
$dateTimeFormatted = date("Y-m-d H:i:s", strtotime($dateTimeRaw));

$isTimedAuction = false;
if (isset($manualAuction->auction_type) && $manualAuction->auction_type === 'online') {
    $isTimedAuction = true;
}
$enableTimedClosingText = SettingsHelper::isChecked(Settings::ENABLE_TIMED_AUCTION_CLOSING_TEXT, false);

// Resolve countdown label text (custom or default)
$countdownType = $isTimedAuction ? 'timed' : 'live';
$countdownLabel = Settings::getCountdownText($countdownType, 'shortcode');
?>
<div class="bpw-auction-card">
    <div class="bpw-auction-img">
        <?php if (!empty($auctionLink)) { ?>
            <a href="<?= $auctionLink ?>" <?= $target ?>>
                <img src="<?= esc_url($featureImage) ?>" alt="<?= $auctionTitle ?>" />
            </a>
        <?php } else { ?>
            <div class="bpw-img-wrap">
                <img src="<?= esc_url($featureImage) ?>" alt="<?= $auctionTitle ?>" />
            </div>
        <?php } ?>
    </div>
    <div class="bpw-auction-body">
        <h3 class="bpw-title">
            <?php if (!empty($auctionLink)) { ?>
                <a href="<?= $auctionLink ?>" <?= $target ?>><?= $auctionTitle ?></a>
            <?php } else { ?>
                <?= $auctionTitle ?>
            <?php } ?>
        </h3>

        <?php if (!empty($description)) { ?>
            <div class="bpw-desc"><?= $description ?></div>
        <?php } ?>

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

        <div class="bpw-countdown-row">
                <span class="bpw-countdown-label"><?= esc_html($countdownLabel) ?></span>
            <div id="countdown-wide-<?= $index ?>" class="bp-countdown" data-time="<?= preg_replace('/\s/', 'T', $dateTimeFormatted) ?>" 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>
        </div>

        <?php $hasRegister = !empty($registerUrl); ?>
        <div class="bpw-buttons<?= $hasRegister ? '' : ' bpw-buttons-single' ?>">
            <?php if (!empty($auctionLink)) { ?>
                <a href="<?= $auctionLink ?>" <?= $target ?> class="bpw-btn-primary"><?= __('View Auction', 'auctionforge') ?></a>
            <?php } ?>
            <?php if (!empty($registerUrl)) { ?>
                <a href="#" class="bpw-btn-secondary" data-micromodal-trigger="modal-register"><?= __('Register', 'auctionforge') ?></a>
            <?php } ?>
        </div>
    </div>
</div>
