<?php
require_once 'config/database.php';

$message = "";
$status = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['appnum'])) {
    $appnum = trim($_POST['appnum']);
    
    try {
        $db = Database::getInstance();
        $conn = $db->getConn();
        
        $sql = "SELECT id, student_id, full_name, app_num FROM students WHERE app_num = ? LIMIT 1";
        $stmt = $conn->prepare($sql);
        $stmt->execute([$appnum]);
        $student = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($student) {
            $message = "✅ Match found!<br>";
            $message .= "• Student ID: " . $student['student_id'] . "<br>";
            $message .= "• Name: " . $student['full_name'] . "<br>";
            $message .= "• AppNum: " . $student['app_num'];
            $status = "success";
        } else {
            $message = "❌ No student found with AppNum: " . htmlspecialchars($appnum);
            $status = "danger";
        }
        
    } catch (Exception $e) {
        $message = "❌ Error: " . $e->getMessage();
        $status = "danger";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Photo Matching</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h4>Test AppNum Matching</h4>
                    </div>
                    <div class="card-body">
                        <?php if ($message): ?>
                            <div class="alert alert-<?php echo $status; ?>">
                                <?php echo $message; ?>
                            </div>
                        <?php endif; ?>
                        
                        <form method="POST">
                            <div class="mb-3">
                                <label class="form-label">Enter AppNum/GTS Number</label>
                                <input type="text" name="appnum" class="form-control" 
                                       placeholder="e.g., GTS2210167" required>
                                <div class="form-text">Test if this AppNum exists in the database</div>
                            </div>
                            <button type="submit" class="btn btn-primary">Test Matching</button>
                            <a href="upload_pictures.php" class="btn btn-secondary">Back to Upload</a>
                        </form>
                        
                        <div class="mt-4">
                            <h6>Sample AppNum from your CSV:</h6>
                            <ul>
                                <li>GTS2210167</li>
                                <li>GTS2210192</li>
                                <li>GTS2210457</li>
                                <li>GTS2210469</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>