<?php
if (!defined('ABSPATH')) {
    exit;
}

ob_start();
$brainx_key = 'brainx_crm_settings_for_form_' . $brainx_form_id;
$brainx_saved_data = get_option($brainx_key);

$brainx_req = isset($brainx_saved_data['req']) ? sanitize_text_field($brainx_saved_data['req']) : '';
$brainx_mapping = isset($brainx_saved_data['mapping']) ? $brainx_saved_data['mapping'] : '';
$brainx_accessToken = brainxGetApiToken();
$brainx_select_fields = array();
$brainx_matched_fields_array = array();
$brainx_key = 'brainx_ninja_entity_id_for_form_' . $brainx_form_id;
$brainx_entity_id = get_option($brainx_key);
$brainx_all_fields = brainx_get_ninja_form_fields($brainx_form_id);
$brainx_fields_array = array();
$brainx_all_fields_data = array();

foreach ($brainx_all_fields as $brainx_fl) {
    if ($brainx_fl->type == 'submit') {
        continue;
    }
    $brainx_all_fields_data[$brainx_fl->id] = $brainx_fl;
}

$brainx_fields = array();
if (!empty($brainx_accessToken)) {
    $brainx_fields = BrainxGetFields($brainx_accessToken, $brainx_entity_id);
}

$brainx_key2 = 'brainx_ninja_brainx_api_enabled_form_fields_' . $brainx_form_id;
$brainx_enabled_fields = get_option($brainx_key2);

if (!empty($brainx_fields)) {
    foreach ($brainx_fields as $brainx_field) {
        if ($brainx_field['type'] == 'select' || $brainx_field['type'] == 'PickList' || $brainx_field['type'] == 'MultiPickList') {
            $brainx_name = sanitize_text_field($brainx_field['name']);
            $brainx_label = sanitize_text_field($brainx_field['label']);

            if (!empty($brainx_enabled_fields)) {
                if (!in_array($brainx_name, $brainx_enabled_fields)) {
                    continue;
                }
            }

            $brainx_is_enabled_field = 0;

            if (!empty($brainx_mapping) && isset($brainx_mapping[$brainx_name])) {
                $brainx_matched_id = $brainx_mapping[$brainx_name];
                $brainx_is_enabled_field = 1;
            }

            if ($brainx_is_enabled_field == 1) {
                $brainx_select_fields[] = $brainx_field;

                if (isset($brainx_all_fields_data[$brainx_matched_id])) {
                    $brainx_matched_fields_array[] = isset($brainx_all_fields_data[$brainx_matched_id]) ? $brainx_all_fields_data[$brainx_matched_id] : '';
                }
            }
        }
    }
}

if (!empty($brainx_select_fields)) {
    $brainx_key_matched = '_brainx_ninja_form_api_select_matching_' . esc_attr($brainx_form_id);
    $brainx_saved_mapping = get_option($brainx_key_matched);
    ?>
    <div class="wrap">
        <h3><?php esc_html_e('Auswahlfelder', 'brainx-form-connector'); ?></h3>
        <form method="post" action="">
            <?php
            foreach ($brainx_select_fields as $brainx_key => $brainx_api_field) {
                if (isset($brainx_matched_fields_array[$brainx_key])) {
                    $brainx_matched_field_data = $brainx_matched_fields_array[$brainx_key];
                    $brainx_id = $brainx_matched_field_data->id;
                    $brainx_label = esc_html($brainx_matched_field_data->label);
                    $brainx_options = brainx_get_ninja_form_option_by_id($brainx_id);

                    if (!empty($brainx_options)) {
                        $brainx_options = unserialize($brainx_options);
                        $brainx_field_name = sanitize_text_field($brainx_api_field['name']);
                        $brainx_saved_values = isset($brainx_saved_mapping[$brainx_field_name]) ? $brainx_saved_mapping[$brainx_field_name] : '';
                        ?>
                        <div class="field_wrap_custom">
                            <h4 class="form_tab_div"><?php echo esc_html($brainx_api_field['label']); ?></h4>
                            <input type="hidden" name="select_map_fields_name[]" value="<?php echo esc_attr($brainx_api_field['name']); ?>"/>
                            <table class="form-table hide_show">
                                <thead>
                                <th><?php esc_html_e('brainX WERTE', 'brainx-form-connector'); ?></th>
                                <th><?php esc_html_e('NINJA FORMS WERTE', 'brainx-form-connector'); ?></th>
                                </thead>
                                <?php
                                foreach ($brainx_api_field['values'] as $brainx_kk => $brainx_oparr) {
                                    $brainx_opt_value = '';

                                    if (!empty($brainx_saved_values)) {
                                        $brainx_inn_array = isset($brainx_saved_values[$brainx_oparr['value']]) ? $brainx_saved_values[$brainx_oparr['value']] : '';
                                        $brainx_opt_value = isset($brainx_inn_array['form_field_value']) ? $brainx_inn_array['form_field_value'] : '';
                                    }
                                    ?>
                                    <tr>
                                        <td>
                                            <select name="api_options_<?php echo esc_attr($brainx_api_field['name']); ?>[]">
                                                <option value="<?php echo esc_attr($brainx_oparr['value']) ?>">
                                                    <?php echo esc_html($brainx_oparr['label']) ?>
                                                </option>
                                            </select>
                                        </td>
                                        <td class="optionsdata">
                                            <select name="form_options_<?php echo esc_attr($brainx_api_field['name']); ?>[]">
                                                <?php
                                                echo '<option value=""></option>';

                                                foreach ($brainx_options as $brainx_option) {
                                                    $brainx_label = esc_html($brainx_option['label']);
                                                    $brainx_value = esc_attr($brainx_option['value']);
                                                    $brainx_selected = '';

                                                    if ($brainx_opt_value == $brainx_value) {
                                                        $brainx_selected = 'selected=""';
                                                    }

                                                    echo '<option value="' . esc_attr($brainx_value) . '" ' . esc_attr($brainx_selected) . '>' . esc_html($brainx_label) . ' (' . esc_html($brainx_value) . ')</option>';
                                                }
                                                ?>
                                            </select>
                                        </td>
                                    </tr>
                                    <?php
                                }

                                wp_nonce_field('brainx_save_settings', 'brainx_nonce');
                                ?>
                                <tr class="save_action">
                                    <td>
                                        <input type="submit" name="ninja_single_field_mapping[]" value="<?php esc_attr_e('Anwenden', 'brainx-form-connector'); ?>" class="button button-primary" />
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <?php
                    }
                }
            }
            ?>
            <input type="hidden" name="form_id_send" value="<?php echo esc_attr($brainx_form_id); ?>" />
        </form>
    </div>
    <?php
} else {
    echo esc_html__('Keine Auswahlfelder vorhanden', 'brainx-form-connector');
}

$brainx_html = ob_get_clean();
$brainx_allowed_html = wp_kses_allowed_html('post');

// Add missing form-related tags + attributes used in your markup
$brainx_allowed_html['form'] = [
    'method' => true,
    'action' => true,
];

$brainx_allowed_html['input'] = [
    'type' => true,
    'name' => true,
    'value' => true,
    'class' => true,
];

$brainx_allowed_html['select'] = [
    'name' => true,
    'class' => true,
];

$brainx_allowed_html['option'] = [
    'value' => true,
    'selected' => true,
];

$brainx_allowed_html['table'] = [
    'class' => true,
];

$brainx_allowed_html['tr'] = [];
$brainx_allowed_html['td'] = [
    'class' => true,
];
$brainx_allowed_html['th'] = [];

$brainx_allowed_html['div'] = [
    'class' => true,
];
$brainx_allowed_html['h3'] = [];
$brainx_allowed_html['h4'] = [
    'class' => true,
];

$brainx_allowed_html['input'] = [
    'type' => true,
    'name' => true,
    'value' => true,
    'class' => true,
];

$brainx_allowed_html['input']['*'] = true; // optional: allow all attributes for inputs

echo wp_kses($brainx_html, $brainx_allowed_html);
