<?php
// Debug the search display issue
require_once 'config/database.php';

// Get all the variables that affect search display
$nameInput = isset($_GET['name']) ? trim($_GET['name']) : '';
$rollInput = isset($_GET['roll']) ? trim($_GET['roll']) : '';
$classIdInput = isset($_GET['class']) ? trim($_GET['class']) : '';
$sessionInput = isset($_GET['session']) ? trim($_GET['session']) : '';
$view = isset($_GET['view']) ? ($_GET['view'] === 'table' ? 'table' : 'grid') : 'grid';

$is_search = isset($_GET['name']) || isset($_GET['roll']) || isset($_GET['class']) || isset($_GET['session']);

// Get classes
$classes = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);

// Get sessions
$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);

?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search Display Debug</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-4">
        <h2>Search Display Debug Page</h2>
        
        <div class="card mb-4">
            <div class="card-header">
                <h5>Debug Information</h5>
            </div>
            <div class="card-body">
                <h6>Current State:</h6>
                <ul>
                    <li>Is Search: <?php echo $is_search ? 'TRUE' : 'FALSE'; ?></li>
                    <li>Name Input: "<?php echo htmlspecialchars($nameInput); ?>"</li>
                    <li>Roll Input: "<?php echo htmlspecialchars($rollInput); ?>"</li>
                    <li>Class ID: "<?php echo htmlspecialchars($classIdInput); ?>"</li>
                    <li>Session: "<?php echo htmlspecialchars($sessionInput); ?>"</li>
                    <li>View: <?php echo $view; ?></li>
                </ul>
                
                <h6>Classes Available:</h6>
                <ul>
                    <?php foreach ($classes as $cls): ?>
                        <li><?php echo $cls['id']; ?>: <?php echo htmlspecialchars($cls['name'] . ' - ' . $cls['section']); ?></li>
                    <?php endforeach; ?>
                </ul>
                
                <h6>Sessions Available:</h6>
                <ul>
                    <?php foreach ($sessions as $sess): ?>
                        <li><?php echo htmlspecialchars($sess); ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>

        <!-- This is the exact search form from public_home.php -->
        <div class="card mb-4">
            <div class="card-body">
                <h5 class="mb-3" style="color: #006747;">
                    <i class="fas fa-search"></i> উন্নত সার্চ (Copy from public_home.php)
                </h5>
                <form method="GET" action="#students" class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">নাম</label>
                        <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($nameInput); ?>" placeholder="শিক্ষার্থীর নাম">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">রোল নম্বর</label>
                        <input type="text" name="roll" class="form-control" value="<?php echo htmlspecialchars($rollInput); ?>" 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 ($classIdInput !== '' && (int)$classIdInput === (int)$cls['id']) ? 'selected' : ''; ?>>
                                    <?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 ($sessionInput !== '' && (string)$sessionInput === (string)$session) ? 'selected' : ''; ?>><?php echo $session; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2 d-flex align-items-end justify-content-between">
                        <div class="btn-group flex-grow-1 me-2">
                            <button type="button" class="btn btn-outline-primary <?php echo ($view === 'grid') ? 'active' : ''; ?>" data-view="grid" title="Grid View"><i class="fas fa-th-large"></i></button>
                            <button type="button" class="btn btn-outline-primary <?php echo ($view === 'table') ? 'active' : ''; ?>" data-view="table" title="Table View"><i class="fas fa-list"></i></button>
                        </div>
                        <button type="submit" class="btn btn-primary" style="min-width: 100px;">
                            <i class="fas fa-search"></i>
                        </button>
                    </div>
                    <input type="hidden" name="view" value="<?php echo htmlspecialchars($view); ?>">
                </form>
            </div>
        </div>

        <div class="alert alert-info">
            <h5>If you can see the search form above, the issue might be:</h5>
            <ol>
                <li>The search form is hidden by CSS/display conditions in public_home.php</li>
                <li>There's a JavaScript error preventing display</li>
                <li>The section is not being rendered due to PHP conditions</li>
            </ol>
        </div>
    </div>
</body>
</html>