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

$salesforce_credentials = SftcyWordformConnectSalesforce::get_salesforce_credentials();
$salesforce_client_id   = isset( $salesforce_credentials['clientID'] ) ? $salesforce_credentials['clientID'] : '';
$salesforce_client_secret = isset( $salesforce_credentials['clientSecret'] ) ? $salesforce_credentials['clientSecret'] : '';
$salesforce_login_url   = isset( $salesforce_credentials['loginUrl'] ) ? $salesforce_credentials['loginUrl'] : 'https://login.salesforce.com';
$salesforce_redirect_url = SftcyWordformConnectBase::get_wordform_connect_oauth_redirect_url();
$salesforce_auth_managed_available = SftcyWordformConnectSalesforce::is_salesforce_managed_oauth_available();
$salesforce_auth_uri = SftcyWordformConnectSalesforce::get_salesforce_authorization_uri();
$wordform_connect_is_local_env = SftcyWordformConnectBase::is_wordform_connect_local_environment();
?>
<!-- Start Salesforce Configure Modal Form -->
<div id="SalesforceAPIModal" title="Salesforce Connection Configure" class="" style="display: none;">
	<div class="wordform-connect-modal-quickstart">
		<strong>Quick setup:</strong>
		<?php if ( $salesforce_auth_managed_available ) : ?>
			Use <strong>Connect with Salesforce</strong> to verify the account and finish setup in one provider flow.
		<?php else : ?>
			Enter the connected-app credentials, save them, then continue to Salesforce to verify and approve access.
		<?php endif; ?>
	</div>
	<h5>How to configure Salesforce connection</h5>
	<p>
		<a href="https://help.salesforce.com/s/articleView?id=platform.ev_relay_create_connected_app.htm&type=5" target="_blank" rel="noopener">Salesforce Connected App Help</a> |
		<a href="https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm" target="_blank" rel="noopener">Salesforce OAuth Web Server Flow</a> |
		<a href="https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm" target="_blank" rel="noopener">Salesforce REST SObject Docs</a>
	</p>
	<div id="salesforce-api-create-help" class="accordion">
		<?php if ( $salesforce_auth_managed_available ) : ?>
		<h3>Step 1 - Click Connect with Salesforce</h3>
		<div>
			<p>Use the <strong>Connect with Salesforce</strong> button below. WordForm will open the Salesforce login and approval screen for the account that should receive your form contacts.</p>
		</div>

		<h3>Step 2 - Sign in and approve access</h3>
		<div>
			<p>Log in to Salesforce, approve the requested access, and return to WordForm. Once the callback succeeds, WordForm will mark the app as <strong>Configured</strong>.</p>
		</div>

		<h3>Step 3 - Connect only the forms you want</h3>
		<div>
			<p>Select a form, choose <strong>Salesforce</strong>, and click <strong>Create Connection</strong>. WordForm will sync only those connected form submissions.</p>
		</div>

		<h3>Step 4 - Reconfigure if access is revoked</h3>
		<div>
			<p>WordForm keeps the connection active using stored refresh access. If the connected Salesforce account revokes the app or authentication data becomes invalid, click <strong>Reconfigure</strong> and connect again the same way.</p>
		</div>
		<?php else : ?>
		<h3>Step 1 - Create a Connected App</h3>
		<div>
			<p>Create a Salesforce <strong>Connected App</strong> and enable OAuth so WordForm can request access to your CRM.</p>
		</div>

		<h3>Step 2 - Add the callback URL</h3>
		<div>
			<p>Use the redirect URL shown below as the exact Salesforce callback URL. It must match exactly in the Connected App settings.</p>
		</div>

		<h3>Step 3 - Copy Consumer Key and Consumer Secret</h3>
		<div>
			<p>After saving the Connected App, copy the <strong>Consumer Key</strong> and <strong>Consumer Secret</strong>, paste them below, then click <strong>Save / Update</strong>.</p>
		</div>

		<h3>Step 4 - Authorize and connect forms</h3>
		<div>
			<p>WordForm will redirect you to Salesforce for authorization. After that, connect only the forms you want using <strong>Select Form + Select Application + Create Connection</strong>. Salesforce needs at least one valid email field in the form submission.</p>
		</div>
		<?php endif; ?>
	</div>

	<div class="salesforce-connection-input-elements-wrapper" style="display:flex; flex-direction:column;">
		<div class="wordform-salesforce-app-feedback-msg"></div>
		<?php if ( $salesforce_auth_managed_available ) : ?>
		<div class="wordform-managed-oauth-connect-action">
			<p>
				<strong>Quick Connect is enabled for this site.</strong> Use the button below to sign in to Salesforce and finish the connection.
			</p>
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-shield-check"></i> Salesforce verifies the connection during sign-in and app approval.</p>
			<button
				type="button"
				class="button button-primary wordform-start-managed-oauth-authentication-btn"
				data-auth-url="<?php echo esc_url( $salesforce_auth_uri ); ?>"
			><span class="dashicons dashicons-admin-network" aria-hidden="true"></span> Connect with Salesforce</button>
		</div>
		<?php else : ?>
		<div>
			<label for="SalesforceClientId"><span class="dashicons dashicons-admin-network" aria-hidden="true"></span> Salesforce Consumer Key
				<a class="wordform-field-help-link" href="https://help.salesforce.com/s/articleView?id=platform.ev_relay_create_connected_app.htm&type=5" target="_blank" rel="noopener" aria-label="How to get the Salesforce Consumer Key">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="SalesforceClientId" name="salesforce-client-id" class="salesforce-client-id" value="<?php echo esc_attr( $salesforce_client_id ); ?>" placeholder="Salesforce Consumer Key">
		</div>

		<div>
			<label for="SalesforceClientSecret"><span class="dashicons dashicons-lock" aria-hidden="true"></span> Salesforce Consumer Secret
				<a class="wordform-field-help-link" href="https://help.salesforce.com/s/articleView?id=platform.ev_relay_create_connected_app.htm&type=5" target="_blank" rel="noopener" aria-label="How to get the Salesforce Consumer Secret">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="SalesforceClientSecret" name="salesforce-client-secret" class="salesforce-client-secret" value="<?php echo esc_attr( $salesforce_client_secret ); ?>" placeholder="Salesforce Consumer Secret">
		</div>

		<div>
			<label for="SalesforceLoginUrl"><span class="dashicons dashicons-admin-site" aria-hidden="true"></span> Salesforce Login URL
				<a class="wordform-field-help-link" href="https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm" target="_blank" rel="noopener" aria-label="How to choose the Salesforce login URL">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="SalesforceLoginUrl" name="salesforce-login-url" class="salesforce-login-url" value="<?php echo esc_attr( $salesforce_login_url ); ?>" placeholder="https://login.salesforce.com">
		</div>

		<div>
			<label for="SalesforceRedirectUrl"><span class="dashicons dashicons-admin-links" aria-hidden="true"></span> Redirect URL
				<a class="wordform-field-help-link" href="https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm" target="_blank" rel="noopener" aria-label="How to use the Salesforce callback URL">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="SalesforceRedirectUrl" name="salesforce-redirect-url" class="salesforce-redirect-url" value="<?php echo esc_attr( $salesforce_redirect_url ); ?>" readonly>
			<?php if ( $wordform_connect_is_local_env ) : ?>
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-flask"></i> WordForm now uses the direct local callback URL. If your Salesforce setup requires a public HTTPS callback for development, use a local tunnel and set <code>WORDFORM_CONNECT_OAUTH_REDIRECT_URL</code> in <code>wp-config.php</code>.</p>
			<?php else : ?>
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-shield-check"></i> WordForm uses your own site callback directly. No third-party redirect service is required.</p>
			<?php endif; ?>
		</div>

		<div class="wordform-connect-save-update-salesforce-info-button-div-wrapper">
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-shield-check"></i> WordForm saves these credentials first. Salesforce verifies them during the next authorization step.</p>
			<button
				class="button button-primary wordform-connect-save-update-app-fields-button"
				data-save-action="wordform_connect_save_salesforce_auth_credentials_with_authentication"
				data-feedback-selector=".wordform-salesforce-app-feedback-msg"
				data-fields-config='[{"selector":".salesforce-client-id","paramKey":"salesforceClientId","validation":"text","invalidMessage":"Please enter Salesforce Consumer Key."},{"selector":".salesforce-client-secret","paramKey":"salesforceClientSecret","validation":"text","invalidMessage":"Please enter Salesforce Consumer Secret."},{"selector":".salesforce-login-url","paramKey":"salesforceLoginUrl","validation":"url","invalidMessage":"Please enter valid Salesforce Login URL."},{"selector":".salesforce-redirect-url","paramKey":"salesforceRedirectUrl","validation":"url","invalidMessage":"Salesforce Redirect URL is invalid."}]'
			><i class="fa-solid fa-gear"></i> Save / Update</button>
		</div>
		<?php endif; ?>
	</div>
</div>
<!-- End Salesforce Configure Modal Form -->
