<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Global plugin settings page.
 *
 * Currently used for the "Email Marketing Integration" (Mailchimp) section.
 *
 * @link       https://webequipe.com
 * @since      1.0.7
 *
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/admin/partials
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template-local assignments in this partial.
// Fetch current Mailchimp settings (helper handles defaults).
if ( class_exists( '\Webequipe\SpinWinWheel\Mailchimp' ) ) {
	$mailchimp_settings = \Webequipe\SpinWinWheel\Mailchimp::get_settings();
} else {
	$mailchimp_settings = [
		'enabled'         => 0,
		'api_key'         => '',
		'list_id'         => '',
		'tag'             => 'SpinWheel',
		'consent_enabled' => 0,
		'consent_text'    => __( 'I agree to receive promotional emails.', 'webequipe-spin-win-wheel' ),
		'last_status'     => '',
	];
}

$enabled         = ! empty( $mailchimp_settings['enabled'] );
$api_key         = isset( $mailchimp_settings['api_key'] ) ? (string) $mailchimp_settings['api_key'] : '';
$selected_list   = isset( $mailchimp_settings['list_id'] ) ? (string) $mailchimp_settings['list_id'] : '';
$tag             = isset( $mailchimp_settings['tag'] ) ? (string) $mailchimp_settings['tag'] : 'SpinWheel';
$consent_enabled = ! empty( $mailchimp_settings['consent_enabled'] );
$consent_text    = isset( $mailchimp_settings['consent_text'] ) && '' !== $mailchimp_settings['consent_text']
	? (string) $mailchimp_settings['consent_text']
	: __( 'I agree to receive promotional emails.', 'webequipe-spin-win-wheel' );
$last_status     = isset( $mailchimp_settings['last_status'] ) ? (string) $mailchimp_settings['last_status'] : '';

// Treat "connected" as having both API key and audience selected.
$is_connected = ( '' !== $api_key && '' !== $selected_list );

// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

?>

<div class="wrap">
	<h1><?php esc_html_e( 'WebEquipe Spin & Win Wheel – Settings', 'webequipe-spin-win-wheel' ); ?></h1>

	<form method="post" action="options.php">
		<?php
		// Settings API – outputs hidden fields for wsww_mailchimp_settings.
		settings_fields( 'webequipe_sww_mailchimp' );
		?>

		<h2 class="nav-tab-wrapper">
			<a href="#" class="nav-tab nav-tab-active" data-wsww-tab="wsww-general-tab"><?php esc_html_e( 'Genarel Settings', 'webequipe-spin-win-wheel' ); ?></a>
			<a href="#" class="nav-tab" data-wsww-tab="wsww-mailchimp-tab"><?php esc_html_e( 'CMS/Email API', 'webequipe-spin-win-wheel' ); ?></a>
		</h2>
		<div class="card wsww-tab-content" id="wsww-general-tab" style="max-width: 900px; margin-top: 20px;">
			<div class="card-body">
				<h2><?php esc_html_e( 'Genarel Settings', 'webequipe-spin-win-wheel' ); ?></h2>
				<table class="form-table" role="presentation">
					<tbody>
						<tr>
							<th scope="row">
								<label for="wsww-mailchimp-consent-enabled"><?php esc_html_e( 'Consent Checkbox on Spin Form', 'webequipe-spin-win-wheel' ); ?></label>
							</th>
							<td>
								<label for="wsww-mailchimp-consent-enabled">
									<input type="checkbox"
										id="wsww-mailchimp-consent-enabled"
										name="wsww_mailchimp_settings[consent_enabled]"
										value="1"
										<?php checked( $consent_enabled ); ?>
									/>
									<?php esc_html_e( 'Show a required marketing consent checkbox on the spin form.', 'webequipe-spin-win-wheel' ); ?>
								</label>
								<p class="description">
									<?php esc_html_e( 'When enabled, visitors must check the consent box before spinning. Without consent, Mailchimp will not be called.', 'webequipe-spin-win-wheel' ); ?>
								</p>
							</td>
						</tr>

						<tr>
							<th scope="row">
								<label for="wsww-mailchimp-consent-text"><?php esc_html_e( 'Consent Text', 'webequipe-spin-win-wheel' ); ?></label>
							</th>
							<td>
								<input type="text"
									id="wsww-mailchimp-consent-text"
									name="wsww_mailchimp_settings[consent_text]"
									class="regular-text"
									value="<?php echo esc_attr( $consent_text ); ?>"
								/>
								<p class="description">
									<?php
									esc_html_e(
										'Shown under the email field on the spin form when the consent checkbox is enabled.',
										'webequipe-spin-win-wheel'
									);
									?>
								</p>
							</td>
						</tr>
					</tbody>
				</table>
			</div>
		</div>

		<div class="wsww-tab-content" id="wsww-mailchimp-tab">
		<div class="card" style="max-width: 900px; margin-top: 20px;">
			<div class="card-body">
				<h2><?php esc_html_e( 'Mailchimp', 'webequipe-spin-win-wheel' ); ?></h2>
				<p class="description">
					<?php
					echo esc_html__(
						'Automatically send spin wheel subscribers to your Mailchimp audience. This is the currently available integration in this release.',
						'webequipe-spin-win-wheel'
					);
					?>
				</p>

				<table class="form-table" role="presentation">
					<tbody>
						<tr>
							<th scope="row">
								<label for="wsww-mailchimp-enabled"><?php esc_html_e( 'Enable Mailchimp Integration', 'webequipe-spin-win-wheel' ); ?></label>
							</th>
							<td>
								<label for="wsww-mailchimp-enabled">
									<input type="checkbox"
										id="wsww-mailchimp-enabled"
										name="wsww_mailchimp_settings[enabled]"
										value="1"
										<?php checked( $enabled ); ?>
									/>
									<?php esc_html_e( 'Send spin wheel subscribers to Mailchimp.', 'webequipe-spin-win-wheel' ); ?>
								</label>
								<p class="description">
									<?php esc_html_e( 'You can disable this at any time. Existing subscribers in Mailchimp are not removed.', 'webequipe-spin-win-wheel' ); ?>
								</p>
							</td>
						</tr>

						<tr id="wsww-mailchimp-api-row" <?php echo $enabled ? '' : 'style="display:none;"'; ?>>
							<th scope="row">
								<label for="wsww-mailchimp-api-key"><?php esc_html_e( 'Mailchimp API Key', 'webequipe-spin-win-wheel' ); ?></label>
							</th>
							<td>
								<input type="password"
									id="wsww-mailchimp-api-key"
									name="wsww_mailchimp_settings[api_key]"
									class="regular-text"
									value="<?php echo esc_attr( $api_key ); ?>"
									placeholder="<?php echo esc_attr__( 'Enter Mailchimp API key', 'webequipe-spin-win-wheel' ); ?>"
									autocomplete="off"
								/>
								<p class="description">
									<?php esc_html_e( 'Paste your Mailchimp API key. The datacenter (e.g. us21) is detected automatically from the key suffix.', 'webequipe-spin-win-wheel' ); ?>
								</p>
								<p>
									<button
										type="button"
										class="button<?php echo $is_connected ? ' wsww-mailchimp-connected' : ''; ?>"
										id="wsww-mailchimp-connect-button"
										data-connected="<?php echo $is_connected ? '1' : '0'; ?>"
										data-label-connect="<?php echo esc_attr__( 'Connect', 'webequipe-spin-win-wheel' ); ?>"
										data-label-connected="<?php echo esc_attr__( 'Re-Connect', 'webequipe-spin-win-wheel' ); ?>"
									>
										<?php
										echo $is_connected
											? esc_html__( 'Re-Connect', 'webequipe-spin-win-wheel' )
											: esc_html__( 'Connect', 'webequipe-spin-win-wheel' );
										?>
									</button>

									<button
										type="button"
										class="button button-secondary<?php echo $is_connected ? ' wsww-mailchimp-disconnect' : ''; ?>"
										id="wsww-mailchimp-disconnect-button"
										<?php echo $is_connected ? '' : 'style="display:none;"'; ?>
									>
										<?php esc_html_e( 'Disconnect', 'webequipe-spin-win-wheel' ); ?>
									</button>

									<span id="wsww-mailchimp-connect-status" style="margin-left:8px;">
										<?php echo esc_html( $last_status ); ?>
									</span>
								</p>
							</td>
						</tr>

						<tr id="wsww-mailchimp-list-row" <?php echo ( $enabled && $is_connected ) ? '' : 'style="display:none;"'; ?>>
							<th scope="row">
								<label for="wsww-mailchimp-list-id"><?php esc_html_e( 'Audience (List)', 'webequipe-spin-win-wheel' ); ?></label>
							</th>
							<td>
								<?php
								// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Scoped to this cell.
								// Derive a human‑readable label for the saved audience ID when reloading the page.
								$selected_list_label = $selected_list;

								if ( '' !== $api_key && '' !== $selected_list && class_exists( '\Webequipe\SpinWinWheel\Mailchimp' ) ) {
									$connection = \Webequipe\SpinWinWheel\Mailchimp::test_connection( $api_key );

									if ( ! empty( $connection['success'] ) && ! empty( $connection['lists'][ $selected_list ] ) ) {
										$selected_list_label = $connection['lists'][ $selected_list ];
									}
								}
								// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
								?>
								<select name="wsww_mailchimp_settings[list_id]" id="wsww-mailchimp-list-id" class="regular-text" >
									<option value=""><?php esc_html_e( 'Select an audience', 'webequipe-spin-win-wheel' ); ?></option>
									<?php
									// If we already have a saved list, keep it visible even before re‑connecting.
									if ( '' !== $selected_list ) :
										?>
										<option value="<?php echo esc_attr( $selected_list ); ?>" selected="selected">
											<?php echo esc_html( $selected_list_label ); ?>
										</option>
										<?php
									endif;
									?>
								</select>
								<p class="description">
									<?php esc_html_e( 'After clicking "Connect", available audiences will be loaded here. The free version supports one audience.', 'webequipe-spin-win-wheel' ); ?>
								</p>
							</td>
						</tr>

						<tr id="wsww-mailchimp-tag-row" <?php echo ( $enabled && $is_connected ) ? '' : 'style="display:none;"'; ?>>
							<th scope="row">
								<label for="wsww-mailchimp-tag"><?php esc_html_e( 'Optional Tag', 'webequipe-spin-win-wheel' ); ?></label>
							</th>
							<td>
								<input type="text"
									id="wsww-mailchimp-tag"
									name="wsww_mailchimp_settings[tag]"
									class="regular-text"
									value="<?php echo esc_attr( $tag ); ?>"
								/>
								<p class="description">
									<?php esc_html_e( 'Tag applied to subscribers created from the spin wheel. Example: SpinWheel, Giveaway, Popup.', 'webequipe-spin-win-wheel' ); ?>
								</p>
							</td>
						</tr>


					</tbody>
				</table>
			</div>
		</div>
		<div class="card" style="max-width: 900px; margin-top: 16px;">
			<div class="card-body">
				<h2><?php esc_html_e( 'Upcoming Features', 'webequipe-spin-win-wheel' ); ?></h2>
				<p class="description">
					<?php esc_html_e( 'Additional CMS/Email API integrations planned for upcoming releases.', 'webequipe-spin-win-wheel' ); ?>
				</p>
				<div class="notice notice-info inline" style="margin: 8px 0 0;">
					<p style="margin: 8px 0;">
						<strong><?php esc_html_e( 'Coming Soon:', 'webequipe-spin-win-wheel' ); ?></strong>
						<?php esc_html_e( 'Klaviyo, HubSpot, ActiveCampaign.', 'webequipe-spin-win-wheel' ); ?>
					</p>
				</div>
			</div>
		</div>
		</div>

		<?php submit_button( esc_html__( 'Save Settings', 'webequipe-spin-win-wheel' ) ); ?>
	</form>
</div>
