<?php
/**
 * Certificate Reports Content Template (without page wrapper)
 * Used by shortcode to output just the content
 *
 * @package HVAC_Community_Events
 * @subpackage Templates/Certificates
 */

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

// Ensure proper CSS classes for styling
echo '<div class="hvac-certificate-reports-content">';

// Get current user ID
$current_user_id = get_current_user_id();

// Initialize variables with defaults
$certificates = array();
$certificate_stats = array('total' => 0, 'active' => 0, 'revoked' => 0, 'emailed' => 0);
$events = array();
$filter_event = isset($_GET['filter_event']) ? absint($_GET['filter_event']) : 0;
$filter_status = isset($_GET['filter_status']) ? sanitize_text_field($_GET['filter_status']) : 'active';

// Removed problematic output buffering that interferes with WordPress header rendering
    // Get user's events directly from database to bypass TEC issues
    global $wpdb;
    
    $events_query = $wpdb->prepare("
        SELECT DISTINCT p.ID, p.post_title 
        FROM {$wpdb->posts} p
        LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
        WHERE p.post_type = 'tribe_events' 
        AND p.post_status = 'publish'
        AND p.post_author = %d
        ORDER BY p.post_date DESC
    ", $current_user_id);
    
    $events_results = $wpdb->get_results($events_query);
    
    if ($events_results) {
        foreach ($events_results as $event) {
            $events[$event->ID] = $event->post_title;
        }
    }
    
    // Get certificate stats
    $stats_query = $wpdb->prepare("
        SELECT 
            COUNT(DISTINCT c.id) as total,
            COUNT(DISTINCT CASE WHEN c.status = 'active' THEN c.id END) as active,
            COUNT(DISTINCT CASE WHEN c.status = 'revoked' THEN c.id END) as revoked,
            COUNT(DISTINCT e.certificate_id) as emailed
        FROM {$wpdb->prefix}hvac_certificates c
        LEFT JOIN {$wpdb->prefix}hvac_certificate_emails e ON c.id = e.certificate_id
        WHERE c.trainer_id = %d
    ", $current_user_id);
    
    $stats = $wpdb->get_row($stats_query);
    if ($stats) {
        $certificate_stats = array(
            'total' => (int)$stats->total,
            'active' => (int)$stats->active,
            'revoked' => (int)$stats->revoked,
            'emailed' => (int)$stats->emailed
        );
    }
    
    // Build certificate query
    $cert_query = "
        SELECT DISTINCT c.*, a.name as attendee_name, a.email as attendee_email, 
               e.post_title as event_name, pm.meta_value as event_date
        FROM {$wpdb->prefix}hvac_certificates c
        LEFT JOIN {$wpdb->prefix}hvac_attendees a ON c.attendee_id = a.id
        LEFT JOIN {$wpdb->posts} e ON c.event_id = e.ID
        LEFT JOIN {$wpdb->postmeta} pm ON e.ID = pm.post_id AND pm.meta_key = '_EventStartDate'
        WHERE c.trainer_id = %d
    ";
    
    $query_params = array($current_user_id);
    
    // Apply filters
    if ($filter_event > 0) {
        $cert_query .= " AND c.event_id = %d";
        $query_params[] = $filter_event;
    }
    
    if ($filter_status && $filter_status !== 'all') {
        $cert_query .= " AND c.status = %s";
        $query_params[] = $filter_status;
    }
    
    $cert_query .= " ORDER BY c.date_generated DESC LIMIT 100";
    
    $certificates = $wpdb->get_results($wpdb->prepare($cert_query, $query_params));
?>

<div class="hvac-certificate-reports-content">
    <div class="hvac-page-header">
        <h1><?php _e('Certificate Reports', 'hvac-community-events'); ?></h1>
        <p><?php _e('View and manage all certificates you\'ve generated for event attendees.', 'hvac-community-events'); ?></p>
    </div>
    
    <!-- Certificate Statistics -->
    <div class="hvac-certificate-stats">
        <h2><?php _e('Certificate Statistics', 'hvac-community-events'); ?></h2>
        <div class="hvac-stats-grid">
            <div class="hvac-stat-card">
                <div class="hvac-stat-label"><?php _e('Total Certificates', 'hvac-community-events'); ?></div>
                <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['total']); ?></div>
            </div>
            <div class="hvac-stat-card">
                <div class="hvac-stat-label"><?php _e('Active Certificates', 'hvac-community-events'); ?></div>
                <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['active']); ?></div>
            </div>
            <div class="hvac-stat-card">
                <div class="hvac-stat-label"><?php _e('Revoked Certificates', 'hvac-community-events'); ?></div>
                <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['revoked']); ?></div>
            </div>
            <div class="hvac-stat-card">
                <div class="hvac-stat-label"><?php _e('Emailed Certificates', 'hvac-community-events'); ?></div>
                <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['emailed']); ?></div>
            </div>
        </div>
    </div>
    
    <!-- Certificate Filters -->
    <div class="hvac-certificate-filters">
        <h2><?php _e('Certificate Filters', 'hvac-community-events'); ?></h2>
        <form method="get" action="" class="hvac-filter-form">
            <div class="hvac-filter-row">
                <div class="hvac-filter-group">
                    <label for="filter_event"><?php _e('Event:', 'hvac-community-events'); ?></label>
                    <select name="filter_event" id="filter_event">
                        <option value="0"><?php _e('All Events', 'hvac-community-events'); ?></option>
                        <?php foreach ($events as $event_id => $event_name): ?>
                            <option value="<?php echo esc_attr($event_id); ?>" <?php selected($filter_event, $event_id); ?>>
                                <?php echo esc_html($event_name); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="hvac-filter-group">
                    <label for="filter_status"><?php _e('Status:', 'hvac-community-events'); ?></label>
                    <select name="filter_status" id="filter_status">
                        <option value="active" <?php selected($filter_status, 'active'); ?>><?php _e('Active Only', 'hvac-community-events'); ?></option>
                        <option value="all" <?php selected($filter_status, 'all'); ?>><?php _e('All Certificates', 'hvac-community-events'); ?></option>
                        <option value="revoked" <?php selected($filter_status, 'revoked'); ?>><?php _e('Revoked Only', 'hvac-community-events'); ?></option>
                    </select>
                </div>
                
                <div class="hvac-filter-group">
                    <button type="submit" class="hvac-button hvac-button-primary"><?php _e('Apply Filters', 'hvac-community-events'); ?></button>
                </div>
            </div>
        </form>
    </div>
    
    <!-- Certificate Listing -->
    <div class="hvac-certificate-listing">
        <h2><?php _e('Certificate Listing', 'hvac-community-events'); ?></h2>
        
        <?php if (empty($events)): ?>
            <div class="hvac-notice hvac-notice-info">
                <p><?php _e('You don\'t have any events yet. Create your first event to start generating certificates.', 'hvac-community-events'); ?></p>
                <a href="/trainer/event/manage/" class="hvac-button hvac-button-primary"><?php _e('Create Event', 'hvac-community-events'); ?></a>
            </div>
        <?php elseif (empty($certificates)): ?>
            <div class="hvac-notice hvac-notice-info">
                <p><?php _e('No certificates found matching your criteria.', 'hvac-community-events'); ?></p>
                <a href="/trainer/generate-certificates/" class="hvac-button hvac-button-primary"><?php _e('Generate Certificates', 'hvac-community-events'); ?></a>
            </div>
        <?php else: ?>
            <div class="hvac-table-wrapper">
                <table class="hvac-certificates-table">
                    <thead>
                        <tr>
                            <th><?php _e('Certificate ID', 'hvac-community-events'); ?></th>
                            <th><?php _e('Attendee', 'hvac-community-events'); ?></th>
                            <th><?php _e('Event', 'hvac-community-events'); ?></th>
                            <th><?php _e('Date Generated', 'hvac-community-events'); ?></th>
                            <th><?php _e('Status', 'hvac-community-events'); ?></th>
                            <th><?php _e('Actions', 'hvac-community-events'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($certificates as $certificate): ?>
                            <tr>
                                <td><?php echo esc_html($certificate->certificate_number); ?></td>
                                <td>
                                    <?php echo esc_html($certificate->attendee_name); ?><br>
                                    <small><?php echo esc_html($certificate->attendee_email); ?></small>
                                </td>
                                <td><?php echo esc_html($certificate->event_name); ?></td>
                                <td><?php echo esc_html(date('M j, Y', strtotime($certificate->date_generated))); ?></td>
                                <td>
                                    <span class="hvac-status hvac-status-<?php echo esc_attr($certificate->status); ?>">
                                        <?php echo esc_html(ucfirst($certificate->status)); ?>
                                    </span>
                                </td>
                                <td>
                                    <div class="hvac-actions">
                                        <a href="#" class="hvac-action-link hvac-view-certificate" 
                                           data-certificate-id="<?php echo esc_attr($certificate->id); ?>">
                                            <?php _e('View', 'hvac-community-events'); ?>
                                        </a>
                                        <?php if ($certificate->status === 'active'): ?>
                                            <a href="#" class="hvac-action-link hvac-email-certificate" 
                                               data-certificate-id="<?php echo esc_attr($certificate->id); ?>">
                                                <?php _e('Email', 'hvac-community-events'); ?>
                                            </a>
                                            <a href="#" class="hvac-action-link hvac-revoke-certificate" 
                                               data-certificate-id="<?php echo esc_attr($certificate->id); ?>">
                                                <?php _e('Revoke', 'hvac-community-events'); ?>
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>