<?php
/**
 * License Activation Form View
 *
 * @package WP_PowerSuite
 * @since 1.0.0
 *
 * @var WPPS_License_Manager $manager
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use WP_PowerSuite\Core\WhiteLabel;

$purchase_email = $manager->get_license_email() ?: get_bloginfo( 'admin_email' );
$product_url    = WPPS_License_Manager::get_product_url();
$renew_url      = WPPS_License_Manager::get_renew_url();
$status         = $manager->get_status_data();
$status_key     = sanitize_key( $status['status'] ?? 'inactive' );
$status_labels  = [
	'active'           => __( 'Active', 'wp-powersuite' ),
	'inactive'         => __( 'Inactive', 'wp-powersuite' ),
	'expired'          => __( 'Expired', 'wp-powersuite' ),
	'invalid'          => __( 'Invalid', 'wp-powersuite' ),
	'connection_issue' => __( 'Connection issue', 'wp-powersuite' ),
	'grace'            => __( 'Grace period', 'wp-powersuite' ),
	'unknown'          => __( 'Unknown', 'wp-powersuite' ),
];
$checked_at = ! empty( $status['checked_at'] ) ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $status['checked_at'] ) : __( 'Not checked yet', 'wp-powersuite' );
$notice            = isset( $_GET['wpps_license_notice'] ) ? sanitize_key( wp_unslash( $_GET['wpps_license_notice'] ) ) : '';
$is_active_status  = in_array( $status_key, [ 'active', 'grace' ], true );
$admin_post_url    = \WP_PowerSuite\Core\NetworkScope::adminUrl( 'admin-post.php' );
$pending           = $manager->get_page_pending_update();
?>
<div class="wpps-license-page">
	<div class="el-license-container">
		<div class="el-license-content">
		<?php if ( $notice === 'refreshed' ) : ?>
			<div class="wpps-license-alert wpps-license-alert--success"><p><?php esc_html_e( 'License status refreshed.', 'wp-powersuite' ); ?></p></div>
		<?php elseif ( $notice === 'updates_checked' ) : ?>
			<div class="wpps-license-alert wpps-license-alert--success"><p><?php esc_html_e( 'Update check completed.', 'wp-powersuite' ); ?></p></div>
		<?php elseif ( $notice === 'activated' && $is_active_status ) : ?>
			<div class="wpps-license-alert wpps-license-alert--success"><p><?php esc_html_e( 'License activated successfully.', 'wp-powersuite' ); ?></p></div>
		<?php elseif ( $notice === 'deactivated' ) : ?>
			<div class="wpps-license-alert wpps-license-alert--success"><p><?php esc_html_e( 'License deactivated successfully.', 'wp-powersuite' ); ?></p></div>
		<?php elseif ( $notice === 'failed' || ( $notice === 'activated' && ! $is_active_status ) ) : ?>
			<div class="wpps-license-alert wpps-license-alert--error"><p><?php echo esc_html( ! empty( $status['message'] ) ? (string) $status['message'] : __( 'License could not be activated. Check your key and email, then try again.', 'wp-powersuite' ) ); ?></p></div>
		<?php endif; ?>
		<?php
		$update_partial = defined( 'WP_POWERSUITE_PLUGIN_DIR' )
			? WP_POWERSUITE_PLUGIN_DIR . 'src/Admin/Views/partials/license-update-available.php'
			: dirname( __DIR__, 3 ) . '/src/Admin/Views/partials/license-update-available.php';
		if ( is_readable( $update_partial ) ) {
			include $update_partial;
		}
		?>
		<div class="wpps-license-status-card wpps-license-status-card--<?php echo esc_attr( $status_key ); ?>">
			<div>
				<span class="wpps-license-eyebrow"><?php esc_html_e( 'Current status', 'wp-powersuite' ); ?></span>
				<strong class="wpps-license-status-badge"><?php echo esc_html( $status_labels[ $status_key ] ?? $status_labels['unknown'] ); ?></strong>
			</div>
			<div class="wpps-license-status-meta">
				<span><?php esc_html_e( 'Site', 'wp-powersuite' ); ?>: <?php echo esc_html( home_url( '/' ) ); ?></span>
				<span><?php esc_html_e( 'Site hash', 'wp-powersuite' ); ?>: <code><?php echo esc_html( $manager->get_site_hash() ); ?></code></span>
				<span><?php esc_html_e( 'Last checked', 'wp-powersuite' ); ?>: <?php echo esc_html( $checked_at ); ?></span>
			</div>
			<?php if ( ! empty( $status['message'] ) && ! in_array( $status_key, [ 'active' ], true ) ) : ?>
				<p class="wpps-license-status-message wpps-license-status-message--<?php echo esc_attr( $status_key ); ?>"><?php echo esc_html( $status['message'] ); ?></p>
			<?php endif; ?>
		</div>
		<form method="post" action="<?php echo esc_url( $admin_post_url ); ?>" class="el-license-form-block">
		<input type="hidden" name="action" value="WP_PowerSuite_el_activate_license"/>
		<p class="el-license-intro"><?php echo esc_html( sprintf( __( 'Enter your license key and the email address used at purchase to activate %s. You will receive update notifications at this email.', 'wp-powersuite' ), WhiteLabel::pluginName() ) ); ?></p>
		<div class="el-license-field">
			<label for="el_license_key"><?php esc_html_e( 'License Key', 'wp-powersuite' ); ?></label>
			<input type="text" class="regular-text code" name="el_license_key" id="el_license_key" size="50" placeholder="xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx" required>
		</div>
		<div class="el-license-field">
			<label for="el_license_email"><?php esc_html_e( 'Email Address', 'wp-powersuite' ); ?></label>
			<input type="email" class="regular-text code" name="el_license_email" id="el_license_email" size="50" value="<?php echo esc_attr( $purchase_email ); ?>" required>
			<p class="description"><?php esc_html_e( 'We will send update notifications to this email. We do not spam.', 'wp-powersuite' ); ?></p>
		</div>
		<div class="el-license-active-btn">
			<?php wp_nonce_field( 'el-license' ); ?>
			<?php submit_button( __( 'Activate License', 'wp-powersuite' ) ); ?>
		</div>
		</form>

<?php if ( $manager->get_masked_license_key() !== '' ) : ?>
		<form method="post" action="<?php echo esc_url( $admin_post_url ); ?>" class="wpps-license-refresh-form">
			<input type="hidden" name="action" value="WP_PowerSuite_el_refresh_license"/>
			<?php wp_nonce_field( 'el-license' ); ?>
			<button type="submit" class="button button-secondary">
				<span class="dashicons dashicons-update" aria-hidden="true"></span>
				<?php esc_html_e( 'Refresh / Check License', 'wp-powersuite' ); ?>
			</button>
			<p class="description"><?php esc_html_e( 'Re-check your saved license with the server without re-entering your key.', 'wp-powersuite' ); ?></p>
		</form>
<?php endif; ?>

<?php if ( $product_url !== '' || $renew_url !== '' ) : ?>
		<div class="el-license-links">
			<?php if ( $product_url !== '' ) : ?>
			<p class="el-license-links__heading"><?php esc_html_e( "Don't have a license?", 'wp-powersuite' ); ?></p>
			<a href="<?php echo esc_url( $product_url ); ?>" target="_blank" rel="noopener noreferrer" class="button button-primary">
				<span class="dashicons dashicons-cart" aria-hidden="true"></span>
				<?php echo esc_html( sprintf( __( 'Purchase %s', 'wp-powersuite' ), WhiteLabel::pluginName() ) ); ?>
			</a>
			<?php endif; ?>
			<?php if ( $renew_url !== '' ) : ?>
			<p class="el-license-links__sub">
				<?php esc_html_e( 'Need to renew?', 'wp-powersuite' ); ?>
				<a href="<?php echo esc_url( $renew_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Renew your license', 'wp-powersuite' ); ?></a>
			</p>
			<?php endif; ?>
		</div>
<?php endif; ?>
		</div>
	</div>
</div>
