<?php
/**
 * Template for displaying category listings in a modern design
 *
 * This template can be overridden by copying it to yourtheme/bestebiz/listings-modern.php.
 */

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="bestebiz-modern-listings">
    <?php while ($query->have_posts()) : $query->the_post(); 
        // Get listing data
        $post_id = get_the_ID();
        $is_premium = get_post_meta($post_id, '_bestebiz_premium', true);
        $is_featured = get_post_meta($post_id, '_bestebiz_featured', true);
        $votes = get_post_meta($post_id, '_bestebiz_votes', true) ? intval(get_post_meta($post_id, '_bestebiz_votes', true)) : 0;
        $logo = get_post_meta($post_id, '_bestebiz_logo', true);
        $short_description = get_post_meta($post_id, '_bestebiz_short_description', true);
        $phone = get_post_meta($post_id, '_bestebiz_phone', true);
        $website = get_post_meta($post_id, '_bestebiz_website', true);
        $address = get_post_meta($post_id, '_bestebiz_address', true);
        $city = get_post_meta($post_id, '_bestebiz_city', true);
        
        // Format address
        $location = '';
        if (!empty($city)) {
            $location = $city;
        }
        
        // Additional classes
        $listing_classes = array('bestebiz-modern-item');
        if ($is_premium) {
            $listing_classes[] = 'bestebiz-premium-item';
        }
        if ($is_featured) {
            $listing_classes[] = 'bestebiz-featured-item';
        }
        
        // Determine ranking number based on loop index
        $ranking_number = $query->current_post + 1;
    ?>
    
    <div class="<?php echo esc_attr(implode(' ', $listing_classes)); ?>">
        <div class="bestebiz-modern-card">
            <?php if ($is_premium) : ?>
                <div class="bestebiz-premium-badge">
                    <span>Premium</span>
                </div>
            <?php endif; ?>
            
            <?php if ($is_featured) : ?>
                <div class="bestebiz-featured-badge">
                    <span>Featured</span>
                </div>
            <?php endif; ?>
            
            <div class="bestebiz-ranking-number">
                <span><?php echo esc_html($ranking_number); ?></span>
            </div>
            
            <div class="bestebiz-modern-header">
                <div class="bestebiz-modern-image">
                    <a href="<?php the_permalink(); ?>">
                        <?php if (!empty($logo)) : ?>
                            <img src="<?php echo esc_url($logo); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php elseif (has_post_thumbnail()) : ?>
                            <?php the_post_thumbnail('medium'); ?>
                        <?php else : ?>
                            <div class="bestebiz-no-image">
                                <span class="bestebiz-image-placeholder"></span>
                            </div>
                        <?php endif; ?>
                    </a>
                </div>
                
                <div class="bestebiz-modern-title-area">
                    <h3 class="bestebiz-modern-title">
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </h3>
                    
                    <?php if (!empty($location)) : ?>
                        <div class="bestebiz-modern-location">
                            <i class="bestebiz-icon-map-marker"></i>
                            <span><?php echo esc_html($location); ?></span>
                        </div>
                    <?php endif; ?>
                    
                    <div class="bestebiz-modern-votes">
                        <div class="bestebiz-votes-display">
                            <span class="bestebiz-votes-count"><?php echo esc_html($votes); ?></span>
                            <i class="bestebiz-heart-icon"></i>
                        </div>
                        <button class="bestebiz-vote-button" data-post-id="<?php echo esc_attr($post_id); ?>">
                            <span><?php _e('Gefällt mir', 'bestebiz-directory'); ?></span>
                        </button>
                    </div>
                </div>
            </div>
            
            <div class="bestebiz-modern-content">
                <?php if (!empty($short_description)) : ?>
                    <div class="bestebiz-modern-description">
                        <?php echo esc_html($short_description); ?>
                    </div>
                <?php else : ?>
                    <div class="bestebiz-modern-description">
                        <?php echo wp_trim_words(get_the_excerpt(), 25); ?>
                    </div>
                <?php endif; ?>
                
                <div class="bestebiz-modern-actions">
                    <?php if (!empty($phone)) : ?>
                        <a href="tel:<?php echo esc_attr(preg_replace('/[^0-9+]/', '', $phone)); ?>" class="bestebiz-action-button bestebiz-phone-button">
                            <i class="bestebiz-icon-phone"></i>
                            <span>Anrufen</span>
                        </a>
                    <?php endif; ?>
                    
                    <?php if (!empty($website)) : ?>
                        <a href="<?php echo esc_url($website); ?>" target="_blank" class="bestebiz-action-button bestebiz-website-button">
                            <i class="bestebiz-icon-globe"></i>
                            <span>Website</span>
                        </a>
                    <?php endif; ?>
                    
                    <a href="<?php the_permalink(); ?>" class="bestebiz-action-button bestebiz-details-button">
                        <i class="bestebiz-icon-info"></i>
                        <span>Details</span>
                    </a>
                </div>
            </div>
        </div>
    </div>
    
    <?php endwhile; ?>
</div>

<div class="bestebiz-pagination">
    <?php
    echo paginate_links(array(
        'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'total' => $query->max_num_pages,
        'prev_text' => '&laquo; ' . __('Previous', 'bestebiz-directory'),
        'next_text' => __('Next', 'bestebiz-directory') . ' &raquo;',
    ));
    ?>
</div>