<?php
if (!defined('ABSPATH')) {
    exit;
}
if (class_exists('FrmForm')) {
    // Retrieve all 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['formidable_connect']) ? intval($_GET['formidable_connect']) : '';
    $brainx_inside = 0;
    if (!empty($brainx_form_id)) {
        $brainx_fields = FrmField::get_all_for_form($brainx_form_id);
        if (!empty($brainx_fields)) {
            $brainx_inside = 1;
            $brainx_all_fields_data = array();
            $brainx_fields_array = array();
            foreach ($brainx_fields as $brainx_field) {
                if ($brainx_field->type == 'submit') {
                    continue;
                }
                $brainx_id = $brainx_field->id;
                $brainx_name = $brainx_field->name;
                $brainx_fields_array[$brainx_id] = $brainx_name;
                $brainx_all_fields_data[$brainx_id] = $brainx_field;
            }
            $brainx_form_name = FrmForm::getName($brainx_form_id);
            ?>
            <div class="card-wrapper">
<div class="card-head">
                <h1 class="form_title"><?php echo esc_html($brainx_form_name); ?></h1>
                <p><?php esc_html_e('Hier kannst du das Field Mapping von deinem Formidable Formular zum brainX vornehmen:', 'brainx-form-connector'); ?></p>
            </div> <div class="card-wrap-inner">
            <?php
            include 'fields/manage-formidable-form-dragable.php';
        }
    }
    if ($brainx_inside == 0) {
        $brainx_admin_url = admin_url();
        $brainx_forms = FrmForm::getAll();
        echo '<div class="wrap form_wrap" style="margin-top:0">';
        echo '<table class="forms_table formidable">';
        echo '<thead>';
        echo '<tr>';
        echo '<th style="width:40%">' . esc_html__('Formular', 'brainx-form-connector') . '</th>';
        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) {
            if ($brainx_form->status != 'published') {
                continue;
            }
            $brainx_key = 'brainx_crm_formidable_form_status_' . $brainx_form->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_formidable&formidable_connect=' . $brainx_form->id); ?>"><?php echo esc_html($brainx_form->name); ?></a>
                </td>

                <td>
                    <?php
                    $brainx_key = 'brainx_formindable_form_last_updated_data_' . $brainx_form->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 = $brainx_user_info->display_name;
                        $brainx_formatted_date = '';
                        $brainx_datetime = $brainx_dd['datetime'];
                        if (!empty($brainx_datetime)) {
                            $brainx_formatted_date = wp_date('d.m.Y \u\m H:i', strtotime($brainx_datetime)) . ' Uhr';
                        }
                        ?>
                        <span><?php echo esc_html($brainx_uname); ?></span>
                    </td>
                    <td><span><?php echo esc_html($brainx_formatted_date); ?></span></td>
                    <?php
                } else {
                    echo '<td></td>';
                }
                ?>
<td class="status-cell">
    <div class="bx-switch" data-form-id="<?php echo esc_attr($brainx_form->id); ?>">

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

        <label for="form-<?php echo esc_attr($brainx_form->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>';
    }
}
?>
