<?php
/**
 * Sidebar filter for candidate listing
 *
 * This template can be overridden by copying it to yourtheme/jobus/contents-candidate/sidebar-classic-filters.php.
 *
 * HOWEVER, on occasion Jobus will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @package Jobus
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

// Hardcode post type to ensure filters work on initial page load
$post_type = 'jobus_candidate';

// Get filter widgets configuration
$filter_widgets = jobus_opt('candidate_sidebar_widgets');
$taxonomy_widgets = jobus_opt('candidate_taxonomy_widgets');
$show_search_form = jobus_opt('candidate_show_search_form', true);

// Check if any filter widgets are configured
$has_meta_widgets = !empty($filter_widgets) && is_array($filter_widgets);
$has_taxonomy_widgets = false;

// Check for taxonomy widgets independently (removed the !$has_meta_widgets condition)
if (!empty($taxonomy_widgets) && is_array($taxonomy_widgets)) {
    foreach ($taxonomy_widgets as $is_enabled) {
        if ($is_enabled) {
            $has_taxonomy_widgets = true;
            break;
        }
    }
}

// Only render filter button if filters exist or search form is enabled
if (!$has_meta_widgets && !$has_taxonomy_widgets && !$show_search_form) {
    // Return empty div structure to maintain layout
    ?>
    <div class="jbs-col-xl-3 jbs-col-lg-4"></div>
    <?php
    return;
}

// Setup helper variables
$meta_opt_parent_key = 'jobus_meta_candidate_options';
$specifications = jobus_get_specs('candidate_specifications');
$specs_options = jobus_get_specs_options('candidate_specifications');
?>
<div class="jbs-col-xl-3 jbs-col-lg-4">
    <button type="button"
        class="jbs-filter-btn jbs-w-100 jbs-pt-2 jbs-pb-2 jbs-h-auto jbs-fw-500 tran3s jbs-d-lg-none jbs-mb-40 jbs_filter-transparent"
        data-jbs-toggle="jbs-offcanvas" data-jbs-target="#filteroffcanvas">
        <i class="bi bi-funnel"></i>
        <?php esc_html_e('Filter', 'jobus'); ?>
    </button>
    <div class="jbs-filter-area-tab jbs-offcanvas jbs-offcanvas-start" id="filteroffcanvas">
        <button type="button" class="jbs-btn-close jbs-text-reset jbs-d-lg-none jbs-offcanvas-close"
            aria-label="Close"></button>
        <div class="jbs-filter-heading jbs-fw-500 jbs-text-dark">
            <?php esc_html_e('Filter By', 'jobus'); ?>
            <?php
            $has_active_filters = false;
            $ignored_keys = array( 'post_type', 'jobus_nonce', '_wp_http_referer' );
            if ( ! empty( $_GET ) ) {
                foreach ( $_GET as $key => $value ) {
                    if ( ! in_array( $key, $ignored_keys, true ) && ! empty( $value ) ) {
                        $has_active_filters = true;
                        break;
                    }
                }
            }?>
            <a href="<?php echo esc_url(get_post_type_archive_link('jobus_candidate')); ?>"
                id="jbs-clear-all-filters" class="jbs-clear-all-btn jbs-fs-13 jbs-fw-500 jbs-float-end tran3s <?php echo $has_active_filters ? '' : 'jbs-d-none'; ?>">
                <?php esc_html_e('Clear All', 'jobus'); ?>
            </a>
        </div>

        <div class="light-bg border-20 jbs-ps-4 jbs-pe-4 jbs-pt-25 jbs-pb-30 jbs-mt-20">
            <form action="<?php echo esc_url(get_post_type_archive_link('jobus_candidate')) ?>" role="search"
                method="get" data-jbs-filter-form="true">

                <input type="hidden" name="post_type" value="jobus_candidate" />
                <?php wp_nonce_field('jobus_search_filter', 'jobus_nonce'); ?>

                <?php
                // Render search form
                if ($show_search_form) {
                    $search_query = get_search_query();
                    $is_search_active = !empty($search_query);
                    $is_search_collapsed = !$is_search_active;


                    ?>
                    <div class="jbs-filter-block jbs-bottom-line">
                        <a class="jbs-filter-title jbs-fw-500 jbs-text-dark jbs-pointer<?php echo $is_search_collapsed ? ' jbs-collapsed' : ''; ?>"
                            data-jbs-toggle="collapse" data-jbs-target="#collapse-search-form" role="button"
                            aria-expanded="<?php echo !$is_search_collapsed ? 'true' : 'false'; ?>">
                            <?php esc_html_e('Keyword Search', 'jobus'); ?>
                        </a>

                        <div class="<?php echo $is_search_collapsed ? 'jbs-collapse' : 'jbs-collapse jbs-show'; ?>"
                            id="collapse-search-form">
                            <div class="jbs-main-body">
                                <?php include __DIR__ . '/../filter-widgets/search-form.php'; ?>
                            </div>
                        </div>
                    </div>
                    <?php
                }

                // Render meta widgets
                if ($has_meta_widgets) {
                    foreach ($filter_widgets as $index => $widget) {
                        $widget_name = $widget['widget_name'] ?? '';
                        $widget_layout = $widget['widget_layout'] ?? '';
                        $widget_param = jobus_get_sanitized_query_param($widget_name, '', 'jobus_search_filter');
                        $is_first = 0 === $index;
                        $is_active = !empty($widget_param);
                        $is_collapsed = !$is_first && !$is_active;

                        $widget_title = $specifications[$widget_name] ?? '';
                        $specifications_data = $specs_options[$widget_name] ?? '';


                        ?>
                        <div class="jbs-filter-block jbs-bottom-line">
                            <a class="jbs-filter-title jbs-fw-500 jbs-text-dark jbs-pointer <?php echo esc_attr($is_collapsed ? 'jbs-collapsed' : ''); ?>"
                                data-jbs-toggle="collapse" data-jbs-target="#collapse-<?php echo esc_attr($widget_name) ?>"
                                aria-expanded="<?php echo !$is_collapsed ? 'true' : 'false'; ?>">
                                <?php echo esc_html($widget_title); ?>
                            </a>
                            <div class="<?php echo esc_attr($is_collapsed ? 'jbs-collapse' : 'jbs-collapse jbs-show') ?>"
                                id="collapse-<?php echo esc_attr($widget_name) ?>">
                                <div class="jbs-main-body">
                                    <?php 
                                    if ( ! empty( $widget_layout ) ) {
                                        include __DIR__ . "/../filter-widgets/{$widget_layout}.php"; 
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>

                        <?php
                    }
                }

                // Render taxonomy widgets
                if ($has_taxonomy_widgets) {
                    $taxonomy_mapping = [
                        'is_candidate_widget_cat' => ['jobus_candidate_cat', 'categories.php'],
                        'is_candidate_widget_location' => ['jobus_candidate_location', 'locations.php'],
                    ];

                    foreach ($taxonomy_mapping as $widget_key => $config) {
                        if (isset($taxonomy_widgets[$widget_key]) && $taxonomy_widgets[$widget_key]) {
                            list($taxonomy, $filter_file) = $config;
                            include __DIR__ . '/../loop/classic-tax-wrapper-start.php';
                            include __DIR__ . "/../filter-widgets/{$filter_file}";
                            include __DIR__ . '/../loop/classic-tax-wrapper-end.php';
                        }
                    }
                }
                ?>

            </form>
        </div>
    </div>
</div>
<div class="jbs-offcanvas-backdrop"></div>