<?php
if (!defined('ABSPATH')) exit;
?>
        <!-- TAB 6: Maintenance -->
        <div id="tab-maintenance" class="mtcam-tab-content">
            <h2>🔧 <?php esc_html_e('Maintenance & Diagnostics', 'my-trail-camera'); ?></h2>
            <p><?php esc_html_e('Tools for maintenance and troubleshooting.', 'my-trail-camera'); ?></p>

            <div class="mtcam-white-box-clean">
                <h3 class="mtcam-mt-0">📊 <?php esc_html_e('System Information', 'my-trail-camera'); ?></h3>
                <table class="mtcam-sysinfo-table">
                    <?php mtcam_table_caption( __( 'System information', 'my-trail-camera' ) ); ?>
                    <tr >
                        <td ><?php esc_html_e('Plugin Version:', 'my-trail-camera'); ?></td>
                        <td><code><?php echo esc_html( $system_info['plugin_version'] ); ?></code></td>
                    </tr>
                    <tr >
                        <td ><?php esc_html_e('WordPress Version:', 'my-trail-camera'); ?></td>
                        <td ><code><?php echo esc_html( $system_info['wp_version'] ); ?></code></td>
                    </tr>
                    <tr >
                        <td ><?php esc_html_e('PHP Version:', 'my-trail-camera'); ?></td>
                        <td><code><?php echo esc_html( $system_info['php_version'] ); ?></code></td>
                    </tr>
                    <tr >
                        <td ><?php esc_html_e('Total Records:', 'my-trail-camera'); ?></td>
                        <td >
                            <strong><?php echo esc_html( $system_info['total_records'] ); ?></strong>
                        </td>
                    </tr>
                </table>

                <h3 class="mtcam-mt-30">💾 <?php esc_html_e('Backup & Import', 'my-trail-camera'); ?></h3>
                <p><?php esc_html_e('Export your entire configuration (cameras + settings) to a JSON file or import from an existing backup.', 'my-trail-camera'); ?></p>

                <button type="button" data-mtcam-action="thorExportBackup"
                        class="mtcam-maint-btn mtcam-maint-btn-green">
                    📥 <?php esc_html_e('Export Backup (JSON)', 'my-trail-camera'); ?>
                </button>

                <label class="mtcam-maint-btn mtcam-maint-btn-blue" style="cursor:pointer;">
                    📤 <?php esc_html_e('Import Backup', 'my-trail-camera'); ?>
                    <input type="file" accept=".json" data-mtcam-import-backup hidden>
                </label>

                <div id="mtcam-import-preview" class="mtcam-mt-15" style="display:none;"></div>

				<h3 class="mtcam-mt-30">🧹 <?php esc_html_e('Orphaned Records Cleanup', 'my-trail-camera'); ?></h3>
                <p>
                    <?php esc_html_e( 'Remove location reviews and detections for cameras that no longer exist in the system.', 'my-trail-camera' ); ?>
                </p>
                <?php
                // Count orphaned records for display
                $mtcam_existing_ids = array_keys(mtcam_get_cameras());
                $mtcam_orphan_info = mtcam_count_orphaned_records($mtcam_existing_ids);
                ?>
                <?php if ($mtcam_orphan_info['total'] > 0): ?>
                    <p class="mtcam-text-warning">
                        ⚠️
                        <?php
                        printf(
                            /* translators: 1: total orphaned records, 2: reviews count, 3: detections count */
                            esc_html__( 'Found %1$s orphaned records (reviews: %2$s, detections: %3$s)', 'my-trail-camera' ),
                            '<strong>' . intval( $mtcam_orphan_info['total'] ) . '</strong>',
                            intval( $mtcam_orphan_info['reviews'] ),
                            intval( $mtcam_orphan_info['detections'] )
                        );
                        ?>
                    </p>
                    <a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=mtcam-settings&tab=maintenance&mtcam_cleanup_orphans=1' ), 'mtcam_cleanup_orphans' ) ); ?>"
                       class="mtcam-maint-btn mtcam-maint-btn-red"
                       data-mtcam-confirm="<?php echo esc_attr(__('Are you sure you want to delete all orphaned records? This action cannot be undone!', 'my-trail-camera')); ?>">
                        🧹 <?php
                        printf(
                            /* translators: %d: number of orphaned records to clean up */
                            esc_html__('Clean up %d orphaned records', 'my-trail-camera'),
                            intval($mtcam_orphan_info['total'])
                        );
                        ?>
                    </a>
                <?php else: ?>
                    <p class="mtcam-text-muted">✅ <?php esc_html_e('No orphaned records. Database is clean.', 'my-trail-camera'); ?></p>
                <?php endif; ?>
            </div>
        </div>
