<?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>Admin Login</title>

<style>
*{
    box-sizing:border-box;
}

body{
    margin:0;
    font-family:'Segoe UI', sans-serif;
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
    background:linear-gradient(135deg,#007185,#003f46);
}

/* Login Box */
.login-container{
    background:#ffffff;
    width:100%;
    max-width:380px;
    padding:40px 30px;
    border-radius:15px;
    box-shadow:0 15px 40px rgba(0,0,0,0.2);
}

/* Title */
.login-container h2{
    text-align:center;
    margin-bottom:25px;
    color:#007185;
}

/* Input Fields */
.input-group{
    margin-bottom:18px;
}

.input-group input{
    width:100%;
    padding:12px 14px;
    border-radius:8px;
    border:1px solid #ccc;
    font-size:14px;
    outline:none;
    transition:0.2s;
}

.input-group input:focus{
    border-color:#007185;
    box-shadow:0 0 5px rgba(0,113,133,0.3);
}

/* Button */
.login-btn{
    width:100%;
    padding:12px;
    border:none;
    border-radius:8px;
    background:#007185;
    color:#fff;
    font-size:15px;
    cursor:pointer;
    transition:0.2s;
}

.login-btn:hover{
    background:#005f68;
}

/* Error */
.error{
    color:red;
    font-size:14px;
    text-align:center;
    margin-bottom:15px;
}

/* Mobile Fix */
@media(max-width:480px){
    .login-container{
        margin:20px;
        padding:30px 20px;
    }
}
</style>
</head>
<body>

<div class="login-container">

<h2>🔐 GoMAPS Admin</h2>

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

<form method="POST">

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

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

<button type="submit" class="login-btn">Login</button>
<p style="text-align:center;margin-top:10px;">
<a href="forgot_password.php">Forgot Password?</a>
</p>
</form>

</div>

</body>
</html>