<?php
/**
 * Template Name: Master Trainer Profile Edit (Simple)
 * Description: Simplified template for master trainers to edit any trainer profile
 */

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

get_header();

// Check permissions
if (!is_user_logged_in()) {
    echo '<div class="container"><p>You must be logged in to view this page.</p></div>';
    get_footer();
    return;
}

if (!current_user_can('hvac_master_trainer') && !current_user_can('administrator')) {
    echo '<div class="container"><p>You must be a master trainer or administrator to access this page.</p></div>';
    get_footer();
    return;
}

// Get the user ID to edit
$edit_user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

if (!$edit_user_id) {
    echo '<div class="container"><p>No user specified for editing.</p></div>';
    get_footer();
    return;
}

// Get basic user data
$edit_user = get_userdata($edit_user_id);

if (!$edit_user) {
    echo '<div class="container"><p>User not found.</p></div>';
    get_footer();
    return;
}

// Check if profile manager exists
if (!class_exists('HVAC_Trainer_Profile_Manager')) {
    echo '<div class="container"><p>Profile management system is not available.</p></div>';
    get_footer();
    return;
}

$profile_manager = HVAC_Trainer_Profile_Manager::get_instance();
$profile = $profile_manager->get_trainer_profile($edit_user_id);

if (!$profile) {
    echo '<div class="container"><p>No trainer profile found for this user.</p></div>';
    get_footer();
    return;
}

$profile_meta = $profile_manager->get_profile_meta($profile->ID);
?>

<div class="hvac-page-wrapper hvac-master-trainer-profile-edit-page">
    <?php
    // Display master trainer navigation menu
    if (class_exists('HVAC_Menu_System')) {
        HVAC_Menu_System::instance()->render_master_trainer_menu();
    }
    ?>
    
    <div class="container">
        <div class="hvac-master-trainer-profile-edit">
            <div class="hvac-page-header">
                <h1>Edit Trainer Profile: <?php echo esc_html($edit_user->display_name); ?></h1>
                <div class="hvac-header-actions">
                    <a href="/master-trainer/master-dashboard/" class="hvac-button hvac-button-secondary">Back to Dashboard</a>
                </div>
            </div>
            
            <!-- Success/Error Messages -->
            <div id="hvac-profile-messages"></div>
            
            <form id="hvac-master-profile-form" class="hvac-form" enctype="multipart/form-data">
                <?php wp_nonce_field('hvac_profile_edit', 'hvac_profile_nonce'); ?>
                <input type="hidden" name="edit_user_id" value="<?php echo $edit_user_id; ?>" />
                <input type="hidden" name="profile_id" value="<?php echo $profile->ID; ?>" />
                
                <!-- Basic Information Test -->
                <div class="hvac-form-section">
                    <h3>Basic Information</h3>
                    
                    <div class="hvac-form-row">
                        <label for="trainer_first_name">First Name *</label>
                        <input type="text" id="trainer_first_name" name="trainer_first_name" required
                               value="<?php echo esc_attr($profile_meta['trainer_first_name'] ?? $edit_user->first_name); ?>" />
                    </div>
                    
                    <div class="hvac-form-row">
                        <label for="trainer_last_name">Last Name *</label>
                        <input type="text" id="trainer_last_name" name="trainer_last_name" required
                               value="<?php echo esc_attr($profile_meta['trainer_last_name'] ?? $edit_user->last_name); ?>" />
                    </div>
                    
                    <div class="hvac-form-row">
                        <label for="trainer_display_name">Display Name *</label>
                        <input type="text" id="trainer_display_name" name="trainer_display_name" required
                               value="<?php echo esc_attr($profile_meta['trainer_display_name'] ?? $edit_user->display_name); ?>" />
                    </div>
                </div>
                
                <div class="hvac-form-actions">
                    <button type="submit" class="hvac-button hvac-button-primary">Save Profile Changes</button>
                    <a href="/master-trainer/master-dashboard/" class="hvac-button hvac-button-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
// Basic form functionality
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('hvac-master-profile-form');
    const saveButton = form.querySelector('button[type="submit"]');
    
    form.addEventListener('submit', function(e) {
        e.preventDefault();
        
        saveButton.textContent = 'Saving...';
        saveButton.disabled = true;
        
        // For now, just show a test message
        document.getElementById('hvac-profile-messages').innerHTML = 
            '<div class="notice notice-info"><p>Profile edit form is working! (Test mode)</p></div>';
            
        setTimeout(() => {
            saveButton.textContent = 'Save Profile Changes';
            saveButton.disabled = false;
        }, 2000);
    });
});
</script>

<?php
get_footer();
?>