<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : import-export.php
 * @location     : /modules/settings/import-export.php
 * @type         : Admin Page
 * @scope        : Module Only
 * @allowed      : Data import/export, backup operations, data transfer
 * @reusability  : Single Use
 * @notes        : Import/export settings page for settings module
 */

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

// Start output buffering
ob_start();

// Get current import/export settings
$import_export_settings = get_option('igny8_import_export_settings', [
    'default_format' => 'csv',
    'overwrite_existing' => false,
    'include_metrics' => true,
    'file_naming_pattern' => 'igny8_export_[date].csv'
]);

// Get recent import/export logs
$recent_logs = get_option('igny8_import_export_logs', []);
$recent_logs = array_slice($recent_logs, 0, 10); // Last 10 entries

// Script localization will be done in the JavaScript section below
?>
<div class="igny8-import-export-page">
    <div class="igny8-container">
        
        
        <!-- Import Data Section -->
        <div class="igny8-card igny8-mb-20">
            <div class="igny8-card-header igny8-standard-header">
                <div class="igny8-card-header-content">
                    <div class="igny8-card-title-text">
                        <h3>Import Data</h3>
                        <div class="igny8-card-subtitle">Download CSV templates and import your data into the Planner module</div>
                    </div>
                    <div class="igny8-card-icon">
                        <span class="dashicons dashicons-upload igny8-dashboard-icon-sm"></span>
                    </div>
                </div>
            </div>
            <div class="igny8-card-body">
                
                <!-- Template Downloads -->
                <div class="igny8-mb-20">
                    <h4>Download Templates</h4>
                    <div class="igny8-grid-2 igny8-gap-10">
                        <a href="<?php echo admin_url('admin-ajax.php?action=igny8_download_template&template_type=keywords&nonce=' . wp_create_nonce('igny8_import_export_nonce')); ?>" class="igny8-btn igny8-btn-outline">
                            <span class="dashicons dashicons-download"></span> Keywords Template
                        </a>
                        <a href="<?php echo admin_url('admin-ajax.php?action=igny8_download_template&template_type=clusters&nonce=' . wp_create_nonce('igny8_import_export_nonce')); ?>" class="igny8-btn igny8-btn-outline">
                            <span class="dashicons dashicons-download"></span> Clusters Template
                        </a>
                        <a href="<?php echo admin_url('admin-ajax.php?action=igny8_download_template&template_type=ideas&nonce=' . wp_create_nonce('igny8_import_export_nonce')); ?>" class="igny8-btn igny8-btn-outline">
                            <span class="dashicons dashicons-download"></span> Ideas Template
                        </a>
                    </div>
                </div>
                
                <!-- Import Form -->
                <form id="igny8-import-form" enctype="multipart/form-data" action="<?php echo admin_url('admin-ajax.php'); ?>" method="post">
                    <input type="hidden" name="action" value="igny8_run_import">
                    <input type="hidden" name="nonce" value="<?php echo wp_create_nonce('igny8_import_export_nonce'); ?>">
                    <div class="igny8-form-group">
                        <label for="import-file">Select CSV File</label>
                        <input type="file" id="import-file" name="import_file" accept=".csv" required>
                        <p class="description">Upload a CSV file with your data. Use the templates above for proper format.</p>
                    </div>
                    
                    <div class="igny8-form-group">
                        <label for="import-type">Import Type</label>
                        <select id="import-type" name="import_type" required>
                            <option value="">Select import type...</option>
                            <option value="keywords">Keywords</option>
                            <option value="clusters">Clusters</option>
                            <option value="ideas">Content Ideas</option>
                        </select>
                    </div>
                    
                    
                    <div class="igny8-form-actions">
                        <button type="submit" class="igny8-btn igny8-btn-success">
                            <span class="dashicons dashicons-upload"></span> Run Import
                        </button>
                    </div>
                </form>
                
                <!-- Import Results -->
                <div id="import-results" class="igny8-mt-20" style="display: none;"></div>
            </div>
        </div>
        
        <!-- Export Data Section -->
        <div class="igny8-card igny8-mb-20">
            <div class="igny8-card-header igny8-standard-header">
                <div class="igny8-card-header-content">
                    <div class="igny8-card-title-text">
                        <h3>Export Data</h3>
                        <div class="igny8-card-subtitle">Export your data in various formats for backup and migration</div>
                    </div>
                    <div class="igny8-card-icon">
                        <span class="dashicons dashicons-download igny8-dashboard-icon-sm"></span>
                    </div>
                </div>
            </div>
            <div class="igny8-card-body">
                
                <form id="igny8-export-form" action="<?php echo admin_url('admin-ajax.php'); ?>" method="post">
                    <input type="hidden" name="action" value="igny8_run_export">
                    <input type="hidden" name="nonce" value="<?php echo wp_create_nonce('igny8_import_export_nonce'); ?>">
                    <div class="igny8-form-group">
                        <label for="export-type">Export Type</label>
                        <select id="export-type" name="export_type" required>
                            <option value="">Select export type...</option>
                            <option value="keywords">Keywords</option>
                            <option value="clusters">Clusters</option>
                            <option value="ideas">Content Ideas</option>
                        </select>
                    </div>
                    
                    <div class="igny8-form-group">
                        <h4>Export Options</h4>
                        <div class="igny8-checkbox-group">
                            <label class="igny8-checkbox-label">
                                <input type="checkbox" id="include-metrics" name="include_metrics" <?php checked($import_export_settings['include_metrics'], true); ?>>
                                <span class="igny8-checkbox-text">Include Metrics</span>
                            </label>
                            <label class="igny8-checkbox-label">
                                <input type="checkbox" id="include-relationships" name="include_relationships">
                                <span class="igny8-checkbox-text">Include Relationships</span>
                            </label>
                            <label class="igny8-checkbox-label">
                                <input type="checkbox" id="include-timestamps" name="include_timestamps">
                                <span class="igny8-checkbox-text">Include Timestamps</span>
                            </label>
                        </div>
                    </div>
                    
                    <div class="igny8-form-actions">
                        <button type="submit" class="igny8-btn igny8-btn-primary">
                            <span class="dashicons dashicons-download"></span> Export CSV
                        </button>
                    </div>
                </form>
                
                <!-- Export Results -->
                <div id="export-results" class="igny8-mt-20" style="display: none;"></div>
            </div>
        </div>
        
        <!-- Settings & Logging Section -->
        <div class="igny8-card">
            <div class="igny8-card-header igny8-standard-header">
                <div class="igny8-card-header-content">
                    <div class="igny8-card-title-text">
                        <h3>Import / Export Preferences</h3>
                        <div class="igny8-card-subtitle">Configure import/export settings and view operation logs</div>
                    </div>
                    <div class="igny8-card-icon">
                        <span class="dashicons dashicons-admin-generic igny8-dashboard-icon-sm"></span>
                    </div>
                </div>
            </div>
            <div class="igny8-card-body">
                <form id="igny8-settings-form" action="<?php echo admin_url('admin-ajax.php'); ?>" method="post">
                    <input type="hidden" name="action" value="igny8_save_import_export_settings">
                    <input type="hidden" name="nonce" value="<?php echo wp_create_nonce('igny8_import_export_nonce'); ?>">
                    <div class="igny8-grid-2 igny8-gap-20">
                        <div class="igny8-form-group">
                            <label for="default-format">Default Format</label>
                            <select id="default-format" name="default_format">
                                <option value="csv" <?php selected($import_export_settings['default_format'], 'csv'); ?>>CSV</option>
                                <option value="json" <?php selected($import_export_settings['default_format'], 'json'); ?>>JSON</option>
                            </select>
                        </div>
                        
                        <div class="igny8-form-group">
                            <label for="file-naming">File Naming Pattern</label>
                            <input type="text" id="file-naming" name="file_naming_pattern" value="<?php echo esc_attr($import_export_settings['file_naming_pattern']); ?>" placeholder="igny8_export_[date].csv">
                            <p class="description">Use [date], [type], [time] placeholders</p>
                        </div>
                    </div>
                    
                    <div class="igny8-form-group">
                        <h4>Default Options</h4>
                        <div class="igny8-checkbox-group">
                            <label class="igny8-checkbox-label">
                                <input type="checkbox" id="overwrite-existing" name="overwrite_existing" <?php checked($import_export_settings['overwrite_existing'], true); ?>>
                                <span class="igny8-checkbox-text">Overwrite Existing Records</span>
                            </label>
                            <label class="igny8-checkbox-label">
                                <input type="checkbox" id="include-metrics-default" name="include_metrics" <?php checked($import_export_settings['include_metrics'], true); ?>>
                                <span class="igny8-checkbox-text">Include Metrics in Export by Default</span>
                            </label>
                        </div>
                    </div>
                    
                    <div class="igny8-form-actions">
                        <button type="submit" class="igny8-btn igny8-btn-success">Save Preferences</button>
                    </div>
                </form>
                
                <!-- Recent Logs -->
                <?php if (!empty($recent_logs)): ?>
                <div class="igny8-mt-30">
                    <h4>Recent Import/Export Activity</h4>
                    <div class="igny8-logs-container" style="max-height: 300px; overflow-y: auto; border: 1px solid #ddd; border-radius: 4px; padding: 10px; background: #f9f9f9;">
                        <?php foreach ($recent_logs as $log): ?>
                        <div class="igny8-log-entry" style="border-bottom: 1px solid #eee; padding: 8px 0; font-size: 12px;">
                            <div style="display: flex; justify-content: space-between; align-items: center;">
                                <span style="font-weight: bold; color: <?php echo $log['status'] === 'success' ? 'green' : 'red'; ?>;">
                                    <?php echo $log['status'] === 'success' ? '✓' : '✗'; ?>
                                    <?php echo esc_html($log['operation']); ?>
                                </span>
                                <span style="color: #666;"><?php echo esc_html($log['timestamp']); ?></span>
                            </div>
                            <div style="color: #666; margin-top: 2px;">
                                <?php echo esc_html($log['message']); ?>
                                <?php if (isset($log['details'])): ?>
                                    <br><small><?php echo esc_html($log['details']); ?></small>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        
    </div>
</div>

<script>
// Localize script variables
window.IGNY8_IMPORT_EXPORT = {
    ajaxUrl: '<?php echo admin_url('admin-ajax.php'); ?>',
    nonce: '<?php echo wp_create_nonce('igny8_import_export_nonce'); ?>',
    settings: <?php echo json_encode($import_export_settings); ?>
};

document.addEventListener('DOMContentLoaded', function() {
    console.log('Import/Export page loaded');
    console.log('IGNY8_IMPORT_EXPORT:', window.IGNY8_IMPORT_EXPORT);
    
    // Initialize Import/Export functionality
    if (typeof window.initializeImportExport === 'function') {
        console.log('Initializing Import/Export functionality');
        window.initializeImportExport();
    } else {
        console.error('initializeImportExport function not found');
    }
});
</script>

