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

$activecampaign_info      = SftcyWordformConnectActivecampaign::get_activecampaign_configured_info();
$activecampaign_api_url   = isset( $activecampaign_info['apiBaseUrl'] ) ? $activecampaign_info['apiBaseUrl'] : '';
$activecampaign_api_key   = isset( $activecampaign_info['apiKey'] ) ? $activecampaign_info['apiKey'] : '';
$activecampaign_list_id   = isset( $activecampaign_info['listId'] ) ? $activecampaign_info['listId'] : '';
$activecampaign_list_label = isset( $activecampaign_info['listLabel'] ) ? $activecampaign_info['listLabel'] : '';
?>
<!-- Start ActiveCampaign Configure Modal Form -->
<div id="ActivecampaignAPIModal" title="ActiveCampaign Connection Configure" class="" style="display: none;">
	<div class="wordform-connect-modal-quickstart">
		<strong>Quick setup:</strong> Enter the API URL and key, load lists, select one, then save. Manual List ID entry remains available.
	</div>
	<h5>How to configure ActiveCampaign connection</h5>
	<p>
		<a href="https://developers.activecampaign.com/reference/sync-a-contacts-data" target="_blank" rel="noopener">ActiveCampaign Contact Sync Docs</a> |
		<a href="https://developers.activecampaign.com/reference/update-list-status-for-contact" target="_blank" rel="noopener">ActiveCampaign List Subscribe Docs</a>
	</p>
	<div id="activecampaign-api-create-help" class="accordion">
		<h3>Step 1 - Copy ActiveCampaign API URL and API key</h3>
		<div>
			<p>Open your ActiveCampaign account settings and copy the account <strong>API URL</strong> and <strong>API Key</strong>.</p>
		</div>

		<h3>Step 2 - Find the list ID</h3>
		<div>
			<p>Open the list you want to use in ActiveCampaign and copy its numeric <strong>List ID</strong>.</p>
		</div>

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

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

	<div class="activecampaign-connection-input-elements-wrapper" style="display: flex; flex-direction:column;">
		<div class="wordform-activecampaign-app-feedback-msg"></div>
		<div>
			<label for="ActivecampaignApiBaseUrl">
				<span class="dashicons dashicons-admin-site" aria-hidden="true"></span>
				ActiveCampaign API URL
				<a class="wordform-field-help-link" href="https://help.activecampaign.com/hc/en-us/articles/221317447-Where-can-I-find-my-API-URL-and-key-" target="_blank" rel="noopener" aria-label="How to find your ActiveCampaign API URL">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="ActivecampaignApiBaseUrl" name="activecampaign-api-url" class="activecampaign-api-url" value="<?php echo esc_attr( $activecampaign_api_url ); ?>" placeholder="https://youraccount.api-us1.com">
		</div>

		<div>
			<label for="ActivecampaignApiKey">
				<span class="dashicons dashicons-admin-network" aria-hidden="true"></span>
				ActiveCampaign API Key
				<a class="wordform-field-help-link" href="https://help.activecampaign.com/hc/en-us/articles/221317447-Where-can-I-find-my-API-URL-and-key-" target="_blank" rel="noopener" aria-label="How to find your ActiveCampaign API key">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="ActivecampaignApiKey" name="activecampaign-api-key" class="activecampaign-api-key" value="<?php echo esc_attr( $activecampaign_api_key ); ?>" placeholder="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
		</div>

		<div>
			<label for="ActivecampaignListId">
				<span class="dashicons dashicons-groups" aria-hidden="true"></span>
				List ID
				<a class="wordform-field-help-link" href="https://help.activecampaign.com/hc/en-us/articles/360000030559-Create-an-email-marketing-list-in-ActiveCampaign" target="_blank" rel="noopener" aria-label="How to find or create an ActiveCampaign list">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="ActivecampaignListId" name="activecampaign-list-id" class="activecampaign-list-id" value="<?php echo esc_attr( $activecampaign_list_id ); ?>" placeholder="1">
			<input type="hidden" id="ActivecampaignListLabel" class="activecampaign-list-label" value="<?php echo esc_attr( $activecampaign_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_activecampaign_lists"
				data-feedback-selector=".wordform-activecampaign-app-feedback-msg"
				data-fields-config='[{"selector":".activecampaign-api-url","paramKey":"activecampaignApiBaseUrl","validation":"url","invalidMessage":"Please enter valid ActiveCampaign API URL first."},{"selector":".activecampaign-api-key","paramKey":"activecampaignApiKey","validation":"text","invalidMessage":"Please enter ActiveCampaign API key first."}]'
				data-target-select="#ActivecampaignListSelect"
				data-target-input="#ActivecampaignListId"
				data-target-label-input="#ActivecampaignListLabel"
				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="ActivecampaignListSelect">
				<span class="dashicons dashicons-groups" aria-hidden="true"></span>
				Loaded Lists
			</label><br/>
			<select id="ActivecampaignListSelect" class="wordform-connect-resource-select">
				<option value=""><?php esc_html_e( 'Select List', 'wordform-pro' ); ?></option>
				<?php if ( ! empty( $activecampaign_list_id ) ) : ?>
					<option value="<?php echo esc_attr( $activecampaign_list_id ); ?>" selected><?php echo esc_html( ! empty( $activecampaign_list_label ) ? $activecampaign_list_label : $activecampaign_list_id ); ?></option>
				<?php endif; ?>
			</select>
		</div>

		<div class="wordform-connect-save-update-activecampaign-info-button-div-wrapper">
			<button
				class="button button-primary wordform-connect-save-update-app-fields-button"
				data-save-action="wordform_connect_save_activecampaign_data_as_wp_option_data"
				data-feedback-selector=".wordform-activecampaign-app-feedback-msg"
				data-fields-config='[{"selector":".activecampaign-api-url","paramKey":"activecampaignApiBaseUrl","validation":"url","invalidMessage":"Please enter valid ActiveCampaign API URL."},{"selector":".activecampaign-api-key","paramKey":"activecampaignApiKey","validation":"text","invalidMessage":"Please enter ActiveCampaign API Key."},{"selector":".activecampaign-list-id","paramKey":"activecampaignListId","validation":"text","invalidMessage":"Please enter ActiveCampaign List ID."},{"selector":".activecampaign-list-label","paramKey":"activecampaignListLabel","validation":"optional"}]'
			><i class="fa-solid fa-gear"></i> Save / Update</button>
		</div>
	</div>
</div>
<!-- End ActiveCampaign Configure Modal Form -->
