<?php
if (!defined('ABSPATH')) {
    exit;
}
$brainx_display = '';
if (isset($_POST['save_brainx_data'])) {
    if (isset($_POST['brainx_nonce'])) {
        $brainx_nonce = sanitize_text_field(wp_unslash($_POST['brainx_nonce']));
        if (!wp_verify_nonce($brainx_nonce, 'brainx_save_settings')) {
            wp_die(esc_html_e('Nonce verification failed', 'brainx-form-connector'));
        }
    }
    $brainx_display = 'shownow';
    // Sanitize and validate input values
    $brainx_server_url = isset($_POST['server_url']) ? esc_url_raw(wp_unslash($_POST['server_url'])) : '';
    $brainx_server_username = isset($_POST['server_username']) ? sanitize_text_field(wp_unslash($_POST['server_username'])) : '';
    $brainx_server_pass = isset($_POST['server_password']) ? sanitize_text_field(wp_unslash($_POST['server_password'])) : '';
    $brainx_two_factor = isset($_POST['brainx_two_factor']) ? sanitize_text_field(wp_unslash($_POST['brainx_two_factor'])) : '';
    $brainxapp_pass = isset($_POST['brainxapp_pass']) ? sanitize_text_field(wp_unslash($_POST['brainxapp_pass'])) : '';
    $brainx_arr = array(
        'server_url' => $brainx_server_url,
        'entity_id' => 7,
        'server_username' => $brainx_server_username,
        'server_password' => $brainx_server_pass,
        'brainx_two_factor' => $brainx_two_factor,
        'brainxapp_pass' => $brainxapp_pass
    );
    update_option('_brainx_api_creds_settings', $brainx_arr);
}

$brainx_settings = get_option('_brainx_api_creds_settings');

$brainx_two_factor = isset($brainx_settings['brainx_two_factor']) ? $brainx_settings['brainx_two_factor'] : '';
$brainx_dis = 'display:none;';
$brainx_checked = "";
if ($brainx_two_factor == 'on') {
    $brainx_dis = '';
    $brainx_checked = 'checked=""';
}
?>
<div class="brainx_success-toast <?php echo esc_attr($brainx_display); ?>" id="brainx_success_toast" role="status" aria-live="polite" aria-atomic="true" hidden>
    <div class="brainx_success-inner">

        <div class="success_message show">
            <img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSI1MTIiIGhlaWdodD0iNTEyIiB4PSIwIiB5PSIwIiB2aWV3Qm94PSIwIDAgNTEyIDUxMiIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNTEyIDUxMiIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgY2xhc3M9IiI+PGc+PHBhdGggZmlsbD0iIzFlNTYxZiIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMjU2IDBDMTE0LjggMCAwIDExNC44IDAgMjU2czExNC44IDI1NiAyNTYgMjU2IDI1Ni0xMTQuOCAyNTYtMjU2UzM5Ny4yIDAgMjU2IDB6IiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIG9wYWNpdHk9IjEiIGRhdGEtb3JpZ2luYWw9IiM0YmFlNGYiIGNsYXNzPSIiPjwvcGF0aD48cGF0aCBmaWxsPSIjZmZmZmZmIiBkPSJNMjA2LjcgMzczLjFjLTMyLjctMzIuNy02NS4yLTY1LjctOTgtOTguNC0zLjYtMy42LTMuNi05LjYgMC0xMy4ybDM3LjctMzcuN2MzLjYtMy42IDkuNi0zLjYgMTMuMiAwbDUzLjkgNTMuOUwzNTIuMSAxMzljMy43LTMuNiA5LjYtMy42IDEzLjMgMGwzNy44IDM3LjhjMy43IDMuNyAzLjcgOS42IDAgMTMuMkwyMTkuOSAzNzMuMWMtMy42IDMuNy05LjUgMy43LTEzLjIgMHoiIG9wYWNpdHk9IjEiIGRhdGEtb3JpZ2luYWw9IiNmZmZmZmYiPjwvcGF0aD48L2c+PC9zdmc+" /> <?php esc_html_e('Änderungen wurden erfolgreich gespeichert!', 'brainx-form-connector'); ?>
        </div>
    </div>
</div>
<div class="wrap mywrap">
    <!--
        <div class="button button-primary testconnectionbrainx"><?php esc_html_e('Testverbindung', 'brainx-form-connector'); ?></div>
        <div class="api_success">
    <?php esc_html_e('Success: API connected', 'brainx-form-connector'); ?>
        </div>
        <div class="api_error"></div>
    -->
    <form method="post" action="">
        <?php wp_nonce_field('brainx_save_settings', 'brainx_nonce'); ?>
        <table class="form-table" style="margin-bottom:40px">
            <tr>
                <th><?php esc_html_e('brainX CRM Url', 'brainx-form-connector'); ?></th>
                <td><input type="url" name="server_url" value="<?php echo isset($brainx_settings['server_url']) ? esc_url($brainx_settings['server_url']) : ''; ?>" /></td>
            </tr>
            <tr style="display:none;">
                <th><?php esc_html_e('Global Entity Id', 'brainx-form-connector'); ?></th>
                <td><input type="number" name="entity_id" value="7" /></td>
            </tr>
            <tr>
                <th><?php esc_html_e('Benutzername', 'brainx-form-connector'); ?></th>
                <td><input type="text" name="server_username" value="<?php echo isset($brainx_settings['server_username']) ? esc_attr($brainx_settings['server_username']) : ''; ?>" autocomplete="off" /></td>
            </tr>

            <tr>
                <th><?php esc_html_e('Passwort', 'brainx-form-connector'); ?></th>
                <td><input type="password" name="server_password" value="<?php echo isset($brainx_settings['server_password']) ? esc_attr($brainx_settings['server_password']) : ''; ?>" autocomplete="off" /></td>
            </tr>
            <tr>
                <th><?php esc_html_e('2FA aktivieren', 'brainx-form-connector'); ?></th>
                <td>
                    <label class="switch" for="brainx_two_factor">
                        <input style="width: 15px;" <?php echo esc_attr($brainx_checked); ?> type="checkbox" name="brainx_two_factor" id="brainx_two_factor"/>
                        <div class="slider round"></div>
                    </label>
                </td>
            </tr>
            <tr class="twofa" style="<?php echo esc_attr($brainx_dis); ?>">
                <th><?php esc_html_e('App-Passwort', 'brainx-form-connector'); ?></th>
                <td><input type="password" name="brainxapp_pass" id="brainxapp_pass" value="<?php echo isset($brainx_settings['brainxapp_pass']) ? esc_attr($brainx_settings['brainxapp_pass']) : ''; ?>"/>
                    <span style="display:block;margin-top:5px;font-size:12px"><?php esc_html_e('Wenn die 2-Faktor-Authentifizierung aktiv ist, bitte App-Passwort einfügen.', 'brainx-form-connector'); ?></span>
                </td>
            </tr>
            <tr>
                <th></th>
                <td>    <div class="button button-primary testconnectionbrainx"><?php esc_html_e('Verbindung testen', 'brainx-form-connector'); ?></div>
                    <div class="api_success">
                        <?php esc_html_e('Erfolgreich: API verbunden', 'brainx-form-connector'); ?>
                    </div>
                    <div class="api_error"></div></td>
            </tr>

        </table>
      <hr/>
        <input style="margin-top:20px" type="submit" name="save_brainx_data" class="button button-primary" value="<?php esc_attr_e('Änderungen speichern', 'brainx-form-connector'); ?>" />
    </form>
</div>

</div>