<?php
session_start();
include("../config/db.php");

$error_message = '';

if(isset($_POST['login'])){
    
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Using prepared statements to prevent SQL injection.
    // NOTE: Storing plain-text passwords is a major security risk.
    // Consider using password_hash() when storing and password_verify() when checking.
    $sql = "SELECT * FROM admin WHERE username=? AND password=?";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, "ss", $username, $password);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    if(mysqli_num_rows($result) == 1){
        $_SESSION['admin'] = $username;
        header("Location: dashboard.php");
        exit();
    }else{
        $error_message = "Invalid Login";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login - Houseboat</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #2780f0;
            --secondary: #1d2432;
            --white: #fff;
            --text-gray: #64748b;
        }
        body {
            font-family: 'Poppins', sans-serif;
            background: url('https://images.unsplash.com/photo-1544551763-46a8723ba3f9?auto=format&fit=crop&q=80&w=2070') no-repeat center center/cover;
            margin: 0;
            padding: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            position: relative;
        }
        body::before {
            content: '';
            position: absolute;
            top: 0; left: 0; right: 0; bottom: 0;
            background: rgba(15, 23, 42, 0.4); /* Dark overlay */
            backdrop-filter: blur(5px);
        }
        .login-container {
            position: relative;
            background: rgba(255, 255, 255, 0.95);
            padding: 3.5em 3em 3em;
            border-radius: 16px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
            width: 100%;
            max-width: 400px;
            text-align: center;
            animation: slideUp 0.5s ease-out;
        }
        @keyframes slideUp {
            from { transform: translateY(20px); opacity: 0; }
            to { transform: translateY(0); opacity: 1; }
        }
        .logo-icon {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, var(--primary), #1e60b5);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 32px;
            margin: -75px auto 20px;
            box-shadow: 0 10px 25px rgba(39, 128, 240, 0.4);
        }
        h2 {
            font-family: 'Playfair Display', serif;
            color: var(--secondary);
            margin: 0 0 10px;
            font-size: 2rem;
        }
        .subtitle {
            color: var(--text-gray);
            margin-bottom: 2rem;
            font-size: 0.95rem;
        }
        .form-group {
            position: relative;
            margin-bottom: 1.25rem;
            text-align: left;
        }
        .form-group i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            transition: color 0.3s;
        }
        input[type="text"], 
        input[type="password"] {
            width: 100%;
            padding: 14px 14px 14px 45px;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            box-sizing: border-box;
            font-size: 15px;
            transition: all 0.3s;
            font-family: inherit;
            background: #f8fafc;
        }
        input:focus {
            outline: none;
            border-color: var(--primary);
            background: white;
            box-shadow: 0 0 0 4px rgba(39, 128, 240, 0.1);
        }
        input:focus + i, .form-group:focus-within i {
            color: var(--primary);
        }
        button {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, var(--primary), #1d4ed8);
            border: none;
            color: white;
            border-radius: 10px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 600;
            margin-top: 10px;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(39, 128, 240, 0.3);
        }
        .error {
            background-color: #fef2f2;
            color: #ef4444;
            border: 1px solid #fee2e2;
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo-icon">
            <i class="fas fa-ship"></i>
        </div>
        <form method="post" action="login.php">
            <h2>Welcome Aboard</h2>
            <p class="subtitle">Admin Dashboard Access</p>
            
            <?php if(!empty($error_message)): ?>
                <div class="error">
                    <i class="fas fa-exclamation-circle"></i> 
                    <?php echo $error_message; ?>
                </div>
            <?php endif; ?>
            
            <div class="form-group">
                <i class="fas fa-user"></i>
                <input type="text" name="username" placeholder="Username" required autocomplete="off">
            </div>
            
            <div class="form-group">
                <i class="fas fa-lock"></i>
                <input type="password" name="password" placeholder="Password" required>
            </div>

            <button type="submit" name="login">Sign In</button>
        </form>
    </div>
</body>
</html>