<?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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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”)', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>
						<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)', 'wp-powersuite' ); ?>
					</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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>
						<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)', 'wp-powersuite' ); ?>
					</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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
								</label>
							</div>
						</div>
					</div>
					<div class="wpps-br-sched-section">
						<div class="wpps-br-sched-section__label"><?php esc_html_e( 'Recipients', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
								</label>
							</div>
						</div>
					</div>
					<div class="wpps-br-sched-section">
						<div class="wpps-br-sched-section__label"><?php esc_html_e( 'Recipients', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></button>
		</div>
	</div>
</form>