<?php
$index = $args['index'];

$section_title = rt_get_option("frontpage_trip_{$index}_title", __('Custom Trips', 'jelajah-text'));
$section_desc = rt_get_option("frontpage_trip_{$index}_desc", __('Trips Description', 'jelajah-text'));

// Post style
$post_style = rt_get_option("frontpage_trip_{$index}_style", "grid");

// Query Data
$posts_per_page = rt_get_option("frontpage_trip_{$index}_posts_per_page", 6);
$query_by = rt_get_option("frontpage_trip_{$index}_query_by", 'latest');
$category = rt_get_option("frontpage_trip_{$index}_category");
$post_id = rt_get_option("frontpage_trip_{$index}_id");
$orderby = rt_get_option("frontpage_trip_{$index}_orderby", 'date');

// Column options
$post_wrapper = "frontpage_trips_1";
$columns = rt_get_option("frontpage_trip_{$index}_column", 3);
$pagination_style = rt_get_option("frontpage_trip_{$index}_pagination", 'none');

// Carousel options
$carousel = rt_get_option("frontpage_trip_{$index}_carousel", false);
$slider_item = rt_get_option("frontpage_trip_{$index}_slide_per_row", 3);

// Query builder
$args['post_type'] = 'trip';
$args['post_status'] = 'publish';
$args['posts_per_page'] = $posts_per_page;
$args['order'] = $orderby;

// Filter by editor picks tag
if ($query_by === 'editor-pick') {
    $args['tax_query'][] = [
        'taxonomy' => 'trip_tag',
        'field' => 'slug',
        'terms' => 'editor-pick',
    ];
}

// Filter by categories
if (!empty($category)) {
    $args['tax_query'][] = [
        'taxonomy' => 'trip_cat',
        'field' => 'id',
        'terms' => $category,
    ];
}

// Order by viewer count
if ($orderby  == 'viewer') {
    $args['orderby'] = 'meta_value_num';
    $args['meta_key'] = 'wp_post_views_count';
}

// Filter by specific post IDs
if (!empty($post_id)) {
    $args['post__in'] = $post_id;
}

// Generate query
$the_query = new WP_Query($args);
?>
<section id="frontpage-trips-<?php echo esc_attr($index) ?>" class="frontpage-section has-global-padding">
    <div class="site-container">

        <?php if ($section_title || $section_desc) : ?>
            <div class="rt-block-heading is-layout-constrained text-center">
                <?php if ($section_title) : ?>
                    <h2 class="rt-block-heading__title"><?php echo esc_html($section_title); ?></h2>
                <?php endif; ?>

                <?php if ($section_desc) : ?>
                    <div class="rt-block-heading__desc"><?php echo esc_html($section_desc); ?></div>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <?php if (!$carousel) : ?>
            <?php if ($the_query->have_posts()) : ?>

                <div class="rt-trips">
                    <div id="<?php echo esc_attr($post_wrapper) ?>" class="is-stacked-on-mobile grids grids-<?php echo esc_attr($columns) ?>">
                        <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
                            <?php rt_get_template_part("trip/trip"); ?>
                        <?php endwhile ?>
                    </div>

                    <?php echo rt_get_pagination(array_merge([
                        'target' => $post_wrapper,
                        'template_part' => "template-parts/trip/trip",
                        'pagination_style' =>  $pagination_style,
                        'total_page' => $the_query->max_num_pages,
                        'post_total' => $the_query->found_posts,
                    ], $args)); ?>

                    <?php wp_reset_postdata(); ?>

                </div>

            <?php else : ?>
                <?php echo __('Trip not found', 'jelajah-text') ?>
            <?php endif ?>
        <?php endif ?>

        <?php if ($carousel) : ?>
            <?php if ($the_query->have_posts()) : ?>

                <?php
                // Swiper options
                $slider_options = htmlspecialchars(json_encode([
                    'slidesPerView' => 1,
                    'spaceBetween' => 30,
                    'itemWrapper' => '.rt-trip__thumbnail',
                    'breakpoints' => [
                        781 => [
                            'slidesPerView' => $slider_item,
                        ],
                    ],
                ]));
                ?>

                <div id="post-carousel-1" class="rt-swiper is-swiper-equal-height is-swiper-pagination-outside" style="--slides: 1; --gap: 30px; --md--slides: 3; --md--gap: 30px;">
                    <div class="swiper js-swiper" data-options="<?php echo esc_attr($slider_options) ?>">

                        <div class="swiper-wrapper">

                            <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
                                <div class="swiper-slide">
                                    <div class="rt-swiper-item">
                                        <?php rt_get_template_part("trip/trip"); ?>
                                    </div>
                                </div>
                            <?php endwhile ?>

                            <?php wp_reset_postdata(); ?>
                        </div>

                        <div class="swiper-pagination">
                            <span class="swiper-pagination-bullet"></span>
                            <span class="swiper-pagination-bullet"></span>
                            <span class="swiper-pagination-bullet"></span>
                        </div>

                    </div>

                    <div class="rt-swiper-navigation">
                        <div class="swiper-button-prev"></div>
                        <div class="swiper-button-next"></div>
                    </div>

                </div>

            <?php else : ?>
                <p class="post-no-results"><?php echo __('Trips not found', 'jelajah-text') ?></p>
            <?php endif ?>
        <?php endif ?>

    </div>
</section>