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

$zohocrm_credentials = SftcyWordformConnectZohocrm::get_zohocrm_credentials();
$zohocrm_client_id   = isset( $zohocrm_credentials['clientID'] ) ? $zohocrm_credentials['clientID'] : '';
$zohocrm_client_secret = isset( $zohocrm_credentials['clientSecret'] ) ? $zohocrm_credentials['clientSecret'] : '';
$zohocrm_accounts_url = isset( $zohocrm_credentials['accountsUrl'] ) ? $zohocrm_credentials['accountsUrl'] : 'https://accounts.zoho.com';
$zohocrm_redirect_url = SftcyWordformConnectBase::get_wordform_connect_oauth_redirect_url();
$zohocrm_auth_managed_available = SftcyWordformConnectZohocrm::is_zohocrm_managed_oauth_available();
$zohocrm_auth_uri = SftcyWordformConnectZohocrm::get_zohocrm_authorization_uri();
$wordform_connect_is_local_env = SftcyWordformConnectBase::is_wordform_connect_local_environment();
?>
<!-- Start Zoho CRM Configure Modal Form -->
<div id="ZohocrmAPIModal" title="Zoho CRM Connection Configure" class="" style="display: none;">
	<div class="wordform-connect-modal-quickstart">
		<strong>Quick setup:</strong>
		<?php if ( $zohocrm_auth_managed_available ) : ?>
			Use <strong>Connect with Zoho CRM</strong> to verify the account and finish setup in one provider flow.
		<?php else : ?>
			Enter the OAuth app credentials, save them, then continue to Zoho CRM to verify and approve access.
		<?php endif; ?>
	</div>
	<h5>How to configure Zoho CRM connection</h5>
	<p>
		<a href="https://www.zoho.com/accounts/protocol/oauth/web-server-applications.html" target="_blank" rel="noopener">Zoho OAuth Web Server Apps</a> |
		<a href="https://www.zoho.com/crm/developer/docs/api/v8/upsert-records.html" target="_blank" rel="noopener">Zoho CRM Upsert Records Docs</a> |
		<a href="https://www.zoho.com/crm/developer/docs/api/v8/insert-records.html" target="_blank" rel="noopener">Zoho CRM Records API Docs</a>
	</p>
	<div id="zohocrm-api-create-help" class="accordion">
		<?php if ( $zohocrm_auth_managed_available ) : ?>
		<h3>Step 1 - Click Connect with Zoho CRM</h3>
		<div>
			<p>Use the <strong>Connect with Zoho CRM</strong> button below. WordForm will open the Zoho sign-in and consent screen for the CRM account that should receive your form contacts.</p>
		</div>

		<h3>Step 2 - Sign in and approve access</h3>
		<div>
			<p>Approve access and return to WordForm. After the callback succeeds, the app status will be shown as <strong>Configured</strong>.</p>
		</div>

		<h3>Step 3 - Connect only the forms you want</h3>
		<div>
			<p>Select a form, choose <strong>Zoho CRM</strong>, and click <strong>Create Connection</strong>. Only connected forms will send submission data into Zoho CRM.</p>
		</div>

		<h3>Step 4 - Reconfigure if authentication is lost</h3>
		<div>
			<p>WordForm keeps the connection active using stored refresh access. If the Zoho token is revoked or removed, use <strong>Reconfigure</strong> and connect again the same way.</p>
		</div>
		<?php else : ?>
		<h3>Step 1 - Create a Zoho self-client or server app</h3>
		<div>
			<p>Create a Zoho OAuth client for a server-based app so WordForm can request CRM access.</p>
		</div>

		<h3>Step 2 - Add the callback URL</h3>
		<div>
			<p>Use the redirect URL shown below as the exact callback URL in Zoho OAuth settings.</p>
		</div>

		<h3>Step 3 - Copy Client ID and Client Secret</h3>
		<div>
			<p>Copy the Zoho <strong>Client ID</strong> and <strong>Client 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 Zoho for authorization. After that, connect only the forms you want using <strong>Select Form + Select Application + Create Connection</strong>. Zoho CRM needs at least one valid email field in the form submission.</p>
		</div>
		<?php endif; ?>
	</div>

	<div class="zohocrm-connection-input-elements-wrapper" style="display:flex; flex-direction:column;">
		<div class="wordform-zohocrm-app-feedback-msg"></div>
		<?php if ( $zohocrm_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 Zoho CRM and complete the connection.
			</p>
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-shield-check"></i> Zoho CRM verifies the connection during sign-in and consent approval.</p>
			<button
				type="button"
				class="button button-primary wordform-start-managed-oauth-authentication-btn"
				data-auth-url="<?php echo esc_url( $zohocrm_auth_uri ); ?>"
			><span class="dashicons dashicons-admin-network" aria-hidden="true"></span> Connect with Zoho CRM</button>
		</div>
		<?php else : ?>
		<div>
			<label for="ZohocrmClientId"><span class="dashicons dashicons-admin-network" aria-hidden="true"></span> Zoho CRM Client ID
				<a class="wordform-field-help-link" href="https://www.zoho.com/accounts/protocol/oauth/web-server-applications.html" target="_blank" rel="noopener" aria-label="How to get the Zoho CRM Client ID">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="ZohocrmClientId" name="zohocrm-client-id" class="zohocrm-client-id" value="<?php echo esc_attr( $zohocrm_client_id ); ?>" placeholder="Zoho CRM Client ID">
		</div>

		<div>
			<label for="ZohocrmClientSecret"><span class="dashicons dashicons-lock" aria-hidden="true"></span> Zoho CRM Client Secret
				<a class="wordform-field-help-link" href="https://www.zoho.com/accounts/protocol/oauth/web-server-applications.html" target="_blank" rel="noopener" aria-label="How to get the Zoho CRM Client Secret">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="ZohocrmClientSecret" name="zohocrm-client-secret" class="zohocrm-client-secret" value="<?php echo esc_attr( $zohocrm_client_secret ); ?>" placeholder="Zoho CRM Client Secret">
		</div>

		<div>
			<label for="ZohocrmAccountsUrl"><span class="dashicons dashicons-admin-site" aria-hidden="true"></span> Zoho Accounts URL
				<a class="wordform-field-help-link" href="https://www.zoho.com/accounts/protocol/oauth/web-server-applications.html" target="_blank" rel="noopener" aria-label="How to choose the Zoho Accounts URL">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="ZohocrmAccountsUrl" name="zohocrm-accounts-url" class="zohocrm-accounts-url" value="<?php echo esc_attr( $zohocrm_accounts_url ); ?>" placeholder="https://accounts.zoho.com">
		</div>

		<div>
			<label for="ZohocrmRedirectUrl"><span class="dashicons dashicons-admin-links" aria-hidden="true"></span> Redirect URL
				<a class="wordform-field-help-link" href="https://www.zoho.com/accounts/protocol/oauth/web-server-applications.html" target="_blank" rel="noopener" aria-label="How to use the Zoho CRM redirect URL">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="ZohocrmRedirectUrl" name="zohocrm-redirect-url" class="zohocrm-redirect-url" value="<?php echo esc_attr( $zohocrm_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. Zoho can work with localhost testing, and if you prefer a public HTTPS tunnel you can 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-zohocrm-info-button-div-wrapper">
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-shield-check"></i> WordForm saves these credentials first. Zoho CRM 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_zohocrm_auth_credentials_with_authentication"
				data-feedback-selector=".wordform-zohocrm-app-feedback-msg"
				data-fields-config='[{"selector":".zohocrm-client-id","paramKey":"zohocrmClientId","validation":"text","invalidMessage":"Please enter Zoho CRM Client ID."},{"selector":".zohocrm-client-secret","paramKey":"zohocrmClientSecret","validation":"text","invalidMessage":"Please enter Zoho CRM Client Secret."},{"selector":".zohocrm-accounts-url","paramKey":"zohocrmAccountsUrl","validation":"url","invalidMessage":"Please enter valid Zoho Accounts URL."},{"selector":".zohocrm-redirect-url","paramKey":"zohocrmRedirectUrl","validation":"url","invalidMessage":"Zoho CRM Redirect URL is invalid."}]'
			><i class="fa-solid fa-gear"></i> Save / Update</button>
		</div>
		<?php endif; ?>
	</div>
</div>
<!-- End Zoho CRM Configure Modal Form -->
