<?php
/**
 * Advanced Settings Display
 */

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

// Handle form submissions
if (isset($_POST['save_advanced'], $_POST['ccspllms_advanced_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ccspllms_advanced_nonce'])), 'ccspllms_advanced')) {
    update_option('ccspllms_email_notifications', isset($_POST['email_notifications']) ? 1 : 0);
    update_option('ccspllms_notification_email', isset($_POST['notification_email']) ? sanitize_email(wp_unslash($_POST['notification_email'])) : '');
    update_option('ccspllms_debug_logging', isset($_POST['debug_logging']) ? 1 : 0);
    update_option('ccspllms_auto_regenerate_new', isset($_POST['auto_regenerate_new']) ? 1 : 0);
    update_option('ccspllms_auto_regenerate_update', isset($_POST['auto_regenerate_update']) ? 1 : 0);
    
    echo '<div class="ccspllms_alert success">Advanced settings saved!</div>';
}

$email_notifications = get_option('ccspllms_email_notifications', 0);
$notification_email = get_option('ccspllms_notification_email', get_option('admin_email'));
$debug_logging = get_option('ccspllms_debug_logging', 0);
$auto_regenerate_new = get_option('ccspllms_auto_regenerate_new', 1);
$auto_regenerate_update = get_option('ccspllms_auto_regenerate_update', 0);


// check file is writable 
global $wp_filesystem;

// Initialize WP_Filesystem if not already
if ( ! function_exists('request_filesystem_credentials') ) {
    require_once ABSPATH . 'wp-admin/includes/file.php';
}
if ( empty( $wp_filesystem ) ) {
    WP_Filesystem();
}

$status = $wp_filesystem->is_writable( ABSPATH )
    ? '<span class="status-success">Writable</span>'
    : '<span class="status-error">Not Writable</span>';
?>




<div class="ccspllms_container">
    
    <div class="ccspllms_header">
        <div class="ccspllmshead_left">
            <div class="ccspllmshead_logo">
            <img src="<?php echo esc_html( CCSPLLMS_PLUGIN_URL . 'assets/cloudcusplogo.svg' ); ?>" alt="Logo" class="ccspllms_logo">
            </div>
            <div class="ccspllmshead_text"><h1>LLMS.txt  Generator</h1></div>
        </div>
        <div class="ccspllmshead_right">
             <h1><i class="fas fa-cog"></i> Advanced Settings</h1>
                <p>Configure advanced plugin features and debugging</p>
        </div>
        
    </div>

    <div class="ccspllms_content">
        <form method="post">
            <?php wp_nonce_field('ccspllms_advanced', 'ccspllms_advanced_nonce'); ?>
            
            <div class="ccspllms_card">
                <h3>Notifications</h3>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">Email Notifications</th>
                        <td>
                            <label class="ccspllms_toggle">
                                <input type="checkbox" name="email_notifications" <?php checked($email_notifications, 1); ?>>
                                <span class="ccspllms_slider"></span>
                            </label>
                            <p class="description">Send email notifications for file generation and bot detection</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row">Notification Email</th>
                        <td>
                            <input type="email" name="notification_email" value="<?php echo esc_attr($notification_email); ?>" class="regular-text">
                            <p class="description">Email address to receive notifications</p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="ccspllms_card">
                <h3>Auto-Regeneration</h3>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">New Posts</th>
                        <td>
                            <label class="ccspllms_toggle">
                                <input type="checkbox" name="auto_regenerate_new" <?php checked($auto_regenerate_new, 1); ?>>
                                <span class="ccspllms_slider"></span>
                            </label>
                            <p class="description">Automatically regenerate when new posts are published</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row">Post Updates</th>
                        <td>
                            <label class="ccspllms_toggle">
                                <input type="checkbox" name="auto_regenerate_update" <?php checked($auto_regenerate_update, 1); ?>>
                                <span class="ccspllms_slider"></span>
                            </label>
                            <p class="description">Automatically regenerate when posts are updated</p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="ccspllms_card">
                <h3>Debugging</h3>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">Debug Logging</th>
                        <td>
                            <label class="ccspllms_toggle">
                                <input type="checkbox" name="debug_logging" <?php checked($debug_logging, 1); ?>>
                                <span class="ccspllms_slider"></span>
                            </label>
                            <p class="description">Enable detailed logging for troubleshooting</p>
                        </td>
                    </tr>
                </table>
            </div>

            <p class="submit">
                <input type="submit" name="save_advanced" class="ccspllms_button" value="Save Advanced Settings">
            </p>
        </form>

        <div class="ccspllms_card">
            <h3>System Information</h3>
            <table class="ccspllms_table">
                <tr>
                    <td>Plugin Version</td>
                    <td><?php echo esc_html( CCSPLLMS_VERSION ); ?></td>
                </tr>
                <tr>
                    <td>WordPress Version</td>
                    <td><?php echo esc_html( get_bloginfo('version') ); ?></td>
                </tr>
                <tr>
                    <td>PHP Version</td>
                    <td><?php echo esc_html( PHP_VERSION ); ?></td>
                </tr>
                <tr>
                    <td>File Permissions</td>
                    <td><?php echo wp_kses_post( $status ); ?></td>
                </tr>
                <tr>
                    <td>Cron Status</td>
                    <td><?php echo defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ? '<span class="status-error">Disabled</span>' : '<span class="status-success">Enabled</span>'; ?></td>
                </tr>
            </table>
        </div>

        <div class="ccspllms_card">
            <h3>Backup & Restore</h3>
            <p>Backup your plugin settings or restore from a previous backup.</p>
            <button id="backup_settings" class="ccspllms_button">
                <i class="fas fa-download"></i> Backup Settings
            </button>
            <button id="restore_settings" class="ccspllms_button">
                <i class="fas fa-upload"></i> Restore Settings
            </button>
            <input type="file" id="restore_file" style="display: none;" accept=".json">
        </div>
    </div>
</div>