<?php
require_once 'includes/auth.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    if (login($username, $password)) {
        $role = $_SESSION['role'] ?? 'Customer';
        
        // Log login attempt
        try {
            $pdo = db();
            $pdo->prepare("INSERT INTO activity_logs (user_id, action, ip_address) VALUES (?, 'Login', ?)")
                ->execute([$_SESSION['user_id'], $_SERVER['REMOTE_ADDR']]);
        } catch (Exception $e) { /* Ignore logging error */ }

        switch ($role) {
            case 'Admin':
            case 'Super Admin':
            case 'Manager':
                header('Location: /admin/index.php');
                break;
            case 'Dealer':
                header('Location: /dealer/index.php');
                break;
            case 'Customer':
            case 'Buyer':
                header('Location: /buyer/index.php');
                break;
            default:
                header('Location: /index.php');
        }
        exit;
    } else {
        $error = "Invalid username or password";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Car Market</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
        .login-container {
            width: 100%;
            max-width: 400px;
            padding: 2.5rem;
            background: var(--card-bg);
            border-radius: var(--border-radius);
            box-shadow: 0 8px 32px rgba(0,0,0,0.4);
            border: 1px solid var(--border-color);
        }
        .form-group { margin-bottom: 1.5rem; }
        label { display: block; margin-bottom: 0.5rem; color: var(--text-secondary); font-size: 0.9rem; }
        input {
            width: 100%;
            padding: 0.8rem;
            background: var(--bg-color);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            color: var(--text-primary);
            font-size: 1rem;
        }
        input:focus { outline: none; border-color: var(--accent-color); }
        button {
            width: 100%;
            padding: 0.8rem;
            background: var(--accent-color);
            border: none;
            border-radius: 6px;
            color: var(--bg-color);
            font-weight: 700;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.2s;
            margin-top: 1rem;
        }
        button:hover { background: var(--accent-hover); transform: translateY(-1px); }
        .error {
            background: rgba(255, 68, 68, 0.1);
            color: #ff4444;
            padding: 0.8rem;
            border-radius: 6px;
            margin-bottom: 1.5rem;
            text-align: center;
            font-size: 0.9rem;
        }
        h2 { text-align: center; margin-bottom: 2rem; color: var(--accent-color); font-size: 1.8rem; }
        .links { text-align: center; margin-top: 1.5rem; font-size: 0.9rem; color: var(--text-secondary); }
        .links a { color: var(--accent-color); text-decoration: none; font-weight: 600; }
        .links a:hover { text-decoration: underline; }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>Welcome Back</h2>
        <?php if ($error): ?>
            <div class="error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label>Username or Email</label>
                <input type="text" name="username" required placeholder="Enter your username">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" name="password" required placeholder="Enter your password">
            </div>
            <button type="submit">Sign In</button>
            <div class="links">
                Don't have an account? <a href="register.php">Create Account</a><br>
                <a href="/index.php" style="font-size: 0.8rem; opacity: 0.7;">Back to Home</a>
            </div>
        </form>
    </div>
</body>
</html>
