<?php defined('ABSPATH') || exit; ?>
<h2><span class="dashicons dashicons-database-remove"></span>&nbsp;<?php esc_html_e('Database Cleanup', 'iv1-wp-tools'); ?></h2>
<p class="description"><?php esc_html_e('Scan your database for unnecessary entries and delete them. Recommendation: Create a database backup first.', 'iv1-wp-tools'); ?></p>
<div id="iv1wpt-cleanup-result" style="margin:15px 0;"></div>
<p><button type="button" id="iv1wpt-scan-all" class="button button-primary"><?php esc_html_e('Scan all', 'iv1-wp-tools'); ?></button> <button type="button" id="iv1wpt-clean-all" class="button button-secondary" style="color:#b32d2e;" disabled><?php esc_html_e('Clean all', 'iv1-wp-tools'); ?></button></p>
<table class="widefat striped" id="iv1wpt-cleanup-table" style="margin-top:15px;">
<thead><tr><th style="width:30px;"><input type="checkbox" id="iv1wpt-check-all" checked></th><th><?php esc_html_e('Category', 'iv1-wp-tools'); ?></th><th style="width:100px;text-align:right;"><?php esc_html_e('Count', 'iv1-wp-tools'); ?></th><th style="width:100px;text-align:right;"><?php esc_html_e('Size', 'iv1-wp-tools'); ?></th><th style="width:180px;text-align:right;"><?php esc_html_e('Action', 'iv1-wp-tools'); ?></th></tr></thead>
<tbody><?php
$cleanup_items = [
    ['group'=>__('Posts','iv1-wp-tools'),'key'=>'revisions','label'=>__('Revisions','iv1-wp-tools')],
    ['group'=>__('Posts','iv1-wp-tools'),'key'=>'auto_drafts','label'=>__('Auto drafts','iv1-wp-tools')],
    ['group'=>__('Posts','iv1-wp-tools'),'key'=>'trashed_posts','label'=>__('Trashed posts','iv1-wp-tools')],
    ['group'=>__('Comments','iv1-wp-tools'),'key'=>'spam_comments','label'=>__('Spam comments','iv1-wp-tools')],
    ['group'=>__('Comments','iv1-wp-tools'),'key'=>'trashed_comments','label'=>__('Trashed comments','iv1-wp-tools')],
    ['group'=>__('Comments','iv1-wp-tools'),'key'=>'pingbacks','label'=>__('Pingbacks & Trackbacks','iv1-wp-tools')],
    ['group'=>__('Meta / Relationships','iv1-wp-tools'),'key'=>'orphan_postmeta','label'=>__('Orphaned post meta','iv1-wp-tools')],
    ['group'=>__('Meta / Relationships','iv1-wp-tools'),'key'=>'orphan_commentmeta','label'=>__('Orphaned comment meta','iv1-wp-tools')],
    ['group'=>__('Meta / Relationships','iv1-wp-tools'),'key'=>'orphan_usermeta','label'=>__('Orphaned user meta','iv1-wp-tools')],
    ['group'=>__('Meta / Relationships','iv1-wp-tools'),'key'=>'orphan_termmeta','label'=>__('Orphaned term meta','iv1-wp-tools')],
    ['group'=>__('Meta / Relationships','iv1-wp-tools'),'key'=>'orphan_relationships','label'=>__('Orphaned relationships','iv1-wp-tools')],
    ['group'=>__('Transients','iv1-wp-tools'),'key'=>'expired_transients','label'=>__('Expired transients','iv1-wp-tools')],
    ['group'=>__('Database','iv1-wp-tools'),'key'=>'optimize_tables','label'=>__('Optimize tables','iv1-wp-tools')],
    ['group'=>__('Database','iv1-wp-tools'),'key'=>'repair_tables','label'=>__('Repair tables','iv1-wp-tools')],
];
$last_group='';
foreach($cleanup_items as $item):
    if($item['group']!==$last_group): $last_group=$item['group'];
?><tr class="iv1wpt-group-row"><td></td><td colspan="4"><strong><?php echo esc_html($item['group']); ?></strong></td></tr>
<?php endif; ?>

<tr data-key="<?php echo esc_attr($item['key']); ?>">
    <td><input type="checkbox" class="iv1wpt-clean-check" checked></td>

    <td><?php echo esc_html($item['label']); ?></td>

    <td style="text-align:right;">
        <span class="iv1wpt-clean-count" style="color:#999;">—</span>
    </td>

    <td style="text-align:right;">
        <span class="iv1wpt-clean-size" style="color:#999;">—</span>
    </td>

    <td style="text-align:right;white-space:nowrap;">
        <button type="button" class="button button-small iv1wpt-icon-btn iv1wpt-scan-single" data-key="<?php echo esc_attr($item['key']); ?>" title="<?php esc_attr_e('Scan', 'iv1-wp-tools'); ?>" aria-label="<?php esc_attr_e('Scan', 'iv1-wp-tools'); ?>">
            <span class="dashicons dashicons-search" aria-hidden="true"></span>
        </button>

        <button type="button" class="button button-small iv1wpt-icon-btn iv1wpt-clean-single iv1wpt-clean-btn" data-key="<?php echo esc_attr($item['key']); ?>" title="<?php esc_attr_e('Clean', 'iv1-wp-tools'); ?>" aria-label="<?php esc_attr_e('Clean', 'iv1-wp-tools'); ?>" disabled>
            <span class="dashicons dashicons-trash" aria-hidden="true"></span>
        </button>
    </td>
</tr>


<?php endforeach; ?>
</tbody></table>
