<?php

use WcElectronInvoice\Admin\Settings\OptionPage;
use function WcElectronInvoice\Functions\filterInput;

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

$uidOption  = (int)get_option('pop_account_user_id', false);
$licenseKey = OptionPage::init()->getOptions('license_key') ?: null;
$secretKey  = OptionPage::getK() ?: null;

// Return if POP connected
if ($uidOption && $licenseKey && $secretKey) : ?>
    <style>.wc_el_inv-form.pop_connected {
        display: none !important;
      }</style>
    <script id="wizard_not_allowed" type="text/javascript">
        window.location = '<?php echo esc_url_raw(admin_url('admin.php?page=wc_el_inv-options-page&tab=general')); ?>';
    </script>
    <?php return; endif; ?>

<?php
$requiredFields = \WcElectronInvoice\Functions\checkOptions('general');
$tab            = filterInput($_GET, 'tab', FILTER_UNSAFE_RAW) ?: null;
$wizard         = filterInput($_GET, 'wizard', FILTER_UNSAFE_RAW) ?: null; ?>

<?php if ('init' === $wizard && 'wizard' === $tab) : ?>
    <?php if ('prod' === WC_EL_INV_ENV && $requiredFields && $uidOption) : ?>
        <div id="config-end">
            <?php
            echo sprintf('<div class="pop-notice notice-info"><p><span class="dashicons dashicons-info"></span> %s</p></div>',
                esc_html__("The data configuration has been completed. You will shortly be redirected to the 'General' section.",
                    WC_EL_INV_TEXTDOMAIN)
            ); ?>
        </div>
        <script id="wizard_end" type="text/javascript">
            setTimeout(() => {
                window.location = '<?php echo esc_url_raw(admin_url('admin.php?page=wc_el_inv-options-page&tab=general')); ?>';
            }, 2500);
        </script>
    <?php else: ?>
        <div id="loading-overlay"><?php echo esc_html__("Loading, please wait...", WC_EL_INV_TEXTDOMAIN) ?></div>
    <?php endif; ?>
<?php endif; ?>
