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

ob_start();
$brainx_fields = FrmField::get_all_for_form($brainx_form_id);
$brainx_all_fields_data = array();
$brainx_fields_array = array();
$brainx_key = 'brainx_crm_settings_for_form_formidable_' . $brainx_form_id;
$brainx_saved_data = get_option($brainx_key);
$brainx_select_fields = array();
$brainx_matched_fields_array = array();
$brainx_status = isset($brainx_saved_data['status']) ? wp_unslash($brainx_saved_data['status']) : '';
$brainx_mapping = isset($brainx_saved_data['mapping']) ? wp_unslash($brainx_saved_data['mapping']) : '';
foreach ($brainx_fields as $brainx_field) {
    if ($brainx_field->type == 'select' || $brainx_field->type == 'radio' || $brainx_field->type == 'checkbox') {
        $brainx_id = $brainx_field->id;
        $brainx_name = $brainx_field->name;
        $brainx_fields_array[$brainx_id] = sanitize_text_field($brainx_name);
        $brainx_all_fields_data[$brainx_id] = $brainx_field;
    }
}
//print_r($brainx_all_fields_data);
$brainx_key = 'brainx_frm_entity_id_for_form_' . $brainx_form_id;
$brainx_entity_id = get_option($brainx_key);
$brainx_accessToken = brainxGetApiToken();
$brainx_fields = array();
if (!empty($brainx_accessToken)) {
    $brainx_fields = BrainxGetFields($brainx_accessToken, $brainx_entity_id);
}
$brainx_key2 = 'brainx_frm_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 = sanitize_text_field($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)) {
    ?>

    <div class="wrap">
        <h3><?php esc_html_e('Auswahlfelder', 'brainx-form-connector'); ?></h3>
        <form method="post" action="">
            <?php
            $brainx_key_matched = 'brainx_formidable_form_api_select_matching_' . $brainx_form_id;
            $brainx_saved_mapping = get_option($brainx_key_matched);
            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 = sanitize_text_field($brainx_matched_field_data->id);
                    $brainx_options = isset($brainx_matched_field_data->options) ? $brainx_matched_field_data->options : [];
                    if (!empty($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('FORMIDABLE WERTE', 'brainx-form-connector'); ?></th>
                                </thead>
                                <tbody>
                                    <?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']) ? sanitize_text_field($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>

                                                <select name="form_options_<?php echo esc_attr($brainx_api_field['name']); ?>[]">
                                                    <?php
                                                    echo '<option value="">--' . esc_html__('select', 'brainx-form-connector') . '--</option>';
                                                    if ($brainx_matched_field_data->type === 'select') {
                                                        $brainx_field = (array) $brainx_matched_field_data;
                                                        foreach ($brainx_field['options'] as $brainx_opt_key => $brainx_opt) {
                                                            $brainx_field_val = FrmFieldsHelper::get_value_from_array($brainx_opt, $brainx_opt_key, $brainx_field);
                                                            $brainx_opt = FrmFieldsHelper::get_label_from_array($brainx_opt, $brainx_opt_key, $brainx_field);
                                                            $brainx_selected = FrmAppHelper::check_selected($brainx_opt_value, $brainx_field_val);
                                                            if ($brainx_other_opt === false) {
                                                                $brainx_other_args = FrmFieldsHelper::prepare_other_input(compact('field', 'field_name', 'opt_key'), $brainx_other_opt, $brainx_selected);
                                                                if (FrmFieldsHelper::is_other_opt($brainx_opt_key) && $brainx_selected) {
                                                                    $brainx_other_checked = true;
                                                                }
                                                            }

                                                            if ($placeholder && $brainx_opt == '' && !$brainx_skipped) {
                                                                $brainx_skipped = true;
                                                                continue;
                                                            }

                                                            $brainx_option_params = array(
                                                                'value' => $brainx_field_val,
                                                            );

                                                            if (FrmFieldsHelper::is_other_opt($brainx_opt_key)) {
                                                                $brainx_option_params['class'] = 'frm_other_trigger';
                                                            }

                                                            FrmHtmlHelper::echo_dropdown_option($brainx_opt, (bool) $brainx_selected, $brainx_option_params);
                                                            unset($brainx_option_params);
                                                        }
                                                    } else {
                                                        foreach ($brainx_options as $brainx_option) {
                                                            $brainx_label = esc_html($brainx_option['label']);
                                                            if (empty($brainx_label)) {
                                                                echo '<option value=""></option>';
                                                                continue;
                                                            }
                                                            $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_attr($brainx_value) . ')</option>';
                                                        }
                                                    }
                                                    ?>
                                                </select></td>
                                            </td>    
                                        </tr>
                                        <?php
                                    }
                                    wp_nonce_field('brainx_save_settings', 'brainx_nonce');
                                    ?>
                                    <tr class="save_action">
                                        <td>
                                            <input type="submit" name="formidable_single_field_mapping[]" value="<?php esc_attr_e('Anwenden', 'brainx-form-connector'); ?>" class="button button-primary" />
                                        </td>
                                    </tr>
                                </tbody>
                            </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);
?>
