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

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

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

// Get current user ID
$current_user_id = get_current_user_id();
$trainer_profile = get_user_meta($current_user_id, 'hvac_trainer_profile', true);

// Get Events
global $wpdb;
$events_query = $wpdb->prepare("
    SELECT DISTINCT p.ID, p.post_title 
    FROM {$wpdb->posts} p
    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 = $wpdb->get_results($events_query);

// Check for selected event
$selected_event_id = isset($_GET['event_id']) ? absint($_GET['event_id']) : 0;
$attendees = array();

if ($selected_event_id) {
    // Get attendees for the selected event
    $attendees_query = $wpdb->prepare("
        SELECT a.*, c.certificate_number 
        FROM {$wpdb->prefix}hvac_attendees a
        LEFT JOIN {$wpdb->prefix}hvac_certificates c ON a.id = c.attendee_id AND c.event_id = %d
        WHERE a.event_id = %d
        ORDER BY a.name ASC
    ", $selected_event_id, $selected_event_id);
    
    $attendees = $wpdb->get_results($attendees_query);
}
?>

<div class="hvac-generate-certificates-content">
    <div class="hvac-page-header">
        <h1><?php _e('Generate Certificates', 'hvac-community-events'); ?></h1>
        <p><?php _e('Select an event and generate certificates for attendees.', 'hvac-community-events'); ?></p>
    </div>
    
    <!-- Event Selection -->
    <div class="hvac-event-selection">
        <h2><?php _e('Select Event', '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 else: ?>
            <form method="get" action="" class="hvac-event-select-form">
                <div class="hvac-form-group">
                    <label for="event_id"><?php _e('Choose Event:', 'hvac-community-events'); ?></label>
                    <select name="event_id" id="event_id" onchange="this.form.submit()">
                        <option value=""><?php _e('-- Select an Event --', 'hvac-community-events'); ?></option>
                        <?php foreach ($events as $event): ?>
                            <option value="<?php echo esc_attr($event->ID); ?>" <?php selected($selected_event_id, $event->ID); ?>>
                                <?php echo esc_html($event->post_title); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </form>
        <?php endif; ?>
    </div>
    
    <?php if ($selected_event_id && !empty($attendees)): ?>
        <!-- Attendee List -->
        <div class="hvac-attendees-section">
            <h2><?php _e('Event Attendees', 'hvac-community-events'); ?></h2>
            
            <form id="hvac-generate-certificates-form" method="post">
                <?php wp_nonce_field('hvac_generate_certificates', 'hvac_generate_nonce'); ?>
                <input type="hidden" name="event_id" value="<?php echo esc_attr($selected_event_id); ?>">
                
                <div class="hvac-actions-bar">
                    <button type="button" id="select-all" class="hvac-button hvac-button-secondary">
                        <?php _e('Select All', 'hvac-community-events'); ?>
                    </button>
                    <button type="button" id="deselect-all" class="hvac-button hvac-button-secondary">
                        <?php _e('Deselect All', 'hvac-community-events'); ?>
                    </button>
                    <button type="submit" name="generate_certificates" class="hvac-button hvac-button-primary">
                        <?php _e('Generate Selected Certificates', 'hvac-community-events'); ?>
                    </button>
                </div>
                
                <div class="hvac-table-wrapper">
                    <table class="hvac-attendees-table">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="check-all"></th>
                                <th><?php _e('Name', 'hvac-community-events'); ?></th>
                                <th><?php _e('Email', 'hvac-community-events'); ?></th>
                                <th><?php _e('Certificate Status', 'hvac-community-events'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($attendees as $attendee): ?>
                                <tr>
                                    <td>
                                        <input type="checkbox" name="attendee_ids[]" 
                                               value="<?php echo esc_attr($attendee->id); ?>"
                                               <?php echo $attendee->certificate_number ? 'disabled' : ''; ?>>
                                    </td>
                                    <td><?php echo esc_html($attendee->name); ?></td>
                                    <td><?php echo esc_html($attendee->email); ?></td>
                                    <td>
                                        <?php if ($attendee->certificate_number): ?>
                                            <span class="hvac-status hvac-status-generated">
                                                <?php echo esc_html__('Generated', 'hvac-community-events'); ?> 
                                                (<?php echo esc_html($attendee->certificate_number); ?>)
                                            </span>
                                        <?php else: ?>
                                            <span class="hvac-status hvac-status-pending">
                                                <?php _e('Not Generated', 'hvac-community-events'); ?>
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </form>
        </div>
    <?php elseif ($selected_event_id && empty($attendees)): ?>
        <div class="hvac-notice hvac-notice-warning">
            <p><?php _e('No attendees found for this event. Add attendees to generate certificates.', 'hvac-community-events'); ?></p>
            <a href="/trainer/event/manage/?event_id=<?php echo $selected_event_id; ?>" class="hvac-button hvac-button-primary">
                <?php _e('Manage Event', 'hvac-community-events'); ?>
            </a>
        </div>
    <?php endif; ?>
</div>

<script>
jQuery(document).ready(function($) {
    // Select all checkbox
    $('#check-all, #select-all').on('click', function() {
        $('.hvac-attendees-table input[type="checkbox"]:not(:disabled)').prop('checked', true);
    });
    
    // Deselect all
    $('#deselect-all').on('click', function() {
        $('.hvac-attendees-table input[type="checkbox"]').prop('checked', false);
    });
});
</script>