<?php
require_once '../config/database.php';

if (isLoggedIn()) {
    redirect('../snug some.html');
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = sanitize($_POST['username']);
    $password = $_POST['password'];
    
    if (!empty($username) && !empty($password)) {
        $pdo = getDBConnection();
        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
        $stmt->execute([$username, $username]);
        $user = $stmt->fetch();
        
        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['full_name'] = $user['full_name'];
            
            // Redirect with success parameter
            redirect('../snug some.html?login=success');
        } else {
            $error = 'Invalid username/email or password';
        }
    } else {
        $error = 'Please fill in all fields';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Baby Buckets & Stands</title>
    <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@400;700&display=swap" rel="stylesheet">
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, rgba(245, 224, 195, 0.9) 0%, rgba(251, 231, 178, 0.9) 100%), url('../wallpaper.png');
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            min-height: 100vh;
            color: #4b2e2b;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .login-container {
            background: #fff;
            border-radius: 18px;
            box-shadow: 0 4px 24px #e7cba9;
            padding: 3rem;
            max-width: 400px;
            width: 90%;
            text-align: center;
        }
        
        .logo {
            width: 120px;
            height: auto;
            margin-bottom: 2rem;
        }
        
        h1 {
            color: #f5e0c3;
            font-size: 2rem;
            margin-bottom: 0.5rem;
        }
        
        .subtitle {
            color: #7b4b3a;
            margin-bottom: 2rem;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
            text-align: left;
        }
        
        label {
            display: block;
            margin-bottom: 0.5rem;
            color: #4b2e2b;
            font-weight: 600;
        }
        
        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 12px;
            border: 2px solid #e7cba9;
            border-radius: 10px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
            box-sizing: border-box;
        }
        
        input[type="text"]:focus, input[type="password"]:focus {
            outline: none;
            border-color: #f5e0c3;
        }
        
        .login-btn {
            background: linear-gradient(135deg, #f5e0c3 0%, #e7cba9 100%);
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 25px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 100%;
            margin-top: 1rem;
        }
        
        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(231, 203, 169, 0.4);
        }
        
        .error {
            background: #fbe7b2;
            color: #c62828;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 1rem;
            border: 1px solid #e7cba9;
        }
        
        .back-link {
            margin-top: 2rem;
        }
        
        .back-link a {
            color: #f5e0c3;
            text-decoration: none;
            font-weight: 600;
        }
        
        .back-link a:hover {
            text-decoration: underline;
        }
        
        .register-link {
            background: linear-gradient(135deg, #4caf50 0%, #66bb6a 100%);
            color: white;
            padding: 8px 16px;
            border-radius: 20px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s ease;
            display: inline-block;
            margin-top: 0.5rem;
        }
        
        .register-link:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(76, 175, 80, 0.4);
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <img src="../logo.jpg" alt="Baby Buckets Logo" class="logo">
        <h1>Welcome Back</h1>
        <p class="subtitle">Login to Your Account</p>
        
        <?php if ($error): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <form method="POST" action="">
            <div class="form-group">
                <label for="username">Username or Email</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <button type="submit" class="login-btn">Login</button>
        </form>
        
        <div class="back-link">
            <a href="register.php">Don't have an account? Register</a>
            <br><br>
            <a href="../snug some.html">← Back to Website</a>
        </div>
    </div>
</body>
</html> 