<?php
/**
 * Template Name: TEC My Events
 * Description: Integrated event management page showing trainer's events
 */

// Define constant to indicate we are in a page template
define('HVAC_IN_PAGE_TEMPLATE', true);

// Check if user is logged in
if (!is_user_logged_in()) {
    wp_redirect(home_url('/training-login/'));
    exit;
}

get_header();

$current_user_id = get_current_user_id();

// Get user's events
$args = [
    'post_type' => 'tribe_events',
    'author' => $current_user_id,
    'posts_per_page' => 20,
    'post_status' => ['publish', 'pending', 'draft', 'future'],
    'orderby' => 'date',
    'order' => 'DESC'
];

$events_query = new WP_Query($args);
?>

<style>
.hvac-tec-wrapper {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.hvac-page-header {
    margin-bottom: 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.hvac-tec-wrapper h1 {
    color: #1a1a1a;
    font-size: 32px;
    margin: 0;
}

.hvac-create-event-btn {
    background: #0073aa;
    color: white;
    padding: 12px 24px;
    border-radius: 4px;
    text-decoration: none;
    font-size: 16px;
    transition: background 0.3s;
}

.hvac-create-event-btn:hover {
    background: #005a87;
    color: white;
}

/* Events table */
.hvac-events-table {
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.hvac-events-table table {
    width: 100%;
    border-collapse: collapse;
}

.hvac-events-table th {
    background: #f7f7f7;
    padding: 15px;
    text-align: left;
    font-weight: 600;
    color: #333;
    border-bottom: 2px solid #ddd;
}

.hvac-events-table td {
    padding: 15px;
    border-bottom: 1px solid #eee;
}

.hvac-events-table tr:hover {
    background: #f9f9f9;
}

.event-title {
    font-weight: 600;
    color: #0073aa;
    text-decoration: none;
}

.event-title:hover {
    text-decoration: underline;
}

.event-status {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 3px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
}

.event-status.publish {
    background: #d4edda;
    color: #155724;
}

.event-status.pending {
    background: #fff3cd;
    color: #856404;
}

.event-status.draft {
    background: #e2e3e5;
    color: #383d41;
}

.event-actions {
    display: flex;
    gap: 10px;
}

.event-actions a {
    color: #666;
    text-decoration: none;
    padding: 4px 8px;
    border: 1px solid #ddd;
    border-radius: 3px;
    font-size: 14px;
    transition: all 0.3s;
}

.event-actions a:hover {
    background: #0073aa;
    color: white;
    border-color: #0073aa;
}

.event-actions a.delete {
    color: #dc3232;
    border-color: #dc3232;
}

.event-actions a.delete:hover {
    background: #dc3232;
    color: white;
}

.no-events {
    text-align: center;
    padding: 60px 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.no-events h2 {
    color: #666;
    font-size: 24px;
    margin-bottom: 10px;
}

.no-events p {
    color: #999;
    margin-bottom: 30px;
}

/* Stats cards */
.hvac-event-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    text-align: center;
}

.stat-card .stat-value {
    font-size: 32px;
    font-weight: bold;
    color: #0073aa;
    margin-bottom: 5px;
}

.stat-card .stat-label {
    color: #666;
    font-size: 14px;
}
</style>

<div class="hvac-tec-wrapper">
    <?php
    // Display trainer navigation menu
    if (class_exists('HVAC_Menu_System')) {
        HVAC_Menu_System::instance()->render_trainer_menu();
    }
    
    // Display breadcrumbs
    if (class_exists('HVAC_Breadcrumbs')) {
        HVAC_Breadcrumbs::instance()->render();
    }
    ?>
    
    <div class="hvac-page-header">
        <h1>My Training Events</h1>
        <a href="<?php echo home_url('/trainer/events/create/'); ?>" class="hvac-create-event-btn">+ Create New Event</a>
    </div>
    
    <?php
    // Get event statistics
    $published_count = count(get_posts([
        'post_type' => 'tribe_events',
        'author' => $current_user_id,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'fields' => 'ids'
    ]));
    
    $upcoming_count = count(get_posts([
        'post_type' => 'tribe_events',
        'author' => $current_user_id,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'fields' => 'ids',
        'meta_query' => [
            [
                'key' => '_EventStartDate',
                'value' => date('Y-m-d H:i:s'),
                'compare' => '>='
            ]
        ]
    ]));
    
    $draft_count = count(get_posts([
        'post_type' => 'tribe_events',
        'author' => $current_user_id,
        'post_status' => 'draft',
        'posts_per_page' => -1,
        'fields' => 'ids'
    ]));
    ?>
    
    <div class="hvac-event-stats">
        <div class="stat-card">
            <div class="stat-value"><?php echo $events_query->found_posts; ?></div>
            <div class="stat-label">Total Events</div>
        </div>
        <div class="stat-card">
            <div class="stat-value"><?php echo $published_count; ?></div>
            <div class="stat-label">Published</div>
        </div>
        <div class="stat-card">
            <div class="stat-value"><?php echo $upcoming_count; ?></div>
            <div class="stat-label">Upcoming</div>
        </div>
        <div class="stat-card">
            <div class="stat-value"><?php echo $draft_count; ?></div>
            <div class="stat-label">Drafts</div>
        </div>
    </div>
    
    <?php if ($events_query->have_posts()) : ?>
        
        <div class="hvac-events-table">
            <table>
                <thead>
                    <tr>
                        <th>Event Title</th>
                        <th>Date</th>
                        <th>Venue</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($events_query->have_posts()) : $events_query->the_post(); 
                        $event_id = get_the_ID();
                        $start_date = get_post_meta($event_id, '_EventStartDate', true);
                        $venue_id = get_post_meta($event_id, '_EventVenueID', true);
                        $venue_name = $venue_id ? get_the_title($venue_id) : get_post_meta($event_id, '_EventVenue', true);
                    ?>
                        <tr>
                            <td>
                                <a href="<?php echo get_permalink(); ?>" class="event-title" target="_blank">
                                    <?php the_title(); ?>
                                </a>
                            </td>
                            <td>
                                <?php echo $start_date ? date('M j, Y', strtotime($start_date)) : 'TBD'; ?>
                            </td>
                            <td>
                                <?php echo $venue_name ?: 'No venue set'; ?>
                            </td>
                            <td>
                                <span class="event-status <?php echo get_post_status(); ?>">
                                    <?php echo get_post_status(); ?>
                                </span>
                            </td>
                            <td>
                                <div class="event-actions">
                                    <a href="<?php echo home_url('/trainer/events/edit/' . $event_id . '/'); ?>">Edit</a>
                                    <a href="<?php echo get_permalink(); ?>" target="_blank">View</a>
                                    <a href="<?php echo home_url('/trainer/certificate-reports/?event_id=' . $event_id); ?>">Certificates</a>
                                </div>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
        
    <?php else : ?>
        
        <div class="no-events">
            <h2>No Events Yet</h2>
            <p>You haven't created any training events. Start sharing your expertise with the HVAC community!</p>
            <a href="<?php echo home_url('/trainer/events/create/'); ?>" class="hvac-create-event-btn">Create Your First Event</a>
        </div>
        
    <?php endif; ?>
    
    <?php wp_reset_postdata(); ?>
</div>

<?php
get_footer();
?>