<?php
// Get terms for category and destination
$terms_category = get_terms(['taxonomy' => 'trip_cat', 'hide_empty' => true]);
$terms_destination = get_terms(['taxonomy' => 'trip_destination', 'hide_empty' => true]);

// Set variables for filtering
$duration = isset($_GET['duration']) ? sanitize_text_field($_GET['duration']) : '';
$category = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
$destination = isset($_GET['destination']) ? sanitize_text_field($_GET['destination']) : '';
?>

<form action="<?php echo get_post_type_archive_link('trip') ?>" method="get" class="rt-trip-filter rt-form has-foreground-background-color flex flex-col md:flex-row flex-wrap gap-30 border rounded-md py-40 px-40">
    <div class="rt-trip-filter__field flex-1">
        <select name="duration" aria-label="<?php echo __('duration', 'jelajah-text') ?>">
            <option value=""><?php echo __('All Duration', 'jelajah-text') ?></option>
            <?php for ($index = 1; $index <= 10; $index++): ?>
                <option <?php selected($duration, $index) ?> value="<?php echo esc_attr($index) ?>"><?php echo $index ?></option>
            <?php endfor; ?>
        </select>
    </div>
    <?php if ($terms_category && !is_wp_error($terms_category)) : ?>
        <div class="rt-trip-filter__field flex-1">
            <select name="category" aria-label="<?php echo __('category', 'jelajah-text') ?>">
                <option value=""><?php echo __('All Categories', 'jelajah-text') ?></option>
                <?php foreach ($terms_category as $term) : ?>
                    <option <?php selected($category, $term->slug) ?> value="<?php echo esc_attr($term->slug) ?>"><?php echo $term->name ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    <?php endif ?>
    <?php if ($terms_destination && !is_wp_error($terms_destination)) : ?>
        <div class="rt-trip-filter__field flex-1">
            <select name="destination" aria-label="<?php echo __('destination', 'jelajah-text') ?>">
                <option value=""><?php echo __('All Destination', 'jelajah-text') ?></option>
                <?php foreach ($terms_destination as $term) : ?>
                    <option <?php selected($destination, $term->slug) ?> value="<?php echo esc_attr($term->slug) ?>"><?php echo $term->name ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    <?php endif ?>
    <div class="rt-trip-filter__submit">
        <button class="wp-element-button" type="submit" aria-label="<?php echo __('Search trip', 'jelajah-text') ?>""><?php echo __('Search', 'jelajah-text') ?></button>
    </div>
</form>