<?php
/**
 * Settings tab - Inline Posts.
 *
 * Variable: $options (Options instance)
 *
 * @package Relatedly
 * @since   2.0.0
 * @since   2.3.0 Rebuilt with native form rows and dropdown theme selection.
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
defined( 'ABSPATH' ) || exit;

use Relatedly\Premium\FeatureGate;

$is_premium       = FeatureGate::is_premium();
$max_inline_posts = FeatureGate::max_inline_posts();
$current_theme    = (string) $options->get( 'inline_theme', 'theme1' );

$themes = [
	'theme1' => [ __( 'Simple', 'relatedly' ), false ],
	'theme2' => [ __( 'Minimal', 'relatedly' ), false ],
	'theme3' => [ __( 'Featured', 'relatedly' ), false ],
	'theme4' => [ __( 'Quote', 'relatedly' ), true ],
	'theme5' => [ __( 'Badge', 'relatedly' ), true ],
	'theme6' => [ __( 'Callout', 'relatedly' ), true ],
	'theme7' => [ __( 'Gradient', 'relatedly' ), true ],
	'theme8' => [ __( 'Card', 'relatedly' ), true ],
];

$theme_fields = [
	'theme1' => [
		[ 'inline_theme1_bg', __( 'Background', 'relatedly' ), 'color' ],
		[ 'inline_theme1_border_color', __( 'Border', 'relatedly' ), 'color' ],
		[ 'inline_theme1_title_color', __( 'Title', 'relatedly' ), 'color' ],
		[ 'inline_theme1_label_color', __( 'Label', 'relatedly' ), 'color' ],
		[ 'inline_theme1_excerpt_color', __( 'Excerpt', 'relatedly' ), 'color' ],
	],
	'theme2' => [
		[ 'inline_theme2_bg', __( 'Background', 'relatedly' ), 'color' ],
		[ 'inline_theme2_border_color', __( 'Border', 'relatedly' ), 'color' ],
		[ 'inline_theme2_color', __( 'Text', 'relatedly' ), 'color' ],
		[ 'inline_theme2_label_color', __( 'Label', 'relatedly' ), 'color' ],
	],
	'theme3' => [
		[ 'inline_theme3_bg', __( 'Background', 'relatedly' ), 'color' ],
		[ 'inline_theme3_border_color', __( 'Border', 'relatedly' ), 'color' ],
		[ 'inline_theme3_title_color', __( 'Title', 'relatedly' ), 'color' ],
		[ 'inline_theme3_label_color', __( 'Label', 'relatedly' ), 'color' ],
	],
];
?>
<div class="relatedly-settings-panel">
	<div class="relatedly-settings-panel-header">
		<h2 class="relatedly-settings-panel-title"><?php esc_html_e( 'Inline Posts', 'relatedly' ); ?></h2>
		<p class="relatedly-settings-panel-desc"><?php esc_html_e( 'Configure recommendations inserted between article paragraphs.', 'relatedly' ); ?></p>
	</div>

	<div class="relatedly-settings-panel-body">
		<table class="form-table relatedly-settings-table">
			<tbody>
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-inline-enabled"><?php esc_html_e( 'Inline Display', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Show related posts inside article content.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-toggle-row">
							<div class="relatedly-toggle-switch">
								<input type="checkbox" name="inline_enabled" id="relatedly-inline-enabled" value="1" <?php checked( $options->get( 'inline_enabled', true ) ); ?>>
								<label for="relatedly-inline-enabled"></label>
							</div>
						</div>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="inline_theme"><?php esc_html_e( 'Theme', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Choose the inline display style.', 'relatedly' ); ?></p>
					</th>
					<td>
						<select name="inline_theme" id="inline_theme" class="relatedly-control">
							<?php foreach ( $themes as $value => $theme ) : ?>
								<?php
								$is_locked = ! $is_premium && $theme[1];
								$label     = $theme[0] . ( $is_locked ? ' - ' . __( 'Pro', 'relatedly' ) : '' );
								?>
								<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current_theme, $value ); ?> <?php disabled( $is_locked ); ?>>
									<?php echo esc_html( $label ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-inline-interval"><?php esc_html_e( 'Paragraph Interval', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Insert after every N paragraphs.', 'relatedly' ); ?></p>
					</th>
					<td>
						<input type="number" name="inline_paragraph_interval" id="relatedly-inline-interval" class="relatedly-control" min="1" value="<?php echo esc_attr( $options->get( 'inline_paragraph_interval', 4 ) ); ?>">
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-inline-max"><?php esc_html_e( 'Display Limit', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Maximum inline recommendations per post.', 'relatedly' ); ?></p>
					</th>
					<td>
						<input type="number" name="inline_max_posts" id="relatedly-inline-max" class="relatedly-control" min="1" max="<?php echo esc_attr( $is_premium ? '50' : (string) $max_inline_posts ); ?>" value="<?php echo esc_attr( $options->get( 'inline_max_posts', 1 ) ); ?>">
						<?php if ( ! $is_premium ) : ?>
							<p class="relatedly-field-hint">
								<?php
								printf(
									/* translators: %d: free plan maximum inline post count. */
									esc_html__( 'Free inline limit: %d.', 'relatedly' ),
									(int) $max_inline_posts
								);
								?>
							</p>
						<?php endif; ?>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<span class="relatedly-field-label"><?php esc_html_e( 'Theme Colors', 'relatedly' ); ?></span>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Adjust the available colors for the selected free theme.', 'relatedly' ); ?></p>
					</th>
					<td>
						<?php foreach ( $theme_fields as $theme_key => $fields ) : ?>
							<div class="relatedly-theme-settings <?php echo $current_theme === $theme_key ? '' : 'relatedly-is-hidden'; ?>" id="<?php echo esc_attr( 'relatedly-inline-settings-' . $theme_key ); ?>">
								<div class="relatedly-color-row">
									<?php foreach ( $fields as $field ) : ?>
										<label class="relatedly-color-field">
											<input type="color" name="<?php echo esc_attr( $field[0] ); ?>" value="<?php echo esc_attr( $options->get( $field[0], '#ffffff' ) ); ?>">
											<span><?php echo esc_html( $field[1] ); ?></span>
										</label>
									<?php endforeach; ?>
								</div>
							</div>
						<?php endforeach; ?>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Premium theme color controls appear after upgrading.', 'relatedly' ); ?></p>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<span class="relatedly-field-label"><?php esc_html_e( 'Inline Preview', 'relatedly' ); ?></span>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Preview belongs with the display controls, not the global sidebar.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-preview-frame" id="relatedly-inline-preview">
							<div class="relatedly-preview-placeholder">
								<strong><?php esc_html_e( 'Related Content', 'relatedly' ); ?></strong>
								<span><?php esc_html_e( 'A compact inline recommendation preview will load here.', 'relatedly' ); ?></span>
							</div>
						</div>
					</td>
				</tr>
			</tbody>
		</table>
	</div>

	<div class="relatedly-save-bar">
		<?php submit_button( __( 'Save Changes', 'relatedly' ), 'primary', 'submit', false ); ?>
	</div>
</div>
