<?php

/**
 * View: admin/onboarding/license.
 */

use MemberPress\GroundLevel\Mothership\AbstractPluginConnection;
use MemberPress\GroundLevel\Mothership\Credentials;
use MemberPress\GroundLevel\Mothership\Manager\LicenseManager;

defined('ABSPATH') || exit;

$plugin_connection = memberpress()->container()->get(AbstractPluginConnection::class);
$credentials       = memberpress()->container()->get(Credentials::class);
$license_manager   = memberpress()->container()->get(LicenseManager::class);
$auth_url          = MeprAuthenticatorCtrl::get_auth_connect_url(
    false,
    false,
    ['onboarding' => 'true'],
    admin_url('admin.php?page=memberpress-onboarding&step=1')
);

// Form.
$plugin_id   = $plugin_connection->pluginId;
$form_name   = "{$plugin_id}_activate_license_form";
$form_button = "{$plugin_id}_license_button";
$form_result = $license_manager->getLastFormResult();
?>

<!-- License already activated -->
<?php if ($plugin_connection->getLicenseActivationStatus()) : ?>
    <div id="mepr-wizard-license-wrapper">
        <h2 class="mepr-wizard-step-title"><?php esc_html_e('Your License', 'memberpress'); ?></h2>
        <?php MeprView::render('/admin/onboarding/active_license', compact('form_result')); ?>
    </div>

<!-- Activate license -->
<?php else : ?>
    <h2 class="mepr-wizard-step-title"><?php esc_html_e('Activate License', 'memberpress'); ?></h2>

    <!-- Connect -->
    <?php if (!MeprOnboardingHelper::is_oem_edition(MEPR_EDITION)) : ?>
        <p class="mepr-wizard-step-description">
            <?php esc_html_e("First thing's first. Let's get your license activated.", 'memberpress'); ?>
        </p>

        <div id="mepr-wizard-activate-license-container">
            <p>
                <a
                    href="<?php echo esc_url($auth_url); ?>"
                    class="mepr-wizard-button-blue"
                >
                    <?php esc_html_e('Activate', 'memberpress'); ?>
                </a>
            </p>

            <!-- Errors -->
            <?php if (isset($_GET['license_error'])) : ?>
                <div class="notice notice-error inline">
                    <p><?php echo esc_html(sanitize_text_field(wp_unslash($_GET['license_error']))); ?></p>
                </div>
            <?php endif; ?>
        </div>

    <!-- Enter license manually -->
    <?php else : ?>
        <p class="mepr-wizard-step-description">
            <?php esc_html_e("First thing's first. Let's get your license activated. Contact your developer agency if you need your license key.", 'memberpress'); ?>
        </p>

        <div id="mepr-wizard-activate-license-container">
            <?php if ($form_result && $form_result->isFailure()) : ?>
                <div class="notice notice-error">
                    <p><?php echo esc_html(sanitize_text_field(wp_unslash($form_result->getMessage()))); ?></p>
                </div>
            <?php endif; ?>

            <form method="post" action="" name="<?php echo esc_attr($form_name); ?>">
                <?php wp_nonce_field('mothership_activate_license', '_wpnonce'); ?>
                <input type="hidden" name="hide_notice" value="1" />
                <input
                    name="license_key"
                    type="text"
                    placeholder="<?php esc_attr_e('Enter license key', 'memberpress'); ?>"
                    value="<?php echo esc_attr($credentials->getLicenseKey()); ?>"
                />
                <input
                    type="hidden"
                    name="activation_domain"
                    value="<?php echo esc_attr($credentials->getDomain()); ?>"
                />
                <input
                    type="hidden"
                    name="<?php echo esc_attr($form_button); ?>"
                    value="activate"
                />
                <p>

                    <button
                        type="submit"
                        id="mepr-activate-license-key"
                        class="mepr-wizard-button-blue"
                    >
                        <?php esc_html_e('Activate', 'memberpress'); ?>
                    </button>
                </p>
            </form>
        </div>
    <?php endif; ?>
<?php endif; ?>
