<?php
session_start();
include __DIR__ . '/../database/search_log_config.php';

$error = "";

if($_SERVER["REQUEST_METHOD"] === "POST"){

    $email = trim($_POST['email']);
    $password = $_POST['password'];

    $stmt = $log_conn->prepare("
        SELECT * FROM admin_users WHERE admin_email = ?
    ");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if($result->num_rows === 1){

        $admin = $result->fetch_assoc();

        if(password_verify($password, $admin['admin_password'])){

            $_SESSION['admin_logged'] = true;
            $_SESSION['admin_id'] = $admin['id'];
            $_SESSION['admin_name'] = $admin['admin_name'];

            header("Location: dashboard.php");
            exit;
        }
    }

    $error = "Invalid email or password!";
}
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GoMAPS Admin Login</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
:root{
    --primary:#007185;
    --primary-dark:#005f6f;
    --light-bg:#f1f5f9;
    --card-bg:#ffffff;
    --text:#111;
    --muted:#555;
    --border:#e5e7eb;
}

*{
    box-sizing:border-box;
}

body{
    margin:0;
    font-family:'Segoe UI',system-ui,-apple-system,sans-serif;
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
    background:linear-gradient(135deg,var(--primary),var(--primary-dark));
}

/* Login Wrapper */
.login-wrapper{
    width:100%;
    max-width:420px;
    padding:20px;
}

/* Login Card */
.login-card{
    background:var(--card-bg);
    padding:45px 35px;
    border-radius:18px;
    box-shadow:0 25px 60px rgba(0,0,0,0.25);
    text-align:center;
}

/* Logo */
.login-logo{
    display:flex;
    justify-content:center;
    align-items:center;
    gap:10px;
    margin-bottom:20px;
}

.login-logo img{
    width:35px;
    height:35px;
}

.login-logo span{
    font-size:20px;
    font-weight:600;
    color:var(--primary);
}

/* Subtitle */
.login-subtitle{
    font-size:14px;
    color:var(--muted);
    margin-bottom:25px;
}

/* Input Group */
.input-group{
    margin-bottom:18px;
    position:relative;
}

.input-group input{
    width:100%;
    padding:13px 15px 13px 40px;
    border-radius:10px;
    border:1px solid var(--border);
    font-size:14px;
    outline:none;
    transition:.2s ease;
}

.input-group i{
    position:absolute;
    left:14px;
    top:50%;
    transform:translateY(-50%);
    color:var(--muted);
    font-size:14px;
}

.input-group input:focus{
    border-color:var(--primary);
    box-shadow:0 0 0 3px rgba(0,113,133,0.15);
}

/* Button */
.login-btn{
    width:100%;
    padding:12px;
    border:none;
    border-radius:10px;
    background:var(--primary);
    color:#fff;
    font-size:15px;
    font-weight:500;
    cursor:pointer;
    transition:.2s ease;
}

.login-btn:hover{
    background:var(--primary-dark);
    transform:translateY(-2px);
}

/* Error */
.error{
    background:#ffe5e5;
    color:#d8000c;
    padding:10px;
    border-radius:8px;
    font-size:13px;
    margin-bottom:18px;
}

/* Footer Text */
.login-footer{
    margin-top:15px;
    font-size:13px;
    color:var(--muted);
}

.login-footer a{
    color:var(--primary);
    text-decoration:none;
    font-weight:500;
}

.login-footer a:hover{
    text-decoration:underline;
}

/* Responsive */
@media(max-width:480px){
    .login-card{
        padding:35px 25px;
    }
}
</style>
</head>
<body>

<div class="login-wrapper">

<div class="login-card">

<div class="login-logo">
    <img src="https://pritsco.com/android-apps/search-engine/assets/gomaps-logo.png" alt="GoMAPS Logo">
    <span>GoMAPS Admin</span>
</div>

<div class="login-subtitle">
    Secure access to admin dashboard
</div>

<?php if(!empty($error)): ?>
<div class="error"><?= $error ?></div>
<?php endif; ?>

<form method="POST">

<div class="input-group">
<i class="fa-solid fa-envelope"></i>
<input type="email" name="email" placeholder="Admin Email" required>
</div>

<div class="input-group">
<i class="fa-solid fa-lock"></i>
<input type="password" name="password" placeholder="Password" required>
</div>

<button type="submit" class="login-btn">
    Login
</button>

<div class="login-footer">
    <a href="forgot_password.php">Forgot Password?</a>
</div>

</form>

</div>
</div>

</body>
</html>