<?php
// --- CUSTOM SESSION PATH FIX ---
$sessionPath = dirname(__DIR__) . '/sessions';
if (!file_exists($sessionPath)) {
    @mkdir($sessionPath, 0755, true);
}
ini_set('session.save_path', $sessionPath);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

include '../config.php';

// যদি ইতিমধ্যে লগইন করা থাকে
if (isset($_SESSION['admin_logged_in'])) {
    header("Location: categories.php");
    exit;
}

$error = "";

if (isset($_POST['login'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $pass  = $_POST['password'];

    $res = $conn->query("SELECT * FROM admin_users WHERE email='$email'");

    if ($res->num_rows > 0) {
        $row = $res->fetch_assoc();

        if (password_verify($pass, $row['password'])) {
            $_SESSION['admin_logged_in'] = true;

            header("Location: categories.php");
            exit;
        } else {
            $error = "Wrong Password!";
        }
    } else {
        $error = "Email not found!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login - TurkyHub</title>

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

<style>
body{
    background:#0b0c10;
    color:#fff;
    font-family:sans-serif;
    display:flex;
    justify-content:center;
    align-items:center;
    height:100vh;
    margin:0;
}

.login-box{
    background:#1f2833;
    padding:40px;
    border-radius:12px;
    border:1px solid #00f2ff;
    width:320px;
    text-align:center;
    box-shadow:0 0 30px rgba(0,242,255,.2);
}

h2{
    color:#00f2ff;
    margin-bottom:25px;
    letter-spacing:2px;
}

input{
    width:100%;
    padding:12px;
    margin:10px 0;
    background:#000;
    border:1px solid #333;
    color:#fff;
    box-sizing:border-box;
    border-radius:5px;
    outline:none;
}

input:focus{
    border-color:#00f2ff;
}

.password-box{
    position:relative;
    width:100%;
    margin:10px 0;
}

.password-box input{
    width:100%;
    padding-right:45px;
    margin:0;
}

.toggle-password{
    position:absolute;
    top:50%;
    right:15px;
    transform:translateY(-50%);
    cursor:pointer;
    color:#00f2ff;
    font-size:18px;
}

.toggle-password:hover{
    color:#fff;
}

button{
    width:100%;
    padding:12px;
    background:#00f2ff;
    border:none;
    font-weight:bold;
    cursor:pointer;
    color:#000;
    margin-top:15px;
    border-radius:5px;
    text-transform:uppercase;
}

button:hover{
    opacity:.9;
}

.error{
    color:#ff3333;
    font-size:13px;
    margin-bottom:15px;
    background:rgba(255,0,0,.1);
    padding:8px;
    border-radius:5px;
}

.back-link{
    display:block;
    margin-top:20px;
    color:#666;
    text-decoration:none;
    font-size:12px;
}

.back-link:hover{
    color:#00f2ff;
}
</style>
</head>

<body>

<div class="login-box">
    <h2>ADMIN LOGIN</h2>

    <?php if($error) echo "<div class='error'>$error</div>"; ?>

    <form method="POST">

        <input type="email" name="email" placeholder="Admin Email" required>

        <div class="password-box">
            <input type="password" id="password" name="password" placeholder="Password" required>
            <i class="fa-solid fa-eye toggle-password" id="eyeIcon" onclick="togglePassword()"></i>
        </div>

        <button type="submit" name="login">LOGIN NOW</button>

    </form>

    <a href="../index.php" class="back-link">← Back to Website</a>

</div>

<script>
function togglePassword() {
    const password = document.getElementById("password");
    const eyeIcon = document.getElementById("eyeIcon");

    if (password.type === "password") {
        password.type = "text";
        eyeIcon.classList.remove("fa-eye");
        eyeIcon.classList.add("fa-eye-slash");
    } else {
        password.type = "password";
        eyeIcon.classList.remove("fa-eye-slash");
        eyeIcon.classList.add("fa-eye");
    }
}
</script>

</body>
</html>