<?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        The front-end for creating, saving, loading, the woo-commernce product
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - VARIATIONS EDIT BLOCK 
 * || 
 * =============================================================================
 */

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

/*
============================================================
    VARIATIONS EDIT BLOCK 
============================================================
*/

    ## =========== Sanitize and assign input content =========== ##

$menuPos            = sanitize_text_field($content[0]);
$attributes         = sanitize_text_field(stripslashes($content[1]));
$variableName       = sanitize_text_field($content[2]);
$veID               = sanitize_text_field(stripslashes($content[3]));
$pid                = absint($content[4]);

global $wpdb;
$table_name = $wpdb->prefix . 'BlueRavenProducts';

    ## =========== Get the existing CSV data =========== ##
    
$existing_csv = $wpdb->get_var($wpdb->prepare( "SELECT variations_CSV FROM $table_name WHERE ID = %d", $pid));

    ## =========== Default CSV =========== ##

if (empty($existing_csv)) {
    $values = [
        'Published'                 => '',
        'Virtual'                   => '',

        'Download limit'            => '',
        'Download expiry days'      => '',

        'SKU'                       => '',
        'GTIN, UPC, EAN, or ISBN'   => '',
        'Stock'                     => '',
        'Backorders allowed?'       => '',
        'Low stock amount'          => '',

        'Regular price'             => '',
        'Sale price'                => '',
        'Date sale price starts'    => '',
        'Date sale price ends'      => '',

        'Weight (kg)'               => '0',
        'Length (cm)'               => '0',
        'Width (cm)'                => '0',
        'Height (cm)'               => '0',
        'Shipping class'            => '',

        'Description'               => '',
        'Images'                    => '',
        'Download 1 ID'             => '',
        'Download 1 name'           => '',
        'Download 1 URL'            => '',
    ];

} else {
    
    ## =========== Parse CSV =========== ##
    
    $existing_variations = [];
    if (empty($rows = array_map('str_getcsv', explode("\n", $existing_csv)))) {return '';}

    $header = array_shift($rows);
    if (!is_array($header)) return '';

    $existing_variations = array_filter(array_map(function ($row) use ($header) { return (is_array($row) && count($row) === count($header)) ? array_combine($header, $row) : null; }, $rows));

    ## =========== Find matching variation =========== ##

    $values = null;

    foreach ($existing_variations as $variation) {
        $CSVveID = $variation['ID'];
        $trimmedVeID = trim($CSVveID);

        if (isset($trimmedVeID) && $trimmedVeID === $veID) {
            $values = (array) $variation;
            break;
        }
    }

    if (!is_array($values) || empty($values)) { 
        $values = [
            'Published'                 => '',
            'Virtual'                   => '',

            'Download limit'            => '',
            'Download expiry days'      => '',

            'SKU'                       => '',
            'GTIN, UPC, EAN, or ISBN'   => '',
            'Stock'                     => '',
            'Backorders allowed?'       => '',
            'Low stock amount'          => '',

            'Regular price'             => '',
            'Sale price'                => '',
            'Date sale price starts'    => '',
            'Date sale price ends'      => '',

            'Weight (kg)'               => '0',
            'Length (cm)'               => '0',
            'Width (cm)'                => '0',
            'Height (cm)'               => '0',
            'Shipping class'            => '',

            'Description'               => '',
            'Images'                    => '',
            'Download 1 ID'             => '',
            'Download 1 name'           => '',
            'Download 1 URL'            => '',
        ];
    }
}
 
    ## =========== Define CSV helper =========== ##

function BlueRaven_get_csv_value($row, $columnName) { return $row[$columnName] ?? '';  }

    ## =========== Extract variation values =========== ##
    
$enabled                = BlueRaven_get_csv_value($values, 'Published');
$virtual                = BlueRaven_get_csv_value($values, 'Virtual'); 

$downloadLimit          = BlueRaven_get_csv_value($values, 'Download limit');
$downloadExpiry         = BlueRaven_get_csv_value($values, 'Download expiry days');

$sku                    = BlueRaven_get_csv_value($values, 'SKU');
$gtin_ect               = BlueRaven_get_csv_value($values, 'GTIN, UPC, EAN, or ISBN');
$stock_quantity         = BlueRaven_get_csv_value($values, 'Stock');
$stock_backorders       = BlueRaven_get_csv_value($values, 'Backorders allowed?');
$stock_lowStock         = BlueRaven_get_csv_value($values, 'Low stock amount');
$stock_status           = BlueRaven_get_csv_value($values, 'In stock?');

$price                  = BlueRaven_get_csv_value($values, 'Regular price');
$sale_price             = BlueRaven_get_csv_value($values, 'Sale price');
$sale_start             = BlueRaven_get_csv_value($values, 'Date sale price starts');
$sale_end               = BlueRaven_get_csv_value($values, 'Date sale price ends');

$shipping_weight        = floatval(BlueRaven_get_csv_value($values, 'Weight (kg)'));
$shipping_length        = floatval(BlueRaven_get_csv_value($values, 'Length (cm)'));
$shipping_width         = floatval(BlueRaven_get_csv_value($values, 'Width (cm)'));
$shipping_height        = floatval(BlueRaven_get_csv_value($values, 'Height (cm)'));
$shipping_class         = BlueRaven_get_csv_value($values, 'Shipping class');

$description            = BlueRaven_get_csv_value($values, 'Description');

$manage_stock           = ($stock_quantity !== '' || $stock_lowStock !== '') ? true : false;

    ## =========== Handle image and alt text =========== ##
    
$imageURL               = BlueRaven_get_csv_value($values, 'Images');
$alt                    = '';
$src                    = '';
$attachment_id          = 0;
$src                    = esc_url_raw($imageURL);

if (!empty(attachment_url_to_postid($src))) {
    $attachment_id = attachment_url_to_postid($src);
    if ($attachment_id) { $alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);$alt = $alt ? esc_attr($alt) : '';} else {$alt = '';  }
} else {
    $src = BLUERAVEN_PLUGIN_URL . 'assets/media/blue_raven_place_holder.webp';
    $alt = 'Placeholder image'; 
}

    ## =========== Handle downloads =========== ##

$downloads = [];
$hasDownloads = BlueRaven_get_csv_value($values, 'Download 1 ID');

if (!empty($hasDownloads)) {
    $count = 1;
    while ($id = BlueRaven_get_csv_value($values, "Download {$count} ID")) {
        $downloads[] = [
            sanitize_text_field($id),
            sanitize_text_field(BlueRaven_get_csv_value($values, "Download {$count} name")),
            esc_url_raw(BlueRaven_get_csv_value($values, "Download {$count} URL"))
        ];
        $count++;
    }
}
    
?>
    <h4 style="margin-top: 3rem;"><?php esc_html_e( 'Editing', 'blue-raven' ); ?></h4>
    <div style='width: 97.5%; margin: 0 0 0 auto;'>
        <div>
            <input type="hidden" id="variable-product-id" name="variable-product-id" value="<?php echo esc_attr( $veID ?? $menuPos ); ?>"> 
            <input type="hidden" id="variable-name" name="variable-name" value="<?php echo esc_attr( $variableName ); ?>"> 
            <input type="hidden" id="ve-menu-pos" name="ve-menu-pos" value="<?php echo esc_attr( $menuPos ); ?>">
            <input type="hidden" id="ve-att-name" name="ve-att-json" value="<?php echo esc_attr( $attributes ); ?>">
        </div>
        
    <!-- =========== SKU =========== -->     

        <div class='flex-sb'>
            <div style='width: 49.5%;'>
                <div>
                    <img src="<?php echo esc_url( $src ?? BLUERAVEN_PLUGIN_URL . 'assets/media/blue_raven_place_holder.webp'); ?>" alt="<?php echo esc_attr( $alt ?: __( 'A Place-holder Image', 'blue-raven' ) ); ?>" width="140" height="auto" style='cursor: pointer;' id='ve-post-image'>
                    <input type="hidden" id="ve-product-img" name="ve-product-img" value=""> 
                </div>
            </div>
        
            <div style='width: 49.5%;' class='spacing-bottom' id='ve-sku-block-variations'>
                <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                    <label class='text-center' for='variable-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='variable-product-sku' id='variable-product-sku' class='br-input' style='width:100%;' value="<?php echo esc_attr( $sku ); ?>">
                </div>
                <!-- <div style="margin: 0; width: 100%;">
                    <label class='text-center' for='variable-product-other-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='variable-product-other-barcode' id='variable-product-barcode' class='br-input'  style='width:100%;'  value="<?php echo esc_attr( $gtin_ect ); ?>">
                </div> -->
            </div>
        </div>
        <div style="margin: 0; width: 100%;" class='spacing-bottom-large'>
            <label class='text-center' for='image-alt'>
                <p class='no-margin-padding spacing-right-small text-center'>
                    <?php esc_html_e( 'Image Alt', 'blue-raven' ); ?>
                </p>
            </label>
            <input type='text' name='image-alt' id='ve-image-alt' placeholder="<?php esc_attr_e( 'A place-holder image for your product.', 'blue-raven' ); ?>" class='br-input' style='width:100%;' value="<?php echo esc_attr( $alt ); ?>">
        </div>
         
    <!-- =========== Settings =========== -->

        <div class='flex-left' style='height: 2.5rem;'>
            
            <label class="br-checkbox spacing-right spacing-bottom" style='min-width: 8rem;'>
                <input type="checkbox" name="enable-product" id="ve-enable" style="display: none;" <?php echo checked($enabled); ?>>
                <span class="br-check-mark"></span>
                <label for="ve-enable"><?php esc_html_e('Enabled', 'blue-raven'); ?></label>
            </label>
        
            <label class="br-checkbox spacing-right spacing-bottom" style='min-width: 8rem;'>
                <input type="checkbox" name="downloadable" id="ve-download" style="display: none;" <?php echo checked(!empty($hasDownloads), true, false); ?>>
                <span class="br-check-mark"></span>
                <label for="ve-download"><?php esc_html_e('Downloadable', 'blue-raven'); ?></label>
            </label>
        
            <label class="br-checkbox spacing-right spacing-bottom" style='min-width: 8rem;'>
                <input type="checkbox" name="virtual" id="ve-virtual" style="display: none;" <?php echo checked($virtual); ?>>
                <span class="br-check-mark"></span>
                <label for="ve-virtual"><?php esc_html_e('Virtual', 'blue-raven'); ?></label>
            </label>
        
            <label class="br-checkbox spacing-right spacing-bottom" style='min-width: 8rem;'>
                <input type="checkbox" name="manage-stock" id="ve-manage-stock" style="display: none;" <?php echo checked($manage_stock); ?>>
                <span class="br-check-mark"></span>
                <label for="ve-manage-stock"><?php esc_html_e('Manage stock?', 'blue-raven'); ?></label>
            </label>
        </div>
        
    <!-- =========== Price =========== -->

        <h4><?php esc_html_e('Price And Sales', 'blue-raven'); ?></h4>
        <div class='flex-sb'>
            <div style="margin: 0; width: 49.5%;" >
                <label class='text-center' for='ve-product-price'>
                    <p class='no-margin-padding spacing-right-small text-center'>
                        <?php /* translators: %s is the currency symbol (e.g., $, €) */ printf( esc_html__('Regular Price in %s', 'blue-raven'), esc_html( get_woocommerce_currency() ) ); ?>
                    </p>
                </label>
                <input type='text' name='ve-product-price' id='ve-product-price' placeholder='0.00' class='br-input' style='width:100%;' value="<?php echo esc_attr($price); ?>">
            </div>
            <div style="margin: 0; width: 49.5%;" class='spacing-bottom'>
                <label class='text-center' for='ve-sales-price'>
                    <p class='no-margin-padding spacing-right-small text-center'>
                        <?php /* translators: %s is the currency symbol (e.g., $, €) */ printf( esc_html__('Sales Price in %s', 'blue-raven'), esc_html( get_woocommerce_currency() ) ); ?>
                        <span class='enterra-highlight' id='ve-schedule-sale'> <?php echo esc_html__('Schedule', 'blue-raven'); ?>  </span>
                    </p>
                </label>
                <input type='text' name='ve-sales-price' id='ve-sales-price' placeholder='0.00' class='br-input' style='width:100%;' value="<?php echo esc_attr($sale_price); ?>">
            </div>
        </div>

    <!-- =========== Sale =========== -->

        <div class='flex-sb spacing-bottom' id='ve-sales-dates' style="display: <?php echo (!empty($sale_price) && !empty($sale_start) && !empty($sale_end)) ? 'flex' : 'none'; ?>;">
            <div style="margin: 0; width: 49.5%;">
                <label class='text-center' for='ve-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='ve-sales-from' id='ve-sales-from' placeholder='YYYY-MM-DD' class='br-input' style='width:100%;' value="<?php echo esc_attr($sale_start); ?>">
            </div> 
            <div style="margin: 0; width: 49.5%;">
                <label class='text-center' for='ve-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='ve-sales-to' id='ve-sales-to' placeholder='YYYY-MM-DD' class='br-input' style='width:100%;' value="<?php echo esc_attr($sale_end); ?>">
            </div> 
        </div>

    <!-- =========== Stock =========== -->

        <h4><?php esc_html_e('Stock', 'blue-raven'); ?></h4>
        <div class='spacing-bottom' style='width: 100%; display: <?php echo $manage_stock ? 'block' : 'none'; ?>;' id='ve-stock-block'>
            <div class='flex-sb'>
                <div style="margin: 0; width: 49.5%;" class='spacing-bottom'>
                    <label class='text-center' for='ve-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='ve-product-stock-quantity' id='ve-product-stock-quantity' placeholder='0' class='br-input' style='width:100%;' value="<?php echo esc_attr($stock_quantity ?? 0); ?>">
                </div>
                <div style='width: 49.5%;'>
                    <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="ve-allow-backorder" value="no" style="display: none;" id="ve-allow-backorder-do-not-allow" <?php if ($stock_backorders == 'no') echo 'checked'; ?> required>
                            <span class="br-check-mark"></span>
                            <label for="ve-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="ve-allow-backorder" value="notify" style="display: none;" id="ve-allow-but-notify-customer" <?php if ($stock_backorders === 'notify') echo 'checked'; ?>>
                            <span class="br-check-mark"></span>
                            <label for="ve-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="ve-allow-backorder" value="yes" style="display: none;" id="ve-allow-backorder-allow" <?php if ($stock_backorders == 'yes') echo 'checked'; ?>>
                            <span class="br-check-mark"></span>
                            <label for="ve-allow-backorder-allow"><?php esc_html_e('Allow', 'blue-raven'); ?></label>
                        </label>
                    </div>
                </div>
            </div>
            <div class='flex-left'>
                <div style="margin: 0; width: 49.5%;" class='spacing-bottom'>
                    <label class='text-center' for='ve-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='ve-low-stock-threshold' id='ve-low-stock-threshold' placeholder="<?php esc_attr_e('Store-wide Threshold', 'blue-raven'); ?>" class='br-input' style='width:100%;' value="<?php echo esc_attr($stock_lowStock ?? 0); ?>">
                </div>
            </div>
        </div>
        <div style="width: 49%; display: <?php echo $manage_stock ? '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="ve-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="ve-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="ve-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>
    
    <!-- =========== Shipping =========== -->

        <h4><?php esc_html_e('Shipping', 'blue-raven'); ?></h4>
        <div class='flex-sb' id='ve-shipping'>
            <div style='width: 49.5%;'>
                <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                    <label class='text-center' for='ve-product-weight'>
                        <p class='no-margin-padding spacing-right-small text-center'>
                            <?php /* translators: %s is the name of the missing field(s) */  printf(esc_html__('Weight (%s)', 'blue-raven'), esc_html(get_option('woocommerce_weight_unit'))); ?>
                        </p>
                    </label>
                    <input type='text' name='ve-product-weight' id='ve-product-weight' placeholder='0' class='br-input' style='width:100%;' value="<?php echo esc_attr(number_format((float)$shipping_weight, 2)); ?>">
                </div>
        
                <label class='text-center' for='product-weight'>
                    <p class='no-margin-padding spacing-right-small text-center'>
                        <?php /* translators: %s is the name of the missing field(s) */ printf(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='ve-product-length'>
                            <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Length', 'blue-raven'); ?></p>
                        </label>
                        <input type='text' name='ve-product-length' id='ve-product-length' placeholder='0' class='br-input' style='width:100%;' value="<?php echo esc_attr(number_format((float)$shipping_length, 2)); ?>">
                    </div>
                    <div style="margin: 0; width: 32%;" class='spacing-bottom'>
                        <label class='text-center' for='ve-product-width'>
                            <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Width', 'blue-raven'); ?></p>
                        </label>
                        <input type='text' name='ve-product-width' id='ve-product-width' placeholder='0' class='br-input' style='width:100%;' value="<?php echo esc_attr(number_format((float)$shipping_width, 2)); ?>">
                    </div>
                    <div style="margin: 0; width: 32%;" class='spacing-bottom'>
                        <label class='text-center' for='ve-product-height'>
                            <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Height', 'blue-raven'); ?></p>
                        </label>
                        <input type='text' name='ve-product-height' id='ve-product-height' placeholder='0' class='br-input' style='width:100%;' value="<?php echo esc_attr(number_format((float)$shipping_height, 2)); ?>">
                    </div>
                </div>
            </div>
            <div style='width: 49.5%;'>
                <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                    <label class='text-center' for='ve-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="ve-product-shipping-class" name="ve-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('Same as parent', 'blue-raven'); ?></option>
                        <?php
                            function BlueRaven_get_shipping_classes_safely() {
                                $shippingClasses = get_terms([
                                    'taxonomy' => 'product_shipping_class',
                                    'hide_empty' => false,
                                    'orderby' => 'name',
                                    'order' => 'ASC'
                                ]);
                                return is_wp_error($shippingClasses) ? [] : $shippingClasses;
                            }
            
                            $shipping_class_key = array_search('Shipping class', $values);
                            $shipping_class = $shipping_class_key !== false ? $values[$shipping_class_key] : '';
                            $classes = BlueRaven_get_shipping_classes_safely();
            
                            foreach ($classes as $class) {
                                $selected = ($shipping_class === $class->name) ? 'selected' : '';
                                printf(
                                    '<option value="%s" %s>%s</option>',
                                    esc_attr($class->slug),
                                    esc_attr($selected),
                                    esc_html($class->name)
                                );
                            }
                        ?>
                    </select>
                </div>
            </div>
        </div>

    <!-- =========== Description =========== -->

        <h4><?php esc_html_e('Description', 'blue-raven'); ?></h4>
        <div id='ve-description'>
            <div class='spacing-bottom' style='width: 100%;' id='ve-description-note-block'>
                <div style="margin: 0; width: 99%;">
                    <label class='text-center' for='ve-description-note'>
                        <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Description', 'blue-raven'); ?></p>
                    </label>
                    <textarea class='br-text-area' id='ve-description-note' style="width: 100%; height: 75px;" name='ve-description-note'><?php echo esc_textarea($description); ?></textarea>
                </div> 
            </div>
        </div>

    <!-- =========== Download =========== -->

        <div id="ve-download-block" style="display: <?php echo $hasDownloads ? 'block' : 'none'; ?>;">
            <h4><?php esc_html_e('Downloads', 'blue-raven'); ?></h4>
            <div>
                <div style="margin: 0; width: 49.5%;" class="spacing-right">
                    <label class="text-center" for="downloadable-files">
                        <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Downloadable files', 'blue-raven'); ?></p>
                    </label>
                    <div style="margin: 1rem 0;">
                        <button class="enterra-button enterra-secondary-button-color spacing-right" id="ve-add-file">
                            <?php esc_html_e('Add File', 'blue-raven'); ?>
                        </button>
                    </div>
                </div>
            </div>
        
            <div id="ve-download-file-table" <?php if (!$hasDownloads) echo 'style="display:none;"'; ?>>
                <table class="blue-raven-table">
                    <thead>
                        <tr>
                            <th style="width: 5%;"></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="ve-download-file-table-body">
                        <?php
                        
                        if (!empty($downloads)) {
                            $idCount = 0;
                            foreach ($downloads as $download) {
                                $idCount++;
                                $name = isset($download[1]) ? esc_attr($download[1]) : '';
                                $url = isset($download[2]) ? esc_url($download[2]) : '';
        
                                echo '<tr>
                                    <td>' . esc_html($idCount) . '</td>
                                    <td><input type="text" class="br-input" style="width:100%" name="ve-fileName' . esc_attr($idCount) . '" placeholder="' . esc_attr__('Enter file name', 'blue-raven') . '" value="' . esc_attr($name) . '"></td>
                                    <td><input type="text" class="file-url br-input" style="width:100%" name="ve-fileURL' . esc_attr($idCount) . '" placeholder="' . esc_attr__('Enter file URL', 'blue-raven') . '" value="' . esc_attr($url) . '"></td>
                                    <td><button class="upload-btn enterra-button enterra-secondary-button-color" data-row-index="' . esc_attr($idCount) . '">' . esc_html__('Upload', 'blue-raven') . '</button></td>
                                    <td><button class="remove-download text-button" data-row-index="' . esc_attr($idCount) . '">❌</button></td>
                                </tr>';
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        
            <div class="flex-sb spacing-bottom" style="width: 100%;">
                <div style="margin: 0; width: 49.5%;" >
                    <label class="text-center" for="ve-download-limit">
                        <p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Download Limit', 'blue-raven'); ?></p>
                    </label>
                    <input type="number" name="ve-download-limit" id="ve-download-limit" placeholder="<?php esc_attr_e('Unlimited', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo $downloadExpiry == -1 ? '' : esc_attr($downloadLimit); ?>">
                </div>
                <div style="margin: 0; width: 49.5%;">
                    <label class="text-center" for="ve-download-expiry">
                        <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="ve-download-expiry" id="ve-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 style="text-align: right; margin-top: 3rem;">
            <button class="enterra-button" id="save-variations-edits">
                <?php esc_html_e('Save Variation Edits', 'blue-raven'); ?>
            </button>
        </div>
    </div>    
</div>
<?php
