<?php
session_start();
require_once 'config/database.php';

$student_id = $_GET['student_id'] ?? '1647';

echo "<h1>🔍 Quick Student Check - ID: $student_id</h1>";

// Check if student exists
$stmt = $pdo->prepare("SELECT s.*, c.name as class_name FROM students s LEFT JOIN classes c ON s.class_id = c.id WHERE s.id = ?");
$stmt->execute([$student_id]);
$student = $stmt->fetch();

if ($student) {
    echo "<div style='background: #d1edff; padding: 15px; border: 1px solid #0d6efd; border-radius: 5px; margin-bottom: 20px;'>";
    echo "<h3>✅ Student Found!</h3>";
    echo "<p><strong>Name:</strong> {$student['first_name']} {$student['last_name']}</p>";
    echo "<p><strong>Student ID:</strong> {$student['student_id']}</p>";
    echo "<p><strong>Class:</strong> {$student['class_name']}</p>";
    echo "<p><strong>Group:</strong> {$student['group_name']}</p>";
    echo "<p><strong>Status:</strong> {$student['status']}</p>";
    echo "</div>";
    
    // Check results
    $stmt = $pdo->prepare("
        SELECT r.*, sub.name as subject_name 
        FROM results r 
        JOIN subjects sub ON r.subject_id = sub.id 
        WHERE r.student_id = ? AND r.session = '2024' AND r.exam_type = 'midterm'
        ORDER BY sub.name
    ");
    $stmt->execute([$student_id]);
    $results = $stmt->fetchAll();
    
    echo "<h3>📊 Results (2024 Midterm): " . count($results) . " subjects</h3>";
    
    if (!empty($results)) {
        echo "<table border='1' style='border-collapse: collapse; width: 100%; margin-bottom: 20px;'>";
        echo "<tr style='background: #f8f9fa;'>";
        echo "<th>Subject</th><th>CQ</th><th>MCQ</th><th>Practical</th><th>Total</th><th>Grade</th>";
        echo "</tr>";
        
        $bangla_found = false;
        foreach ($results as $result) {
            if (stripos($result['subject_name'], 'বাংলা') !== false) {
                $bangla_found = true;
                $row_style = 'style="background: #fff3cd;"';
            } else {
                $row_style = '';
            }
            
            echo "<tr $row_style>";
            echo "<td>{$result['subject_name']}</td>";
            echo "<td>{$result['cq_marks']}</td>";
            echo "<td>{$result['mcq_marks']}</td>";
            echo "<td>{$result['practical_marks']}</td>";
            echo "<td>{$result['marks_obtained']}</td>";
            echo "<td>{$result['grade']}</td>";
            echo "</tr>";
        }
        echo "</table>";
        
        if ($bangla_found) {
            echo "<div style='background: #d1edff; padding: 10px; border: 1px solid #0d6efd; border-radius: 5px; margin-bottom: 15px;'>";
            echo "✅ <strong>বাংলা বিষয় পাওয়া গেছে!</strong>";
            echo "</div>";
        } else {
            echo "<div style='background: #f8d7da; padding: 10px; border: 1px solid #dc3545; border-radius: 5px; margin-bottom: 15px;'>";
            echo "❌ <strong>বাংলা বিষয় পাওয়া যায়নি!</strong>";
            echo "</div>";
        }
        
        // Test links
        echo "<div style='text-align: center; margin: 20px 0;'>";
        echo "<a href='compare_tabulation_marksheet.php?student_id=$student_id&session=2024&exam_type=midterm' style='background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin: 5px;'>🔍 Compare Tools</a>";
        echo "<a href='reports/marksheet.php?student_id=$student_id&session=2024&exam_type=midterm&debug=1' style='background: #28a745; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin: 5px;'>📄 Marksheet Debug</a>";
        echo "<a href='reports/marksheet.php?student_id=$student_id&session=2024&exam_type=midterm' style='background: #6f42c1; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin: 5px;'>📄 Marksheet</a>";
        echo "</div>";
        
    } else {
        echo "<div style='background: #f8d7da; padding: 15px; border: 1px solid #dc3545; border-radius: 5px;'>";
        echo "<h3>❌ No Results Found!</h3>";
        echo "<p>This student has no results for 2024 midterm exam.</p>";
        echo "</div>";
    }
    
} else {
    echo "<div style='background: #f8d7da; padding: 15px; border: 1px solid #dc3545; border-radius: 5px;'>";
    echo "<h3>❌ Student Not Found!</h3>";
    echo "<p>No student found with ID: $student_id</p>";
    echo "</div>";
    
    // Show available students
    echo "<h3>📋 Available Students (with results):</h3>";
    $stmt = $pdo->query("
        SELECT DISTINCT s.id, s.student_id, s.first_name, s.last_name, c.name as class_name
        FROM students s
        JOIN classes c ON s.class_id = c.id
        JOIN results r ON s.id = r.student_id
        WHERE r.session = '2024' AND r.exam_type = 'midterm'
        ORDER BY s.id
        LIMIT 10
    ");
    $available_students = $stmt->fetchAll();
    
    echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
    echo "<tr style='background: #f8f9fa;'>";
    echo "<th>ID</th><th>Student ID</th><th>Name</th><th>Class</th><th>Action</th>";
    echo "</tr>";
    
    foreach ($available_students as $avail_student) {
        echo "<tr>";
        echo "<td>{$avail_student['id']}</td>";
        echo "<td>{$avail_student['student_id']}</td>";
        echo "<td>{$avail_student['first_name']} {$avail_student['last_name']}</td>";
        echo "<td>{$avail_student['class_name']}</td>";
        echo "<td><a href='?student_id={$avail_student['id']}' style='background: #007bff; color: white; padding: 3px 8px; text-decoration: none; border-radius: 3px; font-size: 12px;'>Check</a></td>";
        echo "</tr>";
    }
    echo "</table>";
}

echo "<p><a href='reports/marksheet.php'>← Back to Marksheet</a></p>";
?>