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

$brevo_info    = SftcyWordformConnectBrevo::get_brevo_configured_info();
$brevo_api_key = isset( $brevo_info['apiKey'] ) ? $brevo_info['apiKey'] : '';
$brevo_list_id = isset( $brevo_info['listId'] ) ? $brevo_info['listId'] : '';
$brevo_list_label = isset( $brevo_info['listLabel'] ) ? $brevo_info['listLabel'] : '';
?>
<!-- Start Brevo Configure Modal Form -->
<div id="BrevoAPIModal" title="Brevo Connection Configure" class="" style="display: none;">
	<div class="wordform-connect-modal-quickstart">
		<strong>Quick setup:</strong> Enter the API key, load lists, select one, then save. Manual List ID entry is still available as fallback.
	</div>
	<h5>How to configure Brevo connection</h5>
	<p>
		<a href="https://help.brevo.com/hc/en-us/articles/209467485-Create-and-manage-your-API-keys" target="_blank" rel="noopener">Brevo API Key Help</a> |
		<a href="https://developers.brevo.com/reference/createcontact" target="_blank" rel="noopener">Brevo Contacts API Docs</a>
	</p>
	<div id="brevo-api-create-help" class="accordion">
		<h3>Step 1 - Create Brevo API key</h3>
		<div>
			<p>Open your Brevo account and create or copy an API key with contact management access.</p>
		</div>

		<h3>Step 2 - Find Contact List ID</h3>
		<div>
			<p>Create or open the target contact list in Brevo and copy its numeric <strong>List ID</strong>.</p>
		</div>

		<h3>Step 3 - Save credentials here</h3>
		<div>
			<p>Paste the API key and List ID below, then click <strong>Save / Update</strong> in WordForm.</p>
		</div>

		<h3>Step 4 - Connect forms</h3>
		<div>
			<p>After saving, connect only the forms you want by using <strong>Select Form + Select Application + Create Connection</strong>. Brevo needs at least one valid email field in the form submission.</p>
		</div>
	</div>

	<div class="brevo-connection-input-elements-wrapper" style="display: flex; flex-direction:column;">
		<div class="wordform-brevo-app-feedback-msg"></div>
		<div>
			<label for="BrevoApiKey">
				<span class="dashicons dashicons-admin-network" aria-hidden="true"></span>
				Brevo API Key
				<a class="wordform-field-help-link" href="https://help.brevo.com/hc/en-us/articles/209467485-Create-and-manage-your-API-keys" target="_blank" rel="noopener" aria-label="How to get your Brevo API key">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="BrevoApiKey" name="brevo-api-key" class="brevo-api-key" value="<?php echo esc_attr( $brevo_api_key ); ?>" placeholder="xkeysib-xxxxxxxxxxxxxxxxxxxx">
		</div>

		<div>
			<label for="BrevoListId">
				<span class="dashicons dashicons-groups" aria-hidden="true"></span>
				List ID
				<a class="wordform-field-help-link" href="https://developers.brevo.com/reference/getlists-1" target="_blank" rel="noopener" aria-label="How Brevo contact lists and list IDs are retrieved">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="BrevoListId" name="brevo-list-id" class="brevo-list-id" value="<?php echo esc_attr( $brevo_list_id ); ?>" placeholder="12">
			<input type="hidden" id="BrevoListLabel" class="brevo-list-label" value="<?php echo esc_attr( $brevo_list_label ); ?>">
		</div>

		<div class="wordform-connect-resource-actions">
			<button
				type="button"
				class="button button-secondary wordform-connect-fetch-app-resource-button"
				data-fetch-action="wordform_connect_fetch_brevo_lists"
				data-feedback-selector=".wordform-brevo-app-feedback-msg"
				data-fields-config='[{"selector":".brevo-api-key","paramKey":"brevoApiKey","validation":"text","invalidMessage":"Please enter Brevo API key first."}]'
				data-target-select="#BrevoListSelect"
				data-target-input="#BrevoListId"
				data-target-label-input="#BrevoListLabel"
				data-empty-label="Select List"
			><i class="fa-solid fa-rotate"></i> Verify & Load Lists</button>
		</div>

		<div class="wordform-connect-resource-select-wrap">
			<label for="BrevoListSelect">
				<span class="dashicons dashicons-groups" aria-hidden="true"></span>
				Loaded Lists
			</label><br/>
			<select id="BrevoListSelect" class="wordform-connect-resource-select">
				<option value=""><?php esc_html_e( 'Select List', 'wordform-pro' ); ?></option>
				<?php if ( ! empty( $brevo_list_id ) ) : ?>
					<option value="<?php echo esc_attr( $brevo_list_id ); ?>" selected><?php echo esc_html( ! empty( $brevo_list_label ) ? $brevo_list_label : $brevo_list_id ); ?></option>
				<?php endif; ?>
			</select>
		</div>

		<div class="wordform-connect-save-update-brevo-info-button-div-wrapper">
			<button
				class="button button-primary wordform-connect-save-update-app-fields-button"
				data-save-action="wordform_connect_save_brevo_data_as_wp_option_data"
				data-feedback-selector=".wordform-brevo-app-feedback-msg"
				data-fields-config='[{"selector":".brevo-api-key","paramKey":"brevoApiKey","validation":"text","invalidMessage":"Please enter Brevo API key."},{"selector":".brevo-list-id","paramKey":"brevoListId","validation":"text","invalidMessage":"Please enter Brevo List ID."},{"selector":".brevo-list-label","paramKey":"brevoListLabel","validation":"optional"}]'
			><i class="fa-solid fa-gear"></i> Save / Update</button>
		</div>
	</div>
</div>
<!-- End Brevo Configure Modal Form -->
