<?php
/**
 * Pro Preset Modals (Save + Load)
 *
 * Included from switchboard.php admin_page() when pro/templates/ exists.
 *
 * @package Switchboard
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Variables expected from parent:
// $presets - array of saved presets
?>

<!-- Save Preset Modal -->
<div id="save-preset-modal" class="switchboard-modal" style="display:none;">
    <div class="modal-content">
        <div class="modal-header">
            <h2>Save Configuration</h2>
            <button class="modal-close">&times;</button>
        </div>
        <form method="post" id="save-preset-form">
            <?php wp_nonce_field('switchboard_preset_save'); ?>
            <div class="modal-body">
                <label>Preset Name</label>
                <input type="text" name="preset_name" required class="regular-text">

                <label>Description (optional)</label>
                <textarea name="preset_description" rows="3" class="large-text"></textarea>
            </div>
            <div class="modal-footer">
                <button type="button" class="sb-module-button sb-module-button-secondary sb-module-button-sm modal-cancel">Cancel</button>
                <button type="submit" name="switchboard_save_preset" class="sb-module-button sb-module-button-primary sb-module-button-sm">Save Preset</button>
            </div>
        </form>
    </div>
</div>

<!-- Load Preset Modal -->
<div id="load-preset-modal" class="switchboard-modal" style="display:none;">
    <div class="modal-content">
        <div class="modal-header">
            <h2>Load Configuration</h2>
            <button class="modal-close">&times;</button>
        </div>
        <div class="modal-body">
            <?php if (empty($presets)): ?>
                <p>No saved presets yet. Save your current configuration to create one!</p>
            <?php else: ?>
                <div class="presets-list">
                    <?php foreach ($presets as $preset_id => $preset):
                        // Handle v2.0 format (modules contains meta and modules keys) vs v1.0 format
                        $modules_data = $preset['modules'];
                        if (isset($modules_data['meta']) && isset($modules_data['modules'])) {
                            $module_count = count($modules_data['modules']);
                        } else {
                            $module_count = count($modules_data);
                        }
                    ?>
                        <div class="preset-item">
                            <div class="preset-info">
                                <h3><?php echo esc_html($preset['name']); ?></h3>
                                <p><?php echo esc_html($preset['description']); ?></p>
                                <small><?php echo $module_count; ?> modules • <?php echo esc_html($preset['created']); ?></small>
                            </div>
                            <div class="preset-actions">
                                <form method="post" style="display:inline;">
                                    <?php wp_nonce_field('switchboard_preset_load'); ?>
                                    <input type="hidden" name="preset_id" value="<?php echo esc_attr($preset_id); ?>">
                                    <button type="submit" name="switchboard_load_preset" class="sb-module-button sb-module-button-primary sb-module-button-sm">Load</button>
                                </form>
                                <button type="button" class="sb-module-button sb-module-button-secondary sb-module-button-sm export-single-preset" data-preset-id="<?php echo esc_attr($preset_id); ?>">Export</button>
                                <form method="post" style="display:inline;">
                                    <?php wp_nonce_field('switchboard_preset_delete'); ?>
                                    <input type="hidden" name="preset_id" value="<?php echo esc_attr($preset_id); ?>">
                                    <button type="submit" name="switchboard_delete_preset" class="sb-module-button sb-module-button-danger sb-module-button-sm" onclick="return confirm('Delete this preset?');">Delete</button>
                                </form>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
        <div class="modal-footer">
            <button type="button" class="sb-module-button sb-module-button-secondary sb-module-button-sm modal-cancel">Close</button>
        </div>
    </div>
</div>
