<?php defined('ABSPATH') || exit;

$iv1wpt_has_pro = iv1wpt_has_pro();

$tools_sub = sanitize_text_field($_GET['sub'] ?? 'errorlog');
if (!in_array($tools_sub, ['errorlog', 'debug', 'cron', 'options'], true)) {
    $tools_sub = 'errorlog';
}

$sub_tabs = [
    'errorlog' => __('Error Log', 'iv1-wp-tools'),
    'debug'    => __('Debug Settings', 'iv1-wp-tools'),
    'cron'     => __('Cron Jobs', 'iv1-wp-tools'),
    'options'  => __('Options Cleanup', 'iv1-wp-tools'),
];
?>

<div id="iv1wpt-tools-result" style="margin:15px 0;"></div>

<div style="margin-bottom:0px; border-bottom:1px solid #ddd; padding-bottom:0;">
    <?php foreach ($sub_tabs as $key => $label) : ?>
        <a href="?page=iv1-wp-tools&tab=tools&sub=<?php echo esc_attr($key); ?>"
           style="display:inline-block; padding:8px 16px; margin-right:4px; margin-bottom:-1px; font-weight:<?php echo $tools_sub === $key ? '600' : '400'; ?>; border:1px solid <?php echo $tools_sub === $key ? '#ddd' : 'transparent'; ?>; border-bottom-color:<?php echo $tools_sub === $key ? '#fff' : 'transparent'; ?>; background:<?php echo $tools_sub === $key ? '#fff' : 'transparent'; ?>; color:#1d2327; text-decoration:none; border-radius:3px 3px 0 0;">
            <?php echo (in_array($key, ['debug', 'cron', 'options'], true) && !$iv1wpt_has_pro) ? '🔒 ' : ''; echo esc_html($label); ?>
        </a>
    <?php endforeach; ?>
</div>

<div style="background:#fff; border:1px solid #ddd; border-top:none; padding:20px; border-radius:0 0 4px 4px;">

<?php if ($tools_sub === 'errorlog') : ?>

<!-- ══════════════════════════════════════════════ -->
<!-- ERROR LOG                                      -->
<!-- ══════════════════════════════════════════════ -->
<h2 style="margin-top:0;"><?php esc_html_e('Error Log Viewer', 'iv1-wp-tools'); ?></h2>
<p class="description"><?php esc_html_e('Shows the WordPress debug.log file. WP_DEBUG and WP_DEBUG_LOG must be enabled in wp-config.php.', 'iv1-wp-tools'); ?></p>

<div style="display:flex; gap:10px; align-items:center; flex-wrap:wrap; margin:15px 0;">
    <label>
        <?php esc_html_e('Lines:', 'iv1-wp-tools'); ?>
        <select id="iv1wpt-log-lines" style="margin-left:4px;">
            <option value="50">50</option>
            <option value="100">100</option>
            <option value="200" selected>200</option>
            <option value="500">500</option>
            <option value="1000">1000</option>
        </select>
    </label>
    <label>
        <?php esc_html_e('Filter:', 'iv1-wp-tools'); ?>
        <input type="text" id="iv1wpt-log-filter" placeholder="<?php esc_attr_e('e.g. Fatal, Warning, plugin-name', 'iv1-wp-tools'); ?>" style="margin-left:4px; width:220px;">
    </label>
    <button type="button" id="iv1wpt-log-load" class="button button-primary"><?php esc_html_e('Load log', 'iv1-wp-tools'); ?></button>
    <?php if ($iv1wpt_has_pro) : ?>
    <button type="button" id="iv1wpt-log-clear" class="button" style="color:#b32d2e;"><?php esc_html_e('Clear log', 'iv1-wp-tools'); ?></button>
    <?php else : ?>
    <button type="button" id="iv1wpt-log-clear" class="button" style="color:#b32d2e;" disabled><?php esc_html_e('Clear log', 'iv1-wp-tools'); ?></button>
    <?php endif; ?>
</div>

<div id="iv1wpt-log-meta" style="font-size:12px; color:#666; margin-bottom:8px;"></div>

<div id="iv1wpt-log-output" style="
    background:#1e1e1e; color:#d4d4d4;
    font-family:monospace; font-size:12px; line-height:1.6;
    padding:14px; border-radius:4px;
    max-height:500px; overflow-y:auto;
    white-space:pre-wrap; word-break:break-all;
    display:none;
"></div>

<?php elseif ($tools_sub === 'cron') : ?>

<!-- ══════════════════════════════════════════════ -->
<!-- CRON JOBS                                      -->
<!-- ══════════════════════════════════════════════ -->
<h2 style="margin-top:0;"><?php esc_html_e('Scheduled Tasks (WP-Cron)', 'iv1-wp-tools'); ?></h2>
<p class="description"><?php esc_html_e('All scheduled WordPress Cron events. WordPress Core events are protected and cannot be deleted.', 'iv1-wp-tools'); ?></p>

<p>
    <button type="button" id="iv1wpt-cron-load" class="button button-primary"><?php esc_html_e('Load Cron events', 'iv1-wp-tools'); ?></button>
</p>

<div id="iv1wpt-cron-meta" style="font-size:12px; color:#666; margin-bottom:8px;"></div>
<div id="iv1wpt-cron-list"></div>

<?php elseif ($tools_sub === 'options') : ?>

<!-- ══════════════════════════════════════════════ -->
<!-- OPTIONS CLEANUP                                -->
<!-- ══════════════════════════════════════════════ -->
<h2 style="margin-top:0;"><?php esc_html_e('Options Cleanup', 'iv1-wp-tools'); ?></h2>
<p class="description"><?php esc_html_e('Analyzes wp_options for large autoloaded entries and unknown options. Autoloaded options are loaded on every page load and can slow down your site.', 'iv1-wp-tools'); ?></p>

<div class="notice notice-warning inline" style="margin:12px 0; padding:10px 14px; border-left-color:#d63638;">
    <p><strong>⚠️ <?php esc_html_e('Caution:', 'iv1-wp-tools'); ?></strong>
    <?php esc_html_e('Deleting or changing options can break plugins and themes. Only delete options you are sure are no longer needed. Always create a database backup first.', 'iv1-wp-tools'); ?></p>
</div>

<p>
    <button type="button" id="iv1wpt-options-scan" class="button button-primary"><?php esc_html_e('Scan options', 'iv1-wp-tools'); ?></button>
</p>

<div id="iv1wpt-options-meta" style="font-size:12px; color:#666; margin:8px 0;"></div>
<div id="iv1wpt-options-list"></div>

<?php elseif ($tools_sub === 'debug') : ?>

<!-- ══════════════════════════════════════════════ -->
<!-- DEBUG SETTINGS                                 -->
<!-- ══════════════════════════════════════════════ -->
<h2 style="margin-top:0;"><?php esc_html_e('Debug Settings', 'iv1-wp-tools'); ?></h2>
<p class="description"><?php esc_html_e('Manage WP_DEBUG and WP_DEBUG_LOG constants. Changes to wp-config.php take effect after the next page load.', 'iv1-wp-tools'); ?></p>

<div id="iv1wpt-debug-status" style="margin:15px 0;"></div>

<table class="form-table" style="max-width:700px;">
    <tr>
        <th><?php esc_html_e('Current status', 'iv1-wp-tools'); ?></th>
        <td>
            <div id="iv1wpt-debug-current" style="font-size:13px; color:#666;"><?php esc_html_e('Loading...', 'iv1-wp-tools'); ?></div>
        </td>
    </tr>
    <tr>
        <th><?php esc_html_e('Method', 'iv1-wp-tools'); ?></th>
        <td>
            <label style="display:block; margin-bottom:6px;">
                <input type="radio" name="iv1wpt_debug_method" value="config" checked>
                <?php esc_html_e('Edit wp-config.php directly (recommended)', 'iv1-wp-tools'); ?>
            </label>
            <label style="display:block;">
                <input type="radio" name="iv1wpt_debug_method" value="muplugin">
                <?php esc_html_e('Create MU-Plugin (fallback if wp-config.php is not writable)', 'iv1-wp-tools'); ?>
            </label>
            <p class="description" style="margin-top:6px;">
                <?php esc_html_e('The MU-Plugin method only activates WP_DEBUG_LOG. WP_DEBUG must still be set in wp-config.php for PHP errors to be logged.', 'iv1-wp-tools'); ?>
            </p>
        </td>
    </tr>
</table>

<div class="notice notice-warning inline" style="margin:12px 0; padding:10px 14px; border-left-color:#d63638;">
    <p><strong>⚠️ <?php esc_html_e('Caution:', 'iv1-wp-tools'); ?></strong>
    <?php esc_html_e('Never leave WP_DEBUG enabled on production sites. Debug output can expose sensitive information. Disable after troubleshooting.', 'iv1-wp-tools'); ?></p>
</div>

<p style="display:flex; gap:10px; flex-wrap:wrap;">
    <?php if ($iv1wpt_has_pro) : ?>
    <button type="button" id="iv1wpt-debug-enable" class="button button-primary"><?php esc_html_e('Enable debug logging', 'iv1-wp-tools'); ?></button>
    <button type="button" id="iv1wpt-debug-disable" class="button" style="color:#b32d2e;"><?php esc_html_e('Disable debug logging', 'iv1-wp-tools'); ?></button>
    <button type="button" id="iv1wpt-debug-refresh" class="button"><?php esc_html_e('Refresh status', 'iv1-wp-tools'); ?></button>
    <?php else : ?>
    <button type="button" id="iv1wpt-debug-enable" class="button button-primary" disabled><?php esc_html_e('Enable debug logging', 'iv1-wp-tools'); ?></button>
    <button type="button" id="iv1wpt-debug-disable" class="button" style="color:#b32d2e;" disabled><?php esc_html_e('Disable debug logging', 'iv1-wp-tools'); ?></button>
    <button type="button" id="iv1wpt-debug-refresh" class="button"><?php esc_html_e('Refresh status', 'iv1-wp-tools'); ?></button>
    <?php endif; ?>
</p>

<div id="iv1wpt-debug-snippet" style="display:none; margin-top:16px;">
    <p><strong><?php esc_html_e('Manual snippet for wp-config.php:', 'iv1-wp-tools'); ?></strong></p>
    <pre style="background:#1e1e1e; color:#d4d4d4; padding:12px; border-radius:4px; font-size:12px; overflow-x:auto;">define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);</pre>
    <p class="description"><?php esc_html_e('Add these lines above &quot;That\'s all, stop editing!&quot; in your wp-config.php.', 'iv1-wp-tools'); ?></p>
</div>

<?php endif; ?>

</div>
