<?php
/**
 * The final, intelligent, and reusable template for all admin tables.
 * It now renders the full interactive "click-to-edit" UI for any 'status' column.
 */
if (!defined('ABSPATH'))
    exit;

$row_count = 1;
?>
<div class="module-wrapper">
    <section class="module module-headings">
        <div class="module-inner">
            <div class="module-heading row">
                <div class="col-md-6">
                    <h3 class="module-title"><?php echo esc_html($page_title); ?></h3>
                </div>

                <?php // --- THIS IS THE NEW CONDITIONAL LOGIC --- ?>
                <div class="col-md-6 text-right">
                    <?php if (isset($page_slug) && $page_slug === 'em-visitors-page'): ?>

                        <?php // --- HEADER FOR VISITORS PAGE --- ?>
                        <select name="select_event" id="select_event">
                            <option value="all"><?php _e('All Events', 'event-monster'); ?></option>
                            <?php if (!empty($all_events)): ?>
                                <?php foreach ($all_events as $event): ?>
                                    <option value="<?php echo esc_attr($event->ID); ?>">
                                        <?php echo esc_html($event->post_title); ?>
                                    </option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                        <button class="btn btn-success" id="download_visitor">
                            <i class="fa fa-download"></i> <?php esc_html_e('Download Visitor List', 'event-monster'); ?>
                        </button>

                    <?php elseif (isset($page_slug) && $page_slug === 'em-tickets-page'): ?>

                        <?php // --- HEADER FOR TICKETS PAGE --- ?>
                        <button class="btn btn-success" id="add_new_ticket_btn" data-toggle="modal"
                            data-target="#modal-ticket-manager">
                            <i class="fa fa-plus"></i> <?php _e('Add Ticket', 'event-monster'); ?>
                        </button>
                        <?php // --- HEADER FOR COUPON PAGE --- ?>
                    <?php elseif (isset($page_slug) && $page_slug === 'em-coupons-page'): ?>
                        <button class="btn btn-success" id="add_new_coupon_btn"><i class="fa fa-plus"></i>
                            <?php _e('Add Coupon', 'event-monster'); ?></button>

                    <?php endif; ?>
                    <?php // You can add more 'elseif' blocks for coupons and payments here ?>
                </div>
            </div>
            <div class="module-content">
                <div class="em-bg-white em-rounded-xl em-shadow-sm em-border em-border-gray-200 em-overflow-hidden em-p-4">
                    <table id="datatables-1" class="em-w-full em-bg-white">
                        <thead class="em-bg-gradient-to-r em-from-gray-50 em-to-gray-100 em-border-b em-border-gray-200">
                            <tr>
                                <?php foreach ($columns as $slug => $name): ?>
                                    <?php if ($slug === 'cb'): ?>
                                        <th class="em-px-6 em-py-4 em-text-left em-text-xs em-font-semibold em-text-gray-600 em-uppercase em-tracking-wider em-w-12">
                                            <input type="checkbox" id="select-all-checkbox" class="em-w-4 em-h-4 em-text-indigo-600 em-bg-gray-100 em-border-gray-300 em-rounded focus:em-ring-indigo-500 focus:em-ring-2" />
                                        </th>
                                    <?php else: ?>
                                        <th class="em-px-6 em-py-4 em-text-left em-text-xs em-font-semibold em-text-gray-600 em-uppercase em-tracking-wider em-border-b em-border-gray-200">
                                            <?php echo esc_html($name); ?>
                                        </th>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody class="em-bg-white em-divide-y em-divide-gray-200">
                            <?php if (!empty($table_data)): ?>
                                <?php foreach ($table_data as $row): ?>
                                    <?php $row_array = (array) $row; ?>
                                    <tr id="record-<?php echo esc_attr($row_array['id']); ?>" class="em-hover:bg-gray-50 em-transition-colors em-duration-200">
                                        <?php foreach ($columns as $slug => $name): ?>
                                            <td class="em-px-6 em-py-4 em-whitespace-nowrap em-text-sm">
                                                <?php
                                                // --- Special Column Handlers ---
                                                if ($slug === 'cb') {
                                                    echo '<input type="checkbox" class="table-item-checkbox em-w-4 em-h-4 em-text-indigo-600 em-bg-gray-100 em-border-gray-300 em-rounded focus:em-ring-indigo-500 focus:em-ring-2" value="' . esc_attr($row_array['id']) . '" />';
                                                } else if ($slug === 'number') {
                                                    echo '<span class="em-inline-flex em-items-center em-justify-center em-w-8 em-h-8 em-bg-indigo-100 em-text-indigo-800 em-text-xs em-font-medium em-rounded-full">' . $row_count++ . '</span>';
                                                }
                                                // **THIS IS THE NEW LOGIC FOR YOUR CUSTOM STATUS UI**
                                                else if ($slug === 'status' && isset($row_array['status'])) {
                                                    $current_status = $row_array['status'];
                                                    $item_id = $row_array['id'];

                                                    // Check if this is the Visitors page - only show edit functionality there
                                                    $is_visitors_page = (isset($page_slug) && $page_slug === 'em-visitors-page');

                                                    if ($is_visitors_page) {
                                                        // Full edit functionality for Visitors page
                                                        ?>
                                                            <div class="em-flex em-items-center em-gap-2">
                                                                <button class="status-edit-btn em-inline-flex em-items-center em-justify-center em-w-8 em-h-8 em-bg-indigo-100 hover:em-bg-indigo-200 em-text-indigo-600 em-rounded-full em-transition-colors em-duration-200"
                                                                    data-id="<?php echo esc_attr($item_id); ?>">
                                                                    <i class="fa fa-pencil-square em-text-xs"></i>
                                                                </button>
                                                                <span class="insert_status em-inline-flex em-items-center em-px-3 em-py-1 em-rounded-full em-text-xs em-font-medium <?php
                                                                    echo $current_status === 'Completed' ? 'em-bg-green-100 em-text-green-800' :
                                                                        ($current_status === 'Cancelled' ? 'em-bg-red-100 em-text-red-800' : 'em-bg-yellow-100 em-text-yellow-800');
                                                                ?>"
                                                                    id="span_status_<?php echo esc_attr($item_id); ?>">
                                                                    <?php echo esc_html($current_status); ?>
                                                                </span>
                                                                <select class="status-select-dropdown em-border em-border-gray-300 em-rounded-md em-px-3 em-py-1 em-text-sm focus:em-ring-2 focus:em-ring-indigo-500 focus:em-border-indigo-500"
                                                                    data-id="<?php echo esc_attr($item_id); ?>" style="display: none;">
                                                                    <option value="Pending" <?php selected($current_status, 'Pending'); ?>>
                                                                <?php _e('Pending', 'event-monster'); ?>
                                                                    </option>
                                                                    <option value="Completed" <?php selected($current_status, 'Completed'); ?>>
                                                                <?php _e('Completed', 'event-monster'); ?>
                                                                    </option>
                                                                    <option value="Cancelled" <?php selected($current_status, 'Cancelled'); ?>>
                                                                <?php _e('Cancelled', 'event-monster'); ?>
                                                                    </option>
                                                                </select>
                                                                <span class="close_status status-close-btn em-inline-flex em-items-center em-justify-center em-w-8 em-h-8 em-bg-red-100 hover:em-bg-red-200 em-text-red-600 em-rounded-full em-cursor-pointer em-transition-colors em-duration-200"
                                                                    data-id="<?php echo esc_attr($item_id); ?>" style="display:none;">
                                                                    <i class="fa fa-close em-text-xs"></i>
                                                                </span>
                                                                <div id="status-updating-msg-<?php echo esc_attr($item_id); ?>" class="em-flex em-items-center em-gap-2 em-text-indigo-600" style="display: none;">
                                                                    <i class="fa fa-cog fa-spin em-text-sm"></i>
                                                                    <span class="em-text-sm">Updating...</span>
                                                                </div>
                                                            </div>
                                                        <?php
                                                    } else {
                                                        // Read-only status badge for other pages (Tickets, Coupons, Payments)
                                                        ?>
                                                            <div class="em-flex em-items-center em-gap-2">
                                                                <span class="insert_status em-inline-flex em-items-center em-px-3 em-py-1 em-rounded-full em-text-xs em-font-medium <?php
                                                                    echo $current_status === 'Completed' ? 'em-bg-green-100 em-text-green-800' :
                                                                        ($current_status === 'Cancelled' ? 'em-bg-red-100 em-text-red-800' : 'em-bg-yellow-100 em-text-yellow-800');
                                                                ?>"
                                                                    id="span_status_<?php echo esc_attr($item_id); ?>">
                                                                    <?php echo esc_html($current_status); ?>
                                                                </span>
                                                            </div>
                                                        <?php
                                                    }
                                                } else if ($slug === 'actions' && isset($row_actions_template)) {
                                                    echo '<div class="em-flex em-items-center em-gap-2">';
                                                    include($row_actions_template);
                                                    echo '</div>';
                                                }
                                                // --- Default Column Handler ---
                                                else if (isset($row_array[$slug])) {
                                                    // Special handling for name_email column to allow line breaks
                                                    if ($slug === 'name_email') {
                                                        // Split name and email, then display with line break
                                                        $name_email_parts = explode(' (', $row_array[$slug]);
                                                        if (count($name_email_parts) === 2) {
                                                            $name = esc_html($name_email_parts[0]);
                                                            $email = esc_html(rtrim($name_email_parts[1], ')'));
                                                            echo '<div class="em-text-gray-900 em-font-medium">';
                                                            echo '<div>' . $name . '</div>';
                                                            echo '<div class="em-text-sm em-text-gray-500 em-italic">(' . $email . ')</div>';
                                                            echo '</div>';
                                                        } else {
                                                            echo '<span class="em-text-gray-900 em-font-medium">' . esc_html($row_array[$slug]) . '</span>';
                                                        }
                                                    } else {
                                                        echo '<span class="em-text-gray-900 em-font-medium">' . esc_html($row_array[$slug]) . '</span>';
                                                    }
                                                }
                                                ?>
                                            </td>
                                        <?php endforeach; ?>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="<?php echo count($columns); ?>" class="em-px-6 em-py-12 em-text-center">
                                        <div class="em-flex em-flex-col em-items-center em-justify-center em-text-gray-500">
                                            <svg class="em-w-12 em-h-12 em-text-gray-400 em-mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                                            </svg>
                                            <p class="em-text-lg em-font-medium em-text-gray-900 em-mb-1">No data found</p>
                                            <p class="em-text-sm em-text-gray-500">
                                                <?php printf(__('No %s found.', 'event-monster-premium'), strtolower($page_title)); ?>
                                            </p>
                                        </div>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                        <tfoot class="em-bg-gray-50 em-border-t em-border-gray-200">
                            <tr>
                                <?php foreach ($columns as $slug => $name): ?>
                                    <?php // When the loop is on the first column ('cb'), print the button. ?>
                                    <?php if ($slug === 'cb'): ?>
                                        <th class="em-px-6 em-py-4">
                                            <?php // Conditionally show the bulk delete button if the page needs it ?>
                                            <?php if (isset($page_slug) && in_array($page_slug, ['em-visitors-page', 'em-tickets-page', 'em-coupons-page', 'em-payments-page'])): ?>
                                                <button class="button button-danger em-inline-flex em-items-center em-px-4 em-py-2 em-bg-red-600 hover:em-bg-red-700 em-text-white em-text-sm em-font-medium em-rounded-md em-transition-colors em-duration-200" id="bulk-delete-button">
                                                    <i class="fa fa-trash em-mr-2"></i>
                                                    <?php esc_html_e('Delete', 'event-monster-premium'); ?>
                                                </button>
                                            <?php endif; ?>
                                        </th>
                                    <?php else: ?>
                                        <?php // For all other columns, print an empty header cell to maintain alignment. ?>
                                        <th class="em-px-6 em-py-4"></th>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </section>
</div>
