<?php
/**
 * Settings page template.
 *
 * @var \MDCE\Settings  $settings
 * @var array          $available_types   WP_Post_Type[]
 * @var array          $supported_types   string[]
 * @var string         $default_action
 */

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

$is_pro = defined( 'MDCE_PRO_VERSION' );
?>
<div class="spe-settings-page">

	<?php settings_errors( 'mdce_settings' ); ?>

	<!-- Header -->
	<div class="spe-settings-header">
		<div class="spe-settings-header__brand">
			<div class="spe-settings-header__icon">
				<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
					<circle cx="12" cy="12" r="10"></circle>
					<polyline points="12 6 12 12 16 14"></polyline>
				</svg>
			</div>
			<div>
				<div class="spe-settings-header__title"><?php esc_html_e( 'Content Expiry', 'monkeydesign-content-expiry' ); ?></div>
				<div class="spe-settings-header__sub"><?php esc_html_e( 'by MonkeyDesign', 'monkeydesign-content-expiry' ); ?></div>
			</div>
		</div>
		<div class="spe-settings-header__meta">
			<span class="spe-badge <?php echo esc_attr( $is_pro ? 'spe-badge--pro' : 'spe-badge--free' ); ?>">
				<?php echo $is_pro ? esc_html__( 'PRO', 'monkeydesign-content-expiry' ) : esc_html__( 'FREE', 'monkeydesign-content-expiry' ); ?>
			</span>
			<span class="spe-settings-version">v<?php echo esc_html( MDCE_VERSION ); ?></span>
		</div>
	</div>

	<!-- Body -->
	<div class="spe-settings-body">

		<!-- Main column -->
		<div class="spe-settings-main">
			<form method="post">
				<?php wp_nonce_field( 'spe_save_settings', 'spe_settings_nonce' ); ?>

				<div class="spe-card">
					<div class="spe-card__header">
						<h2 class="spe-card__title"><?php esc_html_e( 'General Settings', 'monkeydesign-content-expiry' ); ?></h2>
					</div>
					<div class="spe-card__body">

						<div class="spe-settings-field">
							<span class="spe-settings-label"><?php esc_html_e( 'Supported post types', 'monkeydesign-content-expiry' ); ?></span>
							<div class="spe-check-list">
								<?php foreach ( $available_types as $type ) : ?>
									<label class="spe-check-item">
										<input
											type="checkbox"
											name="post_types[]"
											value="<?php echo esc_attr( $type->name ); ?>"
											<?php checked( in_array( $type->name, $supported_types, true ) ); ?>
										>
										<?php echo esc_html( $type->label ); ?>
										<code><?php echo esc_html( $type->name ); ?></code>
									</label>
								<?php endforeach; ?>
							</div>
						</div>

						<div class="spe-settings-field">
							<label class="spe-settings-label" for="spe_default_action">
								<?php esc_html_e( 'Default expiration action', 'monkeydesign-content-expiry' ); ?>
							</label>
							<select id="spe_default_action" name="default_action" class="spe-settings-select">
								<option value="draft"    <?php selected( $default_action, 'draft' ); ?>>
									<?php esc_html_e( 'Change to Draft', 'monkeydesign-content-expiry' ); ?>
								</option>
								<option value="private"  <?php selected( $default_action, 'private' ); ?>>
									<?php esc_html_e( 'Make Private', 'monkeydesign-content-expiry' ); ?>
								</option>
								<option value="trash"    <?php selected( $default_action, 'trash' ); ?>>
									<?php esc_html_e( 'Move to Trash', 'monkeydesign-content-expiry' ); ?>
								</option>
								<option value="delete"   <?php selected( $default_action, 'delete' ); ?>>
									<?php esc_html_e( 'Delete permanently', 'monkeydesign-content-expiry' ); ?>
								</option>
								<option value="category" <?php selected( $default_action, 'category' ); ?>>
									<?php esc_html_e( 'Change category', 'monkeydesign-content-expiry' ); ?>
								</option>
								<option value="title"    <?php selected( $default_action, 'title' ); ?>>
									<?php esc_html_e( 'Change title', 'monkeydesign-content-expiry' ); ?>
								</option>
								<optgroup label="✦ PRO">
									<?php if ( defined( 'MDCE_PRO_VERSION' ) ) : ?>
										<?php do_action( 'mdce_settings_action_options', $default_action ); ?>
									<?php else : ?>
										<option value="" disabled><?php esc_html_e( 'Redirect to URL', 'monkeydesign-content-expiry' ); ?></option>
										<option value="" disabled><?php esc_html_e( 'Replace content', 'monkeydesign-content-expiry' ); ?></option>
									<?php endif; ?>
								</optgroup>
							</select>
							<p class="spe-settings-desc">
								<?php esc_html_e( 'Applied when no specific action is set on a post.', 'monkeydesign-content-expiry' ); ?>
							</p>
						</div>

					</div>
					<div class="spe-card__footer">
						<button type="submit" class="spe-btn spe-btn--primary">
							<?php esc_html_e( 'Save Settings', 'monkeydesign-content-expiry' ); ?>
						</button>
					</div>
				</div>

				<?php do_action( 'mdce_settings_fields' ); ?>

			</form>

			<?php $server_cron = defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON; ?>
			<div class="spe-card">
				<div class="spe-card__header">
					<h3 class="spe-card__title"><?php esc_html_e( 'System Status', 'monkeydesign-content-expiry' ); ?></h3>
				</div>
				<div class="spe-card__body">
					<div class="spe-status-indicator">
						<span class="spe-status-dot <?php echo esc_attr( $server_cron ? 'spe-status-dot--ok' : 'spe-status-dot--warn' ); ?>"></span>
						<div>
							<p class="spe-status-label">
								<?php echo $server_cron
									? esc_html__( 'Server cron active', 'monkeydesign-content-expiry' )
									: esc_html__( 'WP-Cron in use', 'monkeydesign-content-expiry' );
								?>
							</p>
							<p class="spe-status-desc">
								<?php if ( $server_cron ) : ?>
									<?php esc_html_e( 'Expirations and notifications run reliably regardless of site traffic.', 'monkeydesign-content-expiry' ); ?>
								<?php else : ?>
									<?php esc_html_e( 'Tasks only run when the site receives a visit. On low-traffic sites, expirations may be delayed. For guaranteed reliability, set up a real server cron job:', 'monkeydesign-content-expiry' ); ?>
									<div class="spe-cron-command">*/5 * * * * php <?php echo esc_html( ABSPATH ); ?>wp-cron.php &gt;/dev/null 2&gt;&amp;1</div>
								<?php endif; ?>
							</p>
						</div>
					</div>
				</div>
			</div>

		</div><!-- .spe-settings-main -->

		<!-- Sidebar -->
		<div class="spe-sidebar">

			<?php if ( ! $is_pro ) : ?>
			<div class="spe-card spe-pro-card">
				<div class="spe-card__header">
					<h3 class="spe-card__title">&#10024; <?php esc_html_e( 'Upgrade to PRO', 'monkeydesign-content-expiry' ); ?></h3>
				</div>
				<div class="spe-card__body">
					<ul class="spe-pro-feature-list">
						<li><?php esc_html_e( 'Email notifications before expiry', 'monkeydesign-content-expiry' ); ?></li>
						<li><?php esc_html_e( 'Redirect expired posts to a custom URL', 'monkeydesign-content-expiry' ); ?></li>
						<li><?php esc_html_e( 'Replace post content after expiry', 'monkeydesign-content-expiry' ); ?></li>
						<li><?php esc_html_e( 'Expired post activity log', 'monkeydesign-content-expiry' ); ?></li>
					</ul>
					<a href="https://monkeydesign.hu/en/products/content-expiry" class="spe-btn spe-btn--upgrade" target="_blank" rel="noopener noreferrer">
						<?php esc_html_e( 'Get PRO', 'monkeydesign-content-expiry' ); ?> &rarr;
					</a>
				</div>
			</div>
			<?php endif; ?>

			<div class="spe-card">
				<div class="spe-card__body">
					<ul class="spe-info-list">
						<li>
							<span class="spe-info-label"><?php esc_html_e( 'Version', 'monkeydesign-content-expiry' ); ?></span>
							<span class="spe-info-value"><?php echo esc_html( MDCE_VERSION ); ?></span>
						</li>
						<li>
							<span class="spe-info-label"><?php esc_html_e( 'License', 'monkeydesign-content-expiry' ); ?></span>
							<span class="spe-info-value"><?php echo $is_pro ? esc_html__( 'PRO', 'monkeydesign-content-expiry' ) : esc_html__( 'Free', 'monkeydesign-content-expiry' ); ?></span>
						</li>
						<li>
							<span class="spe-info-label"><?php esc_html_e( 'Author', 'monkeydesign-content-expiry' ); ?></span>
							<span class="spe-info-value"><a href="https://monkeydesign.hu/en/" target="_blank" rel="noopener noreferrer">MonkeyDesign</a></span>
						</li>
					</ul>
				</div>
			</div>

		</div><!-- .spe-sidebar -->

	</div><!-- .spe-settings-body -->


</div><!-- .spe-settings-page -->
