<?php
/**
 * Admin Booking Statistics Page Template
 *
 * @since   PBOOKING_SINCE
 * @package Pulse_Booking
 *
 * @var array $statistics  Booking statistics
 */

// Security check
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$nemtly_booking_statistics = $statistics ?? [];
?>

<div class="wrap nemtly-booking-admin">
    <h1 class="wp-heading-inline">
        <?php esc_html_e( 'Booking Statistics', 'nemtly-booking' ); ?>
    </h1>

    <div class="nemtly-booking-stats">
        <!-- Total Bookings -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'Total Bookings', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                <?php echo absint( $nemtly_booking_statistics['total_bookings'] ?? 0 ); ?>
            </div>
        </div>

        <!-- Pending Bookings -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'Pending Bookings', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                <?php echo absint( $nemtly_booking_statistics['pending_bookings'] ?? 0 ); ?>
            </div>
        </div>

        <!-- Confirmed Bookings -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'Confirmed Bookings', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                <?php echo absint( $nemtly_booking_statistics['confirmed_bookings'] ?? 0 ); ?>
            </div>
        </div>

        <!-- Cancelled Bookings -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'Cancelled Bookings', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                <?php echo absint( $nemtly_booking_statistics['cancelled_bookings'] ?? 0 ); ?>
            </div>
        </div>

        <!-- Today's Bookings -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'Today\'s Bookings', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                <?php echo absint( $nemtly_booking_statistics['todays_bookings'] ?? 0 ); ?>
            </div>
        </div>

        <!-- This Week's Bookings -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'This Week', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                <?php echo absint( $nemtly_booking_statistics['this_week_bookings'] ?? 0 ); ?>
            </div>
        </div>

        <!-- This Month's Bookings -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'This Month', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                <?php echo absint( $nemtly_booking_statistics['this_month_bookings'] ?? 0 ); ?>
            </div>
        </div>

        <!-- Revenue (if applicable) -->
        <div class="nemtly-stat-card">
            <h3><?php esc_html_e( 'Revenue', 'nemtly-booking' ); ?></h3>
            <div class="nemtly-stat-number">
                $<?php echo number_format( $nemtly_booking_statistics['total_revenue'] ?? 0, 2 ); ?>
            </div>
        </div>
    </div>

    <!-- Additional Statistics -->
    <div class="nemtly-admin-section">
        <h2><?php esc_html_e( 'Popular Events', 'nemtly-booking' ); ?></h2>

        <?php if ( ! empty( $nemtly_booking_statistics['popular_events'] ) ) : ?>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php esc_html_e( 'Event', 'nemtly-booking' ); ?></th>
                        <th><?php esc_html_e( 'Total Bookings', 'nemtly-booking' ); ?></th>
                        <th><?php esc_html_e( 'Confirmed', 'nemtly-booking' ); ?></th>
                        <th><?php esc_html_e( 'Pending', 'nemtly-booking' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $nemtly_booking_statistics['popular_events'] as $nemtly_booking_event_stat ) : ?>
                        <tr>
                            <td>
                                <strong><?php echo esc_html( $nemtly_booking_event_stat['title'] ?? 'Unknown Event' ); ?></strong>
                            </td>
                            <td><?php echo absint( $nemtly_booking_event_stat['total'] ?? 0 ); ?></td>
                            <td><?php echo absint( $nemtly_booking_event_stat['confirmed'] ?? 0 ); ?></td>
                            <td><?php echo absint( $nemtly_booking_event_stat['pending'] ?? 0 ); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else : ?>
            <p><?php esc_html_e( 'No event statistics available yet.', 'nemtly-booking' ); ?></p>
        <?php endif; ?>
    </div>

    <!-- Recent Activity -->
    <div class="nemtly-admin-section">
        <h2><?php esc_html_e( 'Recent Bookings', 'nemtly-booking' ); ?></h2>

        <?php if ( ! empty( $nemtly_booking_statistics['recent_bookings'] ) ) : ?>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php esc_html_e( 'Customer', 'nemtly-booking' ); ?></th>
                        <th><?php esc_html_e( 'Event', 'nemtly-booking' ); ?></th>
                        <th><?php esc_html_e( 'Status', 'nemtly-booking' ); ?></th>
                        <th><?php esc_html_e( 'Date', 'nemtly-booking' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $nemtly_booking_statistics['recent_bookings'] as $nemtly_booking_booking ) : ?>
                        <tr>
                            <td><?php echo esc_html( $nemtly_booking_booking['attendee_name'] ?? 'Unknown' ); ?></td>
                            <td><?php echo esc_html( $nemtly_booking_booking['event_title'] ?? 'Unknown Event' ); ?></td>
                            <td>
                                <span
                                    class="nb-status-badge <?php echo esc_attr( 'nb-status-' . ( $nemtly_booking_booking['status'] ?? 'unknown' ) ); ?>">
                                    <?php echo esc_html( ucfirst( $nemtly_booking_booking['status'] ?? 'Unknown' ) ); ?>
                                </span>
                            </td>
                            <td><?php echo esc_html( gmdate( 'Y-m-d H:i', strtotime( $nemtly_booking_booking['created_at'] ?? 'now' ) ) ); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else : ?>
            <p><?php esc_html_e( 'No recent bookings available.', 'nemtly-booking' ); ?></p>
        <?php endif; ?>
    </div>

    <!-- Quick Actions -->
    <div class="nemtly-admin-section">
        <h2><?php esc_html_e( 'Quick Actions', 'nemtly-booking' ); ?></h2>
        <div class="nemtly-quick-actions">
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=nemtly-booking-bookings' ) ); ?>"
                class="button button-primary">
                <?php esc_html_e( 'View All Bookings', 'nemtly-booking' ); ?>
            </a>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=nemtly-booking' ) ); ?>" class="button">
                <?php esc_html_e( 'Manage Events', 'nemtly-booking' ); ?>
            </a>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=nemtly-booking-settings' ) ); ?>" class="button">
                <?php esc_html_e( 'Settings', 'nemtly-booking' ); ?>
            </a>
        </div>
    </div>
</div>