<?php
/**
 * Entries Admin Page Template
 *
 * @package Spin_Wheel
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Variables in this template are scoped to the including render method (not truly
// global); the plugin's established "swp" prefix is part of its data schema and hook API.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals

// Calculate statistics if wheel is selected - Based on FULL database (unfiltered), not filtered
$stats = [];
$prize_counts = [];
if ($wheel_id && !empty($all_entries_unfiltered)) {
    $total = count($all_entries_unfiltered);
    $winners = array_filter($all_entries_unfiltered, function ($e) {
        return $e->is_winner == 1;
    });
    $selected_winners = array_filter($all_entries_unfiltered, function ($e) {
        return $e->is_selected_winner == 1;
    });
    $participated = $total - count($winners);

    $stats = [
        'total' => $total,
        'winners' => count($winners),
        'selected_winners' => count($selected_winners),
        'participated' => $participated,
        'conversion_rate' => $total > 0 ? round((count($winners) / $total) * 100, 1) : 0,
    ];

    // Prize distribution - Based on ALL entries (unfiltered)
    foreach ($all_entries_unfiltered as $entry) {
        $prize = $entry->prize_label ?: 'No Prize';
        if (!isset($prize_counts[$prize])) {
            $prize_counts[$prize] = 0;
        }
        $prize_counts[$prize]++;
    }
}
?>
<div class="swp-entries-page">
    <div>
        <div class="swp-entries-header-wrap">
            <span class="swp-entries-head-icon">
                <svg xmlns="http://www.w3.org/2000/svg" id="a" viewBox="0 0 255.89 256.11"
                    style="font-family: element-pack, Bangla951, sans-serif;">
                    <path
                        d="m40.5,102.42c.57.43,1.43.72,2.29,1.15,14.47,6.02,28.79,11.89,43.11,17.9.43.14.86.29,1.15.43,1,.29,1.29.86,1.15,1.86-.57,3.01-.57,6.02-.29,9.02.14.86.29,1.86.29,2.72.29,1.43.29,1.58-1.15,2.01-6.3,2.72-12.6,5.3-19.05,8.02-19.77,8.16-39.39,16.33-59.01,24.49-.57.14-1.15.57-1.72.86-.72-.29-.72-1-1-1.58-2.72-8.31-4.58-16.76-5.44-25.49-.72-7.02-1-13.75-.72-20.77.57-11.17,2.58-22.06,6.02-32.8,0-.29.29-.57.29-.86.14-.43.43-.72,1-.43.43.14.72.29,1.15.43,9.74,4.01,19.34,8.02,29.08,12.03.57.29,1.15.57,1.86.57h0c.43.29.72.43,1,.43h0Z"
                        style="fill:#fff; stroke-width:0px;"></path>
                    <path
                        d="m167.11,12.19c-2.58,6.02-5.16,12.17-7.59,18.19-8.45,20.34-16.9,40.96-25.35,61.3-.72,1.72-.72,1.86-2.58,1.58-3.72-.43-7.45-.43-11.17,0q-1.72.29-2.43-1.58c-6.3-15.47-12.75-31.08-19.19-46.55-.29-.86-.57-1.72-1.15-2.43,0-.29-.14-.57-.29-.86h0c0-.57-.29-1.15-.57-1.72-3.72-8.88-7.3-17.62-11.03-26.5q-.86-2.01,1.15-2.58c9.31-3.01,18.76-5.01,28.5-5.73,16.9-1.43,33.52.43,49.7,5.73.72.29,1.58.29,2.01,1.15h0Z"
                        style="fill:#fff; stroke-width:0px;"></path>
                    <path
                        d="m71.58,17.49c1.43,3.58,2.86,7.16,4.44,10.89,9.17,22.49,18.48,44.69,27.64,67.17.57,1.43.29,2.58-1,3.58-2.72,2.15-5.3,4.58-7.45,7.45-1.15,1.58-.86,1.58-2.72.72-19.05-7.88-38.24-15.76-57.29-23.63-6.73-2.72-13.18-5.44-19.91-8.31-.43-.14-.86-.29-1.15-.43v.14c-.14-.29-.43-.29-.72-.43-.72-.57-.14-1.29.14-1.86,1.86-3.72,4.01-7.45,6.16-10.89,12.17-18.91,28.22-33.66,47.98-44.26.43-.29,1-.43,1.58-.72.86-.57,1.58-.29,2.29.57h0Z"
                        style="fill:#ff6706; stroke-width:0px;"></path>
                    <path
                        d="m18.16,182.63c.72,0,1.15-.29,1.72-.57,24.06-9.88,47.98-19.91,71.9-29.93,1.58-.57,2.72-.29,3.72.86,2.15,2.72,4.58,5.3,7.45,7.45,1.58,1.15,1.58.86.72,2.72-6.73,16.04-13.32,32.37-19.91,48.41-4.01,9.88-8.16,19.91-12.32,29.79-1,2.15-.57,2.01-2.72,1-7.45-3.87-14.61-8.45-21.2-13.75-.43-.43-1-1-1.72-1.15l-.14.14.14.14c.14-.29-.14-.43-.43-.43h0c0-.29-.14-.43-.43-.43h0c0-.29-.14-.29-.43-.43h0c0-.29-.14-.29-.43-.43l.14.14c-.14-.14-.29-.29-.43-.43-1.86-1.72-3.87-3.58-5.73-5.44-10.03-9.74-18.05-20.62-24.35-33.09-.14-.14-.29-.43-.29-.57q-.72-1.43.72-2.01c1.29-.72,2.72-1,4.01-2.01h0Z"
                        style="fill:#ff6706; stroke-width:0px;"></path>
                    <path
                        d="m192.18,22.35c-4.15,11.46-7.45,23.2-10.6,34.8-2.29,8.45,3.15,15.9,10.6,17.19,2.15.43,4.3.29,6.3-.29,7.45-2.01,14.9-4.15,22.06-6.45,3.72-1.15,7.45-2.58,11.32-3.87q1.58-.57,2.58.86c1.58,2.58,3.01,5.3,4.44,8.02q.72,1.58-.86,2.15c-7.3,3.01-14.47,6.02-21.77,9.02-18.62,7.73-37.24,15.47-56,23.2-.29.14-.72.29-1.15.43-.86.43-1.43.29-2.01-.57-2.29-3.29-5.16-6.02-8.45-8.45q-1.15-.86-.57-2.29c3.44-8.59,7.02-17.04,10.6-25.78,6.3-15.18,12.6-30.36,18.76-45.55.29-.72.72-1.43.86-2.29,1-1.72,1.58-3.58,2.29-5.3q.57-1.43,2.01-.72c2.86,1.43,5.59,3.01,8.45,4.58.43.57,1,.72,1.15,1.29h0Z"
                        style="fill:#ff6706; stroke-width:0px;"></path>
                    <path d="m38.33,86.3c.22,0,.56,0,.56.34-.22-.11-.45-.11-.56-.34Z"
                        style="fill:#c747a3; stroke-width:0px;"></path>
                    <path d="m222.59,39.62c-.22,0-.22-.22-.34-.34.11.11.34.22.34.34Z"
                        style="fill:#c747a3; stroke-width:0px;"></path>
                    <path d="m222.93,39.84c-.22,0-.34-.11-.34-.34.22.22.22.34.34.34Z"
                        style="fill:#c747a3; stroke-width:0px;"></path>
                    <path d="m43.79,225.74q.29.14.43.43c0-.14-.29-.29-.43-.43Z" style="fill:#c747a3; stroke-width:0px;">
                    </path>
                    <path d="m44.37,226.17q.29.14.43.43c-.29-.29-.43-.29-.43-.43Z"
                        style="fill:#c747a3; stroke-width:0px;">
                    </path>
                    <path d="m44.65,226.6c.29,0,.43.14.43.43-.14-.29-.29-.29-.43-.43Z"
                        style="fill:#c747a3; stroke-width:0px;"></path>
                    <path d="m45.08,226.88c.29,0,.43.14.43.43-.29-.14-.43-.29-.43-.43Z"
                        style="fill:#c747a3; stroke-width:0px;"></path>
                    <path d="m45.37,227.17c.29,0,.43.14.43.43-.14-.14-.29-.14-.43-.43Z"
                        style="fill:#c747a3; stroke-width:0px;"></path>
                    <path
                        d="m252.19,127.77c0,15.33-2.01,28.22-6.16,40.82q0,.14-.14.14-.57,2.01-2.58,1.29c-25.92-10.89-51.85-21.63-77.92-32.37q-1.72-.72-1.58-2.72c.57-3.72.57-7.3,0-11.03q-.29-1.72,1.43-2.58c26.07-10.89,52.14-21.77,78.35-32.51q1.58-.57,2.29,1.15c2.72,8.45,4.58,17.04,5.59,26.07.43,4.73.72,9.31.72,11.74h0Z"
                        style="fill:#fff; stroke-width:0px;"></path>
                    <path
                        d="m126.29,256.1c-13.75.14-26.64-2.01-39.39-6.16-2.29-.72-2.01-.57-1.15-2.72,10.6-25.92,21.34-51.71,32.08-77.63.14-.29.29-.72.43-1.15.29-.86.57-1.15,1.58-.86,4.01.72,8.16.72,12.17,0,1.15-.14,1.43.14,1.72,1.15,1.72,4.44,3.58,8.74,5.44,13.03,8.88,21.77,18.05,43.54,26.93,65.31,1,2.43,1.15,2.01-1.29,2.86-9.45,3.01-19.19,5.01-29.22,5.87-3.15.29-6.45.29-9.31.29h0Z"
                        style="fill:#fff; stroke-width:0px;"></path>
                    <path
                        d="m239.16,185.49c-.14.43-.43.86-.72,1.43-3.44,6.73-7.3,13.03-12.03,19.05-11.6,15.33-25.92,27.36-43.11,36.24-2.29,1.29-2.01,1.43-3.01-1.15-10.6-25.92-21.34-51.85-32.08-77.63-.14-.29-.14-.57-.29-.86-.29-.57-.14-1.15.43-1.58,3.44-2.43,6.3-5.44,8.74-8.74.57-.86,1.29-.72,2.01-.29,4.3,1.72,8.59,3.58,12.89,5.44,21.77,9.02,43.54,18.05,65.46,27.07.43.14.86.29,1.29.57.29,0,.43.14.43.43h0Z"
                        style="fill:#ff6706; stroke-width:0px;"></path>
                    <path
                        d="m126.01,152.69c-12.75,0-23.06-10.31-23.06-23.06s10.31-23.06,22.92-23.06,23.06,10.46,23.06,23.2c.14,12.75-10.17,22.92-22.92,22.92Zm-7.59-22.92c.14,4.3,3.44,7.73,7.73,7.73,4.15-.14,7.59-3.58,7.45-7.88-.14-4.15-3.44-7.45-7.59-7.45-4.3-.14-7.73,3.29-7.59,7.59Z"
                        style="fill:#ff6706; stroke-width:0px;"></path>
                    <path d="m97.36,42.12c.29.14.29.43.29.86-.14-.29-.29-.57-.29-.86Z"
                        style="fill:#ea54c0; stroke-width:0px;"></path>
                    <path d="m40.5,102.42c-.29,0-.57-.14-.86-.29.43-.14.57,0,.86.29Z"
                        style="fill:#ea54c0; stroke-width:0px;"></path>
                    <path
                        d="m190.03,60.6l14.04-42.4c6.3-18.76,29.93-24.35,43.97-10.46h0c14.04,14.04,8.45,37.81-10.31,43.97l-42.4,14.18c-3.15,1.15-6.45-2.01-5.3-5.3Z"
                        style="fill:#fff; stroke-width:0px;"></path>
                </svg>
            </span>
            <div class="swp-entries-header-content">
                <h1><?php esc_html_e('Spin Wheel Entries', 'spin-wheel'); ?></h1>
                <p class="swp-header-description">
                    <?php esc_html_e('Manage all spin wheel entries. Filter, track winners, and export data', 'spin-wheel'); ?>
                </p>
            </div>
        </div>

        <div class="swp-entries-content-wrap">
            <!-- Filter Section -->
            <div class="swp-entries-filter">
                <form method="get" action="<?php echo esc_url(admin_url('edit.php')); ?>" id="swp-filter-form">
                    <input type="hidden" name="post_type" value="swp_wheel">
                    <input type="hidden" name="page" value="swp-entries">

                    <div class="swp-filter-row">
                        <div class="swp-filter-group">
                            <label for="swp-wheel-select">
                                <span class="dashicons dashicons-admin-settings"></span>
                                <?php esc_html_e('Select Wheel:', 'spin-wheel'); ?>
                            </label>
                            <?php if (!empty($wheels)): ?>
                                <select name="wheel_id" id="swp-wheel-select">
                                    <?php foreach ($wheels as $wheel): ?>
                                        <option value="<?php echo esc_attr($wheel->ID); ?>" <?php selected($wheel_id, $wheel->ID); ?>>
                                            <?php echo esc_html($wheel->post_title); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            <?php else: ?>
                                <select disabled>
                                    <option><?php esc_html_e('No wheel found', 'spin-wheel'); ?></option>
                                </select>
                            <?php endif; ?>
                        </div>

                        <?php if ($wheel_id): ?>
                            <div class="swp-filter-group">
                                <label for="swp-status-filter">
                                    <span class="dashicons dashicons-filter"></span>
                                    <?php esc_html_e('Filter by Status:', 'spin-wheel'); ?>
                                </label>
                                <select id="swp-status-filter">
                                    <option value=""><?php esc_html_e('All Statuses', 'spin-wheel'); ?></option>
                                    <option value="won_prize"><?php esc_html_e('Won Prize', 'spin-wheel'); ?></option>
                                    <option value="selected_winner"><?php esc_html_e('Selected Winner', 'spin-wheel'); ?></option>
                                    <option value="participated"><?php esc_html_e('Participated Only', 'spin-wheel'); ?></option>
                                </select>
                            </div>
                        <?php endif; ?>
                    </div>

                    <?php if ($wheel_id): ?>
                        <div class="swp-filter-actions">
                            <a href="<?php echo esc_url(add_query_arg(['post_type' => 'swp_wheel', 'page' => 'swp-entries', 'wheel_id' => $wheel_id], admin_url('edit.php'))); ?>"
                                class="button">
                                <span class="dashicons dashicons-update"></span>
                                <?php esc_html_e('Reset Filters', 'spin-wheel'); ?>
                            </a>

                            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin-ajax.php?action=swp_export_entries&wheel_id=' . $wheel_id), 'swp_export_nonce', 'nonce')); ?>"
                                class="button">
                                <span class="dashicons dashicons-download"></span>
                                <?php esc_html_e('Export All CSV', 'spin-wheel'); ?>
                            </a>

                            <button type="button" id="swp-export-filtered" class="button" disabled>
                                <span class="dashicons dashicons-download"></span>
                                <?php esc_html_e('Export Filtered CSV', 'spin-wheel'); ?>
                            </button>
                        </div>
                    <?php endif; ?>
                </form>
            </div>

            <?php if ($wheel_id && !empty($entries)): ?>

                <!-- Statistics Dashboard -->
                <div class="swp-stats-dashboard">
                    <h2><?php esc_html_e('Statistics Overview', 'spin-wheel'); ?></h2>

                    <div class="swp-stats-grid">
                        <div class="swp-stat-card swp-stat-total">
                            <div class="swp-stat-icon">
                                <span class="dashicons dashicons-groups"></span>
                            </div>
                            <div class="swp-stat-content">
                                <div class="swp-stat-label"><?php esc_html_e('Total Entries', 'spin-wheel'); ?></div>
                                <div class="swp-stat-value"><?php echo esc_html(number_format($stats['total'])); ?></div>
                            </div>
                        </div>

                        <div class="swp-stat-card swp-stat-winners">
                            <div class="swp-stat-icon">
                                <span class="dashicons dashicons-star-filled"></span>
                            </div>
                            <div class="swp-stat-content">
                                <div class="swp-stat-label"><?php esc_html_e('Winners', 'spin-wheel'); ?></div>
                                <div class="swp-stat-value"><?php echo esc_html(number_format($stats['winners'])); ?></div>
                            </div>
                        </div>

                        <?php
                        /**
                         * Pro renders the "Selected Winners" stat card (Event Mode).
                         *
                         * @param array  $stats      Entry statistics.
                         * @param string $wheel_mode Current wheel mode.
                         */
                        do_action('swp_entries_stat_cards', $stats, $wheel_mode);
                        ?>

                        <div class="swp-stat-card swp-stat-conversion">
                            <div class="swp-stat-icon">
                                <span class="dashicons dashicons-chart-line"></span>
                            </div>
                            <div class="swp-stat-content">
                                <div class="swp-stat-label"><?php esc_html_e('Win Rate', 'spin-wheel'); ?></div>
                                <div class="swp-stat-value"><?php echo esc_html($stats['conversion_rate']); ?>%</div>
                            </div>
                        </div>
                    </div>

                    <!-- Prize Distribution -->
                    <?php if (!empty($prize_counts)): ?>
                        <div class="swp-prize-distribution">
                            <h3><?php esc_html_e('Prize Distribution', 'spin-wheel'); ?></h3>
                            <div class="swp-prize-bars">
                                <?php
                                arsort($prize_counts);
                                $max_count = max($prize_counts);
                                foreach ($prize_counts as $prize => $count):
                                    $percentage = $max_count > 0 ? ($count / $max_count) * 100 : 0;
                                    ?>
                                    <div class="swp-prize-bar-item">
                                        <div class="swp-prize-bar-label">
                                            <span class="swp-prize-name"><?php echo esc_html($prize); ?></span>
                                            <span class="swp-prize-count"><?php echo esc_html($count); ?>
                                                (<?php echo esc_html(round(($count / $stats['total']) * 100, 1)); ?>%)</span>
                                        </div>
                                        <div class="swp-prize-bar">
                                            <div class="swp-prize-bar-fill" style="width: <?php echo esc_attr($percentage); ?>%;"></div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>

                <?php
                /**
                 * Pro renders the Event Mode instructions notice and the "Bulk Select
                 * Winners" trigger button.
                 *
                 * @param string $wheel_mode Current wheel mode.
                 */
                do_action('swp_entries_notice', $wheel_mode);
                ?>

                <!-- Entries Table -->

                <div class="swp-entries-table-wrapper">
                    <h2><?php esc_html_e('Entries List', 'spin-wheel'); ?></h2>
                    <div class="swp-entries-header">

                        <div class="swp-filter-group swp-search-group">
                            <div class="swp-search-input-wrapper">
                                <span class="dashicons dashicons-search swp-search-icon"></span>
                                <input type="text" id="swp-search-query"
                                    placeholder="<?php esc_html_e('Search by name, email..', 'spin-wheel'); ?>"
                                    class="swp-search-input">
                                <button type="button" class="swp-clear-search"
                                    title="<?php esc_html_e('Clear search', 'spin-wheel'); ?>" style="display:none;">
                                    <span class="dashicons dashicons-dismiss"></span>
                                </button>
                            </div>
                        </div>

                        <!-- Bulk Actions -->
                        <div class="swp-bulk-actions" style="display: none;">
                            <select id="swp-bulk-action-select" class="swp-bulk-action-select">
                                <option value=""><?php esc_html_e('Bulk Actions', 'spin-wheel'); ?></option>
                                <option value="delete"><?php esc_html_e('Delete Selected', 'spin-wheel'); ?></option>
                            </select>
                            <button type="button" id="swp-apply-bulk-action" class="button">
                                <?php esc_html_e('Apply', 'spin-wheel'); ?>
                            </button>
                            <span class="swp-selected-count"></span>
                        </div>
                        <!-- Pagination Top -->
                        <?php if (!empty($entries) && $total_filtered > 0): ?>
                            <div id="swp-pagination-top">
                                <?php
                                $admin_entries = SWP_Admin_Entries::instance();
                                $reflection = new ReflectionClass($admin_entries);
                                $method = $reflection->getMethod('generate_pagination_html');
                                $method->setAccessible(true);
                                echo wp_kses_post($method->invoke($admin_entries, $current_page, $total_pages, $per_page, $total_filtered, false, true));
                                ?>
                            </div>
                        <?php endif; ?>
                    </div>


                    <table class="wp-list-table widefat fixed striped swp-entries-table">
                        <thead>
                            <tr>
                                <th class="swp-col-checkbox">
                                    <input type="checkbox" id="swp-select-all" class="swp-select-all">
                                </th>
                                <th class="swp-col-id"><?php esc_html_e('ID', 'spin-wheel'); ?></th>
                                <th class="swp-col-name">
                                    <!-- <span class="dashicons dashicons-admin-users"></span> -->
                                    <?php esc_html_e('Name', 'spin-wheel'); ?>
                                </th>
                                <th class="swp-col-email">
                                    <!-- <span class="dashicons dashicons-email"></span> -->
                                    <?php esc_html_e('Email', 'spin-wheel'); ?>
                                </th>
                                <th class="swp-col-phone">
                                    <!-- <span class="dashicons dashicons-phone"></span> -->
                                    <?php esc_html_e('Phone', 'spin-wheel'); ?>
                                </th>
                                <th class="swp-col-utm"><?php esc_html_e('Campaign / UTM', 'spin-wheel'); ?></th>
                                <th class="swp-col-prize">
                                    <!-- <span class="dashicons dashicons-awards"></span> -->
                                    <?php esc_html_e('Prize', 'spin-wheel'); ?>
                                </th>
                                <th class="swp-col-code">
                                    <!-- <span class="dashicons dashicons-tag"></span> -->
                                    <?php esc_html_e('Code', 'spin-wheel'); ?>
                                </th>
                                <th class="swp-col-date">
                                    <!-- <span class="dashicons dashicons-calendar-alt"></span> -->
                                    <?php esc_html_e('Date', 'spin-wheel'); ?>
                                </th>
                                <th class="swp-col-status"><?php esc_html_e('Status', 'spin-wheel'); ?></th>
                                <th class="swp-col-actions"><?php esc_html_e('Actions', 'spin-wheel'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($entries as $entry): ?>
                                <tr class="swp-entry-row <?php echo esc_attr($entry->is_selected_winner ? 'swp-winner-row' : ''); ?>">
                                    <td class="swp-col-checkbox">
                                        <input type="checkbox" class="swp-entry-checkbox" value="<?php echo esc_attr($entry->id); ?>">
                                    </td>
                                    <td class="swp-col-id"><strong>#<?php echo esc_html($entry->id); ?></strong></td>
                                    <td class="swp-col-name">
                                        <strong><?php echo esc_html($entry->user_name); ?></strong>
                                    </td>
                                    <td class="swp-col-email">
                                        <a href="mailto:<?php echo esc_attr($entry->user_email); ?>">
                                            <?php echo esc_html($entry->user_email); ?>
                                        </a>
                                    </td>
                                    <td class="swp-col-phone">
                                        <?php echo esc_html($entry->user_phone ?: '—'); ?>
                                    </td>
                                    <td class="swp-col-utm swp-entry-utm-cell"<?php
                                    $utm_plain = swp_entry_format_utm_csv($entry);
                                    echo $utm_plain !== '' ? ' title="' . esc_attr($utm_plain) . '"' : '';
                                    ?>>
                                        <?php
                                        $utm_html = swp_entry_format_utm_admin_cell($entry);
                                        echo $utm_html !== '' ? wp_kses($utm_html, ['br' => []]) : '<span>—</span>';
                                        ?>
                                    </td>
                                    <td class="swp-col-prize">
                                        <div class="swp-prize-info">
                                            <strong><?php echo esc_html($entry->prize_label); ?></strong>
                                        </div>
                                    </td>
                                    <td class="swp-col-code">
                                        <?php if ($entry->prize_code): ?>
                                            <code class="swp-prize-code"><?php echo esc_html($entry->prize_code); ?></code>
                                        <?php else: ?>
                                            <span>—</span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="swp-col-date">
                                        <div class="swp-date-info">
                                            <div>
                                                <?php echo esc_html(date_i18n(get_option('date_format'), strtotime($entry->created_at))); ?>
                                            </div>
                                            <small><?php echo esc_html(date_i18n(get_option('time_format'), strtotime($entry->created_at))); ?></small>
                                        </div>
                                    </td>
                                    <td class="swp-col-status">
                                        <?php if ($entry->is_selected_winner): ?>
                                            <span class="swp-badge swp-badge-winner">
                                                <!-- <span class="dashicons dashicons-trophy"></span> -->
                                                <?php esc_html_e('Winner', 'spin-wheel'); ?>
                                            </span>
                                        <?php elseif ($entry->is_winner): ?>
                                            <span class="swp-badge swp-badge-won">
                                                <span class="dashicons dashicons-star-filled"></span>
                                                <?php esc_html_e('Won Prize', 'spin-wheel'); ?>
                                            </span>
                                        <?php else: ?>
                                            <span class="swp-badge swp-badge-participated">
                                                <?php esc_html_e('Participated', 'spin-wheel'); ?>
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="swp-col-actions">
                                        <div class="swp-entry-actions">
                                            <?php
                                            /** Event Mode winner actions rendered by Spin Wheel Pro (nothing in free). */
                                            do_action('swp_entry_row_winner_actions', $entry, $wheel_mode);
                                            ?>

                                            <!-- Delete Button -->
                                            <button type="button" class="button button-small swp-delete-entry"
                                                data-entry-id="<?php echo esc_attr($entry->id); ?>"
                                                data-user-name="<?php echo esc_attr($entry->user_name); ?>"
                                                title="<?php esc_html_e('Delete Entry', 'spin-wheel'); ?>">
                                                <span class="dashicons dashicons-trash"></span>
                                                <?php esc_html_e('Delete', 'spin-wheel'); ?>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>

                    <!-- Pagination Bottom -->
                    <?php if (!empty($entries) && $total_filtered > 0): ?>
                        <div id="swp-pagination-bottom">
                            <?php
                            $admin_entries = SWP_Admin_Entries::instance();
                            $reflection = new ReflectionClass($admin_entries);
                            $method = $reflection->getMethod('generate_pagination_html');
                            $method->setAccessible(true);
                            echo wp_kses_post($method->invoke($admin_entries, $current_page, $total_pages, $per_page, $total_filtered, true, false));
                            ?>
                        </div>
                    <?php endif; ?>
                </div>

            <?php elseif ($wheel_id): ?>
                <p class="swp-no-entries-text"><?php esc_html_e('No entries found for this wheel yet.', 'spin-wheel'); ?></p>
            <?php else: ?>
                <p><?php esc_html_e('Please select a wheel to view entries.', 'spin-wheel'); ?></p>
            <?php endif; ?>
        </div>
    </div>

    <footer class="swp-admin-footer">
        <p>
            <?php
            printf(
                /* translators: %s: current year */
                wp_kses( __('&copy; %s Spin Wheel.', 'spin-wheel'), array() ),
                esc_html( gmdate('Y') ),
            ); ?>
            <?php
            printf(
                /* translators: 1: heart icon, 2: BdThemes link */
                esc_html__('Made with %1$s for %2$s.', 'spin-wheel'),
                '<span class="swp-love">♥</span>',
                '<a href="https://bdthemes.com/" target="_blank">BdThemes</a>'
            ); ?>
        </p>
    </footer>
</div>

<?php
/**
 * Pro renders the Event Mode bulk winner-selection modal here.
 *
 * @param int    $wheel_id   Wheel post ID.
 * @param string $wheel_mode Current wheel mode.
 * @param array  $entries    Current page of entries.
 */
do_action('swp_entries_footer', $wheel_id, $wheel_mode, $entries);
?>

<!-- Loading Overlay and Spinner (Fixed Position - Center of Screen) -->
<div class="swp-loading-overlay"></div>
<div class="swp-loading-spinner" style="display:none;">
    <div class="swp-spinner">
        <div class="swp-circle swp-circle-1"></div>
        <div class="swp-circle swp-circle-2"></div>
        <div class="swp-circle swp-circle-3"></div>
    </div>
    <p><?php esc_html_e('Loading entries...', 'spin-wheel'); ?></p>
</div>

<style>
    /* ==========================================================================
   Entries Page Styles
   ========================================================================== */


    .swp-entries-page {
        max-width: 1200px;
        background: #fff;
        border-radius: 10px;
        margin-top: 20px;
        min-height: calc(100vh - 80px);
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        position: relative;
    }

    .swp-entries-page h1 .dashicons {
        vertical-align: middle;
        color: #2271b1;
    }

    .swp-entries-content-wrap {
        padding: 30px;
    }

    /* Filter Section */


    .swp-entries-filter form {
        display: flex;
        flex-direction: row;
        gap: 10px;
        justify-content: space-between;
    }

    .swp-filter-row {
        display: flex;
        gap: 10px;
        align-items: flex-end;
        flex-wrap: wrap;
    }


    .swp-filter-actions {
        display: flex;
        gap: 10px;
        align-items: center;
        flex-wrap: wrap;
        border-top: 1px solid rgba(255, 255, 255, 0.2);
    }

    .swp-filter-group {
        display: flex;
        gap: 8px;
        align-items: center;
    }

    .swp-filter-group label {
        color: #262628;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 5px;
        font-size: 14px;
    }

    .swp-filter-group label .dashicons {
        font-size: 16px;
        width: 16px;
        height: 16px;
    }

    #swp-wheel-select {
        min-width: 180px;
        max-width: 180px;
    }

    #swp-wheel-select,
    #swp-status-filter,
    #swp-winner-modal .swp-form-control,
    #swp-bulk-action-select {
        padding: 8px 12px;
        border-radius: 6px;
        border: 1px solid #dee5ed;
        background: #fff;
        font-size: 14px;
        transition: all 0.2s ease;
        max-width: unset;
    }

    #swp-search-query {
        min-width: 300px;
        padding: 12px 12px 12px 36px !important;
    }

    #swp-wheel-select:focus,
    #swp-status-filter:focus,
    #swp-search-query:focus,
    #swp-winner-modal .swp-form-control:focus,
    #swp-bulk-action-select:focus {
        border-color: #f24101 !important;
        outline: none;
        box-shadow: 0 0 0 1px #f24101 !important;
    }

    .swp-search-input-wrapper {
        position: relative;
        display: flex;
        align-items: center;
    }

    .swp-search-icon {
        position: absolute;
        left: 12px;
        top: 50%;
        transform: translateY(-50%);
        color: #8c8f94;
        font-size: 18px;
        pointer-events: none;
        z-index: 1;
        line-height: 20px;
    }

    .swp-search-input {
        width: 100%;
        padding: 10px 40px 10px 38px;
        border-radius: 6px;
        border: 2px solid transparent;
        background: #fff;
        font-size: 14px;
        transition: all 0.2s ease;
    }

    .swp-search-input:focus {
        border-color: #fff;
        outline: none;
        box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.2);
    }

    .swp-search-input-wrapper:focus-within .swp-search-icon {
        color: #f24101;
    }

    .swp-clear-search {
        position: absolute;
        right: 8px;
        top: 50%;
        transform: translateY(-50%);
        background: #d63638;
        border: none;
        color: #fff;
        width: 24px;
        height: 24px;
        border-radius: 50%;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s ease;
    }

    .swp-clear-search:hover {
        background: #b32d2e;
        transform: translateY(-50%) scale(1.1);
    }

    .swp-clear-search .dashicons {
        font-size: 14px;
        height: 14px;
        width: 14px;
    }

    .swp-entries-filter .button {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        border-radius: 6px;
        font-weight: 600;
        padding: 8px 12px;
        transition: all 0.2s ease;
        border: 0;
        background: #f1f3f7;
        color: #2d2e33;
        outline: none;
    }

    .swp-entries-filter .button:hover:not(:disabled) {
        border-color: #ddd;
    }

    .swp-entries-filter .button .dashicons {
        font-size: 14px;
        height: 14px;
        width: 14px;
    }

    .swp-entries-filter .button:focus {
        outline: none;
        box-shadow: 0 0 0 2px #f24101;
    }

    .swp-entries-header #swp-pagination-top {
        margin-left: auto;
    }

    /* Export Filtered Button */
    #swp-export-filtered {
        background: linear-gradient(35deg, #f24101, #f7994c);
        color: #fff;
        font-weight: 600;
        border: 0;
    }

    #swp-export-filtered:hover:not(:disabled) {
        background: linear-gradient(35deg, #f7994c, #f24101);
        border-color: #38d9a9;
        color: #fff;
    }

    #swp-export-filtered:disabled {
        background: #f0f0f1;
        border-color: #dcdcde;
        color: #c3c4c7;
        cursor: not-allowed;
    }

    #swp-export-filtered .dashicons.spin {
        animation: spin 1s linear infinite;
    }

    /* Statistics Dashboard */
    .swp-stats-dashboard {
        margin: 30px 0;
    }

    .swp-stats-dashboard h2 {
        margin: 0 0 20px;
        padding-bottom: 10px;
        border-bottom: 2px solid #f0f0f1;
    }

    .swp-stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 20px;
        margin-bottom: 30px;
    }

    .swp-stat-card {
        border-radius: 8px;
        padding: 14px;
        display: flex;
        align-items: center;
        gap: 15px;
        border: 1px solid #efefef;
    }

    .swp-stat-icon {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        color: #3b3f45;
        background: #f1f3f7;
    }

    .swp-stat-content {
        flex: 1;
        display: flex;
        justify-content: space-between;
    }

    .swp-stat-label {
        font-size: 14px;
        color: #222325ff;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 0;
        font-weight: 600;
    }

    .swp-stat-value {
        font-size: 22px;
        font-weight: 700;
        color: #1d2327;
        line-height: 1;
    }

    /* Prize Distribution */
    .swp-prize-distribution {
        margin-top: 30px;
    }

    .swp-prize-distribution h3 {
        margin: 0 0 15px;
        font-size: 16px;
    }

    .swp-prize-bars {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .swp-prize-bar-item {
        display: flex;
        flex-direction: column;
        gap: 5px;
    }

    .swp-prize-bar-label {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 13px;
    }

    .swp-prize-name {
        font-weight: 600;
        color: #1d2327;
    }

    .swp-prize-count {
        color: #646970;
        font-size: 12px;
    }

    .swp-prize-bar {
        height: 8px;
        background: #f0f0f1;
        border-radius: 4px;
        overflow: hidden;
    }

    .swp-prize-bar-fill {
        height: 100%;
        background: linear-gradient(35deg, #f24101, #f7994c);
        border-radius: 4px;
        transition: width 0.3s ease;
    }

    /* Event Mode Notice */
    .swp-event-notice {
        color: #2e2e30;
        border-radius: 8px;
        padding: 20px;
        margin: 20px 0;
        display: flex;
        align-items: center;
        gap: 15px;
        justify-content: space-between;
        border: 1px solid #f2f2f3;
    }

    .swp-even-notice-left {
        display: flex;
        gap: 15px;
    }

    .swp-notice-icon span {
        font-size: 30px;
        width: 30px;
        height: 30px;
    }

    .swp-notice-content h3 {
        margin: 0 0 10px;
        font-size: 18px;
        color: #2e2e30;
    }

    .swp-notice-content p {
        margin: 0;
        font-size: 14px;
        line-height: 1.6;
        opacity: 0.95;
    }

    .swp-notice-content p {
        margin: 0;
        font-size: 14px;
        line-height: 1.6;
        opacity: 0.95;
        max-width: 650px;
    }

    .swp-event-notice .swp-open-winner-modal,
    #swp-preview-winners {
        padding: 0 20px !important;
        min-height: unset;
        line-height: 1;
        height: 50px;
        border-radius: 6px;
        background: linear-gradient(35deg, #f24101, #f7994c);
        border: 0;
        outline: none;
    }

    .swp-event-notice .swp-open-winner-modal:hover,
    .swp-event-notice .swp-open-winner-modal:focus,
    #swp-preview-winners:hover,
    #swp-preview-winners:focus {
        background: linear-gradient(35deg, #f7994c, #f24101);
    }

    .swp-event-notice .swp-open-winner-modal:focus,
    #swp-preview-winners:focus {
        box-shadow: 0 0 0 3px rgba(242, 65, 1, 0.5);
    }

    .swp-event-notice .button-hero .dashicons {
        font-size: 20px;
        height: 20px;
        width: 20px;
        vertical-align: middle;
    }

    /* Entries Table */
    .swp-entries-table-wrapper {
        min-height: 400px;
    }

    /* Entries Header */
    .swp-entries-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        flex-wrap: wrap;
        gap: 15px;
    }

    .swp-entries-header h2 {
        margin: 0;
        flex: 1;
    }

    /* Bulk Actions */
    .swp-bulk-actions {
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .swp-bulk-action-select {
        min-width: 150px;
        padding: 6px 10px;
        border: 1px solid #dcdcde;
        border-radius: 4px;
        background: #fff;
    }

    .swp-selected-count {
        color: #646970;
        font-size: 13px;
        font-weight: 600;
        background: #667eea;
        color: #fff;
        padding: 4px 8px;
        border-radius: 12px;
        font-size: 11px;
    }

    /* table css here  */

    .swp-entries-table {
        border-radius: 10px;
        border: 1px solid #efefef !important;
        box-shadow: unset !important;
        overflow: hidden;
    }

    .swp-entries-table-wrapper h2,
    .swp-stats-dashboard h2 {
        margin: 0 0 20px;
        padding-bottom: 10px;
        border-bottom: 2px solid #f0f0f1;
        position: relative;
    }

    .swp-entries-table-wrapper h2::before,
    .swp-stats-dashboard h2::before {
        content: '';
        position: absolute;
        left: 0;
        bottom: -2px;
        width: 60px;
        height: 2px;
        background: linear-gradient(135deg, #f24101, #f7994c);
        border-radius: 4px;
    }

    .swp-entries-table thead th {
        font-weight: 600;
        background: #fff;
        border-bottom: 1px solid #eaebed;
    }

    .swp-entries-table thead th .dashicons {
        vertical-align: middle;
        opacity: 0.7;
    }

    .swp-entries-table td,
    .swp-entries-table th {
        padding: 14px 10px;
        vertical-align: middle;
    }

    .swp-entries-table .swp-col-utm {
        max-width: 220px;
        font-size: 12px;
        line-height: 1.4;
        word-break: break-word;
    }

    /* .swp-winner-row {
        background: #f0f6fc !important;
    } */

    .swp-col-checkbox {
        width: 40px;
        text-align: center;
    }

    /* Entry Actions */
    .swp-entry-actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
        align-items: flex-start;
    }

    .swp-entry-actions .button {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        font-size: 12px;
        padding: 4px 8px;
        height: auto;
        line-height: 1.4;
        border-radius: 6px;
        border: 0;
        width: 100%;
        min-height: 32px !important;
        transition: all 0.2s ease;
    }

    .swp-entry-actions .button.swp-manual-select-winner {
        background: #fff;
        color: #24acff;
        border: 1px solid #24acff;
    }

    .swp-entry-actions .button.swp-manual-select-winner:hover {
        background: #d6e5fcff;
    }

    .swp-entry-actions .button.swp-manual-select-winner:focus {
        box-shadow: 0 0 0 2px #24acff;
    }

    .swp-entry-actions .button.swp-delete-entry {
        background: #fff;
        border: 1px solid #fadfdf;
        color: #ff2424;
    }

    .swp-entry-actions .button.swp-delete-entry:hover {
        background: #feddddff;
    }

    .swp-entry-actions .button.swp-delete-entry:focus {
        box-shadow: 0 0 0 2px #ff2424;
    }

    .swp-entry-actions .button.swp-revert-winner {
        background: #e9f0ff;
        color: #38393c;
        border: 1px solid #dfeaf3;
    }

    .swp-entry-actions .button.swp-revert-winner:hover {
        background: #d1e5f9ff;
    }

    .swp-delete-entry .dashicons {
        font-size: 14px;
        height: 14px;
        width: 14px;
    }

    /* Checkbox Styles */
    .swp-select-all,
    .swp-entry-checkbox {
        margin: 0;
        transform: scale(1.1);
    }

    .swp-date-info {
        font-size: 13px;
    }

    .swp-date-info small {
        color: #646970;
        font-size: 11px;
    }

    .swp-prize-code {
        background: #f0f0f1;
        padding: 4px 8px;
        border-radius: 3px;
        font-family: 'Courier New', monospace;
        font-size: 12px;
        border: 1px solid #ddd;
    }

    /* Badges */
    .swp-badge {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 5px 10px;
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        border-radius: 6px;
    }

    .swp-badge .dashicons {
        font-size: 14px;
        height: 14px;
        width: 14px;
    }

    .swp-badge-winner {
        background: linear-gradient(35deg, #f24101, #f7994c);
        color: #fff;
    }

    .swp-badge-won {
        background: linear-gradient(135deg, #ffeaa7 0%, #fdcb6e 100%);
        color: #2d3436;
    }

    .swp-badge-participated {
        background: #f0f0f1;
        color: #646970;
    }

    /* Winner Actions */
    .swp-manual-select-winner {
        display: inline-flex;
        align-items: center;
        gap: 5px;
    }

    .swp-manual-select-winner .dashicons {
        font-size: 16px;
        height: 16px;
        width: 16px;
    }

    .swp-winner-actions {
        display: flex;
        align-items: center;
        gap: 10px;
        flex-wrap: wrap;
    }

    .swp-winner-checkmark {
        display: inline-flex;
        align-items: center;
        gap: 5px;
        color: #46b450;
        font-weight: 600;
        font-size: 13px;
    }

    .swp-winner-checkmark .dashicons {
        font-size: 20px;
        height: 20px;
        width: 20px;
    }

    .swp-revert-winner {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        background: #f6f7f7;
        border-color: #dcdcde;
        color: #d63638;
    }

    .swp-revert-winner:hover {
        background: #d63638;
        border-color: #d63638;
        color: #fff;
    }

    .swp-revert-winner .dashicons {
        font-size: 14px;
        height: 14px;
        width: 14px;
    }

    .swp-no-action {
        color: #c3c4c7;
    }

    /* Winner Selection Modal */
    .swp-modal {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 100000;
        overflow-y: auto;
    }

    .swp-modal-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        backdrop-filter: blur(4px);
        animation: swpFadeIn 0.3s ease;
    }

    .swp-modal-dialog {
        position: relative;
        width: 90%;
        max-width: 650px;
        margin: 40px auto;
        animation: swpSlideIn 0.3s ease;
    }

    .swp-modal-content {
        background: #fff;
        border-radius: 12px;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
        overflow: hidden;
    }

    .swp-modal-header {
        background: linear-gradient(35deg, #f24101, #f7994c);
        color: #fff;
        padding: 25px 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .swp-modal-header h2 {
        margin: 0;
        font-size: 20px;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 10px;
        color: #fff;
    }

    .swp-modal-header h2 .dashicons {
        font-size: 24px;
        height: 24px;
        width: 24px;
    }

    .swp-modal-close {
        background: rgba(255, 255, 255, 0.2);
        border: none;
        color: #fff;
        width: 32px;
        height: 32px;
        border-radius: 50%;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s;
    }

    .swp-modal-close:hover {
        background: rgba(255, 255, 255, 0.3);
        transform: scale(1.1);
    }

    .swp-modal-close .dashicons {
        font-size: 20px;
        height: 20px;
        width: 20px;
    }

    .swp-modal-body {
        padding: 30px;
        max-height: 60vh;
        overflow-y: auto;
    }

    .swp-form-group {
        margin-bottom: 20px;
    }

    .swp-form-label {
        display: flex;
        align-items: center;
        gap: 8px;
        font-weight: 600;
        font-size: 14px;
        color: #1d2327;
        margin-bottom: 10px;
    }

    .swp-form-label .dashicons {
        color: #667eea;
        font-size: 18px;
    }

    .swp-form-control {
        width: 100%;
        padding: 10px 15px;
        border: 2px solid #dcdcde;
        border-radius: 6px;
        font-size: 14px;
        transition: all 0.2s;
    }

    .swp-form-control:focus {
        border-color: #667eea;
        outline: none;
        box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }

    .swp-input-with-info {
        display: flex;
        gap: 10px;
        align-items: center;
    }

    .swp-input-with-info .swp-form-control {
        flex: 1;
    }

    .swp-info-badge {
        background: linear-gradient(35deg, #f24101, #f7994c);
        color: #fff;
        padding: 12px 16px;
        border-radius: 6px;
        font-size: 12px;
        font-weight: 600;
        white-space: nowrap;
    }

    .swp-form-description {
        margin: 8px 0 0;
        font-size: 12px;
        color: #646970;
        line-height: 1.5;
    }

    /* Toggle Styles for Modal */
    .swp-modal .swp-input-wrapper {
        margin-top: 5px;
    }

    .swp-modal .swp-toggle-label {
        display: flex;
        align-items: center;
        gap: 12px;
        cursor: pointer;
        user-select: none;
    }

    .swp-modal .swp-toggle-input {
        position: absolute;
        opacity: 0;
        pointer-events: none;
    }

    .swp-modal .swp-toggle-switch {
        position: relative;
        display: inline-block;
        width: 44px;
        height: 24px;
        background: #e0e0e0;
        border-radius: 12px;
        transition: background 0.3s ease;
        flex-shrink: 0;
    }

    .swp-modal .swp-toggle-switch::after {
        content: '';
        position: absolute;
        top: 2px;
        left: 2px;
        width: 20px;
        height: 20px;
        background: white;
        border-radius: 50%;
        transition: transform 0.3s ease;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    }

    .swp-modal .swp-toggle-input:checked+.swp-toggle-switch {
        background: linear-gradient(35deg, #f24101, #f7994c);
    }

    .swp-modal .swp-toggle-input:checked+.swp-toggle-switch::after {
        transform: translateX(20px);
    }

    .swp-modal .swp-toggle-text {
        font-size: 14px;
        font-weight: 600;
        color: #1d2327;
        line-height: 1.4;
    }

    /* Hover effects for toggles */
    .swp-modal .swp-toggle-label:hover .swp-toggle-switch {
        background: #d0d0d0;
    }

    .swp-modal .swp-toggle-label:hover .swp-toggle-input:checked+.swp-toggle-switch {
        background: linear-gradient(35deg, #f7994c, #f24101);
    }

    .swp-preview-section {
        margin-top: 30px;
        padding-top: 25px;
        border-top: 2px solid #f0f0f1;
    }

    .swp-preview-section h3 {
        margin: 0 0 15px;
        font-size: 16px;
        display: flex;
        align-items: center;
        gap: 8px;
        color: #1d2327;
    }

    .swp-preview-section h3 .dashicons {
        color: #667eea;
    }

    .swp-preview-list {
        background: #f6f7f7;
        border-radius: 8px;
        padding: 15px;
        max-height: 250px;
        overflow-y: auto;
    }

    .swp-preview-item {
        background: #fff;
        border: 1px solid #dcdcde;
        border-radius: 6px;
        padding: 12px 15px;
        margin-bottom: 10px;
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .swp-preview-item:last-child {
        margin-bottom: 0;
    }

    .swp-preview-icon {
        width: 36px;
        height: 36px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: #fff;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 700;
        font-size: 14px;
        flex-shrink: 0;
    }

    .swp-preview-info {
        flex: 1;
    }

    .swp-preview-name {
        font-weight: 600;
        color: #1d2327;
        font-size: 14px;
        margin-bottom: 3px;
    }

    .swp-preview-details {
        font-size: 12px;
        color: #646970;
    }

    .swp-preview-prize {
        background: linear-gradient(135deg, #ffeaa7 0%, #fdcb6e 100%);
        color: #2d3436;
        padding: 4px 10px;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 600;
        white-space: nowrap;
    }

    .swp-modal-footer {
        background: #f6f7f7;
        padding: 20px 30px;
        display: flex;
        justify-content: flex-end;
        gap: 10px;
        border-top: 1px solid #dcdcde;
    }

    button#swp-preview-winners {
        height: 40px;
        padding: 0 15px !important;
        color: #fff;
    }

    button#swp-confirm-winners {
        border-radius: 6px;
    }

    .swp-modal-footer .button {
        display: inline-flex;
        align-items: center;
        gap: 6px;
    }

    .swp-modal-footer .button .dashicons {
        font-size: 16px;
        height: 16px;
        width: 16px;
    }

    button#swp-apply-bulk-action {
        padding: 14px 20px;
        line-height: 1;
        border-radius: 6px;
        background: linear-gradient(35deg, #f24101, #f7994c);
        border: 0;
        color: #fff;
    }

    button#swp-apply-bulk-action:hover {
        background: linear-gradient(35deg, #f7994c, #f24101);
    }

    button#swp-apply-bulk-action:focus {
        box-shadow: 0 0 0 3px rgba(242, 65, 1, 0.5);
    }


    @keyframes swpFadeIn {
        from {
            opacity: 0;
        }

        to {
            opacity: 1;
        }
    }

    @keyframes swpSlideIn {
        from {
            opacity: 0;
            transform: translateY(-30px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .swp-no-entries-text {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        margin: 0;
        font-size: 18px;
    }

    /* Responsive */
    @media screen and (max-width: 1200px) {
        .swp-stats-grid {
            grid-template-columns: repeat(2, 1fr);
        }
    }

    @media screen and (max-width: 768px) {
        .swp-stats-grid {
            grid-template-columns: 1fr;
        }

        .swp-entries-filter {
            padding: 15px;
        }

        .swp-filter-row {
            flex-direction: column;
            gap: 15px;
        }

        .swp-filter-group {
            min-width: 100%;
        }

        .swp-search-group {
            min-width: 100%;
        }

        .swp-filter-actions {
            flex-direction: column;
            align-items: stretch;
            gap: 10px;
        }

        .swp-filter-actions .button {
            width: 100%;
            justify-content: center;
        }

        #swp-wheel-select,
        #swp-status-filter,
        .swp-search-input {
            width: 100%;
            min-width: 100%;
        }

        .swp-modal-dialog {
            margin: 20px auto;
        }

        .swp-modal-body {
            max-height: 50vh;
        }

        .swp-entries-header {
            flex-direction: column;
            align-items: stretch;
        }

        .swp-bulk-actions {
            justify-content: center;
            flex-wrap: wrap;
        }

        .swp-entry-actions {
            flex-direction: row;
            flex-wrap: wrap;
            gap: 5px;
        }

        .swp-entry-actions .button {
            font-size: 11px;
            padding: 3px 6px;
        }

        .swp-col-actions {
            width: 150px;
        }

        /* Hide some columns on mobile for better readability */
        .swp-entries-table .swp-col-phone,
        .swp-entries-table .swp-col-utm,
        .swp-entries-table .swp-col-code {
            display: none;
        }

        .swp-entries-table {
            font-size: 12px;
        }
    }

    footer.swp-admin-footer {
        border-top: 1px solid #eeeff0;
    }
</style>

<script>
    jQuery(document).ready(function ($) {
        'use strict';

        // Store all entries data for filtering
        const allEntries = <?php echo wp_json_encode($entries ?? [], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_json_encode with JSON_HEX_* is safe for inline <script> ?>;
        const wheelId = <?php echo esc_js($wheel_id ? $wheel_id : 'null'); ?>;
        const wheelMode = '<?php echo esc_js($wheel_mode); ?>';
        // Expose entries data so the Spin Wheel Pro add-on can drive Event Mode winner selection.
        window.swpEntriesData = { allEntries: allEntries, wheelId: wheelId, wheelMode: wheelMode };
        const currentStatusFilter = '<?php echo esc_js($status_filter); ?>';
        const currentSearchQuery = '<?php echo esc_js($search_query); ?>';

        // Auto-redirect to add wheel_id to URL if it was auto-selected
        <?php if ($wheel_id && !isset($_GET['wheel_id'])): // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only URL check for a client-side redirect ?>
            const currentUrl = new URL(window.location.href);
            if (!currentUrl.searchParams.has('wheel_id')) {
                currentUrl.searchParams.set('wheel_id', '<?php echo esc_js($wheel_id); ?>');
                window.history.replaceState({}, '', currentUrl.toString());
            }
        <?php endif; ?>

        // Filtering is now handled by AJAX in admin.js

        // Handle export filtered button click
        $(document).on('click', '#swp-export-filtered', function () {
            const $button = $(this);
            const statusFilter = $('#swp-status-filter').val();
            const searchQuery = $('#swp-search-query').val().trim();

            // Show loading state
            $button.prop('disabled', true).html('<span class="dashicons dashicons-update spin"></span> Exporting...');

            // Create form and submit
            const $form = $('<form>', {
                method: 'POST',
                action: ajaxurl
            });

            $form.append($('<input>', {
                type: 'hidden',
                name: 'action',
                value: 'swp_export_filtered_entries'
            }));

            $form.append($('<input>', {
                type: 'hidden',
                name: 'nonce',
                value: '<?php echo esc_attr(wp_create_nonce('swp_export_filtered_nonce')); ?>'
            }));

            $form.append($('<input>', {
                type: 'hidden',
                name: 'wheel_id',
                value: wheelId
            }));

            $form.append($('<input>', {
                type: 'hidden',
                name: 'status_filter',
                value: statusFilter
            }));

            $form.append($('<input>', {
                type: 'hidden',
                name: 'search_query',
                value: searchQuery
            }));

            // Add form to body and submit
            $('body').append($form);
            $form.submit();
            $form.remove();

            // Reset button after a delay
            setTimeout(function () {
                $button.prop('disabled', false).html('<span class="dashicons dashicons-download"></span> Export Filtered CSV');
            }, 2000);
        });

        // Event Mode winner-selection JS is provided by the Spin Wheel Pro add-on.

        // Helper function
        function escapeHtml(text) {
            const map = {
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                '"': '&quot;',
                "'": '&#039;'
            };
            return text.replace(/[&<>"']/g, function (m) { return map[m]; });
        }

        // Add spin animation
        $('<style>.dashicons.spin { animation: spin 1s linear infinite; } @keyframes spin { 100% { transform: rotate(360deg); } }</style>').appendTo('head');

        // ==========================================================================
        // SEARCH AND FILTER FUNCTIONALITY
        // ==========================================================================

        // Real-time search functionality
        let searchTimeout;
        $('#swp-search-query').on('input', function () {
            const $input = $(this);
            const query = $input.val().trim();

            // Clear existing timeout
            clearTimeout(searchTimeout);

            // Add loading indicator
            if (query.length > 0) {
                $input.addClass('searching');
            }

            // Search is now handled by admin.js via AJAX
            // No client-side search needed here
        });

        // Clear search functionality
        $(document).on('click', '.swp-clear-search', function () {
            $('#swp-search-query').val('').trigger('input');
        });

        // Auto-submit form on wheel selection change
        $('#swp-wheel-select').on('change', function () {
            if ($(this).val()) {
                $('#swp-filter-form').submit();
            }
        });

        // Note: Status filter and search are handled by admin.js via AJAX
        // No inline handlers needed here to avoid conflicts
        // Old client-side filtering functions removed to prevent statistics from being updated

        function updateEntriesTable(entries) {
            const $tbody = $('.swp-entries-table tbody');
            $tbody.empty();

            if (entries.length === 0) {
                $tbody.append('<tr><td colspan="11" style="text-align: center; padding: 40px; color: #646970;">No entries match your search criteria.</td></tr>');
                return;
            }

            entries.forEach(function (entry) {
                const row = createEntryRow(entry);
                $tbody.append(row);
            });

            // Update bulk actions
            updateBulkActions();
        }

        function createEntryRow(entry) {
            const isWinner = entry.is_selected_winner == 1;
            const hasWonPrize = entry.is_winner == 1;
            const participated = entry.is_winner == 0;

            let statusBadge = '';
            if (isWinner) {
                statusBadge = '<span class="swp-badge swp-badge-winner">Winner</span>';
            } else if (hasWonPrize) {
                statusBadge = '<span class="swp-badge swp-badge-won"><span class="dashicons dashicons-star-filled"></span>Won Prize</span>';
            } else {
                statusBadge = '<span class="swp-badge swp-badge-participated">Participated</span>';
            }

            let actionsColumn = '';
            if (wheelMode === 'event') {
                if (isWinner) {
                    actionsColumn = `
                    <div class="swp-winner-actions">
                        <span class="swp-winner-checkmark">
                            <span class="dashicons dashicons-yes-alt"></span>
                            Selected
                        </span>
                        <button type="button" class="button button-small swp-revert-winner" 
                                data-entry-id="${entry.id}"
                                data-user-name="${escapeHtml(entry.user_name)}"
                                title="Revert Selection">
                            <span class="dashicons dashicons-undo"></span>
                            Revert
                        </button>
                    </div>
                `;
                } else {
                    actionsColumn = `
                    <button type="button" class="button button-primary button-small swp-manual-select-winner" 
                            data-entry-id="${entry.id}"
                            data-user-name="${escapeHtml(entry.user_name)}"
                            data-user-email="${escapeHtml(entry.user_email)}"
                            data-prize="${escapeHtml(entry.prize_label)}">
                        <span class="dashicons dashicons-yes"></span>
                        Select Winner
                    </button>
                `;
                }
            }

            actionsColumn += `
            <button type="button" class="button button-small swp-delete-entry" 
                    data-entry-id="${entry.id}"
                    data-user-name="${escapeHtml(entry.user_name)}"
                    title="Delete Entry">
                <span class="dashicons dashicons-trash"></span>
                Delete
            </button>
        `;

            const createdDate = new Date(entry.created_at);
            const dateStr = createdDate.toLocaleDateString();
            const timeStr = createdDate.toLocaleTimeString();

            return `
            <tr class="swp-entry-row ${isWinner ? 'swp-winner-row' : ''}">
                <td class="swp-col-checkbox">
                    <input type="checkbox" class="swp-entry-checkbox" value="${entry.id}">
                </td>
                <td class="swp-col-id"><strong>#${entry.id}</strong></td>
                <td class="swp-col-name">
                    <strong>${escapeHtml(entry.user_name)}</strong>
                </td>
                <td class="swp-col-email">
                    <a href="mailto:${escapeHtml(entry.user_email)}">
                        ${escapeHtml(entry.user_email)}
                    </a>
                </td>
                <td class="swp-col-phone">
                    ${entry.user_phone ? escapeHtml(entry.user_phone) : '—'}
                </td>
                <td class="swp-col-prize">
                    <div class="swp-prize-info">
                        <strong>${escapeHtml(entry.prize_label)}</strong>
                    </div>
                </td>
                <td class="swp-col-code">
                    ${entry.prize_code ? '<code class="swp-prize-code">' + escapeHtml(entry.prize_code) + '</code>' : '<span>—</span>'}
                </td>
                <td class="swp-col-date">
                    <div class="swp-date-info">
                        <div>${dateStr}</div>
                        <small>${timeStr}</small>
                    </div>
                </td>
                <td class="swp-col-status">
                    ${statusBadge}
                </td>
                <td class="swp-col-actions">
                    <div class="swp-entry-actions">
                        ${actionsColumn}
                    </div>
                </td>
            </tr>
        `;
        }

        // updateStatistics function removed - statistics should NOT change when filtering
        // Statistics are calculated from full database on page load only

        function updatePrizeDistribution(prizeCounts, total) {
            const $prizeBars = $('.swp-prize-bars');
            $prizeBars.empty();

            if (Object.keys(prizeCounts).length === 0) {
                return;
            }

            // Sort by count descending
            const sortedPrizes = Object.entries(prizeCounts).sort((a, b) => b[1] - a[1]);
            const maxCount = Math.max(...Object.values(prizeCounts));

            sortedPrizes.forEach(function ([prize, count]) {
                const percentage = maxCount > 0 ? (count / maxCount) * 100 : 0;
                const countPercentage = total > 0 ? Math.round((count / total) * 100 * 10) / 10 : 0;

                const barItem = `
                <div class="swp-prize-bar-item">
                    <div class="swp-prize-bar-label">
                        <span class="swp-prize-name">${escapeHtml(prize)}</span>
                        <span class="swp-prize-count">${count} (${countPercentage}%)</span>
                    </div>
                    <div class="swp-prize-bar">
                        <div class="swp-prize-bar-fill" style="width: ${percentage}%;"></div>
                    </div>
                </div>
            `;

                $prizeBars.append(barItem);
            });
        }

        // Show search loading indicator
        $('<style>')
            .prop('type', 'text/css')
            .html(`
            .swp-search-input.searching {
                background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="%23646970" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></svg>');
                background-repeat: no-repeat;
                background-position: right 12px center;
                background-size: 16px;
            }
        `)
            .appendTo('head');

        // ==========================================================================
        // EXPORT FILTERED FUNCTIONALITY
        // ==========================================================================

        function updateExportFilteredButton(filteredCount) {
            const $button = $('#swp-export-filtered');
            if (filteredCount > 0) {
                $button.prop('disabled', false);
                $button.find('.dashicons').removeClass('dashicons-update').addClass('dashicons-download');
                $button.html('<span class="dashicons dashicons-download"></span> Export Filtered CSV (' + filteredCount + ')');
            } else {
                $button.prop('disabled', true);
                $button.html('<span class="dashicons dashicons-download"></span> Export Filtered CSV');
            }
        }

        // ==========================================================================
        // DELETE FUNCTIONALITY
        // ==========================================================================

        // Select all checkbox functionality
        $('#swp-select-all').on('change', function () {
            const isChecked = $(this).is(':checked');
            $('.swp-entry-checkbox').prop('checked', isChecked);
            updateBulkActions();
        });

        // Individual checkbox change
        $(document).on('change', '.swp-entry-checkbox', function () {
            updateBulkActions();
        });

        // Update bulk actions visibility and count
        function updateBulkActions() {
            const checkedBoxes = $('.swp-entry-checkbox:checked');
            const $bulkActions = $('.swp-bulk-actions');
            const $selectedCount = $('.swp-selected-count');

            if (checkedBoxes.length > 0) {
                $bulkActions.show();
                $selectedCount.text(checkedBoxes.length + ' selected');
            } else {
                $bulkActions.hide();
            }

            // Update select all checkbox state
            const totalCheckboxes = $('.swp-entry-checkbox').length;
            const checkedCount = checkedBoxes.length;

            if (checkedCount === 0) {
                $('#swp-select-all').prop('indeterminate', false).prop('checked', false);
            } else if (checkedCount === totalCheckboxes) {
                $('#swp-select-all').prop('indeterminate', false).prop('checked', true);
            } else {
                $('#swp-select-all').prop('indeterminate', true);
            }
        }

        // Apply bulk action
        $('#swp-apply-bulk-action').on('click', function () {
            const action = $('#swp-bulk-action-select').val();
            const checkedBoxes = $('.swp-entry-checkbox:checked');

            if (!action) {
                alert('Please select a bulk action.');
                return;
            }

            if (checkedBoxes.length === 0) {
                alert('Please select at least one entry.');
                return;
            }

            if (action === 'delete') {
                const entryIds = checkedBoxes.map(function () {
                    return $(this).val();
                }).get();

                if (confirm('Are you sure you want to delete ' + entryIds.length + ' selected entry(ies)? This action cannot be undone.')) {
                    bulkDeleteEntries(entryIds);
                }
            }
        });

        // Single entry delete
        $(document).on('click', '.swp-delete-entry', function () {
            const $button = $(this);
            const entryId = $button.data('entry-id');
            const userName = $button.data('user-name');

            if (confirm('Are you sure you want to delete the entry for ' + userName + '? This action cannot be undone.')) {
                deleteEntry(entryId, $button);
            }
        });

        // Delete single entry function
        function deleteEntry(entryId, $button) {
            $button.prop('disabled', true).html('<span class="dashicons dashicons-update spin"></span> Deleting...');

            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'swp_delete_entry',
                    nonce: '<?php echo esc_attr(wp_create_nonce('swp_delete_nonce')); ?>',
                    entry_id: entryId
                },
                success: function (response) {
                    if (response.success) {
                        // Remove the row with animation
                        $button.closest('tr').fadeOut(300, function () {
                            $(this).remove();
                            updateBulkActions();
                        });

                        // Show success message
                        showNotice('Entry deleted successfully!', 'success');
                    } else {
                        alert('Error: ' + (response.data.message || 'Unknown error'));
                        $button.prop('disabled', false).html('<span class="dashicons dashicons-trash"></span> Delete');
                    }
                },
                error: function () {
                    alert('An error occurred. Please try again.');
                    $button.prop('disabled', false).html('<span class="dashicons dashicons-trash"></span> Delete');
                }
            });
        }

        // Bulk delete entries function
        function bulkDeleteEntries(entryIds) {
            const $button = $('#swp-apply-bulk-action');
            $button.prop('disabled', true).text('Deleting...');

            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'swp_bulk_delete_entries',
                    nonce: '<?php echo esc_attr(wp_create_nonce('swp_bulk_delete_nonce')); ?>',
                    entry_ids: entryIds
                },
                success: function (response) {
                    if (response.success) {
                        // Remove selected rows with animation
                        $('.swp-entry-checkbox:checked').each(function () {
                            $(this).closest('tr').fadeOut(300, function () {
                                $(this).remove();
                            });
                        });

                        // Hide bulk actions
                        $('.swp-bulk-actions').hide();
                        $('#swp-select-all').prop('checked', false);

                        // Show success message
                        showNotice(response.data.message, 'success');
                    } else {
                        alert('Error: ' + (response.data.message || 'Unknown error'));
                    }

                    $button.prop('disabled', false).text('Apply');
                },
                error: function () {
                    alert('An error occurred. Please try again.');
                    $button.prop('disabled', false).text('Apply');
                }
            });
        }

        // Show notice function
        function showNotice(message, type) {
            const $notice = $('<div class="notice notice-' + type + ' is-dismissible"><p>' + message + '</p></div>');
            $('.swp-entries-page h1').after($notice);

            // Auto-dismiss after 3 seconds
            setTimeout(function () {
                $notice.fadeOut(300, function () {
                    $(this).remove();
                });
            }, 3000);
        }
    });
</script>