<?php
/**
 * Test both student login methods
 * This page demonstrates and tests login via customer number and user ID
 */

require_once 'config/database.php';

echo "<!DOCTYPE html>
<html>
<head>
    <title>Student Login Methods Test</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        .warning { color: orange; }
        table { border-collapse: collapse; width: 100%; margin: 10px 0; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .method-box { 
            border: 2px solid #006747; 
            border-radius: 10px; 
            padding: 20px; 
            margin: 20px 0;
            background-color: #f8fff8;
        }
        .method-title { 
            color: #006747; 
            font-weight: bold; 
            font-size: 1.2em;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>";

echo "<h1>🎓 Student Login Methods Test</h1>";

try {
    echo "<div class='method-box'>";
    echo "<div class='method-title'>📋 Current Database Status</div>";
    
    // Check database structure
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM students");
    $total_students = $stmt->fetch()['total'];
    
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM students WHERE customer_number IS NOT NULL AND customer_number != ''");
    $students_with_customer = $stmt->fetch()['count'];
    
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM students WHERE user_id IS NOT NULL AND user_id != 0");
    $students_with_user = $stmt->fetch()['count'];
    
    echo "<p><strong>Total Students:</strong> $total_students</p>";
    echo "<p><strong>Students with Customer Numbers:</strong> $students_with_customer</p>";
    echo "<p><strong>Students with User Accounts:</strong> $students_with_user</p>";
    
    if ($students_with_customer < $total_students) {
        echo "<p class='warning'>⚠️ Some students are missing customer numbers</p>";
    }
    
    if ($students_with_user < $total_students) {
        echo "<p class='warning'>⚠️ Some students are missing user accounts</p>";
    }
    
    echo "</div>";
    
    // Test Method 1: Customer Number Login
    echo "<div class='method-box'>";
    echo "<div class='method-title'>🔑 Method 1: Login via Customer Number</div>";
    echo "<p>This method allows students to login using their customer number (e.g., CUST202500001)</p>";
    
    // Get sample students with customer numbers
    $stmt = $pdo->query("
        SELECT s.id, s.first_name, s.last_name, s.customer_number, s.student_id, u.username
        FROM students s
        LEFT JOIN users u ON s.user_id = u.id
        WHERE s.customer_number IS NOT NULL AND s.customer_number != ''
        LIMIT 5
    ");
    $sample_customers = $stmt->fetchAll();
    
    if (!empty($sample_customers)) {
        echo "<h3>Sample Customer Login Credentials:</h3>";
        echo "<table>";
        echo "<tr><th>Name</th><th>Customer Number</th><th>Student ID</th><th>Username</th><th>Default Password</th></tr>";
        
        foreach ($sample_customers as $student) {
            echo "<tr>";
            echo "<td>" . $student['first_name'] . " " . $student['last_name'] . "</td>";
            echo "<td><strong>" . $student['customer_number'] . "</strong></td>";
            echo "<td>" . $student['student_id'] . "</td>";
            echo "<td>" . ($student['username'] ?? 'N/A') . "</td>";
            echo "<td><code>123456</code></td>";
            echo "</tr>";
        }
        echo "</table>";
        
        echo "<p><a href='student_login_flexible.php' target='_blank' class='success'>👉 Test Customer Number Login</a></p>";
    } else {
        echo "<p class='error'>❌ No students with customer numbers found</p>";
    }
    echo "</div>";
    
    // Test Method 2: User ID Login
    echo "<div class='method-box'>";
    echo "<div class='method-title'>🆔 Method 2: Login via Student ID/User ID</div>";
    echo "<p>This method allows students to login using their student ID (e.g., 20260011)</p>";
    
    // Get sample students with user accounts
    $stmt = $pdo->query("
        SELECT s.id, s.first_name, s.last_name, s.student_id, u.username, u.id as user_id
        FROM students s
        JOIN users u ON s.user_id = u.id
        WHERE u.role = 'student'
        LIMIT 5
    ");
    $sample_users = $stmt->fetchAll();
    
    if (!empty($sample_users)) {
        echo "<h3>Sample User ID Login Credentials:</h3>";
        echo "<table>";
        echo "<tr><th>Name</th><th>Student ID</th><th>Username</th><th>User ID</th><th>Password</th></tr>";
        
        foreach ($sample_users as $student) {
            echo "<tr>";
            echo "<td>" . $student['first_name'] . " " . $student['last_name'] . "</td>";
            echo "<td><strong>" . $student['student_id'] . "</strong></td>";
            echo "<td>" . $student['username'] . "</td>";
            echo "<td>" . $student['user_id'] . "</td>";
            echo "<td><code>123456</code> or student's password</td>";
            echo "</tr>";
        }
        echo "</table>";
        
        echo "<p><a href='student_login_flexible.php' target='_blank' class='success'>👉 Test User ID Login</a></p>";
    } else {
        echo "<p class='error'>❌ No students with user accounts found</p>";
    }
    echo "</div>";
    
    // Test the flexible login page
    echo "<div class='method-box'>";
    echo "<div class='method-title'>🔄 Flexible Login Page</div>";
    echo "<p>The new flexible login page allows students to choose between both methods:</p>";
    echo "<ul>";
    echo "<li>Radio buttons to select login method</li>";
    echo "<li>Dynamic form labels that change based on selection</li>";
    echo "<li>Single interface for both customer number and user ID login</li>";
    echo "</ul>";
    
    echo "<p><a href='student_login_flexible.php' target='_blank' class='success'>👉 Try the Flexible Login Page</a></p>";
    echo "</div>";
    
    // Verification checklist
    echo "<div class='method-box'>";
    echo "<div class='method-title'>✅ Verification Checklist</div>";
    echo "<p>After testing, verify that:</p>";
    echo "<ol>";
    echo "<li>Students can login using customer numbers ✓</li>";
    echo "<li>Students can login using student IDs ✓</li>";
    echo "<li>Both methods redirect to student dashboard ✓</li>";
    echo "<li>Session data is properly set ✓</li>";
    echo "<li>Error handling works for invalid credentials ✓</li>";
    echo "</ol>";
    echo "</div>";
    
    // Quick links
    echo "<div class='method-box'>";
    echo "<div class='method-title'>⚡ Quick Links</div>";
    echo "<ul>";
    echo "<li><a href='student_login_flexible.php'>Flexible Student Login</a></li>";
    echo "<li><a href='student_dashboard.php'>Student Dashboard</a></li>";
    echo "<li><a href='student_customer_list.php'>Student Customer List</a></li>";
    echo "<li><a href='admin/users.php?role_filter=student'>Admin Student Users</a></li>";
    echo "</ul>";
    echo "</div>";
    
} catch (Exception $e) {
    echo "<div class='error'>";
    echo "<h3>❌ Database Error</h3>";
    echo "<p>" . htmlspecialchars($e->getMessage()) . "</p>";
    echo "</div>";
}

echo "</body></html>";
?>