<?php

/**
 * sibs Admin Page.
 */
require_once plugin_dir_path(realpath(__DIR__ . '/')) . '/vendor/autoload.php';

use Sibs2\sibsConstants;
use Sibs2\sibsFunctions;
use Sibs2\sibsPayments;

function sibs_admin_menu()
{
    add_submenu_page(
        'woocommerce',
        'SIBS ' . __('BACKEND_CH_GENERAL', sibsConstants::SIBS_TRANSLATE),
        'SIBS ' . __('BACKEND_CH_GENERAL', sibsConstants::SIBS_TRANSLATE),
        'manage_options',
        'sibs',
        'sibs_page_content',
        3
    );
}

add_action('admin_menu', 'sibs_admin_menu');

function sibs_migrate_menu()
{
    add_submenu_page(
        'woocommerce',
        'SIBS ' . __('MIGRATE_GENERAL', sibsConstants::SIBS_TRANSLATE),
        'SIBS ' . __('MIGRATE_GENERAL', sibsConstants::SIBS_TRANSLATE),
        'manage_options',
        'sibs_migrate',
        'sibs_page_migrate',
        3
    );
}

//add_action('admin_menu', 'sibs_migrate_menu');

function sibs_page_migrate()
{
    global $wpdb;

    if (isset($_POST['sibs_save'])) {
        $save = sibsPayments::saveData($_POST);
    }

    ?>

    <div class="sibs_header">
        <div>
            <img src="<?php echo esc_attr(plugins_url(sibsConstants::SIBS_TRANSLATE . '/assets/images/sibs_logo.png', )); ?>" alt="Sibs">
        </div>
        <div>
            <h1><?php esc_html_e('SIBS Migration', sibsConstants::SIBS_TRANSLATE); ?></h1>
        </div>
    </div>
    <?php if (isset($_POST['sibs_save'])) { ?>
        <div class="sibs-notice notice notice-success">
            <p><strong><?php esc_html_e('Settings saved.', sibsConstants::SIBS_TRANSLATE); ?></strong></p>
        </div>
    <?php } ?>

    <div class="sibs-settings">
        <div class="sibs-migrate">
            <h2><?php echo __('SIBS_MIGRATE', sibsConstants::SIBS_TRANSLATE) ?></h2>
            <div>

            </div>
        </div>
        <div class="sibs-sidebar">
            <h2><?php echo __('SIBS_BACKEND_SYSTEM_INFO', sibsConstants::SIBS_TRANSLATE) ?></h2>
            <?php echo sibsPayments::version_description(); ?>
            <hr>
            <h2><?php echo __('SIBS_BACKEND_SODIUM_DESCRYPTION', sibsConstants::SIBS_TRANSLATE) ?></h2>
            <?php echo sibsPayments::sodium_test_descryption(); ?>
        </div>
    </div>
<?php
}

function sibs_page_content()
{
    if (isset($_POST['sibs_save'])) {
        $save = sibsPayments::saveData($_POST);
        print_r($save);
    }

    $environment_file    = sibsFunctions::get_environment();
    $environment         = $environment_file['environment'];
    ?>

    <div class="sibs_header">
        <div>
            <img src="<?php echo esc_attr(plugins_url(sibsConstants::SIBS_TRANSLATE . '/assets/images/sibs_logo.png', )); ?>" alt="Sibs">
        </div>
        <div>
            <h1>SIBS <?php esc_html_e('BACKEND_CH_GENERAL', sibsConstants::SIBS_TRANSLATE); ?></h1>
        </div>
    </div>
    <?php if (isset($_POST['sibs_save'])) { ?>
        <div class="sibs-notice notice notice-success">
            <p><strong><?php esc_html_e('Settings saved.', sibsConstants::SIBS_TRANSLATE); ?></strong></p>
        </div>
    <?php } ?>

    <div class="sibs-settings">
        <form name="sibs-settings" method="POST" action="">
            <table class="form-table" role="presentation">
                <tbody>
                    <tr>
                        <th scope="row">
                            <label for="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no' ?>">
                                <?php esc_html_e(__('SIBS_BACKEND_GENERAL_MERCHANTNO', sibsConstants::SIBS_TRANSLATE) . ' * '); ?>
                            </label>
                        </th>
                        <td>
                            <input class="regular-text" type="text" name="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no' ?>" value="<?php echo esc_attr(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no')); ?>" required>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token' ?>">
                                <?php esc_html_e(__('SIBS_BACKEND_GENERAL_TOKEN', sibsConstants::SIBS_TRANSLATE) . ' * '); ?>
                            </label>
                        </th>
                        <td>
                            <textarea class="regular-text" type="text" name="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token' ?>" required><?php echo esc_attr(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token')); ?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id'; ?>">
                                <?php esc_html_e(__('SIBS_BACKEND_GENERAL_CLIENT_ID', sibsConstants::SIBS_TRANSLATE) . ' * '); ?>
                            </label>
                        </th>
                        <td>
                            <input class="regular-text" type="text" name="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id' ?>" value="<?php echo esc_attr(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id')); ?>" required>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment' ?>"><?php esc_html_e(__('SIBS_BACKEND_GENERAL_ENVIRONMENT', sibsConstants::SIBS_TRANSLATE)); ?></label>
                        </th>
                        <td>
                            <select name="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment' ?>">
                                <?php
                                    foreach ($environment as $key => $value) {
                                        if (get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment') == $key) {
                                            echo '<option value="' . $key . '" selected>' . $value . '</option>';
                                        } else {
                                            echo '<option value="' . $key . '" >' . $value . '</option>';
                                        }
                                    } ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret' ?>">
                                <?php esc_html_e(__('SIBS WebHook Secret', sibsConstants::SIBS_TRANSLATE)); ?></label>
                        </th>
                        <td>
                            <input class="regular-text" type="text" name="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret' ?>" id="<?php echo sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret' ?>" value="<?php echo esc_attr(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret')) ?>" placeholder="  ">
                            <p>
                                <?php echo sibsPayments::webhook_description(); ?>
                            </p>
                        </td>
                    </tr>
                </tbody>
            </table>
            <p>
                <input class="button button-primary" type="submit" name="sibs_save" value="<?php esc_html_e('Save Changes', sibsConstants::SIBS_TRANSLATE); ?>">
            </p>
        </form>
        <div class="sibs-sidebar">
            <div>
                <?php
                    $raisingIssueLabel = __('BACKEND_GENERAL_REPORT', sibsConstants::SIBS_TRANSLATE);
    echo "<a href=\"#\" id=\"feedback-button\" class=\"btn btn-primary btn-large\">{$raisingIssueLabel}</a>";
    ?><br>
            </div>
            <hr>
            <h2><?php echo __('SIBS_BACKEND_SYSTEM_INFO', sibsConstants::SIBS_TRANSLATE) ?></h2>
            <?php echo sibsPayments::version_description(); ?>
            <hr>
            <h2><?php echo __('SIBS_BACKEND_SODIUM_DESCRYPTION', sibsConstants::SIBS_TRANSLATE) ?></h2>
            <?php echo sibsPayments::sodium_test_descryption(); ?>
        </div>
    </div>
<?php
}

?>