<?php
/**
 * Settings tab (inclusions, exclusions, retention, manual email notifications).
 *
 * @package BackupRestore
 *
 * @var array<string, mixed> $br_settings
 * @var string $settings_tab_url
 * @var bool $settings_updated
 */

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

$s = $br_settings;
?>
<?php if ( ! empty( $settings_updated ) ) : ?>
	<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'wppowersuite-lite' ); ?></p></div>
<?php endif; ?>

<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wpps-br-settings wpps-br-settings-form wpps-br-settings-page-form">
	<?php wp_nonce_field( 'wpps_br_settings' ); ?>
	<input type="hidden" name="action" value="wpps_br_save_settings" />

	<div class="wpps-br-settings-layout">
		<div class="wpps-br-settings-layout__top">
			<div class="wpps-br-card wpps-br-sched wpps-br-settings-card">
				<div class="wpps-br-sched-header">
					<div class="wpps-br-sched-header__text">
						<h2 class="wpps-br-settings-card__title"><?php esc_html_e( 'What to back up', 'wppowersuite-lite' ); ?></h2>
						<p class="wpps-br-muted wpps-br-settings-card__lead"><?php esc_html_e( 'These archives are built for full and file-only backups. Database-only jobs skip them.', 'wppowersuite-lite' ); ?></p>
					</div>
				</div>
				<div class="wpps-br-sched-body">
					<fieldset class="wpps-br-fieldset wpps-br-settings-includes">
						<legend class="screen-reader-text"><?php esc_html_e( 'Include in files backup', 'wppowersuite-lite' ); ?></legend>
						<div class="wpps-br-settings-include-list">
							<label class="wpps-br-check wpps-br-settings-tile">
								<input type="checkbox" name="include_plugins" value="1" <?php checked( ! empty( $s['include_plugins'] ) ); ?> />
								<span class="wpps-br-settings-tile__text"><?php esc_html_e( 'Plugins', 'wppowersuite-lite' ); ?></span>
							</label>
							<label class="wpps-br-check wpps-br-settings-tile">
								<input type="checkbox" name="include_themes" value="1" <?php checked( ! empty( $s['include_themes'] ) ); ?> />
								<span class="wpps-br-settings-tile__text"><?php esc_html_e( 'Themes', 'wppowersuite-lite' ); ?></span>
							</label>
							<label class="wpps-br-check wpps-br-settings-tile">
								<input type="checkbox" name="include_uploads" value="1" <?php checked( ! empty( $s['include_uploads'] ) ); ?> />
								<span class="wpps-br-settings-tile__text"><?php esc_html_e( 'Uploads', 'wppowersuite-lite' ); ?></span>
							</label>
							<label class="wpps-br-check wpps-br-settings-tile">
								<input type="checkbox" name="include_others" value="1" <?php checked( ! empty( $s['include_others'] ) ); ?> />
								<span class="wpps-br-settings-tile__text"><?php esc_html_e( 'Other wp-content (“Others”)', 'wppowersuite-lite' ); ?></span>
							</label>
						</div>
					</fieldset>
				</div>
			</div>

			<div class="wpps-br-card wpps-br-sched wpps-br-settings-card">
				<div class="wpps-br-sched-header">
					<div class="wpps-br-sched-header__text">
						<h2 class="wpps-br-settings-card__title"><?php esc_html_e( 'Path exclusions', 'wppowersuite-lite' ); ?></h2>
						<p class="wpps-br-muted wpps-br-settings-card__lead"><?php esc_html_e( 'Comma-separated tokens (Updraft-style). Use precise patterns—avoid overly broad rules.', 'wppowersuite-lite' ); ?></p>
					</div>
				</div>
				<div class="wpps-br-sched-body">
					<div class="wpps-br-settings-exclude-stack">
						<div class="wpps-br-settings-exclude-block">
							<label for="wpps-br-exclude-uploads" class="wpps-br-settings-exclude-label"><?php esc_html_e( 'Uploads folder', 'wppowersuite-lite' ); ?></label>
							<p class="wpps-br-muted wpps-br-settings-exclude-hint"><?php esc_html_e( 'Each token is matched against path segments under wp-content/uploads.', 'wppowersuite-lite' ); ?></p>
							<textarea name="exclude_uploads" id="wpps-br-exclude-uploads" class="large-text code wpps-br-settings-textarea" rows="5"><?php echo esc_textarea( (string) ( $s['exclude_uploads'] ?? '' ) ); ?></textarea>
						</div>
						<div class="wpps-br-settings-exclude-block">
							<label for="wpps-br-exclude-others" class="wpps-br-settings-exclude-label"><?php esc_html_e( 'Rest of wp-content', 'wppowersuite-lite' ); ?></label>
							<p class="wpps-br-muted wpps-br-settings-exclude-hint"><?php esc_html_e( 'Plugins, themes, uploads, and this module’s backup directory are excluded automatically.', 'wppowersuite-lite' ); ?></p>
							<textarea name="exclude_others" id="wpps-br-exclude-others" class="large-text code wpps-br-settings-textarea" rows="5"><?php echo esc_textarea( (string) ( $s['exclude_others'] ?? '' ) ); ?></textarea>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div class="wpps-br-settings-retain-grid">
			<div class="wpps-br-card wpps-br-sched wpps-br-settings-card wpps-br-settings-card--compact">
				<div class="wpps-br-sched-header">
					<div class="wpps-br-sched-header__text">
						<h2 class="wpps-br-settings-card__title"><?php esc_html_e( 'Completed backup sets', 'wppowersuite-lite' ); ?></h2>
						<p class="wpps-br-muted wpps-br-settings-card__lead"><?php esc_html_e( 'After a successful backup, older sets are pruned. Set to 0 to keep every snapshot.', 'wppowersuite-lite' ); ?></p>
					</div>
				</div>
				<div class="wpps-br-sched-body">
					<label class="wpps-br-settings-number-label" for="wpps-br-retain">
						<?php esc_html_e( 'Keep at most', 'wppowersuite-lite' ); ?>
						<input type="number" name="retain_completed" id="wpps-br-retain" min="0" max="100" step="1" value="<?php echo esc_attr( (string) (int) ( $s['retain_completed'] ?? 0 ) ); ?>" class="small-text" />
						<?php esc_html_e( 'set(s)', 'wppowersuite-lite' ); ?>
					</label>
				</div>
			</div>
			<div class="wpps-br-card wpps-br-sched wpps-br-settings-card wpps-br-settings-card--compact">
				<div class="wpps-br-sched-header">
					<div class="wpps-br-sched-header__text">
						<h2 class="wpps-br-settings-card__title"><?php esc_html_e( 'Log files', 'wppowersuite-lite' ); ?></h2>
						<p class="wpps-br-muted wpps-br-settings-card__lead"><?php esc_html_e( 'Backup and restore logs older than this are removed. Use 0 to disable cleanup.', 'wppowersuite-lite' ); ?></p>
					</div>
				</div>
				<div class="wpps-br-sched-body">
					<label class="wpps-br-settings-number-label" for="wpps-br-log-retain-days">
						<?php esc_html_e( 'Retain for', 'wppowersuite-lite' ); ?>
						<input type="number" name="log_retain_days" id="wpps-br-log-retain-days" min="0" max="365" step="1" value="<?php echo esc_attr( (string) (int) ( $s['log_retain_days'] ?? 30 ) ); ?>" class="small-text" />
						<?php esc_html_e( 'day(s)', 'wppowersuite-lite' ); ?>
					</label>
				</div>
			</div>
		</div>

		<div class="wpps-br-settings-emails-row">
			<div class="wpps-br-card wpps-br-sched wpps-br-settings-card">
				<div class="wpps-br-sched-header">
					<div class="wpps-br-sched-header__text">
						<h2 class="wpps-br-settings-card__title"><?php esc_html_e( 'Email: manual backup', 'wppowersuite-lite' ); ?></h2>
						<p class="wpps-br-muted wpps-br-settings-card__lead"><?php esc_html_e( 'When a backup started from the admin UI completes or fails.', 'wppowersuite-lite' ); ?></p>
					</div>
					<label class="wpps-br-sched-toggle">
						<input type="checkbox" name="manual_backup_email_enabled" value="1" <?php checked( ! empty( $s['manual_backup_email_enabled'] ) ); ?> id="wpps-br-manual-backup-email-enabled" />
						<span class="wpps-br-sched-toggle__slider"></span>
					</label>
				</div>
				<div class="wpps-br-sched-body" id="wpps-br-manual-backup-email-fields">
					<div class="wpps-br-sched-section">
						<div class="wpps-br-sched-section__label"><?php esc_html_e( 'Notify', 'wppowersuite-lite' ); ?></div>
						<div class="wpps-br-sched-section__fields">
							<div class="wpps-br-sched-type-pills">
								<label class="wpps-br-sched-type-pill">
									<input type="radio" name="manual_backup_email_on" value="both" <?php checked( $s['manual_backup_email_on'] ?? 'both', 'both' ); ?> />
									<span class="wpps-br-sched-type-pill__text"><?php esc_html_e( 'Success & failure', 'wppowersuite-lite' ); ?></span>
								</label>
								<label class="wpps-br-sched-type-pill">
									<input type="radio" name="manual_backup_email_on" value="failure" <?php checked( $s['manual_backup_email_on'] ?? '', 'failure' ); ?> />
									<span class="wpps-br-sched-type-pill__text"><?php esc_html_e( 'Failure only', 'wppowersuite-lite' ); ?></span>
								</label>
							</div>
						</div>
					</div>
					<div class="wpps-br-sched-section">
						<div class="wpps-br-sched-section__label"><?php esc_html_e( 'Recipients', 'wppowersuite-lite' ); ?></div>
						<div class="wpps-br-sched-section__fields">
							<textarea name="manual_backup_email_recipients" id="wpps-br-manual-backup-email-recipients" rows="2" class="wpps-br-sched-textarea" placeholder="<?php echo esc_attr( get_option( 'admin_email', '' ) ); ?>"><?php echo esc_textarea( (string) ( $s['manual_backup_email_recipients'] ?? '' ) ); ?></textarea>
							<p class="wpps-br-sched-hint"><?php esc_html_e( 'One address per line. Empty = site admin email.', 'wppowersuite-lite' ); ?></p>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-br-card wpps-br-sched wpps-br-settings-card">
				<div class="wpps-br-sched-header">
					<div class="wpps-br-sched-header__text">
						<h2 class="wpps-br-settings-card__title"><?php esc_html_e( 'Email: manual restore', 'wppowersuite-lite' ); ?></h2>
						<p class="wpps-br-muted wpps-br-settings-card__lead"><?php esc_html_e( 'When a restore from Backup / Restore finishes or hits an error.', 'wppowersuite-lite' ); ?></p>
					</div>
					<label class="wpps-br-sched-toggle">
						<input type="checkbox" name="manual_restore_email_enabled" value="1" <?php checked( ! empty( $s['manual_restore_email_enabled'] ) ); ?> id="wpps-br-manual-restore-email-enabled" />
						<span class="wpps-br-sched-toggle__slider"></span>
					</label>
				</div>
				<div class="wpps-br-sched-body" id="wpps-br-manual-restore-email-fields">
					<div class="wpps-br-sched-section">
						<div class="wpps-br-sched-section__label"><?php esc_html_e( 'Notify', 'wppowersuite-lite' ); ?></div>
						<div class="wpps-br-sched-section__fields">
							<div class="wpps-br-sched-type-pills">
								<label class="wpps-br-sched-type-pill">
									<input type="radio" name="manual_restore_email_on" value="both" <?php checked( $s['manual_restore_email_on'] ?? 'both', 'both' ); ?> />
									<span class="wpps-br-sched-type-pill__text"><?php esc_html_e( 'Success & failure', 'wppowersuite-lite' ); ?></span>
								</label>
								<label class="wpps-br-sched-type-pill">
									<input type="radio" name="manual_restore_email_on" value="failure" <?php checked( $s['manual_restore_email_on'] ?? '', 'failure' ); ?> />
									<span class="wpps-br-sched-type-pill__text"><?php esc_html_e( 'Failure only', 'wppowersuite-lite' ); ?></span>
								</label>
							</div>
						</div>
					</div>
					<div class="wpps-br-sched-section">
						<div class="wpps-br-sched-section__label"><?php esc_html_e( 'Recipients', 'wppowersuite-lite' ); ?></div>
						<div class="wpps-br-sched-section__fields">
							<textarea name="manual_restore_email_recipients" id="wpps-br-manual-restore-email-recipients" rows="2" class="wpps-br-sched-textarea" placeholder="<?php echo esc_attr( get_option( 'admin_email', '' ) ); ?>"><?php echo esc_textarea( (string) ( $s['manual_restore_email_recipients'] ?? '' ) ); ?></textarea>
							<p class="wpps-br-sched-hint"><?php esc_html_e( 'One address per line. Empty = site admin email.', 'wppowersuite-lite' ); ?></p>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div class="wpps-br-settings-actions">
			<button type="submit" class="button button-primary button-large"><?php esc_html_e( 'Save settings', 'wppowersuite-lite' ); ?></button>
		</div>
	</div>
</form>