<?php
// Include database configuration
include('../config/database.php');
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $sql = "SELECT * FROM assessors WHERE username = ?";
    if ($stmt = $conn->prepare($sql)) {
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $result = $stmt->get_result();
        if ($result->num_rows == 1) {
            $row = $result->fetch_assoc();
            if (password_verify($password, $row['password'])) {
                $_SESSION['assessor_id'] = $row['id'];
                $_SESSION['assessor_name'] = $row['name'];
                header("Location: index.php");
                exit();
            } else {
                $error = "Invalid password.";
            }
        } else {
            $error = "No user found with that username.";
        }
        $stmt->close();
    }
}

$conn->close();
?>

<?php include('header.php'); ?>
<div class="container mt-5 d-flex justify-content-center">
    <div class="card" style="width: 50%; background-color: #fff; color: #000;">
        <div class="card-body">
            <h2 class="mb-4 text-center">Assessor Login</h2>

            <?php if (isset($error)): ?>
                <div class="alert alert-danger" role="alert">
                    <?php echo $error; ?>
                </div>
            <?php endif; ?>

            <form method="POST" action="login.php">
                <div class="mb-3">
                    <label for="username" class="form-label">Username</label>
                    <input type="text" class="form-control" id="username" name="username" required>
                </div>
                <div class="mb-3">
                    <label for="password" class="form-label">Password</label>
                    <input type="password" class="form-control" id="password" name="password" required>
                </div>
                <button type="submit" class="btn btn-black" style="width: 100%; background-color: #2c2b7c; color: #fff; border-radius: 0;">Login</button>
            </form>
        </div>
    </div>
</div>
<?php include('../includes/footer.php'); ?>
