<?php
if (!defined('ABSPATH')) {
    exit;
}
// Check if Ninja Forms is active
if (class_exists('Ninja_Forms')) {
    if (
            isset($_GET['brainx-form-nonceverification']) &&
            !wp_verify_nonce(
                    sanitize_text_field(wp_unslash($_GET['brainx-form-nonceverification'])),
                    'brainx-form-nonceverification-action'
            )
    ) {
        wp_die();
    }
    $brainx_form_id = isset($_GET['ninja_connect']) ? sanitize_text_field(wp_unslash($_GET['ninja_connect'])) : '';
    $brainx_inside = 0;
    if (!empty($brainx_form_id)) {
        $brainx_forms = Ninja_Forms()->form()->get_forms();
        foreach ($brainx_forms as $brainx_form) {
            if ($brainx_form->get_id() == $brainx_form_id) {
                $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_fields_array[$brainx_fl->id] = esc_html($brainx_fl->label); // Escape field labels
                    $brainx_all_fields_data[$brainx_fl->id] = $brainx_fl;
                }
                ?>


        
                
<div class="card-wrapper">
<div class="card-head">
                    <h1 class="form_title"><?php echo esc_html('Ninja Forms: ' . $brainx_form->get_setting('title')); ?></h1> <!-- Escape form title -->
                    <p style="margin:0"><?php esc_html_e('Hier kannst du das Field Mapping von deinem Ninja Forms Formular zum brainX vornehmen:', 'brainx-form-connector'); ?></p>
                </div> <div class="card-wrap-inner">
    
                <?php
                include 'fields/manage-ninja-form-dragable.php';
                $brainx_inside = 1;
                break;
            }
        }
    }
    if ($brainx_inside == 0) {
        $brainx_admin_url = esc_url(admin_url()); // Sanitize admin URL
        $brainx_forms = Ninja_Forms()->form()->get_forms();
        echo '<div class="wrap form_wrap" style="margin-top:0">';
        echo '<table class="forms_table ninjaforms">';
        echo '<thead>';
        echo '<tr>';
        echo ' <th style="width:40%">' . esc_html__('Formular', 'brainx-form-connector') . '</th>'; // Escape column headers
        echo '<th style="width:20%">' . esc_html__('Bearbeitet von:', 'brainx-form-connector') . '</th>';
        echo '<th style="width:20%">' . esc_html__('Letzte Änderung:', 'brainx-form-connector') . '</th>';
        echo '<th style="width:20%">' . esc_html__('Status', 'brainx-form-connector') . '</th>';
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
        foreach ($brainx_forms as $brainx_form) {
            $brainx_key = 'brainx_crm_ninja_form_status_' . $brainx_form->get_id();
            $brainx_enabled = get_option($brainx_key);
            if (empty($brainx_enabled) || $brainx_enabled == 'Disable') {
                $brainx_enabled = 'Disable';
            }
            ?>

            <tr>
                <td><a href="<?php echo esc_url($brainx_admin_url . 'admin.php?page=brainx_crm_ninja_forms&ninja_connect=' . $brainx_form->get_id()); ?>"><?php echo esc_html($brainx_form->get_setting('title')); ?></a></td> <!-- Escape form title link -->
                <td><?php
                    $brainx_key = 'brainx_ninja_form_last_updated_data_' . $brainx_form->get_id();
                    $brainx_dd = get_option($brainx_key);
                    if (!empty($brainx_dd)) {
                        $brainx_user_id = $brainx_dd['user'];
                        $brainx_user_info = get_user_by('id', $brainx_user_id);
                        $brainx_uname = esc_html($brainx_user_info->display_name); // Escape user display name
                        $brainx_formatted_date = '';
                        $brainx_datetime = $brainx_dd['datetime'];
                        if (!empty($brainx_datetime)) {
                            $brainx_date = new DateTime($brainx_datetime);

                            // Hole die WordPress-Zeitzone
                            $brainx_timezone_string = get_option('timezone_string');
                            if (!empty($brainx_timezone_string)) {
                                $brainx_timezone = new DateTimeZone($brainx_timezone_string);
                                $brainx_date->setTimezone($brainx_timezone);
                            }

                            // Formatieren des Datums
                            $brainx_formatted_date = $brainx_date->format('d.m.Y \u\m H:i') . ' Uhr';
                        }
                        ?>
                        <span><?php echo esc_attr($brainx_uname); ?></span>
                    </td>
                    <td><span><?php echo esc_html($brainx_formatted_date); ?></span></td> <!-- Escape formatted date -->
                <?php } else { ?>
                    <td></td>
                <?php } ?>
<td class="status-cell">
    <div class="bx-switch" data-form-id="<?php echo esc_attr($brainx_form->get_id()); ?>">

        <input
            type="checkbox"
            data-form="ninja"
            class="ed_field bx-switch-input"
            id="form-<?php echo esc_attr($brainx_form->get_id()); ?>"
            name="switch-<?php echo esc_attr($brainx_form->get_id()); ?>"
            value="Enable"
            <?php checked($brainx_enabled, 'Enable'); ?>
        />

        <label for="form-<?php echo esc_attr($brainx_form->get_id()); ?>" class="bx-switch-label">
            <span class="bx-switch-track"></span>
            <span class="bx-switch-thumb"></span>
        </label>

        <span class="bx-switch-status">
            <?php echo ($brainx_enabled === 'Enable')
                ? esc_html__('Aktiv', 'brainx-form-connector')
                : esc_html__('Inaktiv', 'brainx-form-connector'); ?>
        </span>

    </div>
</td>

            </tr>

            <?php
        }
        echo '</tbody>';
        echo '</table>';
    }
} else {
    echo esc_html__('Ninja Forms is not active.', 'brainx-form-connector'); // Escape output message
}
