<?php
/**
 * Template Name: Trainer Resources
 * Description: Resources page for HVAC trainers including announcements and Google Drive
 *
 * @package HVAC_Community_Events
 */

// Security check
if (!defined('ABSPATH')) {
    exit;
}

// Define template constant
if (!defined('HVAC_IN_PAGE_TEMPLATE')) {
    define('HVAC_IN_PAGE_TEMPLATE', true);
}

// Check if user is a trainer
// Temporarily disable this check for debugging
// TODO: Re-enable after fixing permission class loading
/*
if (!HVAC_Announcements_Permissions::is_trainer()) {
    wp_redirect(home_url('/'));
    exit;
}
*/

get_header();

// Enqueue announcements scripts for modal functionality
wp_enqueue_script(
    'hvac-announcements-view',
    plugin_dir_url(dirname(__FILE__)) . 'assets/js/hvac-announcements-view.js',
    array('jquery'),
    defined('HVAC_VERSION') ? HVAC_VERSION : '1.0.0',
    true
);

// Localize script for AJAX  
wp_localize_script('hvac-announcements-view', 'hvac_announcements_ajax', array(
    'ajax_url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('hvac_announcements_nonce'),
));

// Get menu system instance
$menu_system = HVAC_Menu_System::get_instance();
?>

<div class="hvac-plugin-page hvac-trainer-resources-page">
    <?php 
    // Display navigation menu
    echo $menu_system->render_navigation_menu(); 
    ?>
    
    <?php
    // Display breadcrumbs
    if (class_exists('HVAC_Breadcrumbs')) {
        echo HVAC_Breadcrumbs::instance()->render_breadcrumbs();
    }
    ?>
    
    <div class="container">
        <div class="hvac-resources-wrapper">
            <header class="page-header">
                <h1><?php _e('Trainer Resources', 'hvac'); ?></h1>
                <p class="page-description">
                    <?php _e('Access important announcements, training materials, and shared resources to support your HVAC training programs.', 'hvac'); ?>
                </p>
            </header>
            
            <!-- Announcements Section -->
            <section class="resources-section announcements-section">
                <h2 class="section-title">
                    <span class="dashicons dashicons-megaphone"></span>
                    <?php _e('Latest Announcements', 'hvac'); ?>
                </h2>
                
                <div class="announcements-container">
                    <?php
                    // Query announcements directly
                    $announcements_query = new WP_Query(array(
                        'post_type' => 'hvac_announcement',
                        'posts_per_page' => 10,
                        'orderby' => 'date',
                        'order' => 'DESC',
                        'post_status' => 'publish'
                    ));
                    
                    if ($announcements_query->have_posts()) :
                    ?>
                        <div class="hvac-announcements-list">
                            <?php while ($announcements_query->have_posts()) : $announcements_query->the_post(); ?>
                                <article class="announcement-item">
                                    <div class="announcement-content">
                                        <div class="announcement-text">
                                            <h3 class="announcement-title">
                                                <?php the_title(); ?>
                                            </h3>
                                            <div class="announcement-meta">
                                                <span class="announcement-date"><?php echo get_the_date(); ?></span>
                                                <span class="announcement-author">by <?php echo get_the_author(); ?></span>
                                            </div>
                                            <div class="announcement-excerpt">
                                                <?php 
                                                $excerpt = get_the_excerpt();
                                                if (empty($excerpt)) {
                                                    $excerpt = wp_trim_words(get_the_content(), 30, '...');
                                                }
                                                echo wp_kses_post($excerpt);
                                                ?>
                                            </div>
                                            <div class="announcement-actions">
                                                <button class="read-more-btn" data-id="<?php echo get_the_ID(); ?>">
                                                    <?php _e('Read More', 'hvac'); ?>
                                                </button>
                                            </div>
                                        </div>
                                        <?php if (has_post_thumbnail()) : ?>
                                            <div class="announcement-image">
                                                <?php the_post_thumbnail('medium', array('class' => 'announcement-thumb')); ?>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </article>
                            <?php endwhile; ?>
                        </div>
                        
                        <?php if ($announcements_query->max_num_pages > 1) : ?>
                            <div class="announcements-pagination">
                                <button class="load-more-announcements" data-page="2" data-max="<?php echo esc_attr($announcements_query->max_num_pages); ?>">
                                    <?php _e('Load More Announcements', 'hvac'); ?>
                                </button>
                            </div>
                        <?php endif; ?>
                        
                    <?php else : ?>
                        <div class="no-announcements">
                            <p><?php _e('No announcements available at this time.', 'hvac'); ?></p>
                        </div>
                    <?php endif; ?>
                    
                    <?php wp_reset_postdata(); ?>
                    
                    <!-- Modal for viewing announcement -->
                    <div id="announcement-modal" class="hvac-modal" style="display: none;">
                        <div class="modal-content">
                            <div class="modal-header">
                                <span class="modal-close">&times;</span>
                                <h2 class="modal-title"></h2>
                            </div>
                            <div class="modal-body">
                                <div class="modal-meta"></div>
                                <div class="modal-content-text"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
            
            <!-- Google Drive Resources Section -->
            <section class="resources-section google-drive-section">
                <h2 class="section-title">
                    <span class="dashicons dashicons-media-default"></span>
                    <?php _e('Training Resources Library', 'hvac'); ?>
                </h2>
                
                <div class="google-drive-description">
                    <p><?php _e('Access shared training materials, presentations, documentation, and other resources in our Google Drive folder.', 'hvac'); ?></p>
                </div>
                
                <div class="google-drive-container">
                    <?php
                    // Google Drive embed with proper URL format
                    $drive_url = 'https://drive.google.com/drive/folders/1-SDHGR9Ix6BmUVTHa3wI99K0rwfWL-vs?usp=drive_link';
                    $folder_id = '1-SDHGR9Ix6BmUVTHa3wI99K0rwfWL-vs';
                    
                    // Use the modern embed format that works better
                    $embed_url = 'https://drive.google.com/embeddedfolderview?id=' . $folder_id;
                    ?>
                    
                    <div class="iframe-isolation-wrapper" style="position: relative; isolation: isolate;">
                        <iframe 
                            src="<?php echo esc_url($embed_url); ?>"
                            width="100%"
                            height="600"
                            frameborder="0"
                            class="google-drive-iframe"
                            style="border: 1px solid #ddd; border-radius: 4px;"
                            sandbox="allow-same-origin allow-scripts allow-popups allow-forms"
                            loading="lazy">
                        </iframe>
                    </div>
                    
                    <div class="google-drive-footer">
                        <p>
                            <a href="<?php echo esc_url($drive_url); ?>" target="_blank" class="button">
                                <span class="dashicons dashicons-external"></span>
                                <?php _e('Open in Google Drive', 'hvac'); ?>
                            </a>
                        </p>
                        <p class="help-text">
                            <?php _e('If you have trouble viewing the embedded folder above, click the button to open it directly in Google Drive.', 'hvac'); ?>
                        </p>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>

<?php get_footer(); ?>