<?php
/**
 * Backup & Restore admin screen.
 *
 * @package BackupRestore
 *
 * @var string $tab
 * @var array<string, array<string, mixed>> $history
 * @var int $disk_use
 * @var BackupRestore\Includes\BackupService $service
 * @var string $view_log
 * @var string $log_text
 * @var array<string, mixed> $br_settings
 * @var bool $settings_updated
 * @var array{type: string, message: string}|null $br_flash
 * @var list<array{id: string, has_manifest: bool}> $br_upload_sets
 * @var int $upload_max_bytes
 * @var string $br_module_version Module semver shown for deploy verification.
 * @var string $br_engine_build BackupService::ENGINE_BUILD (must match Network JSON br_service_engine).
 * @var list<array{type: string, backup_id: string, file: string, size: int, mtime: int}> $br_log_files
 * @var bool $br_restore_screen True when br_restore screen should load.
 */

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

if ( ! isset( $br_restore_screen ) ) {
	$br_restore_screen = false;
}

if ( ! empty( $br_restore_screen ) ) {
	require __DIR__ . '/restore-screen.php';
	return;
}

uasort(
	$history,
	static function ( $a, $b ) {
		return ( (int) ( $b['created'] ?? 0 ) ) <=> ( (int) ( $a['created'] ?? 0 ) );
	}
);

$backup_count     = count( $history );
$download_base    = admin_url( 'admin-post.php' );
$screen_url       = admin_url( 'admin.php?page=powersuite-backup-restore' );
$main_tab_url     = $screen_url;
$settings_tab_url = add_query_arg( 'br_tab', 'settings', $screen_url );
$schedule_tab_url = add_query_arg( 'br_tab', 'schedule', $screen_url );
$is_settings      = isset( $tab ) && 'settings' === $tab;
$is_schedule      = isset( $tab ) && 'schedule' === $tab;
?>
<div class="wrap wpps-br-wrap">
	<hr class="wp-header-end" />
	<div class="notice notice-warning wpps-br-privacy-notice">
		<p><?php esc_html_e( 'Backup archives contain your full database (user accounts, orders, API keys, and passwords stored in wp_options), media uploads, and site files. Download only on trusted devices, store them securely, and never share them publicly.', 'wp-powersuite' ); ?></p>
	</div>
	<?php if ( empty( $storage_ok ) ) : ?>
		<div class="notice notice-error wpps-br-storage-notice">
			<p><?php echo esc_html( is_string( $storage_error ?? null ) && $storage_error !== '' ? $storage_error : __( 'Private backup storage is not configured. New backups are disabled until a safe path is available.', 'wp-powersuite' ) ); ?></p>
		</div>
	<?php endif; ?>
	<?php if ( ! empty( $legacy_roots ) && is_array( $legacy_roots ) ) : ?>
		<div class="notice notice-warning wpps-br-legacy-storage-notice">
			<p><?php esc_html_e( 'Legacy backups were found under a web-accessible location. They remain available for download and restore, but new backups are only written to private storage outside the document root. Move or delete legacy archives when convenient.', 'wp-powersuite' ); ?></p>
		</div>
	<?php endif; ?>
	<?php if ( is_multisite() ) : ?>
		<div class="notice notice-info wpps-br-multisite-notice">
			<p><?php esc_html_e( 'Multisite: backups cover the current site only — this site’s tables plus shared network tables (users, sites, …), and this site’s uploads. Other blogs on the network are not included.', 'wp-powersuite' ); ?></p>
		</div>
	<?php endif; ?>
	<?php
	\WP_PowerSuite\Admin\PageHero::render_for_module(
		[
			'icon'      => 'dashicons-backup',
			'title'     => __( 'Backup & Restore', 'wp-powersuite' ),
			'tagline'   => __( 'Create local snapshots and restore them on this server — database, plugins, themes, uploads, and other wp-content data.', 'wp-powersuite' ),
			'meta_html' => '<button type="button" class="button button-primary wpps-br-backup-now" id="wpps-br-backup-now-hero">' . esc_html__( 'New backup', 'wp-powersuite' ) . '</button>',
		]
	);
	?>

	<?php if ( ! $is_settings && is_array( $br_flash ?? null ) && ! empty( $br_flash['message'] ) ) : ?>
		<?php
		$notice_class = ( isset( $br_flash['type'] ) && 'error' === $br_flash['type'] ) ? 'notice-error' : 'notice-success';
		?>
		<div class="notice <?php echo esc_attr( $notice_class ); ?> is-dismissible"><p><?php echo esc_html( (string) $br_flash['message'] ); ?></p></div>
	<?php endif; ?>

	<h2 class="nav-tab-wrapper wp-heading-inline wpps-br-tabs">
		<a href="<?php echo esc_url( $main_tab_url ); ?>" class="nav-tab <?php echo ( ! $is_settings && ! $is_schedule ) ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Backup / Restore', 'wp-powersuite' ); ?></a>
		<a href="<?php echo esc_url( $schedule_tab_url ); ?>" class="nav-tab <?php echo $is_schedule ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Schedule', 'wp-powersuite' ); ?></a>
		<a href="<?php echo esc_url( $settings_tab_url ); ?>" class="nav-tab <?php echo $is_settings ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Settings', 'wp-powersuite' ); ?></a>
	</h2>

	<?php if ( $is_schedule ) : ?>
		<div class="wpps-br-tab-panel">
			<?php require __DIR__ . '/schedule-tab.php'; ?>
		</div>
	<?php elseif ( $is_settings ) : ?>
		<div class="wpps-br-tab-panel wpps-br-tab-panel--settings">
			<?php require __DIR__ . '/settings-tab.php'; ?>
		</div>
	<?php else : ?>

	<div class="wpps-br-tab-panel">
		<div class="wpps-br-card wpps-br-progress-card" id="wpps-br-progress-panel" hidden>
			<div class="wpps-br-progress-head">
				<span class="wpps-br-progress-label-title"><?php esc_html_e( 'Backup in progress', 'wp-powersuite' ); ?></span>
				<span class="wpps-br-progress-started" id="wpps-br-progress-started"></span>
			</div>
			<div class="wpps-br-progress-bar-row">
				<div class="wpps-br-progress-track" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" id="wpps-br-progress-track">
					<div class="wpps-br-progress-fill" id="wpps-br-progress-fill" style="width:0%;"></div>
				</div>
				<span class="wpps-br-progress-pct" id="wpps-br-progress-pct">0%</span>
			</div>
			<p class="wpps-br-progress-status-line" id="wpps-br-progress-status"></p>
			<div class="wpps-br-progress-meta">
				<span class="wpps-br-progress-meta__activity" id="wpps-br-last-activity"></span>
				<span class="wpps-br-progress-meta__actions">
					<button
						type="button"
						class="button wpps-br-progress-meta__btn"
						id="wpps-br-show-backup-live-log"
						aria-expanded="false"
						aria-controls="wpps-br-backup-live-log-section"
					><?php esc_html_e( 'Show log', 'wp-powersuite' ); ?></button>
					<button type="button" class="button wpps-br-progress-meta__btn" id="wpps-br-stop-backup"><?php esc_html_e( 'Stop backup', 'wp-powersuite' ); ?></button>
				</span>
			</div>
			<div id="wpps-br-backup-live-log-section" class="wpps-br-backup-live-log-wrap" hidden>
				<h4 class="wpps-br-backup-live-log-title"><?php esc_html_e( 'Live log', 'wp-powersuite' ); ?></h4>
				<pre id="wpps-br-backup-live-log-pre" class="wpps-br-live-log wpps-br-backup-live-log-pre" aria-live="polite"></pre>
			</div>
		</div>

		<?php if ( $log_text !== '' ) : ?>
			<div class="wpps-br-card wpps-br-log-view">
				<h3><?php esc_html_e( 'Backup log', 'wp-powersuite' ); ?></h3>
				<pre class="wpps-br-log-pre"><?php echo esc_html( $log_text ); ?></pre>
			</div>
		<?php endif; ?>

		<div class="wpps-br-card">
			<div class="wpps-br-card-head">
				<h3>
					<?php esc_html_e( 'Existing backups', 'wp-powersuite' ); ?>
					<?php if ( $backup_count > 0 ) : ?>
						<span class="wpps-br-count"><?php echo esc_html( (string) $backup_count ); ?></span>
					<?php endif; ?>
				</h3>
			</div>
			<p class="wpps-br-disk">
				<?php
				printf(
					/* translators: %s: formatted size */
					esc_html__( 'Web-server disk space in use by this module: %s', 'wp-powersuite' ),
					'<strong>' . esc_html( size_format( $disk_use, 1 ) ) . '</strong>'
				);
				?>
				<button type="button" class="button button-small wpps-br-refresh-disk"><?php esc_html_e( 'Refresh', 'wp-powersuite' ); ?></button>
				<?php
				$br_newest_logs = [];
				$br_log_roots   = [];
				$br_primary     = $service->get_backup_root();
				if ( $br_primary !== '' && is_dir( $br_primary ) ) {
					$br_log_roots[] = $br_primary;
				}
				foreach ( $service->get_legacy_backup_roots() as $br_legacy ) {
					if ( is_dir( $br_legacy ) ) {
						$br_log_roots[] = $br_legacy;
					}
				}
				foreach ( array_unique( $br_log_roots ) as $br_log_root ) {
					$found = glob( trailingslashit( $br_log_root ) . '*/backup.log' );
					if ( is_array( $found ) ) {
						foreach ( $found as $log_path ) {
							$br_newest_logs[] = $log_path;
						}
					}
				}
				$br_newest_log = '';
				if ( count( $br_newest_logs ) > 0 ) {
					usort(
						$br_newest_logs,
						static function ( $a, $b ) {
							return filemtime( $b ) <=> filemtime( $a );
						}
					);
					$br_newest_log = $br_newest_logs[0];
				}
				if ( $br_newest_log && is_readable( $br_newest_log ) ) :
					$br_newest_log_id = basename( dirname( $br_newest_log ) );
					?>
				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'dl_log', rawurlencode( $br_newest_log_id ), $screen_url ), 'wpps_br_dl_log_' . $br_newest_log_id ) ); ?>" class="button button-small wpps-br-latest-log-dl">
					<?php esc_html_e( 'Download latest log', 'wp-powersuite' ); ?>
				</a>
				<?php endif; ?>
			</p>
			<p class="wpps-br-stuck-lock">
				<button type="button" class="button-link wpps-br-clear-stuck-lock" id="wpps-br-clear-stuck-lock">
					<?php esc_html_e( 'Clear stuck backup / restore lock', 'wp-powersuite' ); ?>
				</button>
				<span class="wpps-br-muted">
					<?php esc_html_e( 'Use this if you closed the page during a backup or restore and the progress bar says another job is still running.', 'wp-powersuite' ); ?>
				</span>
			</p>
			<p class="wpps-br-actions-row">
				<button type="button" class="button-link" id="wpps-br-toggle-upload" aria-expanded="false" aria-controls="wpps-br-upload-panel"><?php esc_html_e( 'Upload backup files', 'wp-powersuite' ); ?></button>
				<span class="wpps-br-sep">|</span>
				<button type="button" class="button-link wpps-br-rescan-local"><?php esc_html_e( 'Rescan local folder for new backup sets', 'wp-powersuite' ); ?></button>
			</p>

			<div class="wpps-br-upload-panel" id="wpps-br-upload-panel" hidden>
				<h4 class="wpps-br-upload-heading"><?php esc_html_e( 'Upload backup', 'wp-powersuite' ); ?></h4>
				<p class="wpps-br-muted">
					<?php esc_html_e( 'Drag a full backup ZIP or individual parts (database.sql, plugins.zip, etc.) into the box below. The backup folder and part type are detected automatically.', 'wp-powersuite' ); ?>
				</p>
				<div class="wpps-br-upload-busy" id="wpps-br-upload-busy" hidden>
					<span class="wpps-br-upload-busy__spinner" aria-hidden="true"></span>
					<div class="wpps-br-upload-busy__text-wrap">
						<strong class="wpps-br-upload-busy__title" id="wpps-br-upload-busy-title"></strong>
						<p class="wpps-br-upload-busy__detail" id="wpps-br-upload-busy-detail"></p>
					</div>
				</div>
				<div id="wpps-br-plupload-ui" class="wpps-br-plupload-ui">
					<div id="wpps-br-plupload-drop" class="wpps-br-plupload-drop" tabindex="0">
						<p class="wpps-br-plupload-drop__text" id="wpps-br-plupload-drop-hint"></p>
						<button type="button" class="button button-secondary" id="wpps-br-plupload-browse"><?php esc_html_e( 'Select files', 'wp-powersuite' ); ?></button>
					</div>
				</div>
				<details class="wpps-br-upload-advanced">
					<summary class="wpps-br-upload-advanced-summary"><?php esc_html_e( 'Advanced', 'wp-powersuite' ); ?></summary>
					<div class="wpps-br-upload-options">
						<p class="wpps-br-upload-option">
							<label for="wpps-br-backup-set"><strong><?php esc_html_e( 'Backup folder', 'wp-powersuite' ); ?></strong></label><br />
							<select name="backup_set" id="wpps-br-backup-set" class="regular-text" form="wpps-br-upload-fallback-form">
								<option value="__new__"><?php esc_html_e( 'Auto (new folder)', 'wp-powersuite' ); ?></option>
								<?php foreach ( $br_upload_sets as $row ) : ?>
									<option value="<?php echo esc_attr( $row['id'] ); ?>">
										<?php
										echo esc_html( $row['id'] );
										if ( ! empty( $row['has_manifest'] ) ) {
											echo ' — ' . esc_html__( 'has manifest', 'wp-powersuite' );
										} else {
											echo ' — ' . esc_html__( 'incomplete / imported', 'wp-powersuite' );
										}
										?>
									</option>
								<?php endforeach; ?>
							</select>
						</p>
						<p class="wpps-br-upload-option">
							<label for="wpps-br-upload-component"><strong><?php esc_html_e( 'Backup part', 'wp-powersuite' ); ?></strong></label><br />
							<select name="component" id="wpps-br-upload-component" class="regular-text" form="wpps-br-upload-fallback-form">
								<option value="auto"><?php esc_html_e( 'Auto-detect', 'wp-powersuite' ); ?></option>
								<option value="database"><?php esc_html_e( 'Database (database.sql)', 'wp-powersuite' ); ?></option>
								<option value="plugins"><?php esc_html_e( 'Plugins (plugins.zip)', 'wp-powersuite' ); ?></option>
								<option value="themes"><?php esc_html_e( 'Themes (themes.zip)', 'wp-powersuite' ); ?></option>
								<option value="uploads"><?php esc_html_e( 'Uploads (uploads.zip)', 'wp-powersuite' ); ?></option>
								<option value="others"><?php esc_html_e( 'Others (others.zip)', 'wp-powersuite' ); ?></option>
								<option value="bundle"><?php esc_html_e( 'Full backup bundle', 'wp-powersuite' ); ?></option>
							</select>
						</p>
					</div>
				</details>
				<form id="wpps-br-upload-fallback-form" hidden>
					<?php wp_nonce_field( 'wpps_br_upload' ); ?>
					<input type="hidden" name="action" value="wpps_br_upload_backup" />
				</form>
			</div>

			<div id="wpps-br-part-panel" class="wpps-br-part-panel" hidden>
				<button type="button" class="wpps-br-part-panel__close" id="wpps-br-part-panel-close" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
				<p class="wpps-br-part-panel__status" id="wpps-br-part-panel-status"></p>
				<p class="wpps-br-part-panel__loading" id="wpps-br-part-panel-loading" hidden><?php esc_html_e( 'Loading…', 'wp-powersuite' ); ?></p>
				<div class="wpps-br-part-panel__actions" id="wpps-br-part-panel-actions" hidden>
					<a href="#" class="button button-primary" id="wpps-br-part-download"><?php esc_html_e( 'Download to your computer', 'wp-powersuite' ); ?></a>
					<button type="button" class="button" id="wpps-br-part-delete-srv"><?php esc_html_e( 'Delete from your web server', 'wp-powersuite' ); ?></button>
					<button type="button" class="button" id="wpps-br-part-browse"><?php esc_html_e( 'Browse contents', 'wp-powersuite' ); ?></button>
				</div>
			</div>

			<form method="post" id="wpps-br-bulk-form">
				<?php wp_nonce_field( 'wpps_br_bulk', 'wpps_br_bulk_nonce' ); ?>
				<table class="widefat striped wp-list-table wpps-br-table">
					<thead>
						<tr>
							<td class="check-column"><input type="checkbox" id="wpps-br-select-all" /></td>
							<th><?php esc_html_e( 'Backup date', 'wp-powersuite' ); ?></th>
							<th><?php esc_html_e( 'Backup data (click to download)', 'wp-powersuite' ); ?></th>
							<th><?php esc_html_e( 'Label / type', 'wp-powersuite' ); ?></th>
							<th><?php esc_html_e( 'Actions', 'wp-powersuite' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php if ( empty( $history ) ) : ?>
							<tr class="wpps-br-empty">
								<td colspan="5"><?php esc_html_e( 'No backups yet.', 'wp-powersuite' ); ?></td>
							</tr>
						<?php else : ?>
							<?php foreach ( $history as $row ) : ?>
								<?php
								$bid   = isset( $row['id'] ) ? (string) $row['id'] : '';
								$comps = isset( $row['components'] ) && is_array( $row['components'] ) ? $row['components'] : [];
								$ts    = isset( $row['created'] ) ? (int) $row['created'] : 0;
								if ( ! $bid ) {
									continue;
								}
								$disk_comps    = $service->list_components_on_disk( $bid );
								$can_restore   = count( $disk_comps ) > 0;
								$restore_tab_url = $can_restore
									? wp_nonce_url(
										add_query_arg(
											[
												'page'       => 'powersuite-backup-restore',
												'br_restore' => $bid,
											],
											admin_url( 'admin.php' )
										),
										'wpps_br_open_restore_' . $bid
									)
									: '';
								$part_dl_urls  = [];
								$part_order    = [ 'database', 'plugins', 'themes', 'uploads', 'others' ];
								foreach ( $part_order as $p ) {
									if ( in_array( $p, $disk_comps, true ) ) {
										$part_dl_urls[ $p ] = wp_nonce_url(
											add_query_arg(
												[
													'action' => 'wpps_br_download',
													'backup' => $bid,
													'part'   => $p,
												],
												admin_url( 'admin-post.php' )
											),
											'wpps_br_dl_' . $bid . '_' . $p,
											'_wpnonce',
											false
										);
									}
								}
								?>
								<tr
									data-backup-id="<?php echo esc_attr( $bid ); ?>"
									<?php if ( ! empty( $part_dl_urls ) ) : ?>
										data-wpps-download-parts="<?php echo esc_attr( wp_json_encode( $part_dl_urls, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) ); ?>"
									<?php endif; ?>
								>
									<th scope="row" class="check-column">
										<input type="checkbox" class="wpps-br-row-check" value="<?php echo esc_attr( $bid ); ?>" />
									</th>
									<td>
										<?php echo esc_html( $ts ? date_i18n( 'M d, Y H:i', $ts ) : '' ); ?>
									</td>
									<td class="wpps-br-parts">
										<?php
										$parts_map = [
											'database' => __( 'Database', 'wp-powersuite' ),
											'plugins'  => __( 'Plugins', 'wp-powersuite' ),
											'themes'   => __( 'Themes', 'wp-powersuite' ),
											'uploads'  => __( 'Uploads', 'wp-powersuite' ),
											'others'   => __( 'Others', 'wp-powersuite' ),
										];
										foreach ( $parts_map as $part => $plabel ) :
											if ( ! in_array( $part, $comps, true ) ) {
												continue;
											}
											?>
											<button
												type="button"
												class="button button-small wpps-br-part-trigger"
												data-backup-id="<?php echo esc_attr( $bid ); ?>"
												data-part="<?php echo esc_attr( $part ); ?>"
											><?php echo esc_html( $plabel ); ?></button>
											<?php
										endforeach;
										?>
									</td>
									<td>
										<?php
										$lbl = isset( $row['label'] ) ? (string) $row['label'] : '';
										$typ = isset( $row['type'] ) ? (string) $row['type'] : '';
										echo esc_html( $lbl !== '' ? $lbl : '—' );
										if ( $typ !== '' ) {
											echo '<br /><span class="wpps-br-muted">' . esc_html( ucfirst( $typ ) ) . '</span>';
										}
										?>
									</td>
									<td class="wpps-br-actions">
										<?php if ( $can_restore && $restore_tab_url ) : ?>
											<a
												class="button button-small"
												href="<?php echo esc_url( $restore_tab_url ); ?>"
												target="_blank"
												rel="noopener noreferrer"
												title="<?php esc_attr_e( 'Restore database and/or files from this backup set (opens in a new tab).', 'wp-powersuite' ); ?>"
											><?php esc_html_e( 'Restore', 'wp-powersuite' ); ?></a>
										<?php else : ?>
											<button type="button" class="button button-small" disabled><?php esc_html_e( 'Restore', 'wp-powersuite' ); ?></button>
										<?php endif; ?>
										<button type="button" class="button button-small wpps-br-delete" data-id="<?php echo esc_attr( $bid ); ?>">
											<?php esc_html_e( 'Delete', 'wp-powersuite' ); ?>
										</button>
										<?php
										$log_view_url = wp_nonce_url(
											add_query_arg( [ 'page' => 'powersuite-backup-restore', 'dl_log' => $bid, 'display' => 'inline' ], admin_url( 'admin.php' ) ),
											'wpps_br_dl_log_' . $bid
										);
										?>
										<a
											href="<?php echo esc_url( $log_view_url ); ?>"
											class="button button-small"
											target="_blank"
											rel="noopener noreferrer"
										><?php esc_html_e( 'View Log', 'wp-powersuite' ); ?></a>
										<?php if ( $can_restore ) : ?>
											<button
												type="button"
												class="button button-small wpps-br-download-full-zip"
												data-backup-id="<?php echo esc_attr( $bid ); ?>"
											><?php esc_html_e( 'Download full ZIP', 'wp-powersuite' ); ?></button>
											<?php if ( ! empty( $part_dl_urls ) ) : ?>
												<button
													type="button"
													class="button button-small wpps-br-download-all-parts"
													title="<?php esc_attr_e( 'Starts one download per part (database, zips). Use if a single full ZIP stops around 500 MB—many hosts cap one HTTP response body, even for localhost.', 'wp-powersuite' ); ?>"
												><?php esc_html_e( 'Download all parts', 'wp-powersuite' ); ?></button>
											<?php endif; ?>
										<?php endif; ?>
									</td>
								</tr>
							<?php endforeach; ?>
						<?php endif; ?>
					</tbody>
				</table>
				<?php if ( $backup_count > 0 ) : ?>
				<p class="wpps-br-bulk">
					<button type="button" class="button wpps-br-bulk-delete"><?php esc_html_e( 'Delete', 'wp-powersuite' ); ?></button>
					<button type="button" class="button" id="wpps-br-toggle-select-all"><?php esc_html_e( 'Select all', 'wp-powersuite' ); ?></button>
				</p>
				<?php endif; ?>
			</form>
		</div>

		<?php
		$log_files       = isset( $br_log_files ) ? $br_log_files : [];
		$log_files_count = count( $log_files );
		?>
		<div class="wpps-br-accordion" id="wpps-br-activity-log-section">
			<button type="button" class="wpps-br-accordion-header" aria-expanded="false" aria-controls="wpps-br-logs-body">
				<span class="wpps-br-accordion-title">
					<?php esc_html_e( 'Logs', 'wp-powersuite' ); ?>
					<?php if ( $log_files_count > 0 ) : ?>
						<span class="wpps-br-count"><?php echo esc_html( (string) $log_files_count ); ?></span>
					<?php endif; ?>
				</span>
				<span class="dashicons dashicons-arrow-down-alt2 wpps-br-accordion-chevron"></span>
			</button>
			<div id="wpps-br-logs-body" class="wpps-br-accordion-body">
				<?php if ( $log_files_count > 0 ) : ?>
				<p class="wpps-br-activity-log-filters">
					<label class="wpps-br-activity-filter-label"><?php esc_html_e( 'Filter:', 'wp-powersuite' ); ?></label>
					<button type="button" class="button button-small wpps-br-activity-filter is-active" data-filter="all"><?php esc_html_e( 'All', 'wp-powersuite' ); ?></button>
					<button type="button" class="button button-small wpps-br-activity-filter" data-filter="backup"><?php esc_html_e( 'Backup', 'wp-powersuite' ); ?></button>
					<button type="button" class="button button-small wpps-br-activity-filter" data-filter="restore"><?php esc_html_e( 'Restore', 'wp-powersuite' ); ?></button>
					<button type="button" class="button button-small wpps-br-activity-filter" data-filter="upload"><?php esc_html_e( 'Upload', 'wp-powersuite' ); ?></button>
					<button type="button" class="button button-small wpps-br-activity-clear" id="wpps-br-activity-clear"><?php esc_html_e( 'Delete all log files', 'wp-powersuite' ); ?></button>
				</p>
				<table class="widefat striped wpps-br-activity-log-table" id="wpps-br-activity-log-table">
					<thead>
						<tr>
							<th class="wpps-br-al-col-type"><?php esc_html_e( 'Type', 'wp-powersuite' ); ?></th>
							<th class="wpps-br-al-col-time"><?php esc_html_e( 'Last modified', 'wp-powersuite' ); ?></th>
							<th class="wpps-br-al-col-id"><?php esc_html_e( 'Backup ID', 'wp-powersuite' ); ?></th>
							<th class="wpps-br-al-col-size"><?php esc_html_e( 'Size', 'wp-powersuite' ); ?></th>
							<th class="wpps-br-al-col-actions"><?php esc_html_e( 'Actions', 'wp-powersuite' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $log_files as $lf_entry ) :
							$lf_bid   = $lf_entry['backup_id'];
							$lf_type  = $lf_entry['type'];
							$lf_fname = $lf_entry['file'];
							$lf_mtime = (int) $lf_entry['mtime'];
							$lf_size  = (int) $lf_entry['size'];
							if ( 'restore' === $lf_type ) {
								$lf_dl_url = wp_nonce_url(
									add_query_arg( [ 'page' => 'powersuite-backup-restore', 'dl_restore_log' => $lf_bid ], admin_url( 'admin.php' ) ),
									'wpps_br_dl_restore_log_' . $lf_bid
								);
							} else {
								$lf_dl_url = wp_nonce_url(
									add_query_arg( [ 'page' => 'powersuite-backup-restore', 'dl_log' => $lf_bid ], admin_url( 'admin.php' ) ),
									'wpps_br_dl_log_' . $lf_bid
								);
							}
							$lf_view_url = add_query_arg( 'display', 'inline', $lf_dl_url );
						?>
						<tr data-log-type="<?php echo esc_attr( $lf_type ); ?>" data-backup-id="<?php echo esc_attr( $lf_bid ); ?>" data-log-file="<?php echo esc_attr( $lf_fname ); ?>">
							<td class="wpps-br-al-col-type">
								<span class="wpps-br-al-badge wpps-br-al-badge--<?php echo esc_attr( $lf_type ); ?>">
									<?php echo esc_html( ucfirst( $lf_type ) ); ?>
								</span>
							</td>
							<td class="wpps-br-al-col-time">
								<?php echo $lf_mtime > 0 ? esc_html( wp_date( 'Y-m-d H:i:s', $lf_mtime ) ) : '—'; ?>
							</td>
							<td class="wpps-br-al-col-id"><code><?php echo esc_html( 'upload' === $lf_type ? __( '(upload staging)', 'wp-powersuite' ) : $lf_bid ); ?></code></td>
							<td class="wpps-br-al-col-size"><?php echo esc_html( size_format( $lf_size, 1 ) ); ?></td>
							<td class="wpps-br-al-col-actions">
								<a href="<?php echo esc_url( $lf_dl_url ); ?>" class="button button-small"><?php esc_html_e( 'Download', 'wp-powersuite' ); ?></a>
								<a href="<?php echo esc_url( $lf_view_url ); ?>" class="button button-small" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'View', 'wp-powersuite' ); ?></a>
								<button type="button" class="button button-small wpps-br-delete-log-file"><?php esc_html_e( 'Delete', 'wp-powersuite' ); ?></button>
							</td>
						</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
				<?php else : ?>
				<p class="wpps-br-muted"><?php esc_html_e( 'No log files found. Logs appear after backups, restores, or when you upload backup files (upload progress is written to upload_activity.log).', 'wp-powersuite' ); ?></p>
				<?php endif; ?>
			</div>
		</div>
	</div>

	<?php endif; ?>

	<?php if ( ! $is_settings ) : ?>
	<div id="wpps-br-bundle-modal" class="wpps-br-modal" hidden>
		<div class="wpps-br-modal__backdrop" data-wpps-br-bundle-dismiss aria-hidden="true"></div>
		<div class="wpps-br-dialog" role="dialog" aria-modal="true" aria-labelledby="wpps-br-bundle-modal-title">
			<div class="wpps-br-dialog__header">
				<h2 id="wpps-br-bundle-modal-title" class="wpps-br-dialog__title"><?php esc_html_e( 'Preparing full backup ZIP', 'wp-powersuite' ); ?></h2>
				<button type="button" class="wpps-br-dialog__close" data-wpps-br-bundle-dismiss aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
			</div>
			<div class="wpps-br-dialog__body">
				<p class="wpps-br-bundle-modal-lead" id="wpps-br-bundle-modal-lead"><?php esc_html_e( 'The zip file is being created on the server. Please keep this page open. When it is ready, your browser will start the download.', 'wp-powersuite' ); ?></p>
				<div id="wpps-br-bundle-timeline" class="wpps-br-bundle-timeline" aria-live="polite"></div>
				<p class="wpps-br-dialog__actions">
					<button type="button" class="button" id="wpps-br-bundle-cancel" data-wpps-br-bundle-dismiss><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
				</p>
			</div>
		</div>
	</div>
	<div id="wpps-br-log-text-modal" class="wpps-br-modal wpps-br-modal--log" hidden>
		<div class="wpps-br-modal__backdrop wpps-br-log-text-modal-dismiss" aria-hidden="true"></div>
		<div class="wpps-br-dialog wpps-br-dialog--log-dark" role="dialog" aria-modal="true" aria-labelledby="wpps-br-log-text-modal-title">
			<div class="wpps-br-dialog__header wpps-br-dialog__header--log-dark">
				<h2 id="wpps-br-log-text-modal-title" class="wpps-br-dialog__title"><?php esc_html_e( 'Log', 'wp-powersuite' ); ?></h2>
				<button type="button" class="wpps-br-dialog__close wpps-br-log-text-modal-dismiss" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
			</div>
			<div class="wpps-br-dialog__body wpps-br-dialog__body--log-dark">
				<pre id="wpps-br-log-text-modal-pre" class="wpps-br-log-text-modal-pre"></pre>
			</div>
		</div>
	</div>
	<div id="wpps-br-modal" class="wpps-br-modal" hidden>
		<div class="wpps-br-modal__backdrop" data-close></div>
		<div class="wpps-br-dialog" role="dialog" aria-modal="true" aria-labelledby="wpps-br-modal-title">
			<div class="wpps-br-dialog__header">
				<h2 id="wpps-br-modal-title" class="wpps-br-dialog__title"><?php esc_html_e( 'Create backup', 'wp-powersuite' ); ?></h2>
				<button type="button" class="wpps-br-dialog__close" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>" data-close>&times;</button>
			</div>
			<div class="wpps-br-dialog__body">
				<p class="wpps-br-dialog__intro"><?php esc_html_e( 'Select backup scope:', 'wp-powersuite' ); ?></p>
				<div class="wpps-br-scope-grid">
					<label class="wpps-br-scope-card">
						<input type="radio" name="wpps_br_type" value="full" checked class="wpps-br-scope-card__input" />
						<span class="wpps-br-scope-card__body">
							<span class="wpps-br-scope-card__title"><?php esc_html_e( 'Full backup', 'wp-powersuite' ); ?></span>
							<span class="wpps-br-scope-card__desc"><?php esc_html_e( 'Database plus whichever file groups are enabled under Settings.', 'wp-powersuite' ); ?></span>
						</span>
					</label>
					<label class="wpps-br-scope-card">
						<input type="radio" name="wpps_br_type" value="database" class="wpps-br-scope-card__input" />
						<span class="wpps-br-scope-card__body">
							<span class="wpps-br-scope-card__title"><?php esc_html_e( 'Database only', 'wp-powersuite' ); ?></span>
							<span class="wpps-br-scope-card__desc"><?php esc_html_e( 'Exports the database SQL only (fastest).', 'wp-powersuite' ); ?></span>
						</span>
					</label>
					<label class="wpps-br-scope-card wpps-br-scope-card--span">
						<input type="radio" name="wpps_br_type" value="files" class="wpps-br-scope-card__input" />
						<span class="wpps-br-scope-card__body">
							<span class="wpps-br-scope-card__title"><?php esc_html_e( 'Files only', 'wp-powersuite' ); ?></span>
							<span class="wpps-br-scope-card__desc"><?php esc_html_e( 'Plugins, themes, uploads, and other wp-content paths enabled under Settings—no database.', 'wp-powersuite' ); ?></span>
						</span>
					</label>
				</div>
				<p class="wpps-br-dialog__field">
					<label for="wpps-br-label"><strong><?php esc_html_e( 'Backup label', 'wp-powersuite' ); ?></strong> <span class="wpps-br-muted">(<?php esc_html_e( 'optional', 'wp-powersuite' ); ?>)</span></label><br />
					<input type="text" id="wpps-br-label" class="large-text" maxlength="120" placeholder="<?php esc_attr_e( 'e.g. Before theme update', 'wp-powersuite' ); ?>" />
				</p>
				<p class="wpps-br-dialog__actions">
					<button type="button" class="button button-primary" id="wpps-br-start"><?php esc_html_e( 'Create backup', 'wp-powersuite' ); ?></button>
					<button type="button" class="button" data-close><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
				</p>
			</div>
		</div>
	</div>
	<div id="wpps-br-browse-modal" class="wpps-br-modal" hidden>
		<div class="wpps-br-modal__backdrop wpps-br-browse-dismiss" aria-hidden="true"></div>
		<div class="wpps-br-dialog wpps-br-dialog--browse" role="dialog" aria-modal="true" aria-labelledby="wpps-br-browse-title">
			<div class="wpps-br-dialog__header">
				<h2 id="wpps-br-browse-title" class="wpps-br-dialog__title"></h2>
				<button type="button" class="wpps-br-dialog__close wpps-br-browse-dismiss" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
			</div>
			<div class="wpps-br-dialog__body wpps-br-browse-body">
				<div class="wpps-br-fm-toolbar" id="wpps-br-fm-toolbar" hidden>
					<div class="wpps-br-fm-toolbar__start">
						<button type="button" class="button" id="wpps-br-fm-up" disabled>
							<?php esc_html_e( 'Up', 'wp-powersuite' ); ?>
						</button>
						<nav
							class="wpps-br-fm-breadcrumbs"
							id="wpps-br-fm-breadcrumbs"
							aria-label="<?php esc_attr_e( 'Path inside archive', 'wp-powersuite' ); ?>"
						></nav>
					</div>
					<button type="button" class="button button-primary" id="wpps-br-fm-download" disabled>
						<?php esc_html_e( 'Download', 'wp-powersuite' ); ?>
					</button>
				</div>
				<p class="wpps-br-browse-notice" id="wpps-br-browse-notice" hidden></p>
				<div class="wpps-br-fm-sql" id="wpps-br-fm-sql" hidden>
					<p class="wpps-br-fm-sql__meta" id="wpps-br-fm-sql-meta"></p>
					<p class="wpps-br-fm-sql__hint"><?php esc_html_e( 'This is a SQL dump file. Download it to open in a text editor or import with phpMyAdmin.', 'wp-powersuite' ); ?></p>
					<p class="wpps-br-fm-sql__actions">
						<button type="button" class="button button-primary" id="wpps-br-fm-sql-download">
							<?php esc_html_e( 'Download SQL file', 'wp-powersuite' ); ?>
						</button>
					</p>
				</div>
				<div class="wpps-br-fm-main" id="wpps-br-fm-main" hidden>
					<div class="wpps-br-fm-table-wrap">
						<table class="wpps-br-fm-table widefat striped">
							<thead>
								<tr>
									<th class="wpps-br-fm-col-name"><?php esc_html_e( 'Name', 'wp-powersuite' ); ?></th>
									<th class="wpps-br-fm-col-size"><?php esc_html_e( 'Size', 'wp-powersuite' ); ?></th>
									<th class="wpps-br-fm-col-type"><?php esc_html_e( 'Type', 'wp-powersuite' ); ?></th>
								</tr>
							</thead>
							<tbody id="wpps-br-fm-tbody"></tbody>
						</table>
					</div>
					<p class="wpps-br-fm-status" id="wpps-br-fm-status" aria-live="polite"></p>
				</div>
			</div>
		</div>
	</div>
	<?php endif; ?>
</div>
