<?php
/**
 * Settings page view.
 *
 * @package PrivexPasswordProtect
 */

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

$ppmstr_s     = PPMSTR_Settings::get_all();
$ppmstr_is_pro = function_exists( 'ppp_fs' ) && ppp_fs()->can_use_premium_code__premium_only();
$ppmstr_saved = isset( $_GET['ppmstr_saved'] ) && '1' === $_GET['ppmstr_saved']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

// Get all roles for role-based bypass.
global $wp_roles;
$ppmstr_all_roles = $wp_roles->get_names();

// Get custom templates for default template selection.
$ppmstr_custom_tpls = get_option( 'ppmstr_custom_templates', array() );
$ppmstr_presets     = class_exists( 'PPMSTR_Template_Builder' ) ? PPMSTR_Template_Builder::get_presets() : array();
?>

<div class="ppmstr-page-header">
	<h2><?php esc_html_e( 'Settings', 'privex-password-protect' ); ?></h2>
	<p class="ppmstr-page-desc"><?php esc_html_e( 'Configure security, SEO visibility, and advanced plugin behavior.', 'privex-password-protect' ); ?></p>
</div>

<?php if ( $ppmstr_saved ) : ?>
	<div class="ppmstr-notice ppmstr-notice--success">
		<?php esc_html_e( 'Settings saved successfully.', 'privex-password-protect' ); ?>
	</div>
<?php endif; ?>

<form method="post" action="" class="ppmstr-settings-form">
	<?php wp_nonce_field( 'ppmstr_save_settings', 'ppmstr_settings_nonce' ); ?>
	<input type="hidden" name="ppmstr_redirect_to" value="ppmstr-settings" />

	<!-- Role-Based Bypass -->
	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Role-Based Bypass', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-admin-bypass"><?php esc_html_e( 'Admin Bypass', 'privex-password-protect' ); ?></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Logged-in administrators automatically bypass protection.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input">
					<label class="ppmstr-toggle-wrap">
						<input type="checkbox" name="ppmstr_admin_bypass" id="ppmstr-admin-bypass" value="1" <?php checked( $ppmstr_s['admin_bypass'] ); ?> />
						<span class="ppmstr-toggle-slider"></span>
					</label>
				</div>
			</div>
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label><?php esc_html_e( 'Bypass Roles', 'privex-password-protect' ); ?> <span class="ppmstr-pro-badge">PRO</span></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Selected user roles will bypass password protection when logged in.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input <?php echo ! $ppmstr_is_pro ? 'ppmstr-pro-locked-wrap' : ''; ?>">
					<div class="ppmstr-checkbox-group">
						<?php foreach ( $ppmstr_all_roles as $ppmstr_role_key => $ppmstr_role_name ) : ?>
							<?php if ( 'administrator' === $ppmstr_role_key ) continue; ?>
							<label class="ppmstr-checkbox-label">
								<input type="checkbox" disabled value="<?php echo esc_attr( $ppmstr_role_key ); ?>" />
								<?php echo esc_html( $ppmstr_role_name ); ?>
							</label>
						<?php endforeach; ?>
					</div>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<!-- Security & Anti-Spam -->
	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Security & Anti-Spam', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-limit-toggle"><?php esc_html_e( 'Brute Force Protection', 'privex-password-protect' ); ?></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Temporarily block users after multiple failed password attempts.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input">
					<label class="ppmstr-toggle-wrap">
						<input type="checkbox" name="ppmstr_limit_login_enabled" id="ppmstr-limit-toggle" value="1" <?php checked( $ppmstr_s['limit_login_enabled'] ); ?> />
						<span class="ppmstr-toggle-slider"></span>
					</label>
					<div id="ppmstr-limit-controls" style="margin-top:15px; display: <?php echo $ppmstr_s['limit_login_enabled'] ? 'block' : 'none'; ?>;">
						<div class="ppmstr-flex ppmstr-flex-gap-small" style="align-items:center; margin-bottom:10px;">
							<span style="font-size:13px; color:#6C7293;"><?php esc_html_e( 'Max Attempts:', 'privex-password-protect' ); ?></span>
							<input type="number" name="ppmstr_limit_login_max" value="<?php echo absint( $ppmstr_s['limit_login_max'] ); ?>" min="1" max="20" style="width:70px;" />
						</div>
						<div class="ppmstr-flex ppmstr-flex-gap-small" style="align-items:center;">
							<span style="font-size:13px; color:#6C7293;"><?php esc_html_e( 'Lockout Duration (min):', 'privex-password-protect' ); ?></span>
							<input type="number" name="ppmstr_limit_login_lockout" value="<?php echo absint( $ppmstr_s['limit_login_lockout'] ); ?>" min="1" max="1440" style="width:70px;" />
						</div>
					</div>
				</div>
			</div>
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-ip-whitelist"><?php esc_html_e( 'IP Whitelisting', 'privex-password-protect' ); ?> <span class="ppmstr-pro-badge">PRO</span></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Enter IP addresses (one per line) that should always bypass protection.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input <?php echo ! $ppmstr_is_pro ? 'ppmstr-pro-locked-wrap' : ''; ?>">
					<textarea id="ppmstr-ip-whitelist" rows="3" disabled placeholder="<?php esc_attr_e( "e.g. 192.168.1.1\n203.0.113.5", 'privex-password-protect' ); ?>"></textarea>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<!-- SEO & Privacy -->
	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'SEO & Privacy', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-no-index"><?php esc_html_e( 'No-Index Protected Content', 'privex-password-protect' ); ?></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Ask search engines not to index pages that are password protected.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input">
					<label class="ppmstr-toggle-wrap">
						<input type="checkbox" name="ppmstr_no_index_protected" id="ppmstr-no-index" value="1" <?php checked( $ppmstr_s['no_index_protected'] ); ?> />
						<span class="ppmstr-toggle-slider"></span>
					</label>
				</div>
			</div>
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-hide-rss"><?php esc_html_e( 'Hide from RSS Feeds', 'privex-password-protect' ); ?></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Exclude password-protected posts from your site\'s RSS feeds.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input">
					<label class="ppmstr-toggle-wrap">
						<input type="checkbox" name="ppmstr_hide_from_rss" id="ppmstr-hide-rss" value="1" <?php checked( $ppmstr_s['hide_from_rss'] ); ?> />
						<span class="ppmstr-toggle-slider"></span>
					</label>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<!-- Global Design -->
	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Default Template', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-default-tpl"><?php esc_html_e( 'Active Template', 'privex-password-protect' ); ?></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Select the default template for all password-protected pages.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input">
					<select name="ppmstr_active_template" id="ppmstr-default-tpl">
						<optgroup label="<?php esc_attr_e( 'Presets', 'privex-password-protect' ); ?>">
								<?php foreach ( $ppmstr_presets as $ppmstr_pid => $ppmstr_pdata ) : 
									$ppmstr_p_name = $ppmstr_pdata['name'];
									if ( ! empty( $ppmstr_pdata['pro'] ) ) {
										$ppmstr_p_name .= ' [PRO]';
									}
								?>
									<option value="<?php echo esc_attr( $ppmstr_pid ); ?>" <?php selected( $ppmstr_s['active_template'], $ppmstr_pid ); ?>>
										<?php echo esc_html( $ppmstr_p_name ); ?>
									</option>
								<?php endforeach; ?>
						</optgroup>
						<?php if ( ! empty( $ppmstr_custom_tpls ) ) : ?>
							<optgroup label="<?php esc_attr_e( 'Custom Templates', 'privex-password-protect' ); ?>">
								<?php foreach ( $ppmstr_custom_tpls as $ppmstr_cid => $ppmstr_cdata ) : ?>
									<option value="<?php echo esc_attr( $ppmstr_cid ); ?>" <?php selected( $ppmstr_s['active_template'], $ppmstr_cid ); ?>>
										<?php echo esc_html( $ppmstr_cdata['name'] ); ?>
									</option>
								<?php endforeach; ?>
							</optgroup>
						<?php endif; ?>
					</select>
					<p class="ppmstr-field-hint" style="margin-top:8px;">
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=ppmstr-templates' ) ); ?>"><?php esc_html_e( 'Manage templates &raquo;', 'privex-password-protect' ); ?></a>
					</p>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<!-- Cookie Settings -->
	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Cookie Settings', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label"><label for="ppmstr-cookie-type"><?php esc_html_e( 'Cookie Type', 'privex-password-protect' ); ?></label></div>
				<div class="ppmstr-field-input">
					<select name="ppmstr_cookie_type" id="ppmstr-cookie-type">
						<option value="persistent" <?php selected( $ppmstr_s['cookie_type'], 'persistent' ); ?>><?php esc_html_e( 'Persistent', 'privex-password-protect' ); ?></option>
						<option value="session" <?php selected( $ppmstr_s['cookie_type'], 'session' ); ?>><?php esc_html_e( 'Session', 'privex-password-protect' ); ?></option>
					</select>
				</div>
			</div>
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label"><label for="ppmstr-cookie-dur"><?php esc_html_e( 'Cookie Duration (hours)', 'privex-password-protect' ); ?></label></div>
				<div class="ppmstr-field-input">
					<input type="number" name="ppmstr_cookie_duration" id="ppmstr-cookie-dur" value="<?php echo absint( $ppmstr_s['cookie_duration'] ); ?>" min="1" />
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<!-- Advanced -->
	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Advanced Settings', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-delete-uninstall"><?php esc_html_e( 'Delete Data on Uninstall', 'privex-password-protect' ); ?></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Remove all templates, passwords, and settings when the plugin is deleted.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input">
					<label class="ppmstr-toggle-wrap">
						<input type="checkbox" name="ppmstr_delete_on_uninstall" id="ppmstr-delete-uninstall" value="1" <?php checked( $ppmstr_s['delete_on_uninstall'] ); ?> />
						<span class="ppmstr-toggle-slider"></span>
					</label>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
	const limitToggle = document.getElementById('ppmstr-limit-toggle');
	const limitControls = document.getElementById('ppmstr-limit-controls');
	if (limitToggle && limitControls) {
		limitToggle.addEventListener('change', function() {
			limitControls.style.display = this.checked ? 'block' : 'none';
		});
	}
});
</script>
