<?php
require_once RSWPTHEMES_AWT_PLUGIN_PATH . '/includes/posts-shortcode/popup-full-post.php';
require_once RSWPTHEMES_AWT_PLUGIN_PATH . '/includes/posts-shortcode/post-layouts.php';
require_once RSWPTHEMES_AWT_PLUGIN_PATH . '/includes/posts-shortcode/post-filter-ajax-handler.php';

// Updated function to select the layout dynamically
function get_rswpthemes_awt_filerable_posts_markup($args, $posts_per_row, $layout = '1') {
    $query = new WP_Query($args);

    // Select the layout function based on the layout parameter
    $layout_function = 'get_layout_' . $layout . '_markup';
    if (!function_exists($layout_function)) {
        $layout_function = 'get_layout_1_markup'; // Fallback to layout 1 if the specified layout doesn't exist
    }

    // Call the selected layout function
    return call_user_func($layout_function, $query, $posts_per_row);
}

// Register the shortcode
function rswpthemes_awt_filerable_posts_shortcode($atts) {
    ob_start();

    $atts = shortcode_atts(
        array(
            'posts_per_page' => 5,
            'include_categories' => '',
            'posts_per_row' => 2,
            'layout' => '1',
        ),
        $atts,
        'rswpthemes_awt_filterable_posts'
    );

    $unique_id = uniqid();
    $categories = get_categories(array('hide_empty' => 0));
    $authors = get_users(array('has_published_posts' => true));

    ?>
    <div class="rswpthemes-awt-filterable-posts-container" id="rswpthemes-awt-filterable-posts-<?php echo esc_attr($unique_id); ?>">
        <form class="rswpthemes-awt-posts-search-form rswpthemes-awt-grid-row" data-container="rswpthemes-awt-filterable-posts-<?php echo esc_attr($unique_id); ?>">
            <div class="rswpthemes-awt-input-field-wrapper">
                <input type="text" name="s" placeholder="Search posts...">
            </div>
            <div class="rswpthemes-awt-input-field-wrapper">
                <select name="category">
                    <option value="">All Categories</option>
                    <?php foreach ($categories as $category) : ?>
                        <option value="<?php echo esc_attr($category->term_id); ?>">
                            <?php echo esc_html($category->name); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="rswpthemes-awt-input-field-wrapper">
                <select name="author">
                    <option value="">All Authors</option>
                    <?php foreach ($authors as $author) : ?>
                        <option value="<?php echo esc_attr($author->ID); ?>">
                            <?php echo esc_html($author->display_name); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <!-- Add sorting dropdown -->
            <div class="rswpthemes-awt-input-field-wrapper">
                <select name="sort_order">
                    <option value="">Default Order</option>
                    <option value="title_asc">Alphabetical (A-Z)</option>
                    <option value="title_desc">Alphabetical (Z-A)</option>
                    <option value="date_asc">Date (Oldest First)</option>
                    <option value="date_desc">Date (Newest First)</option>
                </select>
            </div>
            <div class="rswpthemes-awt-input-field-wrapper search-btn">
                <button type="submit">Search</button>
            </div>
        </form>
        <div class="rswpthemes-awt-posts-list rswpthemes-awt-grid-row"
             data-posts-per-page="<?php echo esc_attr($atts['posts_per_page']); ?>"
             data-categories="<?php echo esc_attr($atts['include_categories']); ?>"
             data-posts-per-row="<?php echo esc_attr($atts['posts_per_row']); ?>"
             data-layout="<?php echo esc_attr($atts['layout']); ?>">
        </div>
        <button class="rswpthemes-awt-load-more-posts"
                data-container="rswpthemes-awt-filterable-posts-<?php echo esc_attr($unique_id); ?>"
                data-page="1">
            Load More
        </button>

        <!-- Popup container for displaying full post -->
        <div class="rswpthemes-awt-post-popup" id="rswpthemes-awt-post-popup-<?php echo esc_attr($unique_id); ?>">
            <div class="rswpthemes-awt-post-popup-overlay"></div>
            <div class="rswpthemes-awt-post-popup-content">
                <button class="rswpthemes-awt-post-popup-close">×</button>
                <div class="rswpthemes-awt-post-popup-body">
                    <!-- Post content will be loaded here -->
                </div>
            </div>
        </div>
    </div>
    <?php

    return ob_get_clean();
}
add_shortcode('rswpthemes_awt_filterable_posts', 'rswpthemes_awt_filerable_posts_shortcode');

// Enqueue scripts (unchanged)
function rswpthemes_awt_filerable_posts_scripts() {
    if (!defined('RSWPTHEMES_AWT_PLUGIN_URL')) {
        error_log('RSWPTHEMES_AWT_PLUGIN_URL is not defined. Using fallback.');
        $plugin_url = plugins_url('', __FILE__);
    } else {
        $plugin_url = RSWPTHEMES_AWT_PLUGIN_URL;
    }

    wp_enqueue_style('rswpthemes-awt-filterable-posts-ajax', $plugin_url . '/includes/posts-shortcode/posts-shortcode.css');
    wp_enqueue_script('rswpthemes-awt-filterable-posts-ajax', $plugin_url . '/includes/posts-shortcode/posts-shortcode.js', array('jquery'), '1.2', true);
    wp_localize_script('rswpthemes-awt-filterable-posts-ajax', 'rswpthemes_awt_filerable_posts_vars', array(
        'ajax_url' => admin_url('admin-ajax.php')
    ));
}
add_action('wp_enqueue_scripts', 'rswpthemes_awt_filerable_posts_scripts');
