<?php
/**
 * Settings admin view.
 *
 * Renders all settings sections with form fields. The Pro build adds
 * its own sections and fields via the wtly_settings_* hooks below.
 *
 * @package Waitlytics
 */

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

$wtly_settings = wtly_get_settings();

// Show settings saved notice.
settings_errors( 'wtly_settings' );

// The section key is read for display only (which settings tab to render); no
// data is mutated here, so nonce verification is not applicable.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$wtly_settings_tab = isset( $_GET['section'] ) ? sanitize_key( $_GET['section'] ) : 'general';
$wtly_sections     = array(
	'general'      => __( 'General', 'waitlytics' ),
	'email'        => __( 'Email', 'waitlytics' ),
	'admin_alerts' => __( 'Admin Alerts', 'waitlytics' ),
	'form'         => __( 'Form Appearance', 'waitlytics' ),
);

/**
 * Filter the available settings sections. The Pro build registers its own
 * sections here; the free build ships no Pro section UI at all.
 *
 * @param array $wtly_sections Section key => label pairs.
 */
$wtly_sections = apply_filters( 'wtly_settings_sections', $wtly_sections );

// Fall back to General for unknown or unavailable sections (e.g. a free user
// hitting a Pro section URL directly).
if ( ! array_key_exists( $wtly_settings_tab, $wtly_sections ) ) {
	$wtly_settings_tab = 'general';
}
?>

<div class="wtly-settings">

	<!-- Section Tabs -->
	<div class="wtly-settings-nav">
		<ul>
			<?php foreach ( $wtly_sections as $wtly_key => $wtly_label ) : ?>
				<li class="<?php echo esc_attr( $wtly_settings_tab === $wtly_key ? 'active' : '' ); ?>">
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=waitlytics&tab=settings&section=' . $wtly_key ) ); ?>">
						<?php echo esc_html( $wtly_label ); ?>
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
	</div>

	<div class="wtly-settings-content">
		<form method="post" action="">
			<?php wp_nonce_field( 'wtly_save_settings', 'wtly_save_settings_nonce' ); ?>

			<?php if ( 'general' === $wtly_settings_tab ) : ?>
				<!-- General Settings -->
				<div class="wtly-card">
					<h2><?php esc_html_e( 'General Settings', 'waitlytics' ); ?></h2>
					<table class="form-table">
						<tr>
							<th scope="row"><?php esc_html_e( 'Enable Notifications', 'waitlytics' ); ?></th>
							<td>
								<label>
									<input type="checkbox" name="enable_notifications" value="1" <?php checked( ! empty( $wtly_settings['enable_notifications'] ) ); ?>>
									<?php esc_html_e( 'Enable back-in-stock email notifications.', 'waitlytics' ); ?>
								</label>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Guest Subscriptions', 'waitlytics' ); ?></th>
							<td>
								<label>
									<input type="checkbox" name="guest_subscriptions" value="1" <?php checked( ! empty( $wtly_settings['guest_subscriptions'] ) ); ?>>
									<?php esc_html_e( 'Allow guest visitors to subscribe.', 'waitlytics' ); ?>
								</label>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="notification_position"><?php esc_html_e( 'Form Position', 'waitlytics' ); ?></label></th>
							<td>
								<select id="notification_position" name="notification_position">
									<option value="after_add_to_cart" <?php selected( $wtly_settings['notification_position'], 'after_add_to_cart' ); ?>><?php esc_html_e( 'After Add to Cart', 'waitlytics' ); ?></option>
									<option value="after_summary" <?php selected( $wtly_settings['notification_position'], 'after_summary' ); ?>><?php esc_html_e( 'After Summary', 'waitlytics' ); ?></option>
									<option value="shortcode" <?php selected( $wtly_settings['notification_position'], 'shortcode' ); ?>><?php esc_html_e( 'Shortcode Only', 'waitlytics' ); ?></option>
								</select>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Show Subscriber Count', 'waitlytics' ); ?></th>
							<td>
								<label>
									<input type="checkbox" name="show_subscriber_count" value="1" <?php checked( ! empty( $wtly_settings['show_subscriber_count'] ) ); ?>>
									<?php esc_html_e( 'Show "X people waiting" on product page.', 'waitlytics' ); ?>
								</label>
							</td>
						</tr>
					</table>
				</div>

			<?php elseif ( 'email' === $wtly_settings_tab ) : ?>
				<!-- Email Settings -->
				<div class="wtly-card">
					<h2><?php esc_html_e( 'Email Settings', 'waitlytics' ); ?></h2>
					<table class="form-table">
						<tr>
							<th scope="row"><label for="email_from_name"><?php esc_html_e( 'From Name', 'waitlytics' ); ?></label></th>
							<td><input type="text" id="email_from_name" name="email_from_name" value="<?php echo esc_attr( $wtly_settings['email_from_name'] ); ?>" class="regular-text" placeholder="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="email_from_address"><?php esc_html_e( 'From Email', 'waitlytics' ); ?></label></th>
							<td><input type="email" id="email_from_address" name="email_from_address" value="<?php echo esc_attr( $wtly_settings['email_from_address'] ); ?>" class="regular-text" placeholder="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="email_subject"><?php esc_html_e( 'Subject Line', 'waitlytics' ); ?></label></th>
							<td><input type="text" id="email_subject" name="email_subject" value="<?php echo esc_attr( $wtly_settings['email_subject'] ); ?>" class="regular-text" placeholder="<?php esc_attr_e( '{product_name} is back in stock!', 'waitlytics' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="email_heading"><?php esc_html_e( 'Email Heading', 'waitlytics' ); ?></label></th>
							<td><input type="text" id="email_heading" name="email_heading" value="<?php echo esc_attr( $wtly_settings['email_heading'] ); ?>" class="regular-text" placeholder="<?php esc_attr_e( 'Good news! {product_name} is back.', 'waitlytics' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Include Product Image', 'waitlytics' ); ?></th>
							<td><label><input type="checkbox" name="email_include_product_image" value="1" <?php checked( ! empty( $wtly_settings['email_include_product_image'] ) ); ?>></label></td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Include Price', 'waitlytics' ); ?></th>
							<td><label><input type="checkbox" name="email_include_price" value="1" <?php checked( ! empty( $wtly_settings['email_include_price'] ) ); ?>></label></td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Include CTA Button', 'waitlytics' ); ?></th>
							<td><label><input type="checkbox" name="email_include_cta_button" value="1" <?php checked( ! empty( $wtly_settings['email_include_cta_button'] ) ); ?>></label></td>
						</tr>
					</table>
				</div>

			<?php elseif ( 'admin_alerts' === $wtly_settings_tab ) : ?>
				<!-- Admin Alerts -->
				<div class="wtly-card">
					<h2><?php esc_html_e( 'Admin Alerts', 'waitlytics' ); ?></h2>
					<table class="form-table">
						<tr>
							<th scope="row"><?php esc_html_e( 'Notify on New Subscriber', 'waitlytics' ); ?></th>
							<td><label><input type="checkbox" name="admin_notify_new_subscriber" value="1" <?php checked( ! empty( $wtly_settings['admin_notify_new_subscriber'] ) ); ?>><?php esc_html_e( 'Send email when someone subscribes.', 'waitlytics' ); ?></label></td>
						</tr>
						<tr>
							<th scope="row"><label for="admin_notify_email"><?php esc_html_e( 'Admin Email', 'waitlytics' ); ?></label></th>
							<td><input type="email" id="admin_notify_email" name="admin_notify_email" value="<?php echo esc_attr( $wtly_settings['admin_notify_email'] ); ?>" class="regular-text" placeholder="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Low Stock Alert', 'waitlytics' ); ?></th>
							<td><label><input type="checkbox" name="admin_low_stock_email" value="1" <?php checked( ! empty( $wtly_settings['admin_low_stock_email'] ) ); ?>><?php esc_html_e( 'Send email when stock falls below threshold.', 'waitlytics' ); ?></label></td>
						</tr>
						<tr>
							<th scope="row"><label for="low_stock_threshold"><?php esc_html_e( 'Low Stock Threshold', 'waitlytics' ); ?></label></th>
							<td><input type="number" id="low_stock_threshold" name="low_stock_threshold" value="<?php echo esc_attr( $wtly_settings['low_stock_threshold'] ); ?>" class="small-text" min="0" step="1"></td>
						</tr>
					</table>
				</div>

			<?php elseif ( 'form' === $wtly_settings_tab ) : ?>
				<!-- Form Appearance -->
				<div class="wtly-card">
					<h2><?php esc_html_e( 'Form Appearance', 'waitlytics' ); ?></h2>
					<table class="form-table">
						<tr>
							<th scope="row"><label for="form_button_text"><?php esc_html_e( 'Button Text', 'waitlytics' ); ?></label></th>
							<td><input type="text" id="form_button_text" name="form_button_text" value="<?php echo esc_attr( $wtly_settings['form_button_text'] ); ?>" class="regular-text" placeholder="<?php esc_attr_e( 'Notify Me', 'waitlytics' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="form_success_message"><?php esc_html_e( 'Success Message', 'waitlytics' ); ?></label></th>
							<td><input type="text" id="form_success_message" name="form_success_message" value="<?php echo esc_attr( $wtly_settings['form_success_message'] ); ?>" class="regular-text" placeholder="<?php esc_attr_e( "You'll be notified when this product is back in stock.", 'waitlytics' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="form_placeholder_text"><?php esc_html_e( 'Placeholder Text', 'waitlytics' ); ?></label></th>
							<td><input type="text" id="form_placeholder_text" name="form_placeholder_text" value="<?php echo esc_attr( $wtly_settings['form_placeholder_text'] ); ?>" class="regular-text" placeholder="<?php esc_attr_e( 'Enter your email', 'waitlytics' ); ?>"></td>
						</tr>
					</table>
				</div>

				<?php
				/**
				 * Fires after the Form Appearance card. The Pro build uses
				 * this to render the Advanced Form card.
				 */
				do_action( 'wtly_settings_after_form_fields' );
				?>

			<?php else : ?>
				<?php
				/**
				 * Fires for settings sections this partial does not render
				 * itself. The Pro build uses this to render its sections.
				 *
				 * @param string $wtly_settings_tab Current section key.
				 */
				do_action( 'wtly_settings_render_section', $wtly_settings_tab );
				?>
			<?php endif; ?>

			<p class="submit">
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Settings', 'waitlytics' ); ?></button>
			</p>
		</form>
	</div>

</div>
