<?php
/**
 * Template Name: TEC Create Event
 * Description: Integrated TEC Community Events creation page for HVAC trainers
 */

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

// Check if user is logged in and has trainer capabilities
if (!is_user_logged_in() || !current_user_can('publish_tribe_events')) {
    wp_redirect(home_url('/training-login/'));
    exit;
}

get_header();
?>

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

.hvac-tec-wrapper .hvac-page-header {
    margin-bottom: 30px;
}

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

.hvac-page-description {
    color: #666;
    font-size: 16px;
    margin-bottom: 30px;
}

/* Style the TEC form to match HVAC design */
.hvac-tec-wrapper .tribe-community-events {
    background: #fff;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.hvac-tec-wrapper .tribe-section {
    margin-bottom: 25px;
}

.hvac-tec-wrapper .tribe-section-label {
    font-weight: 600;
    color: #333;
    margin-bottom: 8px;
}

.hvac-tec-wrapper input[type="text"],
.hvac-tec-wrapper input[type="email"],
.hvac-tec-wrapper input[type="url"],
.hvac-tec-wrapper input[type="tel"],
.hvac-tec-wrapper input[type="number"],
.hvac-tec-wrapper textarea,
.hvac-tec-wrapper select {
    width: 100%;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
}

.hvac-tec-wrapper input[type="submit"] {
    background: #0073aa;
    color: white;
    padding: 12px 30px;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.3s;
}

.hvac-tec-wrapper input[type="submit"]:hover {
    background: #005a87;
}

/* Quick action buttons */
.hvac-quick-actions {
    display: flex;
    gap: 15px;
    margin-bottom: 20px;
}

.hvac-quick-actions .button {
    padding: 8px 16px;
    background: #f7f7f7;
    border: 1px solid #ddd;
    border-radius: 4px;
    text-decoration: none;
    color: #333;
    transition: all 0.3s;
}

.hvac-quick-actions .button:hover {
    background: #0073aa;
    color: white;
    border-color: #0073aa;
}

.hvac-quick-actions .button.active {
    background: #0073aa;
    color: white;
    border-color: #0073aa;
}
</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>Create New Training Event</h1>
        <p class="hvac-page-description">
            Share your expertise by creating a training event. Fill out the details below to publish your event to the HVAC community.
        </p>
    </div>
    
    <div class="hvac-quick-actions">
        <a href="<?php echo home_url('/trainer/events/my-events/'); ?>" class="button">My Events</a>
        <a href="<?php echo home_url('/trainer/events/create/'); ?>" class="button active">Create Event</a>
        <a href="<?php echo home_url('/trainer/dashboard/'); ?>" class="button">Dashboard</a>
    </div>
    
    <div class="hvac-tec-form-container">
        <?php
        // Use iframe to embed TEC form to avoid conflicts
        $tec_url = home_url('/events/network/add/');
        ?>
        <iframe 
            src="<?php echo esc_url($tec_url); ?>" 
            width="100%" 
            height="1200" 
            frameborder="0"
            id="tec-create-frame"
            style="width: 100%; min-height: 1200px; border: none;">
        </iframe>
    </div>
</div>

<script>
jQuery(document).ready(function($) {
    // Auto-resize iframe based on content
    function resizeIframe() {
        var iframe = document.getElementById('tec-create-frame');
        if (iframe) {
            try {
                // Try to access iframe content (will fail for cross-origin)
                var height = iframe.contentWindow.document.body.scrollHeight;
                iframe.style.height = height + 'px';
            } catch(e) {
                // Cross-origin, use default height
                console.log('Using default iframe height');
            }
        }
    }
    
    // Check for messages from iframe
    window.addEventListener('message', function(e) {
        if (e.data.type === 'event-created' && e.data.eventId) {
            // Redirect to edit page or success page
            window.location.href = '/trainer/events/edit/' + e.data.eventId + '/?created=1';
        }
    });
    
    // Initial resize
    $('#tec-create-frame').on('load', resizeIframe);
});
</script>

<?php
get_footer();
?>