<?php
// ✅ Delete installer files if present
if (file_exists('.installed')) {
    @unlink('setup.php');       // Delete installer script
    @unlink('.installed');      // Delete the flag
    @unlink('install.sql');     // ✅ NEW: Delete SQL file
}
// ✅ Start session early
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once 'db.php';

$error = "";

// ✅ Only run login logic on POST
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

$username = trim($_POST['username']);

$stmt = $pdo->prepare("SELECT * FROM admins WHERE BINARY username = ?");
$stmt->execute([$username]);
$admin = $stmt->fetch();
file_put_contents('debug-login.txt', "Trying to match: [$username]\nFound user: " . ($admin ? 'YES' : 'NO') . "\n", FILE_APPEND);


file_put_contents('debug-login.txt', "Entered username: $username\n", FILE_APPEND);
file_put_contents('debug-login.txt', "Entered password: $password\n", FILE_APPEND);
file_put_contents('debug-login.txt', "Found user: " . ($admin ? 'yes' : 'no') . "\n", FILE_APPEND);
file_put_contents('debug-login.txt', "Hash: " . ($admin['password'] ?? 'null') . "\n", FILE_APPEND);
file_put_contents('debug-login.txt', "Match: " . (isset($admin['password']) && password_verify($password, $admin['password']) ? 'yes' : 'no') . "\n", FILE_APPEND);


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

        // ✅ Make session cookie last 1 year
        setcookie(session_name(), session_id(), time() + 31536000, "/");

        header("Location: admin.php");
        exit;
    } else {
        $error = "Invalid login details!";
    }
}

// ✅ Fetch branding settings
$stmt = $pdo->query("SELECT background_image, logo_image FROM branding WHERE id = 1");
$branding = $stmt->fetch();

$backgroundImage = $branding['background_image'] ?? '';
$logoImage = $branding['logo_image'] ?? '';
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login</title>
<link rel="stylesheet" href="assets/css/styles.css">

<style>
body {
    font-family: Arial, sans-serif;
    text-align: center;
    background: #f8f9fa; /* Default background */
    <?php if (!empty($backgroundImage)): ?>
        background-image: url('<?= $backgroundImage; ?>');
        background-repeat: no-repeat;
        background-position: center center;
        background-attachment: fixed;
        background-size: cover;
    <?php endif; ?>
}
    
    .footer {
    text-align: center;
    padding: 20px;
    font-size: 14px;
    color: white;
    background: rgba(0, 0, 0, 0.6); /* Semi-transparent */
    border-radius: 8px;
    width: 80%;
    margin: 40px auto 10px; /* Ensures spacing from other elements */
    position: relative;
    bottom: 0;
    clear: both; /* Forces it to clear any floating elements */
}


</style>

</head>
<body> <?php include 'google-translate-widget-front-end.php'; ?>
<?php if (!empty($logoImage)): ?>
    <div style="text-align: center; padding: 20px;">
        <img src="<?= $logoImage; ?>" alt="Logo" style="max-width: 200px; height: auto;">
    </div>
<?php endif; ?>
    <div class="container">
        <h2>Admin Login</h2>
        <?php if (isset($error)) echo "<p class='error'>$error</p>"; ?>
        <form method="POST">
            <input type="text" name="username" placeholder="Username" required><br>
            <input type="password" name="password" placeholder="Password" required><br>
            <button type="submit">Login</button>
        </form>
    </div>
    
    <footer class="footer">
    &copy; <?= str_replace(['https://', 'http://', 'www.'], '', $_SERVER['HTTP_HOST']); ?>, All Rights Reserved.
</footer>


</body>
</html>

