<?php
/**
 * Main Admin Page View.
 */
if (!defined('ABSPATH')) {
	exit;
}
?>
<div class="wpsh-wrapper">
	<div class="wpsh-sidebar">
		<div class="wpsh-logo">
			<span class="dashicons dashicons-html"></span>
			<span>Static HTML</span>
		</div>
		<nav class="wpsh-nav">
			<a href="#" class="wpsh-nav-item active" data-tab="dashboard">
				<span class="dashicons dashicons-dashboard"></span> Dashboard
			</a>
			<a href="#" class="wpsh-nav-item" data-tab="cache">
				<span class="dashicons dashicons-database"></span> Cache Manager
			</a>
			<a href="#" class="wpsh-nav-item" data-tab="logs">
				<span class="dashicons dashicons-media-text"></span> Logs
			</a>
			<a href="#" class="wpsh-nav-item" data-tab="redirections">
				<span class="dashicons dashicons-randomize"></span> Redirections
			</a>
		</nav>
		<div class="wpsh-footer">
			v1.0.0
		</div>
	</div>

	<div class="wpsh-content">
		<header class="wpsh-header">
			<h1>Dashboard</h1>
			<div class="wpsh-actions">
				<button id="wpsh-generate-btn-header" class="wpsh-btn wpsh-btn-primary">
					<span class="dashicons dashicons-controls-play"></span> Generate All
				</button>
			</div>
		</header>

		<!-- Dashboard Tab -->
		<div id="tab-dashboard" class="wpsh-tab-content active">
			<div class="wpsh-grid">
				<div class="wpsh-card wpsh-card-hero">
					<div class="wpsh-hero-content">
						<h2>Static Site Generation</h2>
						<p>Turn your WordPress site into a blazing fast static HTML site. Secure, fast, and scalable.
						</p>

						<?php
						$wpsh_last_gen = get_option('wpsh_last_generated', '');
						$wpsh_date_format = get_option('date_format') . ' ' . get_option('time_format');
						?>
						<p class="wpsh-last-generated"
							style="font-size: 13px; color: #64748b; margin-top: -5px; margin-bottom: 20px;">
							<strong>Last generated:</strong> <span
								id="wpsh-last-gen-time"><?php echo $wpsh_last_gen ? esc_html(date_i18n($wpsh_date_format, $wpsh_last_gen)) : 'Never run'; ?></span>
						</p>

						<button id="wpsh-generate-btn" class="wpsh-btn wpsh-btn-primary wpsh-btn-large">
							Start Generation
						</button>
					</div>
					<div class="wpsh-hero-icon">
						<span class="dashicons dashicons-cloud"></span>
					</div>
				</div>

				<div class="wpsh-card">
					<h3><span class="dashicons dashicons-admin-settings"></span> Manage Post Type Exclusions</h3>
					<div class="wpsh-post-types-grid"
						style="display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; margin: 15px 0; padding: 12px; background: #fafafa; border-radius: 6px; border: 1px solid #eee;">
						<?php
						$wpsh_excluded_types = (array) get_option('wpsh_excluded_post_types', []);
						foreach (get_post_types(['public' => true], 'objects') as $wpsh_type):
							if (in_array($wpsh_type->name, ['attachment']))
								continue;
							?>
							<label
								style="display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 4px 0; font-size: 13px;">
								<input type="checkbox" class="wpsh-exclude-post-type"
									value="<?php echo esc_attr($wpsh_type->name); ?>" <?php checked(in_array($wpsh_type->name, $wpsh_excluded_types)); ?>>
								<?php echo esc_html($wpsh_type->label); ?>
							</label>
						<?php endforeach; ?>
					</div>
				</div>

				<div class="wpsh-card">
					<h3><span class="dashicons dashicons-category"></span> Manage Category / Tag Exclusions</h3>
					<div class="wpsh-tax-grid"
						style="display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; margin: 15px 0; padding: 12px; background: #fafafa; border-radius: 6px; border: 1px solid #eee;">
						<?php
						$wpsh_excluded_tax = (array) get_option('wpsh_excluded_taxonomies', []);
						foreach (get_taxonomies(['public' => true], 'objects') as $wpsh_tax):
							?>
							<label
								style="display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 4px 0; font-size: 13px;">
								<input type="checkbox" class="wpsh-exclude-tax"
									value="<?php echo esc_attr($wpsh_tax->name); ?>" <?php checked(in_array($wpsh_tax->name, $wpsh_excluded_tax)); ?>>
								<?php echo esc_html($wpsh_tax->label); ?>
							</label>
						<?php endforeach; ?>
					</div>
				</div>

				<div class="wpsh-card">
					<h3><span class="dashicons dashicons-performance"></span> Performance Status</h3>
					<?php if (WPSH_Htaccess::check_status()): ?>
						<div class="wpsh-status-badge wpsh-active">
							<span class="dashicons dashicons-yes-alt"></span> High Performance Active (.htaccess)
						</div>
						<p class="description">Serving static files directly via Apache. 0 Database queries for guests.</p>
					<?php else: ?>
						<div class="wpsh-status-badge wpsh-inactive">
							<span class="dashicons dashicons-warning"></span> PHP Fallback Mode
						</div>
						<p class="description">Apache rules missing. Serving via PHP (Minor DB load).</p>
					<?php endif; ?>
					<button id="wpsh-htaccess-btn" class="wpsh-btn wpsh-btn-secondary" style="width: 100%;">
						<span class="dashicons dashicons-admin-network"></span> Update .htaccess Rules
					</button>
				</div>

				<div class="wpsh-card" id="wpsh-nonce-auto-refresh-card">
					<h3><span class="dashicons dashicons-update"></span> Nonce Auto Refresh</h3>
					<p class="description">
						Automatically refresh AJAX nonces in long-running admin sessions to reduce invalid nonce errors.
					</p>
					<?php
					$wpsh_nonce_auto_refresh_enabled = (bool) get_option('wpsh_nonce_auto_refresh_enabled', true);
					$wpsh_nonce_auto_refresh_interval_ms = (int) get_option('wpsh_nonce_auto_refresh_interval_ms', 5 * MINUTE_IN_SECONDS * 1000);
					$wpsh_nonce_interval_options = array(
						5 * MINUTE_IN_SECONDS * 1000  => 'Every 5 minutes',
						15 * MINUTE_IN_SECONDS * 1000 => 'Every 15 minutes',
						30 * MINUTE_IN_SECONDS * 1000 => 'Every 30 minutes',
						HOUR_IN_SECONDS * 1000        => 'Every 1 hour',
						2 * HOUR_IN_SECONDS * 1000    => 'Every 2 hours',
						6 * HOUR_IN_SECONDS * 1000    => 'Every 6 hours',
					);
					if (!isset($wpsh_nonce_interval_options[$wpsh_nonce_auto_refresh_interval_ms])) {
						$wpsh_nonce_auto_refresh_interval_ms = 5 * MINUTE_IN_SECONDS * 1000;
					}
					?>
					<div id="wpsh-nonce-auto-refresh-controls" style="display:flex; flex-wrap:wrap; gap:18px; align-items:center; margin-top:12px;">
						<label style="display:flex; align-items:center; gap:8px; cursor:pointer; font-size:13px;">
							<input type="checkbox" id="wpsh-nonce-auto-refresh-enabled" <?php checked($wpsh_nonce_auto_refresh_enabled); ?>>
							Enable nonce auto refresh
						</label>
						<div style="display:flex; align-items:center; gap:8px;">
							<select id="wpsh-nonce-auto-refresh-interval" class="wpsh-input" style="min-width:240px;">
								<?php foreach ($wpsh_nonce_interval_options as $wpsh_interval_value => $wpsh_interval_label): ?>
									<option value="<?php echo esc_attr($wpsh_interval_value); ?>" <?php selected($wpsh_nonce_auto_refresh_interval_ms, $wpsh_interval_value); ?>>
										<?php echo esc_html($wpsh_interval_label); ?>
									</option>
								<?php endforeach; ?>
							</select>
						</div>
					</div>
					<div style="font-size:12px; color:#64748b; margin-top:8px;">
						Saved with the main "Save All Settings" button below.
					</div>
				</div>

				<div class="wpsh-card" style="grid-column: 1 / -1;">
					<h3><span class="dashicons dashicons-admin-page"></span> CPT Listing Page Mapping</h3>
					<p class="description">
						Map a CPT to the <code>Page</code> URL that lists its items (example: set <code>portfolio</code> to <code>/portfolio-list/</code>).
						When new CPT items are published/updated, the plugin will regenerate the mapped Page static HTML automatically.
					</p>
					
					<div class="wpsh-post-types-grid"
						style="display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; margin: 15px 0; padding: 12px; background: #fafafa; border-radius: 6px; border: 1px solid #eee;">
						<?php
						$wpsh_cpt_listing_page_map = (array) get_option('wpsh_cpt_listing_page_map', []);
						foreach (get_post_types(['public' => true], 'objects') as $wpsh_type):
							if (in_array($wpsh_type->name, ['attachment'], true)) continue;
							$wpsh_val = isset($wpsh_cpt_listing_page_map[$wpsh_type->name]) ? (string) $wpsh_cpt_listing_page_map[$wpsh_type->name] : '';
							?>
							<div style="display:flex; flex-direction:column; gap:6px;">
								<label style="font-size:13px; font-weight:600;"><?php echo esc_html($wpsh_type->label); ?></label>
								<textarea
									class="wpsh-input wpsh-cpt-listing-url-input"
									data-post-type="<?php echo esc_attr($wpsh_type->name); ?>"
									rows="3"
									placeholder="/<?php echo esc_attr($wpsh_type->name); ?>-list/ or Page ID&#10;(one per line)"><?php echo esc_textarea($wpsh_val); ?></textarea>

							</div>
						<?php endforeach; ?>
					</div>
					<div style="font-size:12px; color:#64748b;">
						Leave blank to disable. You can also paste a Page ID or full URL.
					</div>
				</div>

				<div style="grid-column: 1 / -1; display: flex; justify-content: flex-start;">
					<button id="wpsh-save-settings-btn" class="wpsh-btn wpsh-btn-primary">
						<span class="dashicons dashicons-saved"></span> Save All Settings
					</button>
				</div>
			</div>

			<div class="wpsh-section">
				<h3><span class="dashicons dashicons-list-view"></span> Generation Log</h3>
				<div id="wpsh-progress" class="wpsh-progress-box" style="display: none;">
					<div class="wpsh-status-line">
						<strong id="wpsh-status-text">Ready</strong>
						<div class="wpsh-progress-controls" style="display: flex; gap: 10px; align-items: center;">
							<button id="wpsh-pause-btn" class="wpsh-btn wpsh-btn-secondary"
								style="display: none;">Pause</button>
							<button id="wpsh-resume-btn" class="wpsh-btn wpsh-btn-primary"
								style="display: none;">Resume</button>
							<button id="wpsh-cancel-btn" class="wpsh-btn wpsh-btn-danger"
								style="display: none;">Cancel</button>
						</div>
						<span id="wpsh-percent">0%</span>
					</div>
					<div class="wpsh-progress-container">
						<div id="wpsh-progress-bar" class="wpsh-progress-bar"></div>
					</div>
					<div class="wpsh-log-window"></div>
				</div>
			</div>
		</div>

		<!-- Cache Tab -->
		<div id="tab-cache" class="wpsh-tab-content">
			<div class="wpsh-card">
				<h3>Clear Cache</h3>
				<p>Remove all generated static HTML files. They will be regenerated on demand or via the generation
					tool.</p>
				<button id="wpsh-clear-btn" class="wpsh-btn wpsh-btn-danger">Clear All Cache</button>
			</div>
		</div>

		<!-- Logs Tab -->
		<div id="tab-logs" class="wpsh-tab-content">
			<div class="wpsh-card">
				<div class="wpsh-card-header">
					<h3><span class="dashicons dashicons-media-text"></span> System Logs</h3>
					<div class="wpsh-card-actions">
						<button id="wpsh-logs-refresh" class="wpsh-btn wpsh-btn-secondary">
							<span class="dashicons dashicons-update"></span> Refresh
						</button>
						<button id="wpsh-logs-export" class="wpsh-btn wpsh-btn-secondary">
							<span class="dashicons dashicons-download"></span> Export CSV
						</button>
						<button id="wpsh-logs-clear" class="wpsh-btn wpsh-btn-danger">
							<span class="dashicons dashicons-trash"></span> Clear All
						</button>
					</div>
				</div>

				<!-- Statistics -->
				<div class="wpsh-stats-grid">
					<div class="wpsh-stat-item">
						<div class="wpsh-stat-value" id="wpsh-logs-total">0</div>
						<div class="wpsh-stat-label">Total Logs</div>
					</div>
					<div class="wpsh-stat-item">
						<div class="wpsh-stat-value" id="wpsh-logs-24h">0</div>
						<div class="wpsh-stat-label">Last 24 Hours</div>
					</div>
					<div class="wpsh-stat-item">
						<div class="wpsh-stat-value" id="wpsh-logs-errors">0</div>
						<div class="wpsh-stat-label">Errors</div>
					</div>
					<div class="wpsh-stat-item">
						<div class="wpsh-stat-value" id="wpsh-logs-success">0</div>
						<div class="wpsh-stat-label">Success</div>
					</div>
				</div>

				<!-- Filters -->
				<div class="wpsh-filters">
					<div class="wpsh-filter-group">
						<label>Level:</label>
						<select id="wpsh-log-level-filter" class="wpsh-input">
							<option value="">All Levels</option>
							<option value="info">Info</option>
							<option value="success">Success</option>
							<option value="warning">Warning</option>
							<option value="error">Error</option>
							<option value="deleted">Deleted</option>
							<option value="debug">Debug</option>
						</select>
					</div>
					<div class="wpsh-filter-group">
						<label>Search:</label>
						<input type="text" id="wpsh-log-search" class="wpsh-input" placeholder="Search logs...">
					</div>
					<div class="wpsh-filter-group">
						<label>Date From:</label>
						<input type="date" id="wpsh-log-date-from" class="wpsh-input">
					</div>
					<div class="wpsh-filter-group">
						<label>Date To:</label>
						<input type="date" id="wpsh-log-date-to" class="wpsh-input">
					</div>
					<div class="wpsh-filter-group">
						<button id="wpsh-logs-filter-btn" class="wpsh-btn wpsh-btn-primary">Apply Filters</button>
						<button id="wpsh-logs-reset-btn" class="wpsh-btn wpsh-btn-secondary">Reset</button>
					</div>
				</div>

				<!-- Logs Table -->
				<div class="wpsh-table-container">
					<table id="wpsh-logs-table" class="wpsh-table">
						<thead>
							<tr>
								<th>Timestamp</th>
								<th>Level</th>
								<th>Message</th>
								<th>Function</th>
								<th>File</th>
								<th>Line</th>
								<th>User</th>
								<th>IP Address</th>
								<th>Memory</th>
								<th>Actions</th>
							</tr>
						</thead>
						<tbody id="wpsh-logs-tbody">
							<tr>
								<td colspan="10" class="wpsh-loading">Loading logs...</td>
							</tr>
						</tbody>
					</table>
				</div>

				<!-- Pagination -->
				<div class="wpsh-pagination">
					<div class="wpsh-pagination-info">
						<span id="wpsh-logs-showing">Showing 0 of 0 entries</span>
					</div>
					<div class="wpsh-pagination-controls">
						<button id="wpsh-logs-prev" class="wpsh-btn wpsh-btn-secondary" disabled>Previous</button>
						<span class="wpsh-page-numbers">
							<span id="wpsh-logs-current-page">1</span> of <span id="wpsh-logs-total-pages">1</span>
						</span>
						<button id="wpsh-logs-next" class="wpsh-btn wpsh-btn-secondary" disabled>Next</button>
					</div>
				</div>
			</div>
		</div>


		<!-- Redirections Tab -->
		<div id="tab-redirections" class="wpsh-tab-content">
			<!-- Redirection content here ... -->
			<?php include 'tab-redirections.php'; ?>
		</div>

		<!-- Edit Modal -->
		<div id="wpsh-edit-modal" class="wpsh-modal">
			<div class="wpsh-modal-content">
				<span class="wpsh-modal-close">&times;</span>
				<h3>Edit Redirection</h3>
				<input type="hidden" id="wpsh-edit-idx">
				<div class="wpsh-form-group">
					<label>Source Path</label>
					<input type="text" id="wpsh-edit-source" class="wpsh-input">
				</div>
				<div class="wpsh-form-group">
					<label>Target URL</label>
					<input type="text" id="wpsh-edit-target" class="wpsh-input">
				</div>
				<div class="wpsh-form-group">
					<label>Redirect Type</label>
					<select id="wpsh-edit-code" class="wpsh-input">
						<option value="301">301 (Permanent)</option>
						<option value="302">302 (Found)</option>
						<option value="307">307 (Temporary)</option>
					</select>
				</div>
				<div class="wpsh-actions" style="margin-top: 20px; text-align: right;">
					<button id="wpsh-save-edit-btn" class="wpsh-btn wpsh-btn-primary">Save Changes</button>
				</div>
			</div>
		</div>
	</div>
</div>