<?php if (!defined('ABSPATH')) exit; ?>

<div class="vintage-card">
  <h3 class="carnival-text">Auctions</h3>

  <?php if (empty($auctions)) : ?>
    <p>No auctions.</p>
  <?php else : ?>

    <div class="wcmv-list">

      <?php foreach ($auctions as $pid) :
        $product   = wc_get_product($pid);
        if (!$product) continue;

        $current   = function_exists('stakmv_get_highest_bid') ? stakmv_get_highest_bid($pid) : 0;
        $bin       = get_post_meta($pid, '_stakmv_auction_buy_now_price', true);
        $has_bids  = get_post_meta($pid, '_stakmv_auction_has_bids', true);
        $status_raw = (string) get_post_meta($pid, '_stakmv_auction_status', true);
        $status = sanitize_key($status_raw);
        $end_raw = get_post_meta($pid, '_stakmv_auction_end_time', true);
        $end_ts = is_numeric($end_raw) ? (int) $end_raw : ($end_raw ? strtotime((string) $end_raw) : 0);
        $is_expired = ($end_ts > 0 && current_time('timestamp') >= $end_ts);
        $is_relistable = $is_expired || in_array($status, array('ended', 'expired'), true);
        $is_relist_blocked = in_array($status, array('sold', 'pending_payment'), true);
        $relist_url = wp_nonce_url(
          add_query_arg(
            array('tab' => 'inventory', 'inv_tab' => 'auctions', 'relist_listing' => $pid),
            $dashboard_url
          ),
          'stakmv_relist_listing'
        );
      ?>

      <div class="wcmv-row">

        <div class="wcmv-row-check">
          <input type="checkbox" class="inv-check" name="inv_ids[]" value="<?php echo esc_attr($pid); ?>">
        </div>

        <div class="wcmv-row-thumb">
          <?php echo $product->get_image('thumbnail'); ?>
        </div>

        <div class="wcmv-row-main">
          <strong><?php echo esc_html($product->get_name()); ?></strong>
        </div>

        <div class="wcmv-row-meta">
          <span class="badge green">Current: <?php echo wc_price($current); ?></span>
          <?php if ($bin) : ?>
            <span class="badge blue">BIN: <?php echo wc_price($bin); ?></span>
          <?php endif; ?>
        </div>

        <div class="wcmv-row-actions">
          <a href="<?php echo esc_url(get_permalink($pid)); ?>">View</a>
          <a href="<?php echo esc_url(add_query_arg('edit', $pid, $create_listing_base_url)); ?>">Edit</a>
          <?php if ($is_relistable && !$is_relist_blocked) : ?>
            <a href="<?php echo esc_url($relist_url); ?>" class="wcmv-js-confirm" data-confirm="Relist this auction for a new duration?">Relist</a>
          <?php endif; ?>
          <?php if (!$has_bids) : ?>
            <a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('tab' => 'inventory', 'inv_tab' => 'auctions', 'delete_listing' => $pid), $dashboard_url), 'stakmv_delete_listing')); ?>"
               class="wcmv-js-confirm"
               data-confirm="Delete this auction?">Delete</a>
          <?php endif; ?>
        </div>

      </div>

      <?php endforeach; ?>

    </div>

  <?php endif; ?>
</div>
