<?php
/**
 * Subscribe Button & Modal Settings tab display.
 *
 * @package Subs_Sys
 */

if ( ! defined( 'WPINC' ) ) {
	die;
}

// Get all available public post types (excluding built-in ones already covered).
$public_post_types = get_post_types( array( 'public' => true ), 'objects' );
unset( $public_post_types['post'], $public_post_types['page'], $public_post_types['attachment'] );

// Get all WordPress pages.
$all_pages = get_pages( array( 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) );
?>

<form method="post" action="options.php">
	<?php settings_fields( 'subscription_modal_settings' ); ?>

	<h2><?php esc_html_e( 'Subscribe Button & Modal Settings', 'subscription-system' ); ?></h2>
	<p class="description"><?php esc_html_e( 'Configure how and where the subscription modal and triggers appear on your site.', 'subscription-system' ); ?></p>

	<!-- ── TRIGGER OPTIONS ─────────────────────────────────────────────── -->
	<h3 class="ss-settings-section-title"><?php esc_html_e( '1. Trigger Options', 'subscription-system' ); ?></h3>
	<table class="form-table">

		<tr>
			<th scope="row"><?php esc_html_e( 'Auto-Open Modal on Page Load', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_auto_open" value="1"
						<?php checked( 1, get_option( 'subs_modal_auto_open', 0 ) ); ?>>
					<?php esc_html_e( 'Automatically open the subscription modal when the page loads', 'subscription-system' ); ?>
				</label>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Auto-Open Delay (seconds)', 'subscription-system' ); ?></th>
			<td>
				<input type="number" name="subs_modal_auto_open_delay" min="0" max="60" step="1"
					value="<?php echo esc_attr( get_option( 'subs_modal_auto_open_delay', 3 ) ); ?>"
					class="small-text">
				<p class="description"><?php esc_html_e( 'Seconds to wait before automatically opening the modal (0 = immediate).', 'subscription-system' ); ?></p>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Floating Subscribe Button', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_floating_btn" value="1"
						<?php checked( 1, get_option( 'subs_modal_floating_btn', 0 ) ); ?>>
					<?php esc_html_e( 'Show a floating button that stays visible on the page', 'subscription-system' ); ?>
				</label>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Floating Button Text', 'subscription-system' ); ?></th>
			<td>
				<input type="text" name="subs_modal_floating_btn_text"
					value="<?php echo esc_attr( get_option( 'subs_modal_floating_btn_text', __( 'Subscribe', 'subscription-system' ) ) ); ?>"
					class="regular-text">
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Floating Button Position', 'subscription-system' ); ?></th>
			<td>
				<select name="subs_modal_floating_btn_position">
					<?php
					$positions = array(
						'bottom-right'  => __( 'Bottom Right', 'subscription-system' ),
						'bottom-left'   => __( 'Bottom Left', 'subscription-system' ),
						'bottom-center' => __( 'Bottom Center', 'subscription-system' ),
					);
					$current_pos = get_option( 'subs_modal_floating_btn_position', 'bottom-right' );
					foreach ( $positions as $val => $label ) {
						printf(
							'<option value="%s"%s>%s</option>',
							esc_attr( $val ),
							selected( $current_pos, $val, false ),
							esc_html( $label )
						);
					}
					?>
				</select>
			</td>
		</tr>

	</table>

	<!-- ── DISPLAY SETTINGS ────────────────────────────────────────────── -->
	<h3 class="ss-settings-section-title"><?php esc_html_e( '2. Display Settings', 'subscription-system' ); ?></h3>
	<p class="description"><?php esc_html_e( 'Choose where the floating button, auto-open modal, or shortcode functionality becomes active.', 'subscription-system' ); ?></p>
	<table class="form-table">

		<tr>
			<th scope="row"><?php esc_html_e( 'Show on All Posts', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_display_all_posts" value="1"
						<?php checked( 1, get_option( 'subs_modal_display_all_posts', 0 ) ); ?>>
					<?php esc_html_e( 'Display on all posts (any post type)', 'subscription-system' ); ?>
				</label>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Show on All Pages', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_display_all_pages" value="1"
						<?php checked( 1, get_option( 'subs_modal_display_all_pages', 0 ) ); ?>>
					<?php esc_html_e( 'Display on all WordPress pages', 'subscription-system' ); ?>
				</label>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Show on Single Posts Only', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_display_single_posts" value="1"
						<?php checked( 1, get_option( 'subs_modal_display_single_posts', 0 ) ); ?>>
					<?php esc_html_e( 'Display only on single post views', 'subscription-system' ); ?>
				</label>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Show on Single Pages Only', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_display_single_pages" value="1"
						<?php checked( 1, get_option( 'subs_modal_display_single_pages', 0 ) ); ?>>
					<?php esc_html_e( 'Display only on single page views', 'subscription-system' ); ?>
				</label>
			</td>
		</tr>

		<?php if ( ! empty( $public_post_types ) ) : ?>
		<tr>
			<th scope="row"><?php esc_html_e( 'Show on Custom Post Types', 'subscription-system' ); ?></th>
			<td>
				<?php
				$selected_cpts = (array) get_option( 'subs_modal_display_post_types', array() );
				foreach ( $public_post_types as $cpt ) :
				?>
					<label style="display:block;margin-bottom:4px;">
						<input type="checkbox"
							name="subs_modal_display_post_types[]"
							value="<?php echo esc_attr( $cpt->name ); ?>"
							<?php checked( in_array( $cpt->name, $selected_cpts, true ) ); ?>>
						<?php echo esc_html( $cpt->labels->singular_name ); ?>
						<code style="color:#888;font-size:11px;">(<?php echo esc_html( $cpt->name ); ?>)</code>
					</label>
				<?php endforeach; ?>
				<p class="description"><?php esc_html_e( 'Select one or more custom post types.', 'subscription-system' ); ?></p>
			</td>
		</tr>
		<?php endif; ?>

		<?php if ( ! empty( $all_pages ) ) : ?>
		<tr>
			<th scope="row"><?php esc_html_e( 'Show on Specific Pages', 'subscription-system' ); ?></th>
			<td>
				<div class="ss-page-checklist" style="max-height:200px;overflow-y:auto;border:1px solid #ddd;padding:8px;background:#fff;border-radius:3px;">
				<?php
				$selected_pages = (array) get_option( 'subs_modal_display_specific_pages', array() );
				foreach ( $all_pages as $page ) :
				?>
					<label style="display:block;margin-bottom:4px;">
						<input type="checkbox"
							name="subs_modal_display_specific_pages[]"
							value="<?php echo esc_attr( $page->ID ); ?>"
							<?php checked( in_array( (string) $page->ID, array_map( 'strval', $selected_pages ), true ) ); ?>>
						<?php echo esc_html( $page->post_title ); ?>
					</label>
				<?php endforeach; ?>
				</div>
				<p class="description"><?php esc_html_e( 'Show triggers only on the pages checked above.', 'subscription-system' ); ?></p>
			</td>
		</tr>
		<?php endif; ?>

	</table>

	<!-- ── CONTEXT-AWARE MODAL ─────────────────────────────────────────── -->
	<h3 class="ss-settings-section-title"><?php esc_html_e( '3. Context-Aware Modal', 'subscription-system' ); ?></h3>
	<table class="form-table">

		<tr>
			<th scope="row"><?php esc_html_e( 'Auto-Select Current Post\'s Categories', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_context_categories" value="1"
						<?php checked( 1, get_option( 'subs_modal_context_categories', 1 ) ); ?>>
					<?php esc_html_e( 'Pre-select the categories of the post being viewed', 'subscription-system' ); ?>
				</label>
				<p class="description"><?php esc_html_e( 'For example, if a user reads a Sports post, Sports will be pre-checked in the modal.', 'subscription-system' ); ?></p>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Auto-Select Current Post\'s Tags', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_context_tags" value="1"
						<?php checked( 1, get_option( 'subs_modal_context_tags', 1 ) ); ?>>
					<?php esc_html_e( 'Pre-select the tags of the post being viewed', 'subscription-system' ); ?>
				</label>
				<p class="description"><?php esc_html_e( 'For example, if a post is tagged Hockey, Hockey will be pre-checked in the modal.', 'subscription-system' ); ?></p>
			</td>
		</tr>

	</table>

	<!-- ── LOGGED-IN USER EXPERIENCE ───────────────────────────────────── -->
	<h3 class="ss-settings-section-title"><?php esc_html_e( '4. Logged-In User Experience', 'subscription-system' ); ?></h3>
	<table class="form-table">

		<tr>
			<th scope="row"><?php esc_html_e( 'Hide Name & Email Fields for Logged-In Users', 'subscription-system' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="subs_modal_hide_fields_logged_in" value="1"
						<?php checked( 1, get_option( 'subs_modal_hide_fields_logged_in', 1 ) ); ?>>
					<?php esc_html_e( 'Do not ask logged-in users to re-enter their name and email', 'subscription-system' ); ?>
				</label>
				<p class="description"><?php esc_html_e( 'The subscription system will automatically use the logged-in user\'s details.', 'subscription-system' ); ?></p>
			</td>
		</tr>

	</table>

	<!-- ── SHORTCODE REFERENCE ─────────────────────────────────────────── -->
	<div class="subscription-pages-info" style="margin-top:24px;">
		<h3><?php esc_html_e( 'Shortcode Reference', 'subscription-system' ); ?></h3>
		<table class="widefat" style="max-width:700px;">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Shortcode', 'subscription-system' ); ?></th>
					<th><?php esc_html_e( 'Description', 'subscription-system' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><code>[subscriptionsystem_subscribe]</code></td>
					<td><?php esc_html_e( 'Renders an inline subscribe button that opens the subscription modal when clicked.', 'subscription-system' ); ?></td>
				</tr>
				<tr>
					<td><code>[subscriptionsystem_subscribe button_text="Join Now"]</code></td>
					<td><?php esc_html_e( 'Customize the button text using the button_text attribute.', 'subscription-system' ); ?></td>
				</tr>
				<tr>
					<td><code>[subscriptionsystem_subscribe button_class="my-class"]</code></td>
					<td><?php esc_html_e( 'Add a custom CSS class to the button using button_class.', 'subscription-system' ); ?></td>
				</tr>
			</tbody>
		</table>
	</div>

	<?php submit_button( __( 'Save Settings', 'subscription-system' ) ); ?>
</form>

<style>
.ss-settings-section-title {
	font-size: 14px;
	font-weight: 600;
	color: #1d2327;
	border-bottom: 1px solid #dcdcde;
	padding-bottom: 8px;
	margin: 28px 0 8px;
}
.ss-page-checklist label {
	cursor: pointer;
}
.ss-page-checklist label:hover {
	background: #f6f7f7;
}
</style>
