<?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        Load Product Subpage
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN ADMIN MY PRODUCTS
 * ||   - PRODUCT TABLE
 * ||   - PENDING PRODUCT
 * ||   - MY PRODUCT BODY
 * ||   - PRODUCT ABOUT PAGE
 * ||   - WOO
 * ||   - SALE
 * ||   - IMAGES
 * ||   - SEO
 * || 
 * =============================================================================
 */

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

/*
============================================================
    BLUE RAVEN ADMIN MY PRODUCTS
============================================================
*/

class Blue_Raven_Admin_My_Products {
    
    public function render($product) {
        if (class_exists('WooCommerce')) {
            if (isset($product) && !empty($product)) {
                $this->load_new_my_products_product_template($product);
            } else {
                $this->load_new_my_products_table_box_template();
            }
        } else {
            echo '<div class="parent-container"><p class="enterra-highlight" style="padding: 1rem 0rem;">WooCommerce is not active. Please install and activate WooCommerce to use the Product list.</p></div>';
        }
       
    }
    
/*
============================================================
    PRODUCT TABLE
============================================================
*/

    public function innerTableRender($filter, $search, $page) { self::load_new_my_products_table_template($filter, $search, $page); }
        
    private static function load_new_my_products_table_box_template() {
        
    ## =========== Output =========== ##
        
        ob_start();
        
        ?>
            <div class='parent-container'>
                <div class='flex-sb'>
                    <h2><?php esc_html_e('My Products', 'blue-raven'); ?></h2>
                    <h4>
                        <?php esc_html_e( 'Need Help', 'blue-raven' ); ?>
                        <a href="<?php echo esc_url('https://portal.enterrahost.com/knowledgebase/12/'); ?>" target="_noopeing" style="margin-left: 8px;" title="Learn more">
                            <i class="fas fa-question-circle"></i>
                        </a>
                    </h4>
                </div> 
                <p class="enterra-highlight"><?php esc_html_e('My Products', 'blue-raven'); ?></p>
                <div class="my-products-controls border-bottom-white" style='margin-top:1rem;'>
                    <div class='flex-sb' style='margin-bottom: 1rem;'>
                        <div class='flex-sb'>
                            <button class="enterra-button" id="woo-import-button" style="min-width: 6rem; margin-right: 1.5rem;"> <?php esc_html_e('Import Woo Products', 'blue-raven'); ?> </button>
                            <button class="enterra-button" id="delete-button" style="min-width: 6rem; display:none"> <?php esc_html_e('Delete Selected Producs', 'blue-raven'); ?> </button>
                        </div>
                        <div class="search-bar flex-sb">
                            <input type="text" name="product-search" id="product-search" placeholder="<?php esc_attr_e('Search', 'blue-raven'); ?>" class="br-input spacing-right" style="width: 18rem;" />
                            <button class="enterra-button" id="search-myproducts" style="min-width: 6rem;"> <?php esc_html_e('Filter', 'blue-raven'); ?> </button>
                        </div>
                    </div>
                        
                    <?php
                        function BlueRaven_render_category_options($terms, $parent = 0, $prefix = '') {
                            foreach ($terms as $term) {if ((int) $term->parent === $parent) {echo '<option value="' . esc_attr($term->name) . '">' . esc_html($prefix . $term->name) . '</option>';BlueRaven_render_category_options($terms, $term->term_id, $prefix . '— ');}}
                        }
                        
                        $terms = get_terms([
                            'taxonomy' => 'product_cat',
                            'hide_empty' => false,
                            'orderby' => 'name',
                            'order' => 'ASC'
                        ]);
                    ?>
                    
                    <div class='flex-left'>    
                        <select id="filter-category" class="br-dropdown" style="width: 225px; padding: 0.5rem; margin-bottom: 0; margin-right: 1rem;">
                            <option value=""><?php esc_html_e('Filter by category', 'blue-raven'); ?></option>
                            <?php
                            if (!is_wp_error($terms)) {
                                BlueRaven_render_category_options($terms); // Assumes this function safely outputs <option> tags
                            }
                            ?>
                        </select>

                        <select id="filter-Status" class="br-dropdown" style="width: 225px; padding: 0.5rem; margin-bottom: 0; margin-right: 1rem;">
                            <option value=""><?php esc_html_e('Filter by Status', 'blue-raven'); ?></option>
                            <option value="pending"><?php esc_html_e('Pending', 'blue-raven'); ?></option>
                            <option value="draft"><?php esc_html_e('Blue Raven Draft', 'blue-raven'); ?></option>
                            <option value="woo"><?php esc_html_e('Woo Commerce', 'blue-raven'); ?></option>
                            <option value="published"><?php esc_html_e('Blue Raven Published', 'blue-raven'); ?></option>
                            <option value="issue"><?php esc_html_e('Issues', 'blue-raven'); ?></option>
                        </select>

                        <div style="margin: 0.25rem 1rem 0 0; align-content: center; width: 180px;" class="flex-right toggle-text-width">
                            <label class="switch" style="margin: 0.25rem 1rem 0 0; align-content: center;">
                                <input type="checkbox" id="filter-stock" name="filter-stock">
                                <span class="slider align-content: end;"></span>
                            </label>
                            <h4 class="no-margin-padding text-center"><?php esc_html_e('In Stock', 'blue-raven'); ?></h4>
                        </div>

                        <div style="margin: 0.25rem 1rem 0 0; align-content: center; width: 180px;" class="flex-right toggle-text-width">
                            <label class="switch" style="margin: 0.25rem 1rem 0 0; align-content: center;">
                                <input type="checkbox" id="filter-favorite" name="filter-favorite">
                                <span class="slider align-content: end;"></span>
                            </label>
                            <h4 class="no-margin-padding text-center">
                                <?php esc_html_e('Favorite', 'blue-raven'); ?>
                                <label for="filter-favorite" class="br-star-label spacing-left-small">★</label>
                            </h4>
                        </div>
                    </div>
                </div>
                
                <div class="search-bar flex-right spacing-right" style='margin-top:1rem;'></div>
                <div id="ProductTable"> <?php self::load_new_my_products_table_template('', '', 1); ?> </div>
            </div>
        <?php
        
        $output = ob_get_clean();
		echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output was properly escaped within the buffer.
    }
    
    private static function load_new_my_products_table_template($filter = '', $search = '', $page = 1) {
        
    ## =========== Load product data =========== ##
    
        include_once BLUERAVEN_PLUGIN_DIR . 'includes/extras/my-products-load-table.php';
        [$products, $total_items, $total_pages] = BlueRaven_fetchProducts($filter, $search, $page);
    
    ## =========== Output =========== ##
        
        ob_start();
        echo '<div class="blue-raven-products-table">';
        if (!empty($products) && is_array($products)) {foreach ($products as $product) { include BLUERAVEN_PLUGIN_DIR . 'templates/blocks/block-product-table-list-item.php'; }} else {   echo '<p>' . esc_html__('No products found.', 'blue-raven') . '</p>';}
    
    ## =========== Load custom JS =========== ##
    
        wp_enqueue_script('blue-raven-my-products-table-js',BLUERAVEN_PLUGIN_URL . 'assets/js/my-products-table.js', array('jquery'),null,true);

    ## =========== Pagination placeholder =========== ##
    
        echo '<div class="flex-sb">';
        echo '<input type="hidden" id="product-search-val" value="' . esc_attr($search) . '"><input type="hidden" id="product-filter-val" value="' . esc_attr($filter) . '">';
        echo '<p style="align-self: center;">' . esc_html__("Products Found", 'blue-raven') . ' : ' . esc_html($total_items) . '</p>';

		if ($total_pages > 1) {
			echo '<p style="align-self: center;">' . esc_html__("Page", 'blue-raven') . ' : <a id="page-count">' . esc_html($page) . '</a> / ' . esc_html($total_pages) . '</p>';
            
            if($page == 1){
                echo '<div class="flex-sb"><a style="width: 42px; margin-right: 2rem;"></a><a class="page-button" id="forward-page" style="width: 1.5rem;">></a></div>';
            }else if ($page == $total_pages){
                echo '<div class="flex-sb"><a class="page-button" id="back-page" style="width: 1.5rem; margin-right: 2rem;"><</a><a style="width: 42px;"></a></div>';
            }else{
                echo '<div class="flex-sb"><a class="page-button" id="back-page" style="width: 1.5rem; margin-right: 2rem;"><</a><a class="page-button" id="forward-page" style="width: 1.5rem;">></a></div>';
            }
        }
        
        echo '</div></div>';
        
   ## =========== Output buffer contents =========== ##
   		
        $output = ob_get_clean();
		echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output was properly escaped within the buffer.
    }

/*
============================================================
    MY PRODUCT HEAD
============================================================
*/

    private static function load_new_my_products_product_template($product_id) {
        ?>
        <div class="parent-container">
            <?php

            include_once BLUERAVEN_PLUGIN_DIR . "includes/extras/my-product-load.php";
            $product = BlueRaven_fetchAProduct($product_id);
    
            $product_title = $product && isset($product['title']) ? esc_html($product['title']) : '';
            $product_status = $product && isset($product['status']) ? $product['status'] : '';
            $product_pending = $product && isset($product['pending']) ? $product['pending'] : false;
            $product_notification = $product && isset($product['notification']) ? $product['notification'] : '';
    
            if ($product) :
            ?>
                <section class="border-bottom-white">
                    <button class="enterra-button" id="back-to-myproducts"><?php esc_html_e('Back to My Product', 'blue-raven'); ?></button>
                
                    <div class="flex-sb">
                        <div>
                            <h2><?php esc_html_e('My Products', 'blue-raven'); ?></h2>
                            <h4><?php echo esc_html(stripslashes($product_title)); ?></h4>
                        </div>
                        
                        <h4 style="min-width: 400px; text-align: right;">
                            <?php esc_html_e('Need Help', 'blue-raven'); ?>
                            <a href="<?php echo esc_url('https://portal.enterrahost.com/knowledgebase/12/'); ?>" target="_blank" rel="noopener" style="margin-left: 8px;" title="<?php esc_attr_e('Learn more', 'blue-raven'); ?>">
                                <i class="fas fa-question-circle"></i>
                            </a>
                        </h4>
                    </div>
                
                    <?php self::load_product($product);?>
                </section>
            <?php else : ?>
                <section class="border-bottom-white">
                    <button class="enterra-button" id="back-to-myproducts"><?php esc_html_e('Back to My Product', 'blue-raven'); ?></button>
                    <div class="flex-sb">
                        <h3>
                            <?php esc_html_e('My Products - No product found', 'blue-raven'); ?>
                        </h3>
                        <h4>
                            <?php esc_html_e('Need Help', 'blue-raven'); ?>
                            <a href="<?php echo esc_url('https://portal.enterrahost.com/knowledgebase/12/'); ?>" target="_blank" rel="noopener" style="margin-left: 8px;" title="<?php esc_attr_e('Learn more', 'blue-raven'); ?>">
                                <i class="fas fa-question-circle"></i>
                            </a>
                        </h4>
                    </div>
                    <p class="enterra-highlight indent-one"> <?php esc_html_e('This product is missing.', 'blue-raven'); ?></p>
                </section>
            <?php endif; ?>
        </div>
        <?php
    }
    
/*
============================================================
    MY PRODUCT BODY
============================================================
*/ 

    private static function load_product($product) {
         
    ## =========== Get plugin URL properly =========== ##
        
        $plugin_url = BLUERAVEN_PLUGIN_URL;
        
    ## =========== Sanitize the product title and generate a slug =========== ##
        
        $title = isset($product['title']) ? sanitize_text_field($product['title']) : '';
        $slug = sanitize_title($title);
        
    ## =========== Sanitize CSV data (if it's raw string) =========== ##
        
        $csv_data = isset($product['product_CSV']) ? sanitize_textarea_field($product['product_CSV']) : '';
        
        if($csv_data != ''){
        
            function BlueRaven_find_header_partial($headers, $search) {
                foreach ($headers as $key => $header) { return stripos($header, $search) !== false ? $key : null; }
                return false;
            }
            
    ## =========== Parse CSV safely =========== ##    
    		
			// Using php://temp for in-memory stream.
			// Note: May fall back to disk if data exceeds memory limit (default ~2MB).
			// Still safer than direct file access and acceptable for parsing CSV temporarily.
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- php://temp is a safe memory stream
            $stream = fopen('php://temp', 'r+');
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- Safe: using php://temp in-memory stream
            fwrite($stream, $csv_data);
            rewind($stream);
            $headers = fgetcsv($stream);
            $values = fgetcsv($stream);
            
            $get_value = function($key) use ($headers, $values) {
                $index = false;
            
                if (($index = array_search($key, $headers)) === false) foreach ($headers as $i => $header) { if (stripos($header, $key) !== false) { $index = $i; break; } }
                return ($index !== false && isset($values[$index])) ? trim($values[$index]) : '';
            };
            
            $get_comma_array = function($key) use ($headers, $values) {
                $index = array_search($key, $headers);
                return ($index !== false && isset($values[$index])) ? array_map('trim', explode(',', $values[$index])) : [];
                return [];
            };
            
    ## =========== Function to generate item blocks =========== ##   
    
            function BlueRaven_generate_item_blocks($items, $type) {
                
                $items = is_array($items) ? $items : array_map('trim', explode(',', $items));
                $innerBlock = '';
                $id = 1;
                
                foreach ($items as $item) {
                    
                    $trimmedItem = esc_html(trim($item));
                    $escapedType = esc_attr($type);
                    
                    if($trimmedItem != '' && $escapedType != ''){
                        $innerBlock .= sprintf(
                            '<div class="br-block-item br-%s-item flex-sb blue-tag" style="min-width: 3rem; margin: 0.25rem;">
                                <p data-id="%d" class="spacing-right-small">%s</p>
                                <span class="br-block-remove remove-%s" style="padding-top: 0.1rem;">❌</span>
                            </div>',
                            $escapedType,
                            $id++,
                            $trimmedItem,
                            $escapedType
                        );
                    }
                }
                
                return $innerBlock;
            }
                        
    ## =========== Get Standered Values =========== ##
                
            $categories                 = $get_value("Categories", true);
            $brands                     = $get_value("Brands", true);
            $tags                       = $get_value("Tags", true);
            $type                       = $get_value("Type");
            
            $is_virtual                 = ($get_value("Virtual") == '1') ? true : false;
            $is_downloadable            = !empty($get_value("Download 1 URL")) ? true : false;
            
            $sale_price                 = ($raw = trim($get_value("Sale price"))) !== '' ? number_format((float) $raw, 2) : '';
            
            $sale_start_raw             = trim($get_value("Date sale price starts"));
            $sale_start                 = strtotime($sale_start_raw) ? gmdate('Y-m-d', strtotime($sale_start_raw)) : '';
            
            $sale_end_raw               = trim($get_value("Date sale price ends"));
            $sale_end                   = strtotime($sale_end_raw) ? gmdate('Y-m-d', strtotime($sale_end_raw)) : '';
                        
            $downloadLimit              = $get_value("Download limit");
            $downloadExpiry             = $get_value("Download expiry days");
            $aff_ProductURL             = $get_value("External URL");
            $aff_ButtonText             = $get_value("Button text");
            $grouped                    = $get_value("Grouped products");
            $parent                     = $get_value("Parent");
            $sku                        = $get_value("SKU");
            $gtin_ect                   = $get_value("GTIN, UPC, EAN, or ISBN");
            $stock_quantity             = $get_value("Stock");
            $stock_backorders           = $get_value("Backorders allowed?");
            $stock_status               = $get_value("In stock?");

            $stock_lowStock             = $get_value("Low stock amount");
            $stock_individually         = $get_value("Sold individually?");
            
            $shipping_weight            = $get_value("Weight", false, true);
            $shipping_length            = $get_value("Length", false, true);
            $shipping_width             = $get_value("Width", false, true);
            $shipping_height            = $get_value("Height", false, true);
            $shipping_class             = $get_value("Shipping class");
            
            $linked_upsells             = $get_value("Upsells");
            $linked_cross               = $get_value("Cross-sells");
            $linked_grouped             = $get_value("Grouped products");
            
            $advanced_purchase_notes    = $get_value("Purchase note");
            $advanced_order             = $get_value("Position");
            $advanced_reviews           = $get_value("Allow customer reviews?") === '' ? '1' : $get_value("Allow customer reviews?");
            $images                     = $get_value("Images");
            
            $wooProduct = wc_get_product( $product['woo_product_id'] );

            $isDraft = $wooProduct && $wooProduct->get_status() === 'draft';
            $isPublished = $wooProduct && $wooProduct->get_status() === 'publish';
                
    ## =========== BUILD Blocks =========== ##   
    
            $categoryInnerBlock = BlueRaven_generate_item_blocks($categories, 'category');
            $brandInnerBlock = BlueRaven_generate_item_blocks($brands, 'brand');
            $tagInnerBlock = BlueRaven_generate_item_blocks($tags, 'tag');
                
    ## =========== Downloads =========== ##               
                
            $downloadInnerBlock = '';
            $hasDownloads = array_search("Download 1 ID", $headers);
            $downloads = [];
            
            if($hasDownloads){
                $count = 1;
                
                while(array_search("Download $count ID", $headers)){
                    $downloadItem = []; 
                    $downloadItem[] = $values[array_search("Download $count ID", $headers)];
                    $downloadItem[] = $values[array_search("Download $count name", $headers)];
                    $downloadItem[] = $values[array_search("Download $count URL", $headers)]; 
                    $downloads[] = $downloadItem;
                    
                    $count++;
                }
            }
        }
        
        require_once BLUERAVEN_PLUGIN_DIR . "includes/extras/my-product-extras.php" ;
        
        ?>
        
        </section>
            
    <!-- =========== My Product Menu =========== -->        
            
        <div class="my-product-menu" style='margin: 2rem 0rem;'>
            <nav class="flex-left">
                <p class="enterra-nav-button-my-product spacing-right" id="product" data-container='product-about-container'><a><?php esc_html_e('Products', 'blue-raven'); ?> </a></p>
                <p class="enterra-nav-button-my-product spacing-right" id="woo" data-container='product-woo-container'><a><?php esc_html_e('WooCommerce', 'blue-raven'); ?> </a></p>
                <p class="enterra-nav-button-my-product spacing-right" id="sale" data-container='product-sales-container'><a><?php esc_html_e('Sale', 'blue-raven'); ?> </a></p>
                <p class="enterra-nav-button-my-product spacing-right" id="images" data-container='product-images-container'><a><?php esc_html_e('Images', 'blue-raven'); ?> </a></p>
                <p class="enterra-nav-button-my-product spacing-right" id="seo" data-container='product-seo-container'><a><?php esc_html_e('SEO', 'blue-raven'); ?> </a></p>
            </nav>
        </div>
         
        <form id='my-product-form'>    
            <div class='border-bottom-white'>
                <section class='product-container product-about-container' style='display: none'>
                        
<!--
============================================================
    PRODUCT ABOUT PAGE
============================================================
-->  
                        
    <!-- =========== Title, Slug, Price =========== -->                   
                        
                    <input type="hidden" id="product-id-input" name="product-id" value="<?php echo absint($product['id']); ?>">
                    
                    <div id="products-top-block" class="border-left-white" style="margin-top: 4rem;">
                        <h4><?php esc_html_e('My Products', 'blue-raven'); ?></h4>
                        <div class="flex-sb spacing-bottom row-down-full" style="margin-top: 2rem;">
                            <div style="margin: 0; width: 49%;" class="inner-row-down">
                                <label for="product-title" class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Title', 'blue-raven'); ?></p>
                                </label>
                                <input type="text" name="product-title" id="product-title" placeholder="<?php esc_attr_e('Title', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo esc_attr($title); ?>">
                            </div>
                            <div style="margin: 0; width: 49%;" class="inner-row-down">
                                <label for="product-slug" class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Slug', 'blue-raven'); ?></p>
                                </label>
                                <input type="text" name="product-slug" id="product-slug" placeholder="<?php esc_attr_e('Slug', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo esc_attr($slug); ?>">
                            </div>
                        </div>
                    </div>
            
    <!-- =========== description, short description =========== -->  
                    
                    <div id="products-description-block" class="border-left-white" style="margin-top: 4rem;">
                        <h4><?php esc_html_e('Descriptions', 'blue-raven'); ?></h4>
                        
                        <div class="flex-sb spacing-bottom">
                            <div style="margin: 0; width: 100%;">
                                <label for="long-description" class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Long Description', 'blue-raven'); ?></p>
                                </label>
                                <div class="blue-raven-text-container">
                                    <?php 
                                        BlueRaven_createTextEditor(
                                            wp_kses_post($product['description']), 
                                            'long-description'
                                        ); 
                                    ?>
                                    <div class="flex-right">
                                        <button type="button" class="enterra-button" id="save-description-text">
                                            <?php esc_html_e('Save Blue Raven Draft Description', 'blue-raven'); ?>
                                        </button>
                                    </div>
                                </div>
                            </div> 
                        </div>
                        
                        <div class="flex-sb" style="margin-bottom: 4rem;">
                            <div style="margin: 0; width: 100%;">
                                <label for="short-description" class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Short Description', 'blue-raven'); ?></p>
                                </label>
                                <div class="blue-raven-text-container">
                                    <?php 
                                        BlueRaven_createTextEditor(
                                            wp_kses_post($product['short_description']), 
                                            'short-description'
                                        ); 
                                    ?>
                                    <div class="flex-right">
                                         <button type="button" class="enterra-button" id="save-short-description-text">
                                            <?php esc_html_e('Save Blue Raven Draft Short Description', 'blue-raven'); ?>
                                        </button>
                                    </div>
                                </div>
                            </div> 
                        </div>
                    </div>
                
    <!-- =========== Categories, Brands, Tags =========== -->  
                    
                    <div id="products-sorting-block" class="border-left-white" style="margin-top: 4rem;">
                        <h4><?php esc_html_e('Sorting', 'blue-raven'); ?></h4>
                        
                        <div style="margin: 0 0 2rem 0; width: 100%;" class="flex-sb">
                            
                            <!-- Categories Block -->
                            
                            <div style="width: 48%;">
                                <div id="product-category-block">
                                    <?php  $category_block_path = BLUERAVEN_PLUGIN_DIR . 'templates/blocks/block-category-select-add.php'; if (file_exists($category_block_path)) include $category_block_path; ?>
                                </div>
                                <input type="hidden" id="product-category-input" name="product-categories" value="">
                                <div style="margin: 1rem 0rem 4rem;" id="category-list"> <?php echo wp_kses_post($categoryInnerBlock); ?>
                                </div>
                            </div>
                            
                            <!-- Brands Block -->
                            
                            <div style="width: 48%;">
                                <div id='product-brand-block'>
                                    <?php $brand_block_path = BLUERAVEN_PLUGIN_DIR . 'templates/blocks/block-brand-select-add.php'; if (file_exists($brand_block_path)) include $brand_block_path;?>
                                </div>
                                <input type="hidden" id="product-brand-input" name="product-brands" value="">
                                <div style="margin: 1rem 0rem 4rem;" id="brand-list">
                                    <?php echo wp_kses_post($brandInnerBlock); ?>
                                </div>
                            </div>
                        </div>
                        
                        <?php $tagCount = substr_count($tagInnerBlock, 'br-tag-item'); ?>
                        
                        <!-- Tags Block -->
                        <div style="width: 100%;">
                            <label class="text-center">
                                <p class="no-margin-padding spacing-right-small text-center" style="margin-bottom: 8px;"><?php esc_html_e('Tags :', 'blue-raven'); echo " "; ?> <span id='tag-count'><?php echo esc_html($tagCount); ?></span> / 10 <span style='font-size: 10px; opacity: 50%;'><?php esc_html_e(' (not enforced) ', 'blue-raven'); echo " "; ?></span></p>
                            </label>
                            
                            <div style="position: relative; max-height: 36px; max-width: 48%;" id="tag-add">
                                <div class="flex-sb" style="margin: 0.5rem 0rem 0.5rem 0rem;">
                                    <input type="text" name="new-tag" id="add-new-tag" placeholder="<?php esc_attr_e('Birds', 'blue-raven'); ?>" class="br-input" style="width:85%;" autocomplete="off">
                                    <button type="button" class="enterra-button enterra-extra-button-color" id="add-new-tag-button">
                                        <?php esc_html_e('+', 'blue-raven'); ?>
                                    </button>
                                </div>
                                <div class="text-pad" id="tag-text-pad" style="display: none; position: absolute; z-index: 3; padding-bottom: 2rem !important;"></div>
                                <input type="hidden" id="product-tags-input" name="product-tags" value="">
                            </div>
                            
                            <div style="margin: 1rem 0rem 4rem;" id="tag-list">
                                <?php echo wp_kses_post($tagInnerBlock); ?>
                            </div>
                        </div>
                    </div>   
                </section> 
                    
<!--
============================================================
   WOO
============================================================
-->  
                    
                <section class='product-container product-woo-container' style='display: none'>
                    
    <!-- =========== General =========== -->
                        
                        <div class="border-left-white">
                        <h4><?php esc_html_e('General Product Data', 'blue-raven'); ?></h4>
                        
                        <div class="flex-sb spacing-bottom">
                            <div style='width: 49%'>
                                <select id="product-type" name="product-type" class="br-dropdown" style="width: 100%;max-width: 100%;min-width: 0;box-sizing: border-box;" data-type="<?php echo esc_attr($type); ?>">
                                    <?php
                                        $product_types = wc_get_product_types();
                                        foreach ($product_types as $key => $value) {
                                            printf(
                                                '<option value="%s">%s</option>',
                                                esc_attr($key),
                                                esc_html($value)
                                            );
                                        }
                                    ?>
                                </select>
                            </div>
                            
                            <!-- Virtual/Downloadable Checkboxes -->
                            <div class="flex-right" id="simple-product-settings" style='width: 49%'> 
                                <label class="br-checkbox spacing-right">
                                    <input type="checkbox" name="mark-as-virtual" value="1" style="display: none;" id="mark-as-virtual" data-is-virtual="<?php echo $is_virtual ? 'true' : 'false'; ?>">
                                    <span class="br-check-mark"></span>
                                    <span><?php esc_html_e('Virtual', 'blue-raven'); ?></span>
                                </label>
                                <label class="br-checkbox spacing-right">
                                    <input type="checkbox" name="mark-as-downloadable" value="downloadable" style="display: none;" id="mark-as-downloadable" data-is-downloadable="<?php echo $is_downloadable ? 'true' : 'false'; ?>">
                                    <span class="br-check-mark"></span>
                                    <span><?php esc_html_e('Downloadable', 'blue-raven'); ?></span>
                                </label>
                            </div>
                        </div>
                        
                        <!-- Price Block -->
                        <div class="flex-sb spacing-bottom" id="price-block">
                            <div style="margin: 0; width: 49%;">
                                <label for="product-price" class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php /* translators: %s is the WooCommerce currency symbol/code. */  printf(esc_html__('Regular Price in %s', 'blue-raven'), esc_html(get_woocommerce_currency()) );   ?></p>
                                </label>
                                <input type="text" name="product-price" id="product-price" placeholder="0.00" class="br-input" style="width:100%;" value="<?php echo esc_attr(number_format((float)$product['price'], 2, '.', '')); ?>">
                            </div>
                        </div>
                        
                        <!-- External/Affiliate Product Block -->
                        <div class="flex-sb spacing-bottom" id="external-affiliate-block" style="display:none;">
                            <div style="margin: 0; width: 49%;">
                                <label for="product-ex-url" class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Product URL', 'blue-raven'); ?></p>
                                </label>
                                <input type="url" name="product-ex-url" id="product-ex-url" placeholder="<?php esc_attr_e('Enter the external URL to the product.', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo esc_url($aff_ProductURL); ?>">
                            </div>
                            <div style="margin: 0; width: 49%;">
                                <label for="product-ex-text" class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Button text', 'blue-raven'); ?></p>
                                </label>
                                <input type="text" name="product-ex-text" id="product-ex-text" placeholder="<?php esc_attr_e('This text will be shown on the button linking to the external product.', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo esc_attr($aff_ButtonText); ?>">
                            </div>
                        </div>
                        
                        <!-- Downloadable Product Block -->
                        <div id="download-block" style="display:none;">
                            <div>
                                <div style="margin: 0; width: 49%;" >
                                    <label for="downloadable-files" class="text-center">
                                        <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Downloadable files', 'blue-raven'); ?></p>
                                    </label>
                                    <div style="margin: 1rem 0rem;">
                                        <button type="button" class="enterra-button enterra-secondary-button-color spacing-right" id="add-file"><?php esc_html_e('Add File', 'blue-raven'); ?></button>
                                    </div>
                                </div>
                            </div>
                            
                            <div id="download-file-table" <?php if (empty($downloads)) echo 'style="display:none;"'; ?>>
                                <table class="blue-raven-table">
                                    <thead>
                                        <tr>
                                            <th style="width: 5%;"><?php esc_html_e('#', 'blue-raven'); ?></th>
                                            <th style="width: 40%;"><?php esc_html_e('Name', 'blue-raven'); ?></th>
                                            <th style="width: 40%;"><?php esc_html_e('File URL', 'blue-raven'); ?></th>
                                            <th style="width: 10%;"></th>
                                            <th style="width: 5%;"></th>
                                        </tr>
                                    </thead>
                                    <tbody id="download-file-table-body">
                                        <?php 
                                            if (!empty($downloads)) : 
                                                foreach ($downloads as $idCount => $download) :
                                                    $name = isset($download[1]) ? esc_attr($download[1]) : '';
                                                    $url = isset($download[2]) ? esc_url($download[2]) : '';
                                                    $rowId = $idCount + 1;
                                                    
                                                    ?>
                                                        <tr>
                                                            <td><?php echo (int) $rowId; ?></td>
                                                            <td><input type="text" class="br-input" style="width:100%;" name="fileName<?php echo (int) $rowId; ?>" placeholder="<?php echo esc_attr__('Enter file name', 'blue-raven'); ?>" value="<?php echo esc_attr($name); ?>"></td>
                                                            <td><input type="url" class="file-url br-input" style="width:100%;" name="fileURL<?php echo (int) $rowId; ?>" placeholder="<?php echo esc_attr__('Enter file URL', 'blue-raven'); ?>" value="<?php echo esc_url($url); ?>"></td>
                                                            <td><button type="button" class="upload-btn enterra-button enterra-secondary-button-color" data-row-index="<?php echo (int) $rowId; ?>"><?php echo esc_html__('Upload', 'blue-raven'); ?></button></td>
                                                            <td><button type="button" class="remove-download text-button" data-row-index="<?php echo (int) $rowId; ?>"><?php echo esc_html__('❌', 'blue-raven'); ?></button></td>
                                                        </tr>
                                                    <?php 
                                                    
                                                endforeach;
                                            endif; 
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                            
                            <div class="flex-sb spacing-bottom" style="width: 100%;">
                                <div style="margin: 0; width: 50%;" class="spacing-right">
                                    <label for="download-limit" class="text-center">
                                        <p class="no-margin-padding spacing-right-small text-center"><?php  esc_html_e('Download Limit', 'blue-raven'); ?></p>
                                    </label>
                                    <input type="number" name="download-limit" id="download-limit" placeholder="<?php esc_attr_e('Unlimited', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo $downloadLimit == -1 ? '' : esc_attr($downloadLimit); ?>">
                                </div>
                                <div style="margin: 0; width: 50%;" class="spacing-right">
                                    <label for="download-expiry" class="text-center">
                                        <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Download Expiry (days)', 'blue-raven'); ?></p>
                                    </label>
                                    <input type="number" name="download-expiry" id="download-expiry" placeholder="<?php esc_attr_e('Never', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo $downloadExpiry == -1 ? '' : esc_attr($downloadExpiry); ?>">
                                </div>
                            </div>
                        </div>
                    </div>
                    
    <!-- =========== Inventory =========== -->    
    
                    <div id="inventory-block" class="border-left-white" style="margin-top: 4rem;">
                        <h4><?php esc_html_e('Inventory', 'blue-raven'); ?></h4>
                    
                        <div class="flex-sb spacing-bottom" style="width: 100%;" id="sku-block">
                            <div style="margin: 0; width: 49%;">
                                <label class="text-center" for="product-sku">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('SKU', 'blue-raven'); ?></p>
                                </label>
                                <input type="text" name="product-sku" id="product-sku" placeholder="" class="br-input" style="width:100%;" value="<?php echo esc_attr($sku); ?>">
                            </div>
                    
                            <!-- <div style="margin: 0; width: 49%;">
                                <label class="text-center" for="product-barcode">
                                    <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('GTIN, UPC, EAN, or ISBN', 'blue-raven'); ?></p>
                                </label>
                                <input type="text" name="product-barcode" id="product-barcode" placeholder="" class="br-input" style="width:100%;" value="<?php echo esc_attr($gtin_ect); ?>">
                            </div> -->
                        </div>
                    
                        <div class="spacing-bottom" style="width: 100%;" id="stock-block">
                            <?php $should_check = ($stock_quantity != '') || ($stock_lowStock != '');?>
                
                            <label class="br-checkbox spacing-bottom" style="width: 49%;" id="track-stock-block">
                                <input type="checkbox" name="track-stock-check" value="1" style="display: none;" id="track-stock-check" <?php echo $should_check ? 'checked="checked"' : ''; ?>>
                                <span class="br-check-mark"></span>
                                <label for="track-stock-check"><?php esc_html_e('Track stock quantity for this product', 'blue-raven'); ?></label>
                            </label>
                    
                            <div class="flex-sb">
                                <div style="margin: 0; width: 49%; display: <?php echo $should_check ? 'block' : 'none'; ?>;" class="spacing-bottom" id="tracked-stock">
                                    <label class="text-center" for="product-stock-quantity">
                                        <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Quantity', 'blue-raven'); ?></p>
                                    </label>
                                    <input type="number" name="product-stock-quantity" id="product-stock-quantity" placeholder="0" class="br-input" style="width:100%;" value="<?php echo esc_attr(number_format((float) $stock_quantity)); ?>">
                                </div>
                    
                                <div style="width: 49%; display: <?php echo $should_check ? 'block' : 'none'; ?>;" id='show-backorders'>
                                    <label><p><?php esc_html_e('Allow backorders?', 'blue-raven'); ?></p></label>
                                    <div class="flex-left" style="width:100%; height: 2.25rem;">
                                        <label class="br-checkbox spacing-right">
                                            <input type="radio" name="allow-backorder" value="no" style="display: none;" id="allow-backorder-do-not-allow" <?php checked($stock_backorders, 'no'); ?> required>
                                            <span class="br-check-mark"></span>
                                            <label for="allow-backorder-do-not-allow"><?php esc_html_e('Do not allow', 'blue-raven'); ?></label>
                                        </label>
                    
                                        <label class="br-checkbox spacing-right">
                                            <input type="radio" name="allow-backorder" value="notify" style="display: none;" id="allow-but-notify-customer" <?php checked($stock_backorders, 'notify'); ?>>
                                            <span class="br-check-mark"></span>
                                            <label for="allow-but-notify-customer"><?php esc_html_e('Allow, but notify customer', 'blue-raven'); ?></label>
                                        </label>
                    
                                        <label class="br-checkbox spacing-right">
                                            <input type="radio" name="allow-backorder" value="yes" style="display: none;" id="allow-backorder-allow" <?php checked($stock_backorders, 'yes'); ?>>
                                            <span class="br-check-mark"></span>
                                            <label for="allow-backorder-allow"><?php esc_html_e('Allow', 'blue-raven'); ?></label>
                                        </label>
                                    </div>
                                </div>
                                <div style="width: 49%; display: <?php echo ($should_check || $type === 'variable') ? 'none' : 'block'; ?>;" id="tracked-stock-status">
                                    <label><p><?php esc_html_e('Stock status?', 'blue-raven'); ?></p></label>
                                    <div class="flex-left" style="width:100%; height: 2.25rem;">
                                        <label class="br-checkbox spacing-right">
                                            <input type="radio" name="stock_status" value="instock" style="display: none;" id="stock-status-in-stock" <?php checked($stock_status, 'instock'); ?> required>
                                            <span class="br-check-mark"></span>
                                            <label for="stock-status-in-stock"><?php esc_html_e('In Stock', 'blue-raven'); ?></label>
                                        </label>
                                        
                                        <label class="br-checkbox spacing-right">
                                            <input type="radio" name="stock_status" value="outofstock" style="display: none;" id="stock-status-out-of-stock" <?php checked($stock_status, 'outofstock'); ?>>
                                            <span class="br-check-mark"></span>
                                            <label for="stock-status-out-of-stock"><?php esc_html_e('Out of Stock', 'blue-raven'); ?></label>
                                        </label>
                                        
                                        <label class="br-checkbox spacing-right">
                                            <input type="radio" name="stock_status" value="onbackorder" style="display: none;" id="stock-status-on-backorder" <?php checked($stock_status, 'onbackorder'); ?>>
                                            <span class="br-check-mark"></span>
                                            <label for="stock-status-on-backorder"><?php esc_html_e('On Backorder', 'blue-raven'); ?></label>
                                        </label>
                                    </div>
                                </div>
                                
                            </div>
                    
                            <div class="flex-sb">
                                <div style="margin: 0; width: 49%; display: <?php echo $should_check ? 'block' : 'none'; ?>;" class="spacing-bottom" id="tracked-low-stock">
                                    <label class="text-center" for="low-stock-threshold">
                                        <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Low stock threshold', 'blue-raven'); ?></p>
                                    </label>
                                    <input type="number" name="low-stock-threshold" id="low-stock-threshold" placeholder="<?php esc_attr_e('Store-wide Threshold', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo esc_attr(number_format((float) $stock_lowStock)); ?>">
                                </div>
                            </div>
                        </div>
                    
                        <div class="flex-left" id="stock-sold-individually">
                            <label class="br-checkbox spacing-bottom" style="width: 49%;">
                                <input type="checkbox" name="product-stock-sold-individually-check" value="1" style="display: none;" id="product-stock-sold-individually-check" <?php checked($stock_individually); ?>>
                                <span class="br-check-mark"></span>
                                <label for="product-stock-sold-individually-check"><?php esc_html_e('Limit purchases to 1 item per order', 'blue-raven'); ?></label>
                            </label>
                        </div>
                    </div>
                 
    <!-- =========== Shipping =========== -->   
                    
                    <div id="shipping-block" class="border-left-white" style="margin-top: 4rem;">
                        <h4><?php esc_html_e('Shipping', 'blue-raven'); ?></h4>
                        <div class="flex-sb">
                            <div style="width: 49%;">
                                <div style="margin: 0; width: 100%;" class="spacing-bottom">
                                    <label class="text-center" for="product-weight">
                                        <p class="no-margin-padding spacing-right-small text-center"> <?php /* translators: %s is the WooCommerce weight unit symbol/code. */ echo sprintf( esc_html__('Weight (%s)', 'blue-raven'), esc_html(get_option('woocommerce_weight_unit'))); ?></p>
                                    </label>
                                    <input type="number" name="product-weight" id="product-weight" placeholder="0" class="br-input" style="width:100%;" value="<?php echo esc_attr(is_numeric($shipping_weight) ? number_format((float)$shipping_weight, 2, '.', '') : ''); ?>">
                                </div>
                    
                                <label class="text-center">
                                    <p class="no-margin-padding spacing-right-small text-center">
                                        <?php /* translators: %s is the WooCommerce Dimentions unit of measurement */ echo sprintf( esc_html__('Dimensions (%s)', 'blue-raven'), esc_html(get_option('woocommerce_dimension_unit'))); ?>
                                    </p>
                                </label>
                    
                                <div class="flex-sb">
                                    <div style="margin: 0; width: 32%;" class="spacing-bottom">
                                        <label class="text-center" for="product-length">
                                            <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Length', 'blue-raven'); ?></p>
                                        </label>
                                        <input type="number" name="product-length" id="product-length" placeholder="0" class="br-input" style="width:100%;" value="<?php echo esc_attr(is_numeric($shipping_length) ? number_format((float)$shipping_length, 2, '.', '') : ''); ?>">
                                    </div>
                                    <div style="margin: 0; width: 32%;" class="spacing-bottom">
                                        <label class="text-center" for="product-width">
                                            <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Width', 'blue-raven'); ?></p>
                                        </label>
                                        <input type="number" name="product-width" id="product-width" placeholder="0" class="br-input" style="width:100%;" value="<?php echo esc_attr(is_numeric($shipping_width) ? number_format((float)$shipping_width, 2, '.', '') : ''); ?>">
                                    </div>
                                    <div style="margin: 0; width: 32%;" class="spacing-bottom">
                                        <label class="text-center" for="product-height">
                                            <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Height', 'blue-raven'); ?></p>
                                        </label>
                                        <input type="number" name="product-height" id="product-height" placeholder="0" class="br-input" style="width:100%;" value="<?php echo esc_attr(is_numeric($shipping_height) ? number_format((float)$shipping_height, 2, '.', '') : ''); ?>">
                                    </div>
                                </div>
                            </div>
                    
                            <div style="width: 49%;">
                                <div style="margin: 0; width: 100%;" class="spacing-bottom">
                                    <label class="text-center" for="product-shipping-class">
                                        <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Shipping Class', 'blue-raven'); ?></p>
                                    </label>
                                    <select id="product-shipping-class" name="product-shipping-class" class="br-dropdown" style="width: 100%;max-width: 100%;min-width: 0;box-sizing: border-box;">
                                        <option value=""><?php esc_html_e('No Shipping Class', 'blue-raven'); ?></option>
                                        <?php
                                            function BlueRaven_get_shipping_classes_safely() {
                                                $shippingClasses = get_terms([
                                                    'taxonomy'   => 'product_shipping_class',
                                                    'hide_empty' => false,
                                                    'orderby'    => 'name',
                                                    'order'      => 'ASC',
                                                ]);
                        
                                                if (is_wp_error($shippingClasses)) return [];
                        
                                                return $shippingClasses;
                                            }
                        
                                            $classes = BlueRaven_get_shipping_classes_safely();
                        
                                            foreach ($classes as $class) {
												$selected = ($shipping_class === $class->slug) ? 'selected' : '';
												echo '<option value="' . esc_attr($class->slug) . '" ' . esc_attr($selected) . '>' . esc_html($class->name) . '</option>';
											}
                                        ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
    
    <!-- =========== Linked Product =========== -->   
                    
                    <div id='linked-products-block' class='border-left-white' style='margin-top: 4rem;'>
                        <h4>Linked Product </h4>
                        <div class='flex-sb'>
                            <div style='width: 49%; display:none;' id='grouped-products-add-block'>
                                <div style='position: relative; max-height: 36px; max-width: 100%;' id='grouped-products-add'>
                                    <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                                        <label class='text-center' for='product-grouped-products'><p class='no-margin-padding spacing-right-small text-center'>Grouped products</p></label>
                                        <input type='text' name='product-grouped-products' id='product-grouped-products' placeholder='Product' class='br-input' style='width:100%;' autocomplete="off">
                                    </div>
                                    <div class='text-pad' id='grouped-products-text-pad' style='display: none; position: absolute; z-index: 3; padding-bottom: 2rem !important;'></div>
                                </div>
                                <div style='margin: 3rem 0rem 4rem;' id='grouped-products-add-list'><?php echo wp_kses_post( BlueRaven_build_product_list($linked_grouped) ); ?></div>
                                <input type="hidden" id="product-grouped-input" name="product-grouped-list" value=""> 
                            </div>
                            <div style='width: 49%;'>
                                <div style='position: relative; max-height: 36px; max-width: 100%;' id='upsells-add'>
                                    <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                                        <label class='text-center' for='product-upsells'><p class='no-margin-padding spacing-right-small text-center'>Upsells</p></label>
                                        <input type='text' name='product-upsells' id='product-upsells' placeholder='Product' class='br-input' style='width:100%;' autocomplete="off">
                                    </div>
                                    <div class='text-pad' id='upsells-text-pad' style='display: none; position: absolute; z-index: 3; padding-bottom: 2rem !important;'></div>
                                </div>
                                <div style='margin: 3rem 0rem 4rem;' id='upsells-add-list'><?php echo wp_kses_post( BlueRaven_build_product_list($linked_upsells) ); ?></div>
                                <input type="hidden" id="product-upsells-input" name="product-upsells-list" value=""> 
                            </div>
                            <div style='width: 49%;'id='cross-sell-add-block'>
                                <div style='position: relative; max-height: 36px; max-width: 100%;' id='cross-sell-add'>
                                    <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                                        <label class='text-center' for='product-cross-sells'><p class='no-margin-padding spacing-right-small text-center'>Cross-sells</p></label>
                                        <input type='text' name='product-cross-sells' id='product-cross-sells' placeholder='Product' class='br-input' style='width:100%;' autocomplete="off">
                                    </div>
                                    <div class='text-pad' id='cross-sell-text-pad' style='display: none; position: absolute; z-index: 3; padding-bottom: 2rem !important;'></div>
                                </div>
                                <div style='margin: 3rem 0rem 4rem;' id='cross-sell-add-list'><?php echo wp_kses_post( BlueRaven_build_product_list($linked_cross) ); ?></div> 
                                <input type="hidden" id="product-cross-input" name="product-cross-list" value=""> 
                            </div>
                        </div>
                    </div>
        
    <!-- =========== Attributes =========== -->  
    
                    <div id='products-attributes-block' class='border-left-white' style='margin-top: 4rem;'>
                        <h4><?php esc_html_e('Attributes', 'blue-raven'); ?></h4>
                        <p class='enterra-highlight'> <?php esc_html_e('Add descriptive pieces of information that customers can use to search for this product on your store, such as "Material" or "Size".', 'blue-raven'); ?> </p>
                        <div class='flex-left'>
                            <div style='margin: 1rem 0rem;'>
                                <button class='enterra-button enterra-secondary-button-color spacing-right' id='add-attributes'> <?php esc_html_e('Add New Attributes', 'blue-raven'); ?> </button>
                            </div>
                            <div style='align-self: center;'>
                                <select id="product-add-existing-attributes" name="product-add-existing-attributes" class="br-dropdown" style="width: 20rem; padding: 0.5rem; margin-right: 1rem;">
                                    <option value=""><?php esc_html_e('Add Existing', 'blue-raven'); ?></option>
                                    <?php 
                                        $attributes = wc_get_attribute_taxonomies();
                                        foreach ($attributes as $attribute) {echo '<option value="' . esc_attr($attribute->attribute_name) . '">' . esc_html($attribute->attribute_label) . '</option>';}
                                    ?>
                                </select>
                            </div>
                        </div>
                        
                        <div id='attributes-list-table' style="display: <?php echo (esc_html($product['attributes_json']) !== '' && esc_html($product['attributes_json']) !== '[]') ? 'block' : 'none'; ?>">
                            <table class='blue-raven-table'>
                                <thead>
                                    <tr>
                                        <th class="col-name" style='width: 20%;'><?php esc_html_e('Name', 'blue-raven'); ?></th>
                                        <th class="col-values" style='width: 60%;'><?php esc_html_e('Values', 'blue-raven'); ?></th>
                                        <th class="col-visible" style='width: 7.5%;'><?php esc_html_e('Visible', 'blue-raven'); ?></th>
                                        <th class="col-variations" style='width: 7.5%;'><?php esc_html_e('Variations', 'blue-raven'); ?></th>
                                        <th style='width: 5%;'></th>
                                    </tr>
                                </thead>
                                <tbody id="attributes-list-table-body">
									<?php if (!empty($product['attributes_json'])) { 
                                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- BlueRaven_fetchAttributesBlock handles escaping internally. 
                                        echo BlueRaven_fetchAttributesBlock($product['attributes_json']);
                                    } ?>
								</tbody>
                            </table>
                            <div style='text-align: right; margin: 4rem auto'>
                                <button class='enterra-button' id='save-attributes'> <?php esc_html_e('Save Attributes', 'blue-raven'); ?> </button>
                            </div>
                        </div>
                    </div>
                    
    <!-- =========== Variations =========== -->   
    
                        
                    <div id='products-variations-block' class='border-left-white' style='margin-top: 4rem; display:none;'>
                        <h4><?php esc_html_e('Variations', 'blue-raven'); ?></h4>
                        
                        <div class='flex-left'>
                            <div style='margin: 1rem 0rem;'>
                                <button class='enterra-button enterra-secondary-button-color spacing-right' id='add-variations'>
                                    <?php esc_html_e('Add New Variations', 'blue-raven'); ?>
                                </button>
                            </div>
                        </div>
                        
                        <div id='variations-list-table' style='display: none;'>
                            <!--<p class='enterra-highlight'>
                                <span>1</span> 
                                <?php esc_html_e('variation does not have a price. Variations (and their attributes) that do not have prices will not be shown in your store.', 'blue-raven'); ?>
                            </p>-->
                            <table class='blue-raven-table'> 
                                <thead>
                                    <tr>
                                        <th style='width: 5%;'></th>
                                        <th style='width: 80%;'><?php esc_html_e('Options', 'blue-raven'); ?></th>
                                        <th style='width: 5%;'></th>
                                        <th style='width: 5%;'></th>
                                    </tr>
                                </thead>
                                <tbody id='variations-list-table-body' data-att="<?php echo esc_attr($product['attributes_json']); ?>" data-id="<?php echo esc_attr($product['id']); ?>"><?php 
                                        echo BlueRaven_create_rows($product['id'], $product['attributes_json']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- BlueRaven_create_rows handles escaping internally.
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div id='variations-item-edit' style='display: none;'></div>
                    </div>          
    
    <!-- =========== Advanced =========== -->     
                    
                    <div id='products-advacned-block' class='border-left-white' style='margin-top: 4rem;'>
                        <h4><?php esc_html_e('Advanced', 'blue-raven'); ?></h4>
                        
                        <div class='spacing-bottom' style='width: 100%;' id='purchase-note-block'>
                            <div style="margin: 0; width: 100%;">
                                <label class='text-center' for='purchase-note'>
                                    <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Purchase note', 'blue-raven'); ?></p>
                                </label>
                                <textarea class='br-text-area' id='purchase-note' style="width: 100%; height: 100px;" name='purchase-note'><?php echo wp_kses_post($advanced_purchase_notes); ?></textarea>
                            </div> 
                        </div>
                        
                        <div class='spacing-bottom' style='width: 100%;'>
                            <div style="margin: 0; width: 49%;">
                                <label class='text-center' for='menu-order'>
                                    <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Menu order', 'blue-raven'); ?></p>
                                </label>
                                <input type='number' name='menu-order' id='menu-order' placeholder='<?php echo esc_attr__('Title', 'blue-raven'); ?>' class='br-input' style='width:100%;' value="<?php echo esc_attr($advanced_order); ?>">
                            </div>
                        </div>
                        
                        <div style="margin: 0 0 4rem 0; width: 49%;">
                            <label class="br-checkbox spacing-right" style="width: 50%; height: 2rem;" for="enable-reviews">
                                <input type="checkbox" name="enable-reviews" value="1" id="enable-reviews" <?php checked($advanced_reviews, '1'); ?> style="display: none;">
                                <span class="br-check-mark"></span>
                                <?php esc_html_e('Enable Reviews', 'blue-raven'); ?>
                            </label>
                        </div>
                    </div>
                </section>
                
<!--
============================================================
   SALES
============================================================
-->  
    
                <section class='product-container product-sales-container' style='display: none'>
                    <div id='product-sales-block' class='border-left-white' style='margin-top: 4rem;'>
                        <h4><?php esc_html_e('Set up a sale', 'blue-raven'); ?></h4>
                        
                        <div style="margin: 0; width: 49%;" class='spacing-bottom'>
                            <label class='text-center' for='sales-price'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php /* translators: %s is the WooCommerce currency symbol/code. */ printf(esc_html__('Sale Price in %s', 'blue-raven'), esc_html(get_woocommerce_currency())); ?></p>
                            </label>
                            <input type='text' name='sales-price' id='sales-price' placeholder='<?php echo esc_attr__('0.00', 'blue-raven'); ?>' class='br-input' style='width:100%;' value="<?php echo esc_attr($sale_price); ?>">
                        </div>  
                        
                        <div class='flex-sb' style='margin-bottom: 4rem;'>
                            <div style="margin: 0; width: 49%;">
                                <label class='text-center' for='sales-from'> <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('From', 'blue-raven'); ?></p></label>
                                <input type='text' name='sales-from' id='sales-from' placeholder='<?php echo esc_attr__('YYYY-MM-DD', 'blue-raven'); ?>' class='br-input' style='width:100%;' value="<?php echo esc_attr($sale_start); ?>">
                            </div> 
                            <div style="margin: 0; width: 49%;">
                                <label class='text-center' for='sales-to'><p class='no-margin-padding spacing-right-small text-center'> <?php esc_html_e('To', 'blue-raven'); ?> </p></label>
                                <input type='text' name='sales-to' id='sales-to' placeholder='<?php echo esc_attr__('YYYY-MM-DD', 'blue-raven'); ?>' class='br-input' style='width:100%;' value="<?php echo esc_attr($sale_end); ?>">
                            </div> 
                        </div>
                    </div>
                </section>
                
<!--
============================================================
   IMAGES
============================================================
--> 
    
                <section class='product-container product-images-container' style='display: none'>
                    <div id='image-add-content'>
                        <?php  $allowed_html = wp_kses_allowed_html('post');
                            $allowed_html['input'] = ['type'=> true,'id'=> true,'name'=> true,'value'=> true,'class'=> true,'style'=> true,'placeholder' => true,'checked'=> true,];$allowed_html['button'] = ['type'=> true,'class'=> true,'style'=> true,]; echo wp_kses(BlueRaven_build_image_list($images), $allowed_html); 
                        ?>
                    </div>
                    <input type="hidden" id="product-image-input" name="product-image-list" value=""> 
                    <div class='flex-right border-bottom-white' style='margin: 3rem 0rem; padding-bottom: 2rem;'>
                        <p class='enterra-highlight spacing-right' style='align-self: center;'><?php esc_html_e('Dont forget to set your thumbnail with a star', 'blue-raven'); ?></p>
                        <button class='enterra-button enterra-secondary-button-color' id='add-image'><?php esc_html_e('Add Image', 'blue-raven'); ?></button>
                    </div>
                </section>
                
<!--
============================================================
   SEO
============================================================
--> 

                <section class='product-container product-seo-container' style='display: none'>
                    <div id='seo-meta-block' class='border-left-white' style='margin-top: 4rem;'>
                        <h4><?php esc_html_e('Product Meta', 'blue-raven'); ?></h4>
                        <div style="margin: 0 0 2rem 0; width: 99.5%;">
                            <label for="meta-title" class="text-center">
                                <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Meta Title', 'blue-raven'); ?></p>
                            </label>
                            <input type="text" name="meta-title" id="meta-title" placeholder="<?php esc_attr_e('Meta Title', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo esc_attr($product['meta_title']); ?>">
                        </div>
                        <div class='flex-sb'>
                            <div class='flex-sb spacing-bottom' style='width: 75%;'>
                                <div style="margin: 0; width: 99%;">
                                    <label class='text-center' for='meta-description'>
                                        <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Meta Description', 'blue-raven'); ?></p>
                                    </label>
                                    <textarea class='br-text-area' id='meta-description' style="width: 100%; height: 250px;" name='meta-description'><?php echo wp_kses_post($product['meta_description']); ?></textarea>
                                </div> 
                            </div>
                            <div class='flex-sb spacing-bottom' style='width: 25%;'>
                                <div style="margin: 0; width: 99%;">
                                    <label class='text-center' for='meta-keys'>
                                        <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Meta Keywords', 'blue-raven'); ?></p>
                                    </label>
                                    <textarea class='br-text-area' id='meta-keys' style="width: 100%; height: 250px;" name='meta-keys'><?php echo is_array($keywords = json_decode($product['meta_keywords'])) ? esc_textarea(implode(",\n", $keywords)) : ''; ?></textarea>
                                </div> 
                            </div>
                        </div>
                    </div>
                    <div id='seo-schema-block' class='border-left-white' style='margin: 4rem 0rem;'>
                        <h4><?php esc_html_e('Product Schema', 'blue-raven'); ?></h4>
                        <p class='enterra-highlight'><?php esc_html_e('Products will generate their own schema once published.', 'blue-raven'); ?></p>
                    </div>
                </section> 
                
                <div class='flex-sb' style='margin: 2rem 0rem;'>
                    <input type="hidden" id="product-save-type" name="product-save-type" value="">
                    <div class='flex-sb' style='width:45%;'>
                        <button class='enterra-button' id='save-draft'><?php esc_html_e('Save with Blue Raven', 'blue-raven'); ?></button>
                    </div>
                    <div class='inline-spinner' style='display: none;' id='loading-icon'></div>
                    <div class='flex-right' style='width:45%;'>
                        <button class='enterra-button enterra-secondary-button-color spacing-right' id='save-woo-draft'>
                            <?php echo $isDraft ? esc_html__('Update Saved Draft', 'blue-raven') : esc_html__('Save Draft', 'blue-raven'); ?>
                        </button>
                        <button class='enterra-button' id='save-woo-product'>
                            <?php echo $isPublished ? esc_html__('Update Publish Product', 'blue-raven') : esc_html__('Publish Product', 'blue-raven'); ?>
                        </button>
                    </div>
                </div>
            </form>
        <?php
    }
}
