<?php
/**
 * Admin settings page view.
 *
 * @package    100Prints
 */

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

// Check if settings were updated
if ( isset( $_GET['settings-updated'] ) ) {
	add_settings_error( '100p_messages', '100p_message', 'Settings Saved', 'updated' );
}

// Display messages
settings_errors( '100p_messages' );

$settings = hundred_prints_get_settings();
?>

<div class="wrap hundredp-wrap">

	<?php
	// Load shared page header
	$args = [ 'title' => 'Settings' ];
	require HUNDRED_PRINTS_PATH . 'admin/partials/header.php';
	?>

	<div class="hundredp-tabs">
		<nav class="hundredp-tab-nav">
			<a href="#general" class="hundredp-tab active">General</a>
			<a href="#performance" class="hundredp-tab">Performance</a>
			<a href="#privacy" class="hundredp-tab">Privacy</a>
		</nav>

		<div class="hundredp-tab-content">
			<form method="post" action="options.php">
				<?php settings_fields( '100p_settings_group' ); ?>

				<!-- Tab 1: General -->
				<section id="general" style="display:block;">
					<h3 style="margin-top:0;">General Customization</h3>
					<table class="form-table" role="presentation">
						<tr>
							<th scope="row"><label for="default_quality">Default Render Quality</label></th>
							<td>
								<select name="100p_settings[default_quality]" id="default_quality">
									<option value="draft" <?php selected( $settings['default_quality'], 'draft' ); ?>>Draft (72 DPI)</option>
									<option value="standard" <?php selected( $settings['default_quality'], 'standard' ); ?>>Standard (150 DPI)</option>
									<option value="high" <?php selected( $settings['default_quality'], 'high' ); ?>>High (300 DPI - Recommended)</option>
									<option value="ultra" <?php selected( $settings['default_quality'], 'ultra' ); ?>>Ultra (600 DPI)</option>
								</select>
								<p class="description">Sets the default output DPI of rendered PDFs and PNG images.</p>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="default_format">Default Format</label></th>
							<td>
								<select name="100p_settings[default_format]" id="default_format">
									<option value="pdf" <?php selected( $settings['default_format'], 'pdf' ); ?>>PDF Document</option>
									<option value="png" <?php selected( $settings['default_format'], 'png' ); ?>>PNG Image</option>
								</select>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="default_side">Export Pages</label></th>
							<td>
								<select name="100p_settings[default_side]" id="default_side">
									<option value="front" <?php selected( $settings['default_side'], 'front' ); ?>>Front Only</option>
									<option value="back" <?php selected( $settings['default_side'], 'back' ); ?>>Back Only</option>
									<option value="both" <?php selected( $settings['default_side'], 'both' ); ?>>Both Sides (If Available)</option>
								</select>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="filename_pattern">Document Filename Pattern</label></th>
							<td>
								<input type="text" name="100p_settings[filename_pattern]" id="filename_pattern" value="<?php echo esc_attr( $settings['filename_pattern'] ); ?>" class="regular-text" />
								<p class="description">Define the naming format. Supports tokens: <code>{template_name}</code>, <code>{date}</code>.</p>
							</td>
						</tr>
					</table>
				</section>

				<!-- Tab 2: Performance -->
				<section id="performance">
					<h3 style="margin-top:0;">Performance & Caching</h3>
					<table class="form-table" role="presentation">
						<tr>
							<th scope="row"><label for="cache_ttl_hours">Template Cache TTL (Hours)</label></th>
							<td>
								<input type="number" name="100p_settings[cache_ttl_hours]" id="cache_ttl_hours" value="<?php echo esc_attr( $settings['cache_ttl_hours'] ); ?>" class="small-text" min="1" max="168" />
								<p class="description">Duration to cache cloud template schema locally in WordPress transients (max 168 hours / 7 days).</p>
							</td>
						</tr>
						<tr>
							<th scope="row">Optimization Options</th>
							<td>
								<fieldset>
									<label for="preload_sdk_globally">
										<input type="checkbox" name="100p_settings[preload_sdk_globally]" id="preload_sdk_globally" value="1" <?php checked( $settings['preload_sdk_globally'], true ); ?> />
										Preload SDK on all pages (Not recommended - enqueues UMD scripts globally)
									</label>
								</fieldset>
							</td>
						</tr>
					</table>
				</section>

				<!-- Tab 3: Privacy -->
				<section id="privacy">
					<h3 style="margin-top:0;">Privacy & Logs</h3>
					<table class="form-table" role="presentation">
						<tr>
							<th scope="row">Generation Logging</th>
							<td>
								<fieldset>
									<label for="log_enabled">
										<input type="checkbox" name="100p_settings[log_enabled]" id="log_enabled" value="1" <?php checked( $settings['log_enabled'], true ); ?> />
										Log generation events locally (Records date, template, format, status in database)
									</label>
								</fieldset>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="log_retention_days">Log Retention (Days)</label></th>
							<td>
								<input type="number" name="100p_settings[log_retention_days]" id="log_retention_days" value="<?php echo esc_attr( $settings['log_retention_days'] ); ?>" class="small-text" min="1" max="365" />
								<p class="description">Number of days to keep logs in database before auto-pruning.</p>
							</td>
						</tr>
					</table>
				</section>
				<?php submit_button( 'Save Settings', 'hundredp-btn hundredp-btn-primary' ); ?>
			</form>
		</div>
	</div>
</div>
