<?php
if (!defined('ABSPATH')) {
    exit;
}
$rows = isset($rows) && is_array($rows) ? $rows : [];
$events_url = isset($events_url) ? (string) $events_url : $this->get_profile_route_default_url('events');
$ui_labels = isset($ui_labels) && is_array($ui_labels) ? $ui_labels : [];
$profile_key = sanitize_key((string) ($ui_labels['profile_key'] ?? ''));
$attending_title = sanitize_text_field((string) ($ui_labels['attending_title'] ?? __('Saved Listings', 'nexdirectory')));
$attending_description = sanitize_text_field((string) ($ui_labels['attending_description'] ?? __('Track your saved listings in one place.', 'nexdirectory')));
$attending_empty_heading = sanitize_text_field((string) ($ui_labels['attending_empty_heading'] ?? __('No listings yet', 'nexdirectory')));
$attending_empty_description = sanitize_text_field((string) ($ui_labels['attending_empty_description'] ?? __('You have not saved any listings yet.', 'nexdirectory')));
$attending_browse_cta = sanitize_text_field((string) ($ui_labels['attending_browse_cta'] ?? __('Browse Listings', 'nexdirectory')));
$singular_label = sanitize_text_field((string) ($ui_labels['singular'] ?? __('Listing', 'nexdirectory')));
$single_rsvp_cancel = sanitize_text_field((string) ($ui_labels['single_rsvp_cancel'] ?? __('Cancel RSVP', 'nexdirectory')));
$page_kicker = sanitize_text_field((string) $this->get_event_hero_kicker_text());
echo $this->render_notice();
echo $this->render_platform_breadcrumbs($attending_title); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
<section class="eep-page-banner">
    <p class="eep-page-kicker"><?php echo esc_html($page_kicker); ?></p>
    <h1><?php echo esc_html($attending_title); ?></h1>
    <p><?php echo esc_html($attending_description); ?></p>
</section>
<?php echo $this->render_account_tabs('my-tickets', $profile_key); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div class="eep-account-shell">
<div class="eep-my-events eep-my-tickets-wrap">
    <h3><?php echo esc_html($attending_title); ?></h3>
    <?php if (!empty($rows)) : ?>
        <div class="eep-table-wrap">
            <table class="eep-admin-table eep-responsive-table eep-my-tickets-table">
                <thead>
                    <tr>
                        <th><?php echo esc_html($singular_label); ?></th>
                        <th><?php esc_html_e('When', 'nexdirectory'); ?></th>
                        <th><?php esc_html_e('Location', 'nexdirectory'); ?></th>
                        <th><?php esc_html_e('Status', 'nexdirectory'); ?></th>
                        <th><?php esc_html_e('Actions', 'nexdirectory'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rows as $row) : ?>
                        <tr>
                            <td class="eep-my-event-title" data-label="<?php echo esc_attr($singular_label); ?>">
                                <a href="<?php echo esc_url((string) ($row['url'] ?? '#')); ?>"><?php echo esc_html((string) ($row['title'] ?? '')); ?></a>
                                <?php if (!empty($row['event_status_label'])) : ?>
                                    <br><small class="eep-meta"><?php echo esc_html((string) $row['event_status_label']); ?></small>
                                <?php endif; ?>
                            </td>
                            <td data-label="<?php esc_attr_e('When', 'nexdirectory'); ?>"><?php echo esc_html((string) ($row['start_label'] ?? '')); ?></td>
                            <td data-label="<?php esc_attr_e('Location', 'nexdirectory'); ?>"><?php echo esc_html((string) ($row['location'] ?? '')); ?></td>
                            <td data-label="<?php esc_attr_e('Status', 'nexdirectory'); ?>">
                                <span class="eep-status-pill <?php echo (string) ($row['status'] ?? '') === 'waitlist' ? 'eep-status-pending' : 'eep-status-publish'; ?>">
                                    <?php echo esc_html((string) ($row['status_label'] ?? '')); ?>
                                </span>
                            </td>
                            <td class="eep-actions eep-my-events-actions" data-label="<?php esc_attr_e('Actions', 'nexdirectory'); ?>">
                                <a class="eep-btn eep-btn-secondary eep-btn-sm" href="<?php echo esc_url((string) ($row['url'] ?? '#')); ?>"><?php echo esc_html(sprintf(__('Open %s', 'nexdirectory'), $singular_label)); ?></a>
                                <?php if (!empty($row['can_cancel'])) : ?>
                                    <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="eep-inline-form eep-rsvp-form">
                                        <input type="hidden" name="action" value="eep_rsvp_event">
                                        <input type="hidden" name="post_id" value="<?php echo esc_attr((string) ($row['post_id'] ?? '0')); ?>">
                                        <input type="hidden" name="intent" value="leave">
                                        <input type="hidden" name="eep_rsvp_nonce" value="<?php echo esc_attr((string) ($row['rsvp_nonce'] ?? '')); ?>">
                                        <button type="submit" class="eep-btn eep-btn-danger eep-btn-sm"><?php echo esc_html($single_rsvp_cancel); ?></button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php else : ?>
        <div class="eep-empty-state eep-my-tickets-empty-state">
            <h3><?php echo esc_html($attending_empty_heading); ?></h3>
            <p><?php echo esc_html($attending_empty_description); ?></p>
            <a class="eep-btn eep-force-accent-action" href="<?php echo esc_url($events_url); ?>"><?php echo esc_html($attending_browse_cta); ?></a>
        </div>
    <?php endif; ?>
</div>
</div>
