<?php
  if (!defined("\x41\x42\x53\x50\x41\x54\x48")) exit; function unipixel_page_event_logs() { global $wpdb; $log_table = $wpdb->prefix . "\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x65\x76\x65\x6E\x74\x5F\x6C\x6F\x67";    $platform_filter = ''; if (isset($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x70\x6C\x61\x74\x66\x6F\x72\x6D"]) && is_string($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x70\x6C\x61\x74\x66\x6F\x72\x6D"])) { $platform_filter = sanitize_text_field(wp_unslash($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x70\x6C\x61\x74\x66\x6F\x72\x6D"])); } $event_filter = ''; if (isset($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x65\x76\x65\x6E\x74"]) && is_string($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x65\x76\x65\x6E\x74"])) { $event_filter = sanitize_text_field(wp_unslash($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x65\x76\x65\x6E\x74"])); }  $method_filter = ''; if (isset($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x6D\x65\x74\x68\x6F\x64"]) && is_string($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x6D\x65\x74\x68\x6F\x64"])) { $method_filter = sanitize_text_field(wp_unslash($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x6D\x65\x74\x68\x6F\x64"])); } $party_filter = ''; if (isset($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x70\x61\x72\x74\x79"]) && is_string($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x70\x61\x72\x74\x79"])) { $party_filter = sanitize_text_field(wp_unslash($_GET["\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x70\x61\x72\x74\x79"])); }    $platform_query = $wpdb->prepare(  "SELECT DISTINCT platform_name FROM %i ORDER BY platform_name ASC", $log_table );  $platform_rows = $wpdb->get_col($platform_query); if (!is_array($platform_rows)) { $platform_rows = []; }    $where_sql = "WHERE 1=1"; $where_args = []; if ($platform_filter !== "") { $where_sql .= "\x20\x41\x4E\x44\x20\x70\x6C\x61\x74\x66\x6F\x72\x6D\x5F\x6E\x61\x6D\x65\x20\x3D\x20\x25\x73"; $where_args[] = $platform_filter; } if ($event_filter !== "") {  $where_sql .= "\x20\x41\x4E\x44\x20\x65\x76\x65\x6E\x74\x5F\x6E\x61\x6D\x65\x20\x4C\x49\x4B\x45\x20\x25\x73"; $where_args[] = '%' . $wpdb->esc_like($event_filter) . "\x25"; } if ($method_filter !== "") { $where_sql .= "\x20\x41\x4E\x44\x20\x6D\x65\x74\x68\x6F\x64\x20\x3D\x20\x25\x73"; $where_args[] = $method_filter; } if ($party_filter !== "") { $where_sql .= "\x20\x41\x4E\x44\x20\x70\x61\x72\x74\x79\x20\x3D\x20\x25\x73"; $where_args[] = $party_filter; }    $base_sql =  "SELECT * FROM %i {$where_sql} ORDER BY log_time DESC LIMIT %d";  $prepare_args = []; $prepare_args[] = $log_table; foreach ($where_args as $arg) { $prepare_args[] = $arg; } $prepare_args[] = 200;   $query = call_user_func_array([$wpdb, "\x70\x72\x65\x70\x61\x72\x65"], array_merge([$base_sql], $prepare_args));  $logs = $wpdb->get_results($query, ARRAY_A); if (!is_array($logs)) { $logs = []; }    $timezone_string = get_option('timezone_string'); if (empty($timezone_string)) { $gmt_offset = get_option('gmt_offset'); $timezone_string = $gmt_offset ? timezone_name_from_abbr("", $gmt_offset * 3600, 0) : "\x55\x54\x43"; }  $action_url = menu_page_url("\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x65\x76\x65\x6E\x74\x5F\x6C\x6F\x67\x73", false); ?>
    <div class="UniPixelShell pt-4">
        <div class="d-flex justify-content-between align-items-start">
            <h1 class="mb-3"><h2><i class="fa-solid fa-database"></i> Stored Event Logs</h2></h1>
            <?php unipixel_render_feedback_buttons(); ?>
        </div>

        <p class="text-muted mb-4">
            These logs show all events that UniPixel has stored in your database after being sent to connected platforms like Meta and Google.
            Events are stored when database storage is enabled in your
            <a href="<?php echo esc_url(menu_page_url("\x75\x6E\x69\x70\x69\x78\x65\x6C\x5F\x67\x65\x6E\x65\x72\x61\x6C\x5F\x73\x65\x74\x74\x69\x6E\x67\x73", false)); ?>">General Settings</a>.
        </p>

        <!-- Filters (server-side) -->
        <form method="get" action="<?php echo esc_url($action_url); ?>" class="mb-3">
            <!-- Preserve the admin page slug -->
            <input type="hidden" name="page" value="unipixel_event_logs" />

            <div class="row g-2 align-items-end">
                <div class="col-12 col-md-3">
                    <label class="form-label mb-1" for="unipixel_platform">Platform</label>
                    <select class="form-select form-select-sm" id="unipixel_platform" name="unipixel_platform">
                        <option value="">All platforms</option>
                        <?php foreach ($platform_rows as $pname) : ?>
                            <option value="<?php echo esc_attr($pname); ?>" <?php selected($platform_filter, $pname); ?>>
                                <?php echo esc_html($pname); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-12 col-md-3">
                    <label class="form-label mb-1" for="unipixel_event">Event name contains</label>
                    <input
                        type="text"
                        class="form-control form-control-sm"
                        id="unipixel_event"
                        name="unipixel_event"
                        value="<?php echo esc_attr($event_filter); ?>"
                        placeholder="e.g. add_to_cart"
                    />
                </div>

                <div class="col-6 col-md-2">
                    <label class="form-label mb-1" for="unipixel_method">Method</label>
                    <select class="form-select form-select-sm" id="unipixel_method" name="unipixel_method">
                        <option value="">All</option>
                        <option value="client" <?php selected($method_filter, "\x63\x6C\x69\x65\x6E\x74"); ?>>client</option>
                        <option value="server" <?php selected($method_filter, "\x73\x65\x72\x76\x65\x72"); ?>>server</option>
                    </select>
                </div>

                <div class="col-6 col-md-2">
                    <label class="form-label mb-1" for="unipixel_party">Party</label>
                    <select class="form-select form-select-sm" id="unipixel_party" name="unipixel_party">
                        <option value="">All</option>
                        <option value="first" <?php selected($party_filter, "\x66\x69\x72\x73\x74"); ?>>first</option>
                        <option value="third" <?php selected($party_filter, "\x74\x68\x69\x72\x64"); ?>>third</option>
                    </select>
                </div>

                <div class="col-12 col-md-2 d-flex gap-2">
                    <button type="submit" class="btn btn-sm btn-primary w-100">Filter</button>
                    <a class="btn btn-sm btn-outline-secondary w-100" href="<?php echo esc_url($action_url); ?>">Clear</a>
                </div>
            </div>

            <p class="small text-muted mt-2 mb-0">
                Showing up to the 200 most recent logs matching your filters.
            </p>
        </form>

        <?php if (empty($logs)) : ?>
            <div class="alert alert-info">No stored events found for the current filters.</div>
        <?php else : ?>
            <div class="table-responsive">
                <table class="table table-sm table-striped align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>Log Time</th>
                            <th>Event Name</th>
                            <th>Platform</th>
                            <th>Send Method</th>
                            <th>Party</th>
                            <th>Event Trigger</th>
                            <th>Response</th>
                            <th>Sent Data</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($logs as $log) : ?>
                            <?php
 $utc_time = new DateTime($log["\x6C\x6F\x67\x5F\x74\x69\x6D\x65"], new DateTimeZone("\x55\x54\x43")); $local_time = $utc_time->setTimezone(new DateTimeZone($timezone_string)); $sent_data = isset($log["\x73\x65\x6E\x74\x5F\x64\x61\x74\x61"]) ? (string) $log["\x73\x65\x6E\x74\x5F\x64\x61\x74\x61"] : ""; $pretty_sent_data = str_replace(['\"', '\\'], "\x22", $sent_data); ?>
                            <tr>
                                <td><?php echo esc_html($local_time->format("\x59\x2D\x6D\x2D\x64\x20\x48\x3A\x69\x3A\x73")); ?></td>
                                <td><?php echo esc_html($log["\x65\x76\x65\x6E\x74\x5F\x6E\x61\x6D\x65"]); ?></td>
                                <td><?php echo esc_html($log["\x70\x6C\x61\x74\x66\x6F\x72\x6D\x5F\x6E\x61\x6D\x65"]); ?></td>
                                <td><?php echo esc_html($log["\x6D\x65\x74\x68\x6F\x64"]); ?></td>
                                <td><?php echo esc_html($log["\x70\x61\x72\x74\x79"]); ?></td>
                                <td><?php echo esc_html($log["\x65\x76\x65\x6E\x74\x5F\x74\x72\x69\x67\x67\x65\x72"]); ?></td>
                                <td><?php echo esc_html($log["\x72\x65\x73\x70\x6F\x6E\x73\x65\x5F\x6D\x65\x73\x73\x61\x67\x65"]); ?></td>
                                <td>
                                    <i class="fas fa-info-circle text-secondary" role="button"
                                       data-bs-toggle="popover"
                                       data-bs-title="Sent Data"
                                       data-bs-html="true"
                                       data-bs-content="<?php echo esc_attr('<pre style=\'white-space: pre-wrap; word-break: break-all;\'>' . htmlspecialchars($pretty_sent_data, ENT_QUOTES, "\x55\x54\x46\x2D\x38") . '</pre>'); ?>"></i>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
            popoverTriggerList.map(function (el) {
                return new bootstrap.Popover(el, { html: true });
            });
        });
    </script>
<?php
} 