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

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

  <?php if (empty($listings)) : ?>
    <p>No Buy Now listings.</p>
  <?php else : ?>

    <div class="wcmv-list">

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

        $status   = get_post_meta($pid, '_stakmv_auction_status', true) ?: 'active';
        $price    = get_post_meta($pid, '_stakmv_auction_buy_now_price', true);
        $sku      = $product->get_sku();
        $stock    = $product->get_stock_status();
        $views    = apply_filters('stakmv_get_listing_views', 0, $pid);
        $earnings = apply_filters('stakmv_get_listing_earnings', 0, $pid);
        $date     = get_the_date('d M Y', $pid);
      ?>

      <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 class="wcmv-meta">
            SKU: <?php echo $sku ?: '—'; ?> •
            <?php echo esc_html($date); ?>
          </div>
        </div>

        <div class="wcmv-row-meta">
          <span class="badge"><?php echo wc_price($price); ?></span>
          <span class="badge <?php echo $stock === 'instock' ? 'green' : 'gray'; ?>">
            <?php echo ucfirst($stock); ?>
          </span>
          <span class="badge blue">Views: <?php echo (int) $views; ?></span>
          <span class="badge purple">Earned: <?php echo wc_price($earnings); ?></span>
        </div>

        <div class="wcmv-row-status">
          <span class="status <?php echo $status === 'active' ? 'on' : 'off'; ?>">
            <?php echo ucfirst($status); ?>
          </span>
        </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>
          <a href="<?php echo esc_url(wp_nonce_url(add_query_arg(array('tab' => 'inventory', 'inv_tab' => 'listings', 'delete_listing' => $pid), $dashboard_url), 'stakmv_delete_listing')); ?>"
             class="wcmv-js-confirm"
             data-confirm="Delete this listing?">Delete</a>
        </div>

      </div>

      <?php endforeach; ?>

    </div>

  <?php endif; ?>
</div>
