<?php
/**
 * Template Name: TEC Edit Event
 * Description: Integrated TEC Community Events editing 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
if (!is_user_logged_in()) {
    wp_redirect(home_url('/training-login/'));
    exit;
}

get_header();

// Get event ID from URL
$event_id = isset($_GET['event_id']) ? intval($_GET['event_id']) : 0;
if (!$event_id) {
    // Try to get from URL path
    $url_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
    if (preg_match('/edit\/(\d+)/', $url_path, $matches)) {
        $event_id = intval($matches[1]);
    }
}

// Verify user can edit this event
$can_edit = false;
if ($event_id) {
    $event = get_post($event_id);
    if ($event && $event->post_type === 'tribe_events') {
        $can_edit = (current_user_can('edit_tribe_events') || $event->post_author == get_current_user_id());
    }
}
?>

<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;
}

.hvac-event-meta {
    background: #f7f7f7;
    padding: 15px;
    border-radius: 4px;
    margin-bottom: 20px;
}

.hvac-event-meta span {
    display: inline-block;
    margin-right: 20px;
    color: #666;
}

.hvac-event-meta strong {
    color: #333;
}

/* Style the TEC form */
.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-error-notice {
    background: #fff5f5;
    border-left: 4px solid #dc3232;
    padding: 15px;
    margin-bottom: 20px;
}

.hvac-success-notice {
    background: #f0f8ff;
    border-left: 4px solid #0073aa;
    padding: 15px;
    margin-bottom: 20px;
}

/* 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;
}

.hvac-quick-actions .button.danger {
    background: #dc3232;
    color: white;
    border-color: #dc3232;
}
</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>Edit Training Event</h1>
        <?php if ($event_id && $event) : ?>
            <p class="hvac-page-description">
                Editing: <strong><?php echo esc_html($event->post_title); ?></strong>
            </p>
        <?php endif; ?>
    </div>
    
    <?php if (isset($_GET['updated'])) : ?>
        <div class="hvac-success-notice">
            <p>✅ Event updated successfully!</p>
        </div>
    <?php endif; ?>
    
    <?php if ($event_id && $can_edit) : ?>
        
        <div class="hvac-event-meta">
            <span><strong>Status:</strong> <?php echo ucfirst($event->post_status); ?></span>
            <span><strong>Created:</strong> <?php echo date('M j, Y', strtotime($event->post_date)); ?></span>
            <span><strong>Last Modified:</strong> <?php echo date('M j, Y', strtotime($event->post_modified)); ?></span>
        </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">Create New</a>
            <a href="<?php echo get_permalink($event_id); ?>" class="button" target="_blank">View 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 edit form
            $tec_url = home_url('/events/network/edit/' . $event_id . '/');
            ?>
            <iframe 
                src="<?php echo esc_url($tec_url); ?>" 
                width="100%" 
                height="1200" 
                frameborder="0"
                id="tec-edit-frame"
                style="width: 100%; min-height: 1200px; border: none;">
            </iframe>
        </div>
        
    <?php elseif ($event_id && !$can_edit) : ?>
        
        <div class="hvac-error-notice">
            <p>❌ You don't have permission to edit this event.</p>
        </div>
        
        <div class="hvac-quick-actions">
            <a href="<?php echo home_url('/trainer/events/my-events/'); ?>" class="button">Back to My Events</a>
        </div>
        
    <?php else : ?>
        
        <div class="hvac-error-notice">
            <p>❌ No event specified or event not found.</p>
        </div>
        
        <div class="hvac-quick-actions">
            <a href="<?php echo home_url('/trainer/events/my-events/'); ?>" class="button">View My Events</a>
            <a href="<?php echo home_url('/trainer/events/create/'); ?>" class="button">Create New Event</a>
        </div>
        
    <?php endif; ?>
</div>

<script>
jQuery(document).ready(function($) {
    // Auto-resize iframe based on content
    function resizeIframe() {
        var iframe = document.getElementById('tec-edit-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-updated') {
            // Reload page with success message
            window.location.href = window.location.pathname + '?event_id=<?php echo $event_id; ?>&updated=1';
        }
    });
    
    // Initial resize
    $('#tec-edit-frame').on('load', resizeIframe);
});
</script>

<?php
get_footer();
?>