<?php
/**
 * Template Name: Trainer Venue Manage
 * Description: Template for creating/editing training venues
 */

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

get_header();
?>

<div class="hvac-page-wrapper hvac-trainer-venue-manage-page">
    <?php
    // Display trainer navigation menu
    if (class_exists('HVAC_Menu_System')) {
        HVAC_Menu_System::instance()->render_trainer_menu();
    }
    ?>
    
    <?php
    // Display breadcrumbs
    if (class_exists('HVAC_Breadcrumbs')) {
        echo HVAC_Breadcrumbs::instance()->render_breadcrumbs();
    }
    ?>
    
    <div class="container">
        <?php
        // --- Security Check & Data Loading ---
        // Ensure user is logged in and has access
        if (!is_user_logged_in()) {
            // Redirect to login page if not logged in
            wp_safe_redirect(home_url('/training-login/'));
            exit;
        }

        // Check if user has permission to manage venues
        // Check for HVAC trainer roles (not capabilities!)
        $user = wp_get_current_user();
        $has_trainer_role = in_array('hvac_trainer', $user->roles) || in_array('hvac_master_trainer', $user->roles);
        
        if (!$has_trainer_role && !current_user_can('manage_options')) {
            // Show access denied message instead of redirect to prevent loops
            ?>
            <div class="hvac-access-denied">
                <h1><?php _e('Access Denied', 'hvac-community-events'); ?></h1>
                <p><?php _e('Sorry, you do not have permission to access the venue management area.', 'hvac-community-events'); ?></p>
                <p><?php _e('If you are an HVAC trainer, please contact an administrator to get the proper role assigned.', 'hvac-community-events'); ?></p>
                <a href="<?php echo esc_url(home_url()); ?>" class="button"><?php _e('Return to Home', 'hvac-community-events'); ?></a>
            </div>
            <?php
            get_footer();
            return;
        }
        ?>
        
        <div class="hvac-venue-manage-wrapper">
            <!-- Venue Management Content -->
            <?php
            if (class_exists('HVAC_Venues')) {
                echo HVAC_Venues::instance()->render_venue_manage();
            } else {
                ?>
                <div class="hvac-error-message">
                    <p><?php _e('Venue management system is not available. Please contact an administrator.', 'hvac-community-events'); ?></p>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
</div>

<?php
get_footer();
