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

<?php
    use EHxMe\Helpers\Helper;
    use EHxMe\PostTypes\FormPostType;

    $isUpdate = !empty($validated) && isset($validated['key']) ? true : false;
    $_type = $validated['_type'] ?? 'text';
?>

<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 $isUpdate ? esc_html_e('Edit Field', 'ehx-members') : 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>

        <form id="ehxme_render_input_field" action="#" method="post">
            <input type="hidden" name="action" value="ehxme_render_input_field">
            <input type="hidden" name="<?php echo esc_html(Helper::NONCE_NAME) ?>" value="<?php echo esc_html(wp_create_nonce('ehxme_render_input_field')) ?>">

            <input type="hidden" name="_type" value="<?php echo esc_html($_type); ?>">

            <?php if($isUpdate): ?>
                <input type="hidden" name="edit_key" value="<?php echo esc_html($validated['key']); ?>">
            <?php endif ?>

            <div class="ehx-admin-modal-body ehx-admin-metabox">

                <?php FormPostType::ehxCustomFields($fields, $validated) ?>
                
            </div>
            <div class="clear"></div>

            <div class="ehx-admin-modal-foot">
                <input type="submit" value="<?php echo $isUpdate ? esc_html__('Update', 'ehx-members') : esc_html__('Add', 'ehx-members') ?>" class="button-primary">
                <a href="javascript:void(0);" data-modal="ehx_fields" data-action="ehx_remove_modal" class="button"><?php esc_html_e('Cancel', 'ehx-members') ?></a>
            </div>
        </form>

    </div>
</div>