Please log in to view the dashboard.
                Login
            ';
        }
        
        if (!current_user_can('view_hvac_dashboard')) {
            return '
                You do not have permission to view this dashboard.
             ';
        }
        
        return $this->get_dashboard_content();
    }
    
    /**
     * Render dashboard content for the page
     */
    public function render_dashboard_content($content) {
        // Only process if content contains our shortcode
        if (has_shortcode($content, 'hvac_trainer_dashboard')) {
            return do_shortcode($content);
        }
        
        return $content;
    }
    
    /**
     * Get dashboard content
     */
    private function get_dashboard_content() {
        $user_id = get_current_user_id();
        
        // Get dashboard data instance (class is autoloaded)
        if (!class_exists('HVAC_Dashboard_Data')) {
            require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-dashboard-data.php';
        }
        $dashboard_data = new HVAC_Dashboard_Data($user_id);
        
        // Get data
        $data = array(
            'total_events' => $dashboard_data->get_total_events_count(),
            'upcoming_events' => $dashboard_data->get_upcoming_events_count(),
            'past_events' => $dashboard_data->get_past_events_count(),
            'total_sold' => $dashboard_data->get_total_tickets_sold(),
            'total_revenue' => $dashboard_data->get_total_revenue(),
            'revenue_target' => $dashboard_data->get_annual_revenue_target(),
            'events_table' => $dashboard_data->get_events_table_data(isset($_GET['event_status']) ? sanitize_key($_GET['event_status']) : 'all'),
            'current_filter' => isset($_GET['event_status']) ? sanitize_key($_GET['event_status']) : 'all'
        );
        
        // Get dashboard HTML
        ob_start();
        ?>
        
            
            
            
            
                Your Stats
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        Total Revenue
                        $
                        
                            Target: $
                        
                      
            
            
            
                Your Events
                
                
                
                
                    
                        
                            
                                
                                    | Status | Event Name | Date | Organizer | Capacity | Sold | Revenue | Actions | 
                            
                            
                                
                                    
                                        |  |  |  |  |  |  | $ | Edit |
                                            Summary | 
                                
                            
                        
                    
                        No events found.
                    
                 
            
         
        log_error('Dashboard AJAX: Nonce verification failed', $_POST);
                wp_send_json_error(array('message' => 'Security check failed.'));
                return;
            }
            
            // Get current user ID
            $user_id = get_current_user_id();
            if (!$user_id) {
                $this->log_error('Dashboard AJAX: User not logged in');
                wp_send_json_error(array('message' => 'Please log in to continue.'));
                return;
            }
            
            // Check user role (not capability)
            $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')) {
                $this->log_error('Dashboard AJAX: Insufficient permissions', array('user_id' => $user_id, 'roles' => $user->roles));
                wp_send_json_error(array('message' => 'Access denied.'));
                return;
            }
        
        // Get all filters and parameters
        $args = array(
            'status' => isset($_POST['status']) ? sanitize_key($_POST['status']) : 'all',
            'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
            'orderby' => isset($_POST['orderby']) ? sanitize_key($_POST['orderby']) : 'date',
            'order' => isset($_POST['order']) ? sanitize_key($_POST['order']) : 'DESC',
            'page' => isset($_POST['page']) ? absint($_POST['page']) : 1,
            'per_page' => isset($_POST['per_page']) ? absint($_POST['per_page']) : 10,
            'date_from' => isset($_POST['date_from']) ? sanitize_text_field($_POST['date_from']) : '',
            'date_to' => isset($_POST['date_to']) ? sanitize_text_field($_POST['date_to']) : '',
        );
        
        // Get dashboard data instance (class is autoloaded)
        if (!class_exists('HVAC_Dashboard_Data')) {
            require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-dashboard-data.php';
        }
        $dashboard_data = new HVAC_Dashboard_Data($user_id);
        
        // Get filtered events data
        $result = $dashboard_data->get_events_table_data($args);
        $events = $result['events'];
        $pagination = $result['pagination'];
        
        // Build HTML for events table
        ob_start();
        
        ?>
        
        
        
         $html,
            'count' => count($events),
            'pagination' => $pagination,
            'args' => $args
        ));
        
        } catch (Exception $e) {
            $this->log_error('Dashboard AJAX Exception', array(
                'message' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ));
            wp_send_json_error(array('message' => 'An error occurred while loading events.'));
        }
    }
    
    /**
     * Log errors for debugging
     * 
     * @param string $message Error message
     * @param mixed $data Additional data to log
     */
    private function log_error($message, $data = null) {
        if (defined('WP_DEBUG') && WP_DEBUG && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
            $log_message = '[HVAC Dashboard] ' . $message;
            if ($data) {
                $log_message .= ' | Data: ' . print_r($data, true);
            }
            error_log($log_message);
        }
    }
    
    /**
     * Enqueue dashboard assets (CSS and JavaScript)
     */
    public function enqueue_dashboard_assets() {
        // Check if we're on the dashboard page
        global $post;
        if (!is_a($post, 'WP_Post') || !has_shortcode($post->post_content, 'hvac_trainer_dashboard')) {
            return;
        }
        
        // Enqueue UX enhancements (CSS and JS)
        wp_enqueue_style(
            'hvac-ux-enhancements-css',
            HVAC_PLUGIN_URL . 'assets/css/hvac-ux-enhancements.css',
            array(),
            HVAC_PLUGIN_VERSION
        );
        
        wp_enqueue_script(
            'hvac-ux-enhancements-js',
            HVAC_PLUGIN_URL . 'assets/js/hvac-ux-enhancements.js',
            array('jquery'),
            HVAC_PLUGIN_VERSION,
            true
        );
        
        // Enqueue enhanced dashboard CSS
        wp_enqueue_style(
            'hvac-dashboard-enhanced-css',
            HVAC_PLUGIN_URL . 'assets/css/hvac-dashboard-enhanced.css',
            array('hvac-ux-enhancements-css'),
            HVAC_PLUGIN_VERSION
        );
        
        // Enqueue enhanced dashboard JavaScript
        wp_enqueue_script(
            'hvac-dashboard-enhanced-js',
            HVAC_PLUGIN_URL . 'assets/js/hvac-dashboard-enhanced.js',
            array('jquery', 'hvac-ux-enhancements-js'),
            HVAC_PLUGIN_VERSION,
            true
        );
        
        // Localize script with AJAX URL and nonce
        wp_localize_script('hvac-dashboard-enhanced-js', 'hvac_dashboard', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('hvac_dashboard_nonce')
        ));
        
        // Inline CSS for now - can be moved to external file later
        $css = '
            .hvac-dashboard-wrapper {
                max-width: 1200px;
                margin: 0 auto;
                padding: 20px;
            }
            
            .hvac-dashboard-header {
                margin-bottom: 30px;
            }
            
            .hvac-dashboard-header h1 {
                margin-bottom: 20px;
            }
            
            .hvac-dashboard-nav {
                display: flex;
                gap: 10px;
                flex-wrap: wrap;
            }
            
            .hvac-button {
                display: inline-block;
                padding: 10px 20px;
                text-decoration: none;
                border-radius: 4px;
                transition: all 0.3s ease;
            }
            
            .hvac-button-primary {
                background-color: #E9AF28;
                color: #000;
            }
            
            .hvac-button-primary:hover {
                background-color: #d49b20;
            }
            
            .hvac-button-secondary {
                background-color: #0B5C7D;
                color: #fff;
            }
            
            .hvac-button-secondary:hover {
                background-color: #084562;
            }
            
            .hvac-dashboard-stats {
                margin-bottom: 40px;
            }
            
            .hvac-stats-row {
                display: flex;
                flex-direction: row;
                flex-wrap: wrap;
                margin: -10px; /* Counteract the padding on columns */
                justify-content: space-between;
                align-items: stretch;
                margin-top: 20px;
            }
            
            .hvac-stat-col {
                flex: 1;
                min-width: 160px; /* Ensure minimum width for readability */
                padding: 10px;
            }
            
            .hvac-stat-card {
                background: #f8f9fa;
                border: 1px solid #e9ecef;
                border-radius: 8px;
                padding: 20px;
                text-align: center;
            }
            
            .hvac-stat-card h3 {
                margin: 0 0 10px;
                font-size: 16px;
                color: #666;
            }
            
            .hvac-stat-card .metric-value {
                font-size: 32px;
                font-weight: bold;
                color: #E9AF28;
                margin: 0;
            }
            
            .hvac-stat-card small {
                display: block;
                margin-top: 5px;
                color: #666;
            }
            
            .hvac-dashboard-events {
                margin-top: 40px;
            }
            
            .hvac-event-filters {
                margin: 20px 0;
                display: flex;
                gap: 10px;
                align-items: center;
            }
            
            .hvac-filter {
                padding: 5px 15px;
                border: 1px solid #ddd;
                border-radius: 4px;
                text-decoration: none;
                color: #333;
                transition: all 0.3s ease;
            }
            
            .hvac-filter:hover,
            .hvac-filter-active {
                background-color: #E9AF28;
                color: #000;
                border-color: #E9AF28;
            }
            
            .hvac-events-table-wrapper {
                overflow-x: auto;
            }
            
            .hvac-events-table {
                width: 100%;
                border-collapse: collapse;
                margin-top: 20px;
            }
            
            .hvac-events-table th,
            .hvac-events-table td {
                padding: 12px;
                text-align: left;
                border-bottom: 1px solid #ddd;
            }
            
            .hvac-events-table th {
                background-color: #f8f9fa;
                font-weight: bold;
            }
            
            .hvac-events-table tr:hover {
                background-color: #f8f9fa;
            }
            
            .hvac-events-table a {
                color: #0B5C7D;
                text-decoration: none;
            }
            
            .hvac-events-table a:hover {
                text-decoration: underline;
            }
            
            @media (max-width: 768px) {
                .hvac-stats-grid {
                    grid-template-columns: 1fr;
                }
                
                .hvac-events-table {
                    font-size: 14px;
                }
                
                .hvac-events-table th,
                .hvac-events-table td {
                    padding: 8px;
                }
            }
        ';
        
        wp_add_inline_style('astra-theme-css', $css);
    }
}
// Initialize the dashboard
new HVAC_Dashboard();