<?php
/**
 * ExploreXR Addon Settings Page Template
 * 
 * Provides a standardized template for all addon settings pages.
 * This ensures a consistent UI across the plugin and all add-ons.
 */

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

/**
 * Renders a standardized settings page for ExploreXR add-ons
 *
 * @param array $args {
 *     Arguments for the settings page.
 *     
 *     @type string $page_title    The title for the page header
 *     @type string $addon_name    The name of the addon
 *     @type string $addon_version The version of the addon
 *     @type string $doc_url       URL to the addon documentation
 *     @type array  $sections      Array of section definitions
 * }
 */
function explorexr_premium_render_addon_settings_page($args) {
    // Default values
    $defaults = array(
        'page_title'    => '',
        'addon_name'    => '',
        'addon_version' => '',
        'doc_url'       => '',
        'sections'      => array(),
    );
    
    // Parse args
    $settings = wp_parse_args($args, $defaults);
    
    // Verify we have the required fields
    if (empty($settings['page_title']) || empty($settings['sections'])) {
        return;
    }
    
    // Set up header variables
    $page_title = $settings['page_title'];
    $header_actions = '';
    
    // Add documentation button if URL is provided
    if (!empty($settings['doc_url'])) {
        $header_actions .= sprintf(
            '<a href="%s" target="_blank" class="button">
                <span class="dashicons dashicons-book settings-icon"></span> %s
            </a>',
            esc_url($settings['doc_url']),
            esc_html__('Documentation', 'explorexr-premium')
        );
    }
    
    // Include the standard admin header
    include EXPLOREXR_PREMIUM_PLUGIN_DIR . 'admin/templates/admin-header.php';
    ?>
    
    <div class="explorexr-premium-admin-container">       
       
        
        <form method="post" action="options.php">
            <?php 
            // Output security fields if a settings group is specified
            if (!empty($settings['settings_group'])) {
                settings_fields($settings['settings_group']);
            }
            
            // Process sections
            foreach ($settings['sections'] as $section) {
                // Ensure required fields are present
                if (empty($section['title'])) {
                    continue;
                }
                
                // Set card variables
                $card_title = $section['title'];
                $card_icon = !empty($section['icon']) ? $section['icon'] : '';
                
                // Start output buffer for card content
                ob_start();
                
                // Show description if provided
                if (!empty($section['description'])) {
                    echo '<p>' . esc_html($section['description']) . '</p>';
                }
                
                // Display fields for this section
                if (!empty($section['section_id']) && !empty($settings['settings_page'])) {
                    echo '<table class="form-table">';
                    do_settings_fields($settings['settings_page'], $section['section_id']);
                    echo '</table>';
                }
                
                // Add custom content if provided
                if (!empty($section['content'])) {
                    echo wp_kses_post($section['content']);
                }
                
                // Get the buffered content
                $card_content = ob_get_clean();
                
                // Render the card
                include EXPLOREXR_PREMIUM_PLUGIN_DIR . 'admin/templates/card.php';
            }
            
            // Submit button
            if (!empty($settings['show_submit']) && $settings['show_submit']) {
                submit_button();
            }
            ?>
        </form>
    </div>
    <?php
}
