 <?php
 
 /**
 * =============================================================================
 * @package         Blue Raven (Free Version)
 * @Plugin URI      https://enterrahost.com/plugins/blue-raven
 * 
 * @Author          EnterraHost
 * @Author URI      https://enterrahost.com
 * @License         GPL-2.0-or-later
 *  
 * @Copyright       (c) 2025 EnterraHost
 * @Contents        Displays a dynamic Category selector with nested options and a toggleable form to add new Category and optional parent Categories, using WordPress taxonomy terms.
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN CATEGORY SELECTOR
 * || 
 * =============================================================================
 */

if ( ! defined( 'ABSPATH' ) ) exit; 

/*
============================================================
    BLUE RAVEN CATEGORY SELECTOR
============================================================
*/

    ## =========== Recursive function to render category options with hierarchy =========== ##
    
function BlueRaven_render_category_options($terms, $parent = 0, $prefix = '') {
    foreach ($terms as $term) {
        if ((int) $term->parent === $parent) {
            $label = $prefix . $term->name;
            printf('<option value="%1$s">%2$s</option>',esc_attr($label),esc_html($label));
            BlueRaven_render_category_options($terms, $term->term_id, $prefix . '— ');
        }
    }
}

    ## =========== Get product categories =========== ##
    
$terms = get_terms([
    'taxonomy'   => 'product_cat',
    'hide_empty' => false,
    'orderby'    => 'name',
    'order'      => 'ASC',
]);

    ## =========== HTML =========== ##

?>

<label class="text-center">
    <p class="no-margin-padding spacing-right-small text-center" style="margin-bottom: 8px;"><?php esc_html_e('Category', 'blue-raven'); ?></p>
</label>

<div class="flex-sb" style="margin-bottom: 2rem;">
    <select id="product-category" name="product-category" class="br-dropdown" style="min-width: 85%; padding: 0.5rem; margin-right: 1rem;">
        <option value=""><?php esc_html_e('Select a Category or add a new Category', 'blue-raven'); ?></option>
        <?php if (!is_wp_error($terms)) : ?>
            <?php BlueRaven_render_category_options($terms); ?>
        <?php endif; ?>
    </select>
    <button type="button" class="enterra-button enterra-extra-button-color" id="add-new-category">+</button>
</div>

<div id="add-category" class="spacing-right" style="display: none; min-width: 100%; margin: 0;">
    <label class="text-center" for="new-category">
        <p class="no-margin-padding spacing-right-small text-center"> <?php esc_html_e('New Category', 'blue-raven'); ?> </p>
    </label>
    <input type="text" name="new-category" id="new-category" placeholder="<?php esc_attr_e('Birds', 'blue-raven'); ?>" class="br-input" style="width: 85%; margin: 0.5rem 0;">
    
    <div class="flex-sb">
        <select id="parentNewCategory" name="parentNewCategory" class="br-dropdown" style="min-width: 85%; margin-right: 1rem;">
            <option value=""><?php esc_html_e('Parent Category (Optional)', 'blue-raven'); ?></option>
            <?php if (!is_wp_error($terms)) : ?><?php BlueRaven_render_category_options($terms); ?><?php endif; ?>
        </select>
        <button type="button" class="enterra-button enterra-extra-button-color" id="save-new-category">✓</button>
    </div>
</div>