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

$user_id = get_current_user_id();

$products = get_posts([
    'post_type'      => 'product',
    'author'         => $user_id,
    'post_status'    => ['publish', 'draft'],
    'posts_per_page' => -1,
]);
?>

<div class="vintage-card">
  <div class="wcmv-flex-between-center">
    <h3 class="carnival-text">MY LISTINGS</h3>
    <a href="<?php echo site_url('/create-listing'); ?>" class="wcmv-btn">
      + CREATE LISTING
    </a>
  </div>

  <?php if (!$products): ?>
    <p class="wcmv-mt-20">No listings yet.</p>
  <?php else: ?>
    <div class="wcmv-grid-3 wcmv-mt-20">
      <?php foreach ($products as $post): 
        $product = wc_get_product($post->ID);
        $is_auction = get_post_meta($post->ID, '_mv_is_auction', true);
        $buy_now = $product->get_price();
      ?>
        <div class="vintage-card">
          <h4><?php echo esc_html($product->get_name()); ?></h4>

          <p><strong>Type:</strong>
            <?php
              if ($is_auction && $buy_now) echo 'Auction + Buy Now';
              elseif ($is_auction) echo 'Auction';
              else echo 'Buy Now';
            ?>
          </p>

          <p><strong>Status:</strong> <?php echo esc_html($post->post_status); ?></p>

          <div class="wcmv-actions">
            <a href="<?php echo get_permalink($post->ID); ?>" class="wcmv-btn-outline">View</a>
            <a href="<?php echo site_url('/create-listing?edit=' . $post->ID); ?>" class="wcmv-btn-outline">Edit</a>
          </div>
        </div>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</div>
