<?php

use Bdp\Modules\LimitLoginAttempts\Controllers\OptionsPage as LimitLoginAttemptsOptions;

function updateBlockOrAllowList(array $postVars)
{
    $listType = $postVars['save_kompass_balist_list_type'];
if (count($postVars['new_ips']) == 1) {
        foreach (explode(PHP_EOL, $postVars['new_ips'][0]) as $newIp) {
            $newIp = trim($newIp);
            if ('' !== $newIp) {
                if ($listType == 'blocklist') {
                    LimitLoginAttemptsOptions::addToBlocklist($newIp);
                } else {
                    LimitLoginAttemptsOptions::addToAllowlist($newIp);
                }
            }
        }
    }
}

function kompass_print_block_allow_form(string $listType) {
    $elements = get_option('kompass_limit_login_' . $listType, []);
     ?>

    <input type="hidden" name="save_kompass_balist_list_type" value="<?= $listType; ?>" />
    <p style="width: 100%; text-align: right">
        <input type="text" id="searchInput"
               onkeyup="searchTable('myTable', this)"
               placeholder="<?=__('Search for ip address', BDP_LV_PLUGIN_SLUG); ?>">
    </p>
    <table class="wp-list-table widefat fixed striped table-view-list" id="myTable">
        <thead>
        <tr>
            <th scope="col" class="manage-column column-name"><?= __('IP address', BDP_LV_PLUGIN_SLUG); ?></th>
            <th style="width: 100px;" class="manage-column column-name"><?= __('Actions', BDP_LV_PLUGIN_SLUG); ?></th>
        </tr>
        </thead>
        <tbody>
        <?php
            foreach ($elements as $currentIp) {
                echo '<tr>';
                echo '<td>' . $currentIp  .'</td>';
                echo '<td><a href="admin.php?page=bdp-kompass-limit-login-attempts&action=removeFromList' .
                    '&list=' . $listType . '&ip=' . base64_encode($currentIp) . '">'
                    . __('Delete', BDP_LV_PLUGIN_SLUG) . '</a></td>';
                echo '</tr>';
            }
        ?>
        </tbody>
    </table>


    </div>
    <div class="kompass_setting_box">
        <h3><?= __('IP-Adresse hinzufügen', BDP_LV_PLUGIN_SLUG); ?></h3>
        <p>
            <textarea
                    placeholder="<?= __('Please use line breaks to enter multiple ips', BDP_LV_PLUGIN_SLUG); ?>"
                    name="new_ips[]"
                    style="width: 350px;" rows="5"></textarea>
        </p>
    </div>
<?php
}