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

$error_message = '';
$success_message = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $student_id = trim($_POST['student_id']);
    $password = trim($_POST['password']);

    if (empty($student_id) || empty($password)) {
        $error_message = 'ছাত্র আইডি এবং পাসওয়ার্ড প্রয়োজন';
    } else {
        try {
            // First, try to find the student by student_id
            $stmt = $pdo->prepare("SELECT id, user_id, first_name, last_name FROM students WHERE student_id = ?");
            $stmt->execute([$student_id]);
            $student = $stmt->fetch();

            if ($student && $student['user_id']) {
                // Get the user account associated with this student
                $user_stmt = $pdo->prepare("SELECT id, username, password, role, status FROM users WHERE id = ?");
                $user_stmt->execute([$student['user_id']]);
                $user = $user_stmt->fetch();

                if ($user && $user['role'] == 'student' && $user['status'] == 'active') {
                    // Verify the password
                    if (password_verify($password, $user['password'])) {
                        // Login successful
                        $_SESSION['user_id'] = $user['id'];
                        $_SESSION['user_name'] = $student['first_name'] . ' ' . $student['last_name'];
                        $_SESSION['user_role'] = $user['role'];
                        $_SESSION['student_id'] = $student['id'];
                        
                        // Redirect to student dashboard
                        header('Location: student_dashboard.php');
                        exit();
                    } else {
                        $error_message = 'ভুল পাসওয়ার্ড';
                    }
                } else {
                    $error_message = 'ছাত্র অ্যাকাউন্ট পাওয়া যায়নি বা নিষ্ক্রিয়';
                }
            } else {
                $error_message = 'ছাত্র আইডি পাওয়া যায়নি';
            }
        } catch (Exception $e) {
            $error_message = 'লগইন ত্রুটি: ' . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ছাত্র লগইন - শুধু আইডি দিয়ে</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 class="bg-light">
    <div class="container">
        <div class="row min-vh-100 justify-content-center align-items-center">
            <div class="col-lg-5 col-md-6">
                <div class="card shadow">
                    <div class="card-header bg-primary text-white text-center">
                        <h4><i class="fas fa-user-graduate"></i> ছাত্র লগইন</h4>
                        <p class="mb-0">শুধু ছাত্র আইডি দিয়ে লগইন করুন</p>
                    </div>
                    <div class="card-body p-4">
                        <?php if ($error_message): ?>
                            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                <i class="fas fa-exclamation-triangle"></i> <?php echo $error_message; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endif; ?>
                        
                        <?php if ($success_message): ?>
                            <div class="alert alert-success alert-dismissible fade show" role="alert">
                                <i class="fas fa-check-circle"></i> <?php echo $success_message; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endif; ?>

                        <form method="POST">
                            <div class="mb-3">
                                <label for="student_id" class="form-label">ছাত্র আইডি <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="student_id" name="student_id" 
                                       value="<?php echo isset($_POST['student_id']) ? htmlspecialchars($_POST['student_id']) : ''; ?>" required>
                                <div class="form-text">আপনার ছাত্র আইডি দিন</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="password" class="form-label">পাসওয়ার্ড <span class="text-danger">*</span></label>
                                <input type="password" class="form-control" id="password" name="password" required>
                                <div class="form-text">আপনার পাসওয়ার্ড দিন</div>
                            </div>
                            
                            <div class="d-grid">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-sign-in-alt"></i> লগইন
                                </button>
                            </div>
                        </form>
                        
                        <div class="text-center mt-3">
                            <small class="text-muted">
                                শুধুমাত্র ছাত্ররা এই লগইন ব্যবহার করতে পারবেন
                            </small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>