<?php
/**
 * Template Name: Communication Templates
 * Description: Template for the trainer communication templates page
 */

// 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-communication-templates-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 ---
        // Ensure user is logged in and has access
        if ( ! is_user_logged_in() ) {
            wp_safe_redirect( home_url( '/training-login/' ) );
            exit;
        }

        // Check if user has permission to view communication templates
        $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' ) ) {
            ?>
            <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 Communication Templates.', '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-communication-templates-wrapper">
            <!-- Header -->
            <div class="hvac-templates-header">
                <h1 class="entry-title"><?php _e('Communication Templates', 'hvac-community-events'); ?></h1>
                <p class="hvac-templates-description">
                    <?php _e('Ready-to-use email and SMS templates for communicating with your event attendees. Click any template to expand and copy the content.', 'hvac-community-events'); ?>
                </p>
            </div>

            <!-- Search and Filters -->
            <div class="hvac-templates-controls">
                <div class="hvac-search-wrapper">
                    <input type="search" id="hvac-template-search" placeholder="<?php esc_attr_e('Search templates...', 'hvac-community-events'); ?>" class="hvac-search-input">
                    <button type="button" class="hvac-search-button">
                        <span class="dashicons dashicons-search"></span>
                        <span class="screen-reader-text"><?php _e('Search', 'hvac-community-events'); ?></span>
                    </button>
                </div>
                
                <div class="hvac-filter-wrapper">
                    <select id="hvac-template-category" class="hvac-filter-select">
                        <option value=""><?php _e('All Categories', 'hvac-community-events'); ?></option>
                        <option value="pre_event"><?php _e('Pre-Event Communications', 'hvac-community-events'); ?></option>
                        <option value="event_reminder"><?php _e('Event Reminders', 'hvac-community-events'); ?></option>
                        <option value="post_event"><?php _e('Post-Event Follow-up', 'hvac-community-events'); ?></option>
                        <option value="certificate"><?php _e('Certificate Information', 'hvac-community-events'); ?></option>
                        <option value="general"><?php _e('General Communications', 'hvac-community-events'); ?></option>
                    </select>
                    
                    <select id="hvac-template-channel" class="hvac-filter-select">
                        <option value=""><?php _e('All Channels', 'hvac-community-events'); ?></option>
                        <option value="email"><?php _e('Email', 'hvac-community-events'); ?></option>
                        <option value="sms"><?php _e('SMS', 'hvac-community-events'); ?></option>
                    </select>
                </div>
            </div>

            <!-- Templates List -->
            <div class="hvac-templates-list" id="hvac-templates-list">
                <?php
                // Get and display templates
                if (class_exists('HVAC_Trainer_Communication_Templates')) {
                    echo HVAC_Trainer_Communication_Templates::instance()->render_templates_interface();
                } else {
                    echo '<div class="hvac-templates-error">' . __('Communication templates functionality is not available.', 'hvac-community-events') . '</div>';
                }
                ?>
            </div>

            <!-- Loading State -->
            <div class="hvac-templates-loading" id="hvac-templates-loading" style="display: none;">
                <div class="hvac-spinner"></div>
                <p><?php _e('Loading templates...', 'hvac-community-events'); ?></p>
            </div>

            <!-- Empty State -->
            <div class="hvac-templates-empty" id="hvac-templates-empty" style="display: none;">
                <div class="hvac-empty-icon">
                    <span class="dashicons dashicons-email"></span>
                </div>
                <h3><?php _e('No templates found', 'hvac-community-events'); ?></h3>
                <p><?php _e('Try adjusting your search or filter criteria.', 'hvac-community-events'); ?></p>
            </div>
        </div>
    </div>
</div>

<!-- Template Preview Modal -->
<div id="hvac-template-modal" class="hvac-modal" style="display: none;" aria-hidden="true">
    <div class="hvac-modal-overlay" role="dialog" aria-labelledby="hvac-modal-title" aria-describedby="hvac-modal-content">
        <div class="hvac-modal-container">
            <div class="hvac-modal-header">
                <h2 id="hvac-modal-title"><?php _e('Template Preview', 'hvac-community-events'); ?></h2>
                <button type="button" class="hvac-modal-close" aria-label="<?php esc_attr_e('Close modal', 'hvac-community-events'); ?>">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
            <div class="hvac-modal-body">
                <div id="hvac-modal-content"></div>
                <div class="hvac-modal-actions">
                    <button type="button" class="hvac-copy-template button button-primary">
                        <span class="dashicons dashicons-clipboard"></span>
                        <?php _e('Copy Template', 'hvac-community-events'); ?>
                    </button>
                    <button type="button" class="hvac-modal-close button button-secondary">
                        <?php _e('Close', 'hvac-community-events'); ?>
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
get_footer();