<?php
/**
 * Diagnostic Dashboard - Shows what's being loaded
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

define('BASE_PATH', dirname(__DIR__));
require_once BASE_PATH . '/config/config.php';
require_once BASE_PATH . '/config/database.php';
require_once BASE_PATH . '/config/auth.php';
require_once BASE_PATH . '/config/csrf.php';
require_auth();

$current_user = current_user();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Diagnostic</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { padding: 20px; background: #f5f5f5; }
        .success { color: green; }
        .error { color: red; }
        .warning { color: orange; }
        .file-check { font-family: monospace; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Dashboard Asset Diagnostic</h1>
        <p>User: <?php echo htmlspecialchars($current_user['username'] ?? 'Unknown'); ?></p>
        
        <div class="card mt-4">
            <div class="card-header">
                <h3>Asset Loading Test</h3>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <h4>CSS Files</h4>
                        <ul class="list-group">
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                Bootstrap CSS
                                <span class="badge bg-success">Loaded</span>
                            </li>
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                Font Awesome
                                <span class="badge bg-success">Loaded</span>
                            </li>
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                Custom CSS (style.css)
                                <?php
                                $css_url = (function_exists('assets_url') ? assets_url('css/style.css') : '/HMSPOS/assets/css/style.css');
                                $headers = @get_headers($css_url);
                                if ($headers && strpos($headers[0], '200') !== false) {
                                    echo '<span class="badge bg-success">Found</span>';
                                } else {
                                    echo '<span class="badge bg-danger">Not Found - ' . $css_url . '</span>';
                                }
                                ?>
                            </li>
                        </ul>
                    </div>
                    <div class="col-md-6">
                        <h4>JavaScript Files</h4>
                        <ul class="list-group">
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                jQuery
                                <span class="badge bg-success">Loaded</span>
                            </li>
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                Bootstrap JS
                                <span class="badge bg-success">Loaded</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="card mt-4">
            <div class="card-header">
                <h3>File System Check</h3>
            </div>
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>File</th>
                            <th>Status</th>
                            <th>Path</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $files_to_check = [
                            'Custom CSS' => BASE_PATH . '/assets/css/style.css',
                            'Custom JS' => BASE_PATH . '/assets/js/script.js',
                            'Logo' => BASE_PATH . '/assets/img/logo.png',
                            'Favicon' => BASE_PATH . '/assets/img/favicon.png'
                        ];
                        
                        foreach ($files_to_check as $name => $path) {
                            echo '<tr>';
                            echo '<td>' . $name . '</td>';
                            if (file_exists($path)) {
                                echo '<td class="success">✓ Exists</td>';
                            } else {
                                echo '<td class="error">✗ Missing</td>';
                            }
                            echo '<td>' . $path . '</td>';
                            echo '</tr>';
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
        
        <div class="card mt-4">
            <div class="card-header">
                <h3>Test Icons</h3>
            </div>
            <div class="card-body">
                <p>If you see icons below, Font Awesome is working:</p>
                <div class="row">
                    <div class="col-2"><i class="fas fa-home fa-2x"></i> home</div>
                    <div class="col-2"><i class="fas fa-user fa-2x"></i> user</div>
                    <div class="col-2"><i class="fas fa-cog fa-2x"></i> cog</div>
                    <div class="col-2"><i class="fas fa-bell fa-2x"></i> bell</div>
                    <div class="col-2"><i class="fas fa-chart-line fa-2x"></i> chart</div>
                    <div class="col-2"><i class="fas fa-bed fa-2x"></i> bed</div>
                </div>
            </div>
        </div>
        
        <div class="mt-4">
            <a href="dashboard.php" class="btn btn-primary">Try Original Dashboard</a>
            <a href="dashboard_minimal_final.php" class="btn btn-success">Minimal Dashboard</a>
            <a href="logout.php" class="btn btn-danger">Logout</a>
        </div>
    </div>
    
    <!-- Test JavaScript alert -->
    <script>
        console.log('JavaScript is running on diagnostic page');
        document.addEventListener('DOMContentLoaded', function() {
            console.log('DOM fully loaded');
        });
    </script>
</body>
</html>