<?php

/**
 * File Description: This file handles the processing of a specific form submission.
 *
 * @package GSheetConnector Formidable Forms
 */

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

$gscfrmdform_client_id = get_option('gscfrmdform_client_id');
$gscfrmdform_secret_id = get_option('gscfrmdform_secret_id');
//$gscfrmdform_manual_code_db = get_option( 'gscfrmdform_access_manual_code' );
$gscfrmdform_code_db = get_option('frmgsc_gs_access_code');
//$gscfrmdform_manual_setting = get_option('gscfrmdform_manual_setting');
$token = get_option('frmgsc_gs_token');
//$token_manual = get_option('gscfrmdform_token_manual');


$gscfrmdform_code = '';
$header = '';
if (isset($_GET['code'])) {
	if (is_string($_GET['code'])) {
		$gscfrmdform_code = sanitize_text_field(wp_unslash($_GET['code']));
	}
	update_option('is_new_client_secret_frmgsc', 1);
	$header = esc_url_raw(admin_url('admin.php?page=formidable-form-google-sheet-config'));
}

?>
<!-- Save Google API Setting Option-->

<div class="card-wp dropdownoption">
	
	<h2><?php esc_html_e('Formidable Forms - Google Sheet Integration', 'gsheetconnector-formidable-forms'); ?></h2>
	<p class="sub-desc"><?php esc_html_e('Choose your Google API Setting from the dropdown. You can select Use Existing Client/Secret Key (Auto Google API Configuration) or Use Manual Client/Secret Key (Use Your Google API Configuration - Pro Version) or Use Service Account (Recommended- Pro Version) . After saving, the related integration settings will appear, and you can complete the setup.', 'gsheetconnector-formidable-forms'); ?></p>
	
	<div class="row">
	<label
		for="gscfrmdform_dro_option"><?php esc_html_e('Choose Google API Setting', 'gsheetconnector-formidable-forms'); ?></label>
	<select id="gscfrmdform_dro_option" name="gscfrmdform_dro_option">
		<option value="gscfrmdform_existing" selected>
			<?php echo esc_html__('Use Existing Client/Secret Key (Auto Google API Configuration)', 'gsheetconnector-formidable-forms'); ?>
		</option>
		<option value="gscfrmdform_manual" disabled="">
			<?php esc_html_e('Use Manual Client/Secret Key (Use Your Google API Configuration) (Upgrade To PRO)', 'gsheetconnector-formidable-forms'); ?>
		</option>
		<option value="gscfrmdform_service" disabled="">
			<?php esc_html_e('Use Service Account (Recommended- Pro Version) (Upgrade To PRO)', 'gsheetconnector-formidable-forms'); ?>
		</option>
	</select>
	<p class="int-meth-btn-gscfrmdform">
		<a href="https://www.gsheetconnector.com/formidable-forms-google-sheet-connector-pro" target="_blank">
			<button id="upgrade-gscfrmdform" class="btn-upgrade">
				<?php esc_html_e('Upgrade to PRO', 'gsheetconnector-formidable-forms'); ?>
			</button>
		</a>
		&nbsp;
		<span class="loading-sign-gscfrmdform-method-api">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
	</p>
	</div>
</div>
<input type="hidden" name="get_code" id="get_code"
	value="<?php echo esc_attr( ( isset( $_GET['code'] ) && $_GET['code'] !== '' ) ? '1' : '0' ); ?>">

<input type="hidden" name="gscfrmdform_manual_setting" id="gscfrmdform_manual_setting" value="">

<input type="hidden" name="redirect_auth_gscfrmdform" id="redirect_auth_gscfrmdform"
	value="<?php echo isset($header) ? esc_attr($header) : ''; ?>">


<input type="hidden" name="gs-ajax-nonce" id="gs-ajax-nonce"
	value="<?php echo esc_attr(wp_create_nonce('gs-ajax-nonce')); ?>" />

<!-- Auto Settings START -->

<div class="card-wp api_existing_setting_gscfrmdfrm">
	<h2>
		 <?php esc_html_e('Google Sheet Integration - Use Existing Client/Secret Key (Auto Google API Configuration)', 'gsheetconnector-formidable-forms'); ?> 
	</h2>
	<p class="sub-desc">
		 <?php esc_html_e('Automatic integration allows you to connect Formidable Forms with Google Sheets using built-in Google API configuration. By authorizing your Google account, the plugin will handle API setup and authentication automatically, enabling seamless form data sync. Learn more in the documentation', 'gsheetconnector-formidable-forms'); ?> <a href="https://www.gsheetconnector.com/docs/formidable-forms-gsheetconnector/integration-with-google-existing-method" target="_blank"> <?php esc_html_e('click here', 'gsheetconnector-formidable-forms'); ?> </a>	</p>
	 
	<?php if (empty(get_option('frmgsc_gs_token'))) { ?>
		<div class="wpform-gs-alert-kk" id="google-drive-msg">
			<p class="wpform-gs-alert-heading">
				<?php esc_html_e('Authenticate with your Google account, follow these steps:', 'gsheetconnector-formidable-forms'); ?>
			</p>
			<ol class="wpform-gs-alert-steps">
				<li><?php esc_html_e('Click on the "Sign In With Google" button.', 'gsheetconnector-formidable-forms'); ?>
				</li>
				<li><?php esc_html_e('Grant permissions for the following:', 'gsheetconnector-formidable-forms'); ?></li>
				<li><?php esc_html_e('Google Drive', 'gsheetconnector-formidable-forms'); ?></li>
				<li><?php esc_html_e('Google Sheets', 'gsheetconnector-formidable-forms'); ?>
					<span><?php esc_html_e('* Ensure that you enable the checkbox for each of these services.', 'gsheetconnector-formidable-forms'); ?></span>
				</li>
				<li><?php esc_html_e('This will allow the integration to access your Google Drive and Google Sheets.', 'gsheetconnector-formidable-forms'); ?>
				</li>
			</ol>
		</div>
	<?php } ?>
	<div class="row">
		<label><?php esc_html_e('Google Access Code ', 'gsheetconnector-formidable-forms'); ?></label>
		<?php if (!empty(get_option('frmgsc_gs_token')) && get_option('frmgsc_gs_token') !== '') { ?>
			<input type="text" name="google-access-code" id="ffforms-setting-google-access-code" value="" disabled
				placeholder="<?php esc_attr_e('Currently Active', 'gsheetconnector-formidable-forms'); ?>" />
			<input type="button" name="wp-deactivate-log" id="wp-deactivate-log"
				value="<?php esc_attr_e('Deactivate', 'gsheetconnector-formidable-forms'); ?>"
				class="button button-primary" />
		
		
			<span class="loading-sign-gscfrmdform-deactive">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
			<?php
		} else {
			$redirect_uri = esc_url(admin_url('admin.php?page=formidable-form-google-sheet-config'));
			?>
			<input type="text" name="google-access-code" id="ffforms-setting-google-access-code" disabled
				value="<?php echo esc_attr($gscfrmdform_code); ?>"
				placeholder="<?php esc_attr_e('Click on Sign in with Google button', 'gsheetconnector-formidable-forms'); ?>" />
			<?php if (empty($gscfrmdform_code)) { ?>
				<a
					href="https://oauth.gsheetconnector.com/index.php?clien_admin_url=<?php echo esc_url($redirect_uri); ?>&plugin=woocommercegsheetconnector">
					<img class="g-button"
						src="<?php echo esc_url(GSCFRMDFORM_VERSION_URL); ?>/assets/img/btn_google_signin_dark_pressed_web.gif">
				</a>
			<?php } ?>
		<?php } ?>
		<!-- Set nonce -->
		<input type="hidden" name="gs-ajax-nonce" id="gs-ajax-nonce"
			value="<?php echo esc_attr(wp_create_nonce('gs-ajax-nonce')); ?>" />
		<?php if (empty(get_option('frmgsc_gs_token'))) { ?>
			<?php if (!empty($_GET['code'])) { ?>
				<input type="submit" name="save-gs" class="ffforms-btn njforms-btn-md njforms-btn-orange"
					id="save-frmdform-gs-code"
					value="<?php esc_attr_e('Save & Authenticate', 'gsheetconnector-formidable-forms'); ?>">
			<?php } ?>
		<?php } ?>
		<span class="loading-sign-gscfrmdform">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
	</div>
	<?php
	// Resolved - Google Sheet permission issues - START
	if (!empty(get_option('gscfrmdform_verify')) && (get_option('gscfrmdform_verify') == 'invalid-auth')) {
		?>
		<p>
			<?php esc_html_e('Something went wrong! It looks like you have not given the permission for Google Drive and Google Sheets from your Google account. Please Deactivate Auth and Re-Authenticate with the required permissions.', 'gsheetconnector-formidable-forms'); ?>
		</p>
		<p>
			<img width="350px" src="<?php echo esc_url(GSCFRMDFORM_VERSION_URL); ?>assets/img/permission_screen.png">
		</p>
		<p>
			<?php esc_html_e('Also,', 'gsheetconnector-formidable-forms'); ?><a
				href="https://myaccount.google.com/permissions" target="_blank">
				<?php esc_html_e('Click Here', 'gsheetconnector-formidable-forms'); ?></a>
			<?php esc_html_e('and check if "GSheetConnector for WP Contact Forms" is listed under Third-party apps with account access; if it is, remove it.', 'gsheetconnector-formidable-forms'); ?>
		</p>
		<?php
	} else {
		// Resolved - Google Sheet permission issues - END
		if (!empty(get_option('frmgsc_gs_token')) && get_option('frmgsc_gs_token') !== '') {
			$google_sheet = new GSCFRMDFORM_Lib();
			$email_account = $google_sheet->gsheet_print_google_account_email();
			if ($email_account) {
				update_option('formidable_form_gs_auth_expired_free', 'false');
				?>
				<div class="connected-account row">
					<label><?php
					// translators: %s is the connected Google email address.
					printf(esc_html__('Connected Email Account', 'gsheetconnector-formidable-forms'), esc_html($email_account));

					?></label>
					<?php 
					printf(esc_html__('%s', 'gsheetconnector-formidable-forms'), esc_html($email_account));

					?>
				</div>

			<?php } else {
				update_option('formidable_form_gs_auth_expired_free', 'true');
				?>
				<p style="color: red;">
					<?php esc_html_e('Something is wrong! Your Auth code may be incorrect or expired. Please Deactivate and Re-Authenticate with the code.', 'gsheetconnector-formidable-forms'); ?>
				</p>
				<?php
			}
		}
	}
	?>
	
	<div class="msg success-msg">
		<i class="fa-solid fa-lock"></i>
			<p>
			<?php esc_html_e('We do not store any of the data from your Google account on our servers, everything is processed & stored on your server. We take your privacy extremely seriously and ensure it is never misused.', 'gsheetconnector-formidable-forms'); ?> <a href="https://gsheetconnector.com/usage-tracking/" target="_blank" rel="noopener noreferrer"><?php esc_html_e('Learn more.', 'gsheetconnector-formidable-forms'); ?></a></p>
	</div>

	 
	<span class="ffforms-setting-fields">
		<label><?php esc_html_e('Debug Log', 'gsheetconnector-formidable-forms'); ?></label>
		<button class="fmdgsc-logs"><?php esc_html_e('View', 'gsheetconnector-formidable-forms'); ?></button>
		<label><a class="debug-clear-kk"><?php esc_html_e('Clear', 'gsheetconnector-formidable-forms'); ?></a></label>
		<span class="clear-loading-sign-gscfrmdform">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>

	</span>
	<p id="gscfrmdform-validation-message"></p>
	<span id="deactivate-message"></span>
</div>

<div class="frm-system-Error-logs">
	<div class="frmdisplayLogs">
		<?php
		$frmexistdebugfile = get_option('frmgsc_debug_log_file');

		// Check if debug log file exists
		if (!empty($frmexistdebugfile) && file_exists($frmexistdebugfile)) {
			$display_frm_free_logs = file_get_contents($frmexistdebugfile);

			if (!empty($display_frm_free_logs)) {
				// Escape safely and preserve newlines
				echo nl2br(esc_html($display_frm_free_logs));
			} else {
				esc_html_e('No errors found.', 'gsheetconnector-formidable-forms');
			}
		} else {
			// If no log file exists
			esc_html_e('No log file exists as no errors are generated.', 'gsheetconnector-formidable-forms');
		}
		?>
	</div>
</div>

<!-- 2nd div -->
<div class="two-col gscfrmd-form-box-help12">
	<div class="col gscfrmd-form-box12">
		<header>
			<h3><?php esc_html_e('Next steps…', 'gsheetconnector-formidable-forms'); ?></h3>
		</header>
		<div class="gscfrmd-form-box-content12">
			<ul class="gscfrmd-form-list-icon12">
				<li>
					<a href="https://www.gsheetconnector.com/docs/formidable-forms-gsheetconnector/requirements" target="_blank">
						<div>
							<button class="icon-button">
								<span class="dashicons dashicons-download"></span>
							</button>
							<strong><?php esc_html_e('Upgrade to PRO', 'gsheetconnector-formidable-forms'); ?></strong>
							<p><?php esc_html_e('Sync Orders, Order wise data and much more...', 'gsheetconnector-formidable-forms'); ?>
							</p>
						</div>
					</a>
				</li>
				<li>
					<a href="https://www.gsheetconnector.com/docs/formidable-forms-gsheetconnector/requirements" target="_blank">
						<div>
							<button class="icon-button">
								<span class="dashicons dashicons-download"></span>
							</button>
							<strong><?php esc_html_e('Compatibility', 'gsheetconnector-formidable-forms'); ?></strong>
							<p><?php esc_html_e('Compatibility with Formidable-Forms Third-Party Plugins', 'gsheetconnector-formidable-forms'); ?>
							</p>
						</div>
					</a>
				</li>
				<li>
					<a href="https://www.gsheetconnector.com/docs/formidable-forms-gsheetconnector/plugin-settings-pro-version" target="_blank">
						<div>
							<button class="icon-button">
								<span class="dashicons dashicons-chart-bar"></span>
							</button>
							<strong><?php esc_html_e('Multi Languages', 'gsheetconnector-formidable-forms'); ?></strong>
							<p><?php esc_html_e('This plugin supports multi-languages as well', 'gsheetconnector-formidable-forms'); ?>!
							</p>
						</div>
					</a>
				</li>
				<li>
					<a href="https://www.gsheetconnector.com/docs/formidable-forms-gsheetconnector/plugin-settings-free-version" target="_blank">
						<div>
							<button class="icon-button">
								<span class="dashicons dashicons-download"></span>
							</button>
							<strong><?php esc_html_e('Support Wordpress multisites', 'gsheetconnector-formidable-forms'); ?></strong>
							<p><?php esc_html_e('With the use of a Multisite, you’ll also have a new level of user-available: the Super Admin.', 'gsheetconnector-formidable-forms'); ?>
							</p>
						</div>
					</a>
				</li>
			</ul>
		</div>
	</div>

	<!-- 2nd div -->
	<div class="col gscfrmd-form-box13">
		<header>
			<h3><?php esc_html_e('Product Support', 'gsheetconnector-formidable-forms'); ?></h3>
		</header>
		<div class="gscfrmd-form-box-content13">
			<ul class="gscfrmd-form-list-icon13">
				<li>
					<a href="https://www.gsheetconnector.com/docs/formidable-forms-gsheetconnector" target="_blank">
						<span class="dashicons dashicons-book"></span>
						<div>
							<strong><?php esc_html_e('Online Documentation', 'gsheetconnector-formidable-forms'); ?></strong>
							<p><?php esc_html_e('Understand all the capabilities of Formidable-Forms GsheetConnector', 'gsheetconnector-formidable-forms'); ?>
							</p>
						</div>
					</a>
				</li>
				<li>
					<a href="https://www.gsheetconnector.com/support" target="_blank">
						<span class="dashicons dashicons-sos"></span>
						<div>
							<strong><?php esc_html_e('Ticket Support', 'gsheetconnector-formidable-forms'); ?></strong>
							<p><?php esc_html_e('Direct help from our qualified support team', 'gsheetconnector-formidable-forms'); ?>
							</p>
						</div>
					</a>
				</li>
				<li>
					<a href="https://www.gsheetconnector.com/affiliate-area" target="_blank">
						<span class="dashicons dashicons-admin-links"></span>
						<div>
							<strong><?php esc_html_e('Affiliate Program', 'gsheetconnector-formidable-forms'); ?></strong>
							<p><?php esc_html_e('Earn flat 30', 'gsheetconnector-formidable-forms'); ?>%
								<?php esc_html_e('on every sale', 'gsheetconnector-formidable-forms'); ?>!
							</p>
						</div>
					</a>
				</li>
			</ul>
		</div>
	</div>
</div>

<!-- Auto Settings END -->