<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>

<div class="ehx-admin-overlay"></div>
<div class="ehx-admin-modal" id="ehx-modal">
    <div id="ehx_fields">
        <div class="ehx-admin-modal-head">
            <h3><?php esc_html_e('Add a New Field', 'ehx-members') ?></h3>
            <a href="javascript:void(0);" data-action="ehx_remove_modal" class="ehx-admin-modal-close">
                <i class="ehx-faicon-times"></i>
            </a>
        </div>

        <div class="ehx-admin-modal-body">
            <div style="grid-column: span 2 / span 2;">
                <h4><?php esc_html_e('Setup New Field', 'ehx-members') ?></h4>
                <div class="ehx-admin-btns">
                    <?php foreach ($buttons->custom as $newField): ?>
                        <?php
                            $data = array_diff_key((array) $newField, array_flip(['text','meta']));
                            $data = array_combine(array_map(fn($key) => '_' . $key, array_keys($data)), array_values($data));
                        ?>
                        <a href="javascript:void(0);" class="button" id="openEhxModal" data-element="#ehx-modal" data-action="ehxme_custom_field_modal" data-data='<?php echo wp_json_encode($data) ?>' data-nonce="<?php echo esc_html(wp_create_nonce('ehxme_custom_field_modal')) ?>"><?php echo esc_html($newField->text) ?></a>
                    <?php endforeach ?>
                </div>
            </div>
            <div style="grid-column: span 2 / span 2;">
                <h4><?php esc_html_e('Predefined Fields', 'ehx-members') ?></h4>
                <div class="ehx-admin-btns">
                    <?php foreach ($buttons->predefined as $newField): ?>
                        <a href="javascript:void(0);" class="button" id="predefinedFieldBtn" data-action="ehxme_render_input_field" data-content='<?php echo wp_json_encode($newField) ?>' data-nonce="<?php echo esc_html(wp_create_nonce('ehxme_render_input_field')) ?>"><?php echo esc_html($newField->_label) ?></a>
                    <?php endforeach ?>
                </div>
            </div>
        </div>
    </div>
</div>