<?php
/**
 * Cleanup Removed Plugins Admin Page Template
 */

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

$settings = get_option('switchboard_cleanup-removed-plugins_settings', []);
$last_scan = isset($settings['last_scan']) ? $settings['last_scan'] : null;
?>

<div class="wrap sb-module-wrap switchboard-cleanup-removed-plugins">
    <!-- Header -->
    <div class="sb-module-header">
        <div class="sb-module-header-left">
            <svg class="sb-module-title-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
            </svg>
            <div>
                <h1 class="sb-module-title">
                    <?php esc_html_e('Cleanup Removed Plugins', 'wpswitchboard'); ?>
                    <span class="switchboard-badge">SWITCHBOARD PRO</span>
                </h1>
                <p class="sb-module-subtitle">
                    <?php esc_html_e('Find and remove database leftovers from uninstalled plugins and themes', 'wpswitchboard'); ?>
                </p>
            </div>
        </div>
        <div class="sb-module-header-right">
            <a href="https://docs.wpswitchboard.com/modules/optimization/cleanup-removed-plugins" target="_blank" class="sb-module-help-link">
                <span class="sb-module-help-link-icon dashicons dashicons-editor-help"></span>
                Help
            </a>
            <button type="button" class="sb-module-button sb-module-button-primary crp-action-btn" id="crp-scan-btn">
                <span class="dashicons dashicons-search"></span>
                <?php esc_html_e('Scan Database', 'wpswitchboard'); ?>
            </button>
        </div>
    </div>

    <!-- Main Content -->
    <div class="sb-module-container">
        <!-- Stats Cards -->
        <div class="sb-module-stats">
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Orphaned Tables', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-tables-count">--</div>
                    <div class="sb-module-stat-sub" id="stat-tables-size"></div>
                </div>
            </div>
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Orphaned Options', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-options-count">--</div>
                    <div class="sb-module-stat-sub" id="stat-options-size"></div>
                </div>
            </div>
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Zombie Cron Jobs', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-crons-count">--</div>
                </div>
            </div>
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Last Scan', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-last-scan">
                        <?php echo $last_scan ? esc_html($last_scan) : '--'; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Info Boxes Row -->
        <div class="crp-info-row">
            <!-- Backup Warning Box -->
            <div class="crp-info-box crp-info-box--warning">
                <div class="crp-info-icon">
                    <span class="dashicons dashicons-warning"></span>
                </div>
                <div class="crp-info-content">
                    <strong><?php esc_html_e('Important: Backup First!', 'wpswitchboard'); ?></strong>
                    <p><?php esc_html_e('Always create a full database backup before deleting any items. Deleted data cannot be recovered. If you\'re unsure whether something is safe to delete, use the "Ignore" option instead.', 'wpswitchboard'); ?></p>
                </div>
            </div>

            <!-- Info Box -->
            <div class="crp-info-box">
                <div class="crp-info-icon">
                    <span class="dashicons dashicons-info"></span>
                </div>
                <div class="crp-info-content">
                    <strong><?php esc_html_e('How This Works', 'wpswitchboard'); ?></strong>
                    <p><?php esc_html_e('When you uninstall a plugin, it often leaves behind database tables, options, and scheduled tasks. This tool scans your database for these orphaned remnants and helps you safely remove them. Items marked "(Unknown)" can be linked to active plugins via the dropdown, or deleted if they\'re truly orphaned.', 'wpswitchboard'); ?></p>
                </div>
            </div>
        </div>

        <!-- Tab Navigation -->
        <div class="crp-tabs">
            <button type="button" class="crp-tab active" data-tab="tables">
                <span class="dashicons dashicons-editor-table"></span>
                <?php esc_html_e('Orphaned Tables', 'wpswitchboard'); ?>
                <span class="crp-tab-count" id="tab-tables-count">0</span>
            </button>
            <button type="button" class="crp-tab" data-tab="options">
                <span class="dashicons dashicons-admin-settings"></span>
                <?php esc_html_e('Orphaned Options', 'wpswitchboard'); ?>
                <span class="crp-tab-count" id="tab-options-count">0</span>
            </button>
            <button type="button" class="crp-tab" data-tab="crons">
                <span class="dashicons dashicons-clock"></span>
                <?php esc_html_e('Zombie Crons', 'wpswitchboard'); ?>
                <span class="crp-tab-count" id="tab-crons-count">0</span>
            </button>
        </div>

        <!-- Tab Content: Tables -->
        <div class="crp-tab-content active" id="tab-tables">
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <div class="sb-module-card-header-left">
                        <h2 class="sb-module-card-title"><?php esc_html_e('Orphaned Database Tables', 'wpswitchboard'); ?></h2>
                        <p class="sb-module-card-desc"><?php esc_html_e('These tables appear to belong to plugins that are no longer active. Review carefully before deleting.', 'wpswitchboard'); ?></p>
                    </div>
                    <div class="crp-filters">
                        <select id="tables-source-filter" class="crp-source-filter">
                            <option value=""><?php esc_html_e('All Sources', 'wpswitchboard'); ?></option>
                            <option value="__none__"><?php esc_html_e('No Source', 'wpswitchboard'); ?></option>
                        </select>
                        <input type="text" id="tables-search" placeholder="<?php esc_attr_e('Filter tables...', 'wpswitchboard'); ?>" class="regular-text">
                    </div>
                </div>
                <div class="sb-module-card-body">
                    <div class="crp-bulk-actions" id="tables-bulk-actions" style="display: none;">
                        <label class="crp-select-all">
                            <input type="checkbox" id="tables-select-all">
                            <span><?php esc_html_e('Select All', 'wpswitchboard'); ?></span>
                        </label>
                        <button type="button" class="button button-primary" id="delete-selected-tables">
                            <span class="dashicons dashicons-trash"></span>
                            <?php esc_html_e('Delete Selected', 'wpswitchboard'); ?>
                        </button>
                    </div>
                    <div class="sb-module-table-wrapper">
                        <table class="sb-module-table" id="tables-list">
                            <thead>
                                <tr>
                                    <th class="check-column"><input type="checkbox" id="tables-check-all" style="display:none;"></th>
                                    <th class="sortable" data-sort="name"><?php esc_html_e('Table Name', 'wpswitchboard'); ?></th>
                                    <th><?php esc_html_e('Source', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="rows"><?php esc_html_e('Rows', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="size"><?php esc_html_e('Size', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="risk"><?php esc_html_e('Risk', 'wpswitchboard'); ?></th>
                                    <th><?php esc_html_e('Actions', 'wpswitchboard'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="empty-row">
                                    <td colspan="7" class="empty-cell">
                                        <div class="empty-state">
                                            <span class="dashicons dashicons-search"></span>
                                            <p><?php esc_html_e('Click "Scan Database" to find orphaned tables', 'wpswitchboard'); ?></p>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Tab Content: Options -->
        <div class="crp-tab-content" id="tab-options">
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <div class="sb-module-card-header-left">
                        <h2 class="sb-module-card-title"><?php esc_html_e('Orphaned Options', 'wpswitchboard'); ?></h2>
                        <p class="sb-module-card-desc"><?php esc_html_e('These wp_options entries appear to belong to plugins that are no longer active.', 'wpswitchboard'); ?></p>
                    </div>
                    <div class="crp-filters">
                        <select id="options-source-filter" class="crp-source-filter">
                            <option value=""><?php esc_html_e('All Sources', 'wpswitchboard'); ?></option>
                            <option value="__none__"><?php esc_html_e('No Source', 'wpswitchboard'); ?></option>
                        </select>
                        <input type="text" id="options-search" placeholder="<?php esc_attr_e('Filter options...', 'wpswitchboard'); ?>" class="regular-text">
                    </div>
                </div>
                <div class="sb-module-card-body">
                    <div class="crp-bulk-actions" id="options-bulk-actions" style="display: none;">
                        <label class="crp-select-all">
                            <input type="checkbox" id="options-select-all">
                            <span><?php esc_html_e('Select All', 'wpswitchboard'); ?></span>
                        </label>
                        <button type="button" class="button button-primary" id="delete-selected-options">
                            <span class="dashicons dashicons-trash"></span>
                            <?php esc_html_e('Delete Selected', 'wpswitchboard'); ?>
                        </button>
                    </div>
                    <div class="sb-module-table-wrapper">
                        <table class="sb-module-table" id="options-list">
                            <thead>
                                <tr>
                                    <th class="check-column"><input type="checkbox" id="options-check-all" style="display:none;"></th>
                                    <th class="sortable" data-sort="name"><?php esc_html_e('Option Name', 'wpswitchboard'); ?></th>
                                    <th><?php esc_html_e('Source', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="size"><?php esc_html_e('Size', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="autoload"><?php esc_html_e('Autoload', 'wpswitchboard'); ?></th>
                                    <th><?php esc_html_e('Actions', 'wpswitchboard'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="empty-row">
                                    <td colspan="6" class="empty-cell">
                                        <div class="empty-state">
                                            <span class="dashicons dashicons-search"></span>
                                            <p><?php esc_html_e('Click "Scan Database" to find orphaned options', 'wpswitchboard'); ?></p>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Tab Content: Crons -->
        <div class="crp-tab-content" id="tab-crons">
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <h2 class="sb-module-card-title"><?php esc_html_e('Zombie Cron Jobs', 'wpswitchboard'); ?></h2>
                    <p class="sb-module-card-desc"><?php esc_html_e('These scheduled tasks appear to belong to plugins that are no longer active, or have no registered callback.', 'wpswitchboard'); ?></p>
                </div>
                <div class="sb-module-card-body">
                    <div class="crp-bulk-actions" id="crons-bulk-actions" style="display: none;">
                        <label class="crp-select-all">
                            <input type="checkbox" id="crons-select-all">
                            <span><?php esc_html_e('Select All', 'wpswitchboard'); ?></span>
                        </label>
                        <button type="button" class="button button-primary" id="delete-selected-crons">
                            <span class="dashicons dashicons-trash"></span>
                            <?php esc_html_e('Delete Selected', 'wpswitchboard'); ?>
                        </button>
                    </div>
                    <div class="sb-module-table-wrapper">
                        <table class="sb-module-table" id="crons-list">
                            <thead>
                                <tr>
                                    <th class="check-column"><input type="checkbox" id="crons-check-all" style="display:none;"></th>
                                    <th class="sortable" data-sort="name"><?php esc_html_e('Hook Name', 'wpswitchboard'); ?></th>
                                    <th><?php esc_html_e('Source', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="schedule"><?php esc_html_e('Schedule', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="next_run"><?php esc_html_e('Next Run', 'wpswitchboard'); ?></th>
                                    <th class="sortable" data-sort="status"><?php esc_html_e('Status', 'wpswitchboard'); ?></th>
                                    <th><?php esc_html_e('Actions', 'wpswitchboard'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="empty-row">
                                    <td colspan="7" class="empty-cell">
                                        <div class="empty-state">
                                            <span class="dashicons dashicons-search"></span>
                                            <p><?php esc_html_e('Click "Scan Database" to find zombie cron jobs', 'wpswitchboard'); ?></p>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- /.sb-module-container -->
</div>

<!-- Option Value Preview Modal -->
<div id="crp-preview-modal" class="crp-modal" style="display: none;">
    <div class="crp-modal-backdrop"></div>
    <div class="crp-modal-content">
        <div class="crp-modal-header">
            <h3><?php esc_html_e('Option Value Preview', 'wpswitchboard'); ?></h3>
            <button type="button" class="crp-modal-close">&times;</button>
        </div>
        <div class="crp-modal-body">
            <div class="crp-preview-info">
                <strong><?php esc_html_e('Option:', 'wpswitchboard'); ?></strong>
                <code id="preview-option-name"></code>
            </div>
            <div class="crp-preview-value">
                <pre id="preview-option-value"></pre>
            </div>
        </div>
    </div>
</div>

<!-- Confirmation Modal -->
<div id="crp-confirm-modal" class="crp-modal" style="display: none;">
    <div class="crp-modal-backdrop"></div>
    <div class="crp-modal-content crp-confirm-modal-content">
        <div class="crp-modal-header">
            <h3 id="crp-confirm-title"><?php esc_html_e('Confirm Delete', 'wpswitchboard'); ?></h3>
            <button type="button" class="crp-modal-close">&times;</button>
        </div>
        <div class="crp-modal-body">
            <div class="crp-confirm-icon">
                <span class="dashicons dashicons-warning"></span>
            </div>
            <p id="crp-confirm-message" class="crp-confirm-text"></p>
            <p class="crp-confirm-warning"><?php esc_html_e('This action cannot be undone.', 'wpswitchboard'); ?></p>
        </div>
        <div class="crp-modal-footer">
            <button type="button" class="button crp-confirm-cancel"><?php esc_html_e('Cancel', 'wpswitchboard'); ?></button>
            <button type="button" class="button button-danger crp-confirm-delete" id="crp-confirm-btn">
                <span class="dashicons dashicons-trash"></span>
                <?php esc_html_e('Delete', 'wpswitchboard'); ?>
            </button>
        </div>
    </div>
</div>

<!-- Toast Notification -->
<div id="crp-toast" class="crp-toast">
    <span class="toast-icon"></span>
    <span class="toast-message"></span>
</div>
