<?php
// Diagnostic file to check what's happening with the search form
ob_start();
require_once 'config/database.php';

// Capture all output
$content = ob_get_contents();
ob_end_clean();

// Get classes and sessions
$classes = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);
$sessions = $pdo->query("SELECT DISTINCT session FROM students WHERE session IS NOT NULL AND session != '' ORDER BY session DESC LIMIT 10")->fetchAll(PDO::FETCH_COLUMN);

// Check if we have data
$has_classes = !empty($classes);
$has_sessions = !empty($sessions);

?>
<!DOCTYPE html>
<html>
<head>
    <title>Search Form Diagnostic</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; }
        .success { background-color: #d4edda; border-color: #c3e6cb; }
        .warning { background-color: #fff3cd; border-color: #ffeaa7; }
        .danger { background-color: #f8d7da; border-color: #f5c6cb; }
    </style>
</head>
<body>
    <div class="container mt-4">
        <h2>Search Form Diagnostic Tool</h2>
        
        <div class="test-section <?php echo $content ? 'danger' : 'success'; ?>">
            <h4>Output Buffering Check</h4>
            <?php if ($content): ?>
                <p class="text-danger"><strong>ERROR:</strong> There was output before the HTML started:</p>
                <pre><?php echo htmlspecialchars($content); ?></pre>
            <?php else: ?>
                <p class="text-success"><strong>GOOD:</strong> No premature output detected</p>
            <?php endif; ?>
        </div>
        
        <div class="test-section <?php echo $has_classes ? 'success' : 'danger'; ?>">
            <h4>Database Connection & Classes</h4>
            <p>Classes found: <strong><?php echo count($classes); ?></strong></p>
            <?php if ($has_classes): ?>
                <p class="text-success">Database query successful, classes available</p>
                <details>
                    <summary>Show classes</summary>
                    <ul>
                    <?php foreach ($classes as $cls): ?>
                        <li><?php echo htmlspecialchars($cls['name'] . ' (' . $cls['section'] . ') - ID: ' . $cls['id']); ?></li>
                    <?php endforeach; ?>
                    </ul>
                </details>
            <?php else: ?>
                <p class="text-danger">No classes found - database issue or no active classes</p>
            <?php endif; ?>
        </div>
        
        <div class="test-section <?php echo $has_sessions ? 'success' : 'warning'; ?>">
            <h4>Sessions Data</h4>
            <p>Sessions found: <strong><?php echo count($sessions); ?></strong></p>
            <?php if ($has_sessions): ?>
                <p class="text-success">Sessions data available</p>
                <details>
                    <summary>Show sessions</summary>
                    <ul>
                    <?php foreach ($sessions as $sess): ?>
                        <li><?php echo htmlspecialchars($sess); ?></li>
                    <?php endforeach; ?>
                    </ul>
                </details>
            <?php else: ?>
                <p class="text-warning">No sessions found - this might be normal if no student data exists</p>
            <?php endif; ?>
        </div>
        
        <div class="test-section <?php echo ($has_classes && !$content) ? 'success' : 'danger'; ?>">
            <h4>Search Form Test</h4>
            <?php if ($has_classes && !$content): ?>
                <p class="text-success"><strong>Ready to display search form</strong></p>
                <div class="card">
                    <div class="card-body">
                        <h5 class="mb-3" style="color: #006747;">
                            <i class="fas fa-search"></i> উন্নত সার্চ (Test Render)
                        </h5>
                        <form method="GET" class="row g-3">
                            <div class="col-md-3">
                                <label class="form-label">নাম</label>
                                <input type="text" name="name" class="form-control" placeholder="শিক্ষার্থীর নাম">
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">রোল নম্বর</label>
                                <input type="text" name="roll" class="form-control" placeholder="রোল নম্বর">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">ক্লাস</label>
                                <select name="class" class="form-select">
                                    <option value="">সকল ক্লাস</option>
                                    <?php foreach ($classes as $cls): ?>
                                        <option value="<?php echo $cls['id']; ?>">
                                            <?php echo htmlspecialchars($cls['name'] . ' - ' . $cls['section']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">সেশন</label>
                                <select name="session" class="form-select">
                                    <option value="">সকল সেশন</option>
                                    <?php foreach ($sessions as $session): ?>
                                        <option value="<?php echo $session; ?>"><?php echo $session; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                                <button type="submit" class="btn btn-primary w-100">
                                    <i class="fas fa-search"></i> Search
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            <?php else: ?>
                <p class="text-danger"><strong>Cannot display search form due to issues above</strong></p>
            <?php endif; ?>
        </div>
        
        <div class="test-section warning">
            <h4>Troubleshooting Steps</h4>
            <ol>
                <li>If you see the search form above but not on public_home.php, the issue is in the integration</li>
                <li>If you don't see the form above, check:
                    <ul>
                        <li>MAMP/Apache is running properly</li>
                        <li>Database connection is working</li>
                        <li>No PHP errors in the error log</li>
                    </ul>
                </li>
                <li>Try accessing: <a href="simple_search_test.php">simple_search_test.php</a></li>
                <li>Try accessing: <a href="debug_search_display.php">debug_search_display.php</a></li>
            </ol>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>