<?php
/**
 * Template Name: Trainer Documentation
 * Description: Help and documentation page for HVAC trainers
 */

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

get_header();
?>

<div class="hvac-page-wrapper hvac-trainer-documentation-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();
    }
    ?>
    
    <!-- Mobile TOC Accordion (visible only on small screens) -->
    <div class="hvac-mobile-toc-accordion" id="hvac-mobile-toc">
        <button class="hvac-mobile-toc-toggle" id="hvac-mobile-toc-toggle">
            <span class="hvac-toc-toggle-text">Table of Contents</span>
            <span class="hvac-toc-toggle-icon">▼</span>
        </button>
        <div class="hvac-mobile-toc-content" id="hvac-mobile-toc-content">
            <nav class="hvac-mobile-toc-nav" id="hvac-mobile-toc-nav">
                <!-- TOC will be populated by JavaScript -->
            </nav>
        </div>
    </div>
    
    <div class="container hvac-documentation-container">
        <div class="hvac-doc-layout">
            <!-- Main Content Area -->
            <main class="hvac-doc-main">
                <?php
                // Check if user is logged in
                if (!is_user_logged_in()) {
                    echo '<div class="hvac-notice hvac-notice-warning">';
                    echo '<p>Please <a href="' . home_url('/training-login/') . '">log in</a> to access the documentation.</p>';
                    echo '</div>';
                } else {
                    // Display the page content (editable via Gutenberg)
                    while (have_posts()) : the_post();
                        ?>
                        <article id="post-<?php the_ID(); ?>" <?php post_class('hvac-doc-article'); ?>>
                            <?php 
                            // If content exists, show it
                            if (trim(get_the_content()) !== '') {
                                the_content();
                            } else {
                                // Show default content if page is empty
                                echo do_shortcode('[hvac_documentation_content]');
                            }
                            ?>
                        </article>
                        <?php
                    endwhile;
                }
                ?>
            </main>
            
            <!-- Table of Contents Sidebar -->
            <aside class="hvac-doc-sidebar" id="hvac-toc-sidebar">
                <div class="hvac-toc-wrapper">
                    <h3 class="hvac-toc-title">Table of Contents</h3>
                    <nav class="hvac-toc-nav" id="hvac-toc-nav">
                        <!-- TOC will be populated by JavaScript -->
                    </nav>
                </div>
            </aside>
        </div>
    </div>
</div>

<style>
/* Documentation Page Layout */
.hvac-documentation-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

.hvac-doc-layout {
    display: flex;
    gap: 40px;
    align-items: flex-start;
}

.hvac-doc-main {
    flex: 1;
    min-width: 0;
}

.hvac-doc-sidebar {
    width: 280px;
    position: sticky;
    top: 100px;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
}

.hvac-toc-wrapper {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    padding: 20px;
}

.hvac-toc-title {
    font-size: 18px;
    font-weight: 600;
    margin: 0 0 15px 0;
    color: #333;
}

.hvac-toc-nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.hvac-toc-nav li {
    margin: 0;
}

.hvac-toc-nav a {
    display: block;
    padding: 8px 12px;
    color: #666;
    text-decoration: none;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}

.hvac-toc-nav a:hover {
    color: #E9AF28;
    background: #fff;
    border-left-color: #E9AF28;
}

.hvac-toc-nav a.active {
    color: #E9AF28;
    font-weight: 600;
    background: #fff;
    border-left-color: #E9AF28;
}

/* Nested TOC items */
.hvac-toc-nav ul ul {
    margin-left: 20px;
    margin-top: 5px;
}

.hvac-toc-nav ul ul a {
    font-size: 14px;
    padding: 6px 12px;
}

/* Documentation Content Styles */
.hvac-doc-article {
    background: white;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.hvac-doc-article h1 {
    color: #333;
    font-size: 36px;
    margin-bottom: 10px;
    padding-bottom: 15px;
    border-bottom: 3px solid #E9AF28;
}

.hvac-doc-article h2 {
    color: #333;
    font-size: 28px;
    margin-top: 40px;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid #f0f0f0;
}

.hvac-doc-article h3 {
    color: #555;
    font-size: 22px;
    margin-top: 30px;
    margin-bottom: 15px;
}

.hvac-doc-article h4 {
    color: #666;
    font-size: 18px;
    margin-top: 25px;
    margin-bottom: 12px;
}

.hvac-doc-article p {
    line-height: 1.8;
    margin-bottom: 15px;
    color: #555;
}

.hvac-doc-article ul,
.hvac-doc-article ol {
    margin-bottom: 20px;
    padding-left: 30px;
}

.hvac-doc-article li {
    margin-bottom: 10px;
    line-height: 1.8;
}

.hvac-doc-article strong {
    color: #333;
    font-weight: 600;
}

.hvac-doc-article a {
    color: #E9AF28;
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s;
}

.hvac-doc-article a:hover {
    border-bottom-color: #E9AF28;
}

/* Notice Styles */
.hvac-notice {
    padding: 15px 20px;
    border-radius: 6px;
    margin-bottom: 20px;
}

.hvac-notice-warning {
    background: #fff3cd;
    border: 1px solid #ffc107;
    color: #856404;
}

.hvac-notice-info {
    background: #d1ecf1;
    border: 1px solid #bee5eb;
    color: #0c5460;
}

.hvac-notice-success {
    background: #d4edda;
    border: 1px solid #c3e6cb;
    color: #155724;
}

/* Mobile TOC Accordion Styles */
.hvac-mobile-toc-accordion {
    display: none;
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    margin: 20px auto;
    max-width: 1400px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.hvac-mobile-toc-toggle {
    width: 100%;
    padding: 15px 20px;
    background: #fff;
    border: none;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    font-size: 16px;
    font-weight: 600;
    color: #333;
    transition: background 0.3s;
}

.hvac-mobile-toc-toggle:hover {
    background: #f8f9fa;
}

.hvac-mobile-toc-toggle.active {
    background: #E9AF28;
    color: white;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

.hvac-toc-toggle-icon {
    transition: transform 0.3s;
    font-size: 18px;
}

.hvac-mobile-toc-toggle.active .hvac-toc-toggle-icon {
    transform: rotate(180deg);
}

.hvac-mobile-toc-content {
    display: none;
    padding: 20px;
    background: white;
    border-top: 1px solid #e9ecef;
    border-bottom-left-radius: 8px;
    border-bottom-right-radius: 8px;
    max-height: 400px;
    overflow-y: auto;
}

.hvac-mobile-toc-content.active {
    display: block;
}

.hvac-mobile-toc-nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.hvac-mobile-toc-nav li {
    margin: 0;
}

.hvac-mobile-toc-nav a {
    display: block;
    padding: 10px 15px;
    color: #666;
    text-decoration: none;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}

.hvac-mobile-toc-nav a:hover,
.hvac-mobile-toc-nav a.active {
    color: #E9AF28;
    background: #f8f9fa;
    border-left-color: #E9AF28;
}

.hvac-mobile-toc-nav ul ul {
    margin-left: 20px;
    margin-top: 5px;
}

.hvac-mobile-toc-nav ul ul a {
    font-size: 14px;
    padding: 8px 15px;
}

/* Responsive Design */
@media (max-width: 992px) {
    .hvac-doc-layout {
        flex-direction: column;
    }
    
    /* Hide desktop sidebar on mobile */
    .hvac-doc-sidebar {
        display: none;
    }
    
    /* Show mobile TOC accordion */
    .hvac-mobile-toc-accordion {
        display: block;
        padding: 0 20px;
    }
}

@media (max-width: 768px) {
    .hvac-doc-article {
        padding: 20px;
    }
    
    .hvac-doc-article h1 {
        font-size: 28px;
    }
    
    .hvac-doc-article h2 {
        font-size: 24px;
    }
    
    .hvac-doc-article h3 {
        font-size: 20px;
    }
}
</style>

<script>
jQuery(document).ready(function($) {
    // Generate Table of Contents from headings
    function generateTOC() {
        var headings = $('.hvac-doc-article').find('h2, h3, h4');
        
        if (headings.length === 0) return;
        
        var tocHTML = '<ul>';
        var lastLevel = 2;
        
        headings.each(function() {
            var heading = $(this);
            var level = parseInt(heading.prop('tagName').substring(1));
            var text = heading.text();
            var id = heading.attr('id');
            
            // Generate ID if not exists
            if (!id) {
                id = text.toLowerCase()
                    .replace(/[^\w\s-]/g, '')
                    .replace(/\s+/g, '-')
                    .replace(/-+/g, '-')
                    .trim();
                heading.attr('id', id);
            }
            
            // Handle nesting
            if (level > lastLevel) {
                tocHTML += '<ul>';
            } else if (level < lastLevel) {
                for (var i = level; i < lastLevel; i++) {
                    tocHTML += '</ul></li>';
                }
            } else if (lastLevel > 2) {
                tocHTML += '</li>';
            }
            
            tocHTML += '<li><a href="#' + id + '">' + text + '</a>';
            lastLevel = level;
        });
        
        // Close remaining tags
        for (var i = 2; i < lastLevel; i++) {
            tocHTML += '</ul></li>';
        }
        tocHTML += '</li></ul>';
        
        // Populate both desktop and mobile TOCs
        $('#hvac-toc-nav').html(tocHTML);
        $('#hvac-mobile-toc-nav').html(tocHTML);
    }
    
    // Toggle mobile TOC accordion
    $('#hvac-mobile-toc-toggle').on('click', function() {
        $(this).toggleClass('active');
        $('#hvac-mobile-toc-content').toggleClass('active');
    });
    
    // Smooth scroll to anchor (works for both desktop and mobile)
    $(document).on('click', '.hvac-toc-nav a, .hvac-mobile-toc-nav a', function(e) {
        e.preventDefault();
        var target = $($(this).attr('href'));
        if (target.length) {
            // Close mobile TOC if open
            $('#hvac-mobile-toc-toggle').removeClass('active');
            $('#hvac-mobile-toc-content').removeClass('active');
            
            // Scroll to target
            $('html, body').animate({
                scrollTop: target.offset().top - 100
            }, 500);
        }
    });
    
    // Highlight active section in TOC
    function highlightActiveSection() {
        var scrollPos = $(window).scrollTop();
        
        $('.hvac-doc-article h2, .hvac-doc-article h3, .hvac-doc-article h4').each(function() {
            var currLink = $('.hvac-toc-nav a[href="#' + $(this).attr('id') + '"], .hvac-mobile-toc-nav a[href="#' + $(this).attr('id') + '"]');
            var refElement = $(this);
            
            if (refElement.position() && refElement.position().top <= scrollPos + 150) {
                $('.hvac-toc-nav a, .hvac-mobile-toc-nav a').removeClass('active');
                currLink.addClass('active');
            }
        });
    }
    
    // Initialize TOC
    generateTOC();
    
    // Update active section on scroll
    $(window).on('scroll', highlightActiveSection);
    highlightActiveSection();
    
    // Close mobile TOC when clicking outside
    $(document).on('click', function(e) {
        if (!$(e.target).closest('#hvac-mobile-toc').length) {
            $('#hvac-mobile-toc-toggle').removeClass('active');
            $('#hvac-mobile-toc-content').removeClass('active');
        }
    });
});
</script>

<?php
get_footer();