<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
// phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment

// Don't load directly
if (!defined('ABSPATH')) { exit; }

if (function_exists('r34ics_admin_full_access') && r34ics_admin_full_access()) {
	?>
	<div class="inside" id="license">

		<h2 class="screen-reader-text"><?php esc_html_e('License', 'ics-calendar-pro'); ?></h2>

		<div class="r34ics-inner-box">
		
			<?php
			if (r34icspro_ltwc()) {
				?>
				<p><em><?php

				printf(esc_html__('Your %1$s license has been activated manually.', 'ics-calendar-pro'), 'ICS Calendar Pro');
				?></em></p>
				<p class="r34ics"><mark class="alert"><?php

				printf(esc_html__('%1$sImportant:%2$s With manual license activation, ICS Calendar Pro cannot automatically check for updates. %3$s Please %4$scheck our website%5$s regularly to see if a new version is available for download.', 'ics-calendar-pro'), '<strong style="color: var(--r34ics--color--ics-red);">', '</strong>', '<br />', '<a href="https://icscalendar.com/my-account/api-downloads/" target="_blank">', '</a>');
				?></mark></p>
				<?php
			}
			else {
				if (!empty($R34ICSPro->licensed())) {
					$confirm = __('Are you sure? You will need to re-enter your license key. You can obtain your license key by visiting:', 'ics-calendar-pro') . '\n\nhttps://icscalendar.com/my-account/api-keys/';
					?>
					<form id="r34icspro-deactivate-license" method="post" action="" onsubmit="if (!confirm('<?php echo esc_attr($confirm); ?>')) { return false; }">
						<?php
						wp_nonce_field('r34icspro', 'r34icspro-deactivate-license-nonce');
						?>
						<p><input type="password" value="****" disabled="disabled" style="width: 8rem;" />
						<input type="password" value="****************************************" disabled="disabled" style="width: 24rem;" />
						<?php
						if (!r34icspro_ltwc()) {
							?><input type="submit" class="button" value="<?php echo esc_attr(__('Deactivate License', 'ics-calendar-pro')); ?>" /></p><?php
						}
						?>
						<p><em><?php

						printf(esc_html__('Your %1$s license is valid. License expires: %2$s', 'ics-calendar-pro'), 'ICS Calendar Pro',
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							'<strong>' . ($R34ICSPro->license_expires() ?? '') . '</strong>'
						);
						?></em></p>
					</form>
					<?php
				}
				else {
					?>
					<form id="r34icspro-activate-license" method="post" action="">
						<?php
						wp_nonce_field('r34icspro', 'r34icspro-activate-license-nonce');
						?>
						<p><input type="text" name="product_id" value="" placeholder="Product ID" style="width: 8rem;" />
						<input type="text" name="api_key" value="" placeholder="License Key" style="width: 24rem;" />
						<input type="submit" class="button button-primary" value="<?php echo esc_attr(__('Activate License', 'ics-calendar-pro')); ?>" /></p>
						<p><em><?php

						printf(esc_html__('Please enter your %1$sProduct ID%2$s and %3$sLicense Key%4$s to activate your license. %5$s A valid, activated license is required to access some plugin features, and to receive updates and support. %6$s Licenses can be purchased at: %7$s', 'ics-calendar-pro'), '<strong>', '</strong>', '<strong>', '</strong>', '<br />', '<br />', '<a href="https://icscalendar.com/" target="_blank">ICSCalendar.com</a>');
						?> </em></p>
					</form>
					<?php
				}
				
				if (get_option('r34icspro_license_debug')) {
					?>
					<p><mark class="alert"><?php esc_html_e('Please copy the following text and include it in your message when emailing for support with your license.', 'ics-calendar-pro'); ?></mark></p>
					<textarea class="diagnostics-window" readonly="readonly" style="cursor: copy;" onclick="this.select(); document.execCommand('copy');"><?php 
						echo wp_kses_post(strtoupper(__('License Diagnostics', 'ics-calendar-pro'))) . "\n=================================\n";
						r34ics_array_diagnostic_output($R34ICSPro->license_debug(true));
					?></textarea>
					<?php
				}
				?>

			</div>

			<div class="r34ics-inner-box">
			
				<h3><?php esc_html_e('Having trouble with your license?', 'ics-calendar-pro'); ?></h3>
			
				<p><?php

				printf(esc_html__('If you are seeing error messages related to your %1$s license, or if you are unable to run an update, please reset your license by doing the following:', 'ics-calendar-pro'), 'ICS Calendar Pro');
				?></p>
				
				<ol>
					<?php
					if (!empty($R34ICSPro->licensed())) {
						?>
						<li><?php

						printf(esc_html__('Click %1$sDeactivate License%2$s above.', 'ics-calendar-pro'), '<strong>', '</strong>');
						?></li>
						<?php
					}
					$confirm = __('Are you sure? You will need to re-enter your license key. You can obtain your license key by visiting:', 'ics-calendar-pro') . '\n\nhttps://icscalendar.com/my-account/api-keys/';
					?>
					<li>
						<p><?php

						printf(esc_html__('Click the button below to regenerate your installation "instance." (Also, if you have %1$s defined in your %2$s file, please remove that line of code before proceeding.)', 'ics-calendar-pro'), '<code>R34ICSPRO_INSTANCE</code>', '<strong>wp-config.php</strong>');
						?></p>
			
						<form id="r34icspro-regenerate-instance" method="post" action="" onsubmit="if (!confirm('<?php echo esc_attr($confirm); ?>')) { return false; }">
							<?php
							wp_nonce_field('r34icspro', 'r34icspro-regenerate-instance-nonce');
							?>
							<p><input type="submit" class="button" value="<?php echo esc_attr(__('Regenerate Instance', 'ics-calendar-pro')); ?>" /></p>
						</form>
					</li>
					<li><?php

					printf(esc_html__('Visit the %1$sLicense Keys%2$s page on our website. If you see a red DELETE button for your site&rsquo;s domain listed in the table, click it. Then copy your %3$s and %4$s and paste them into the corresponding fields above.', 'ics-calendar-pro'), '<a href="https://icscalendar.com/my-account/api-keys/" target="_blank">', '</a>', '<strong>License Key</strong>', '<strong>Product ID</strong>');
					?></li>
				</ol>
				
				<p><?php

				printf(esc_html__('If none of the above suggestions resolve the issue, please turn on %1$sLicense diagnostics%2$s under the %3$sSettings%4$s tab on this page, then return to the %5$sLicense%6$s tab to obtain additional technical details to include when %7$scontacting support%8$s.', 'ics-calendar-pro'), '<strong>', '</strong>', '<a href="#settings">', '</a>', '<strong>', '</strong>', '<a href="https://icscalendar.com/support/" target="_blank">', '</a>');
				?></p>
			
			</div>
		
			<?php
			if (!defined('R34ICSPRO_INSTANCE')) {
				?>
				<div class="r34ics-inner-box">
				
					<h3><?php esc_html_e('Using a load balancer (e.g. Cloudflare)?', 'ics-calendar-pro'); ?></h3>
					
					<p><?php

					printf(esc_html__('With load balancing (or any other configuration that may cause your site&rsquo;s IP address to change), the standard method %1$s uses to identify individual "instances" of license installations may break. If you find that your license needs to be entered repeatedly, try adding the following code to your site&rsquo;s %2$s file.', 'ics-calendar-pro'), 'ICS Calendar Pro', '<strong>wp-config.php</strong>');
					?></p>
					
					<code>define('R34ICSPRO_INSTANCE', '<?php echo esc_html(get_option('r34icspro_instance', '')); ?>');</code>
				
				</div>
				<?php
			}
		}
		?>

	</div>

	<?php
}
