 <?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 brand selector with nested options and a toggleable form to add new brands and optional parent brands, using WordPress taxonomy terms.
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN BRAND SELECTOR
 * || 
 * =============================================================================
 */

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

/*
============================================================
    BLUE RAVEN BRAND SELECTOR
============================================================
*/

    ## =========== Helper to recursively render brand options =========== ##
     
function BlueRaven_render_brand_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_brand_options($terms, $term->term_id, $prefix . '— ');
        }
    }
}

    ## =========== Fetch brand terms =========== ##
    
$terms = get_terms([
    'taxonomy'   => 'product_brand',
    '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('Brand', 'blue-raven'); ?></p>
</label>

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

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