<?php
require_once __DIR__ . '/connect.php';
if (is_logged_in()) {
    header('Location: users.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = isset($_POST['username']) ? trim($_POST['username']) : '';
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    if ($username !== '' && $password !== '') {
        $stmt = $conn->prepare('SELECT username, password FROM admins WHERE username = ? LIMIT 1');
        if ($stmt) {
            $stmt->bind_param('s', $username);
            $stmt->execute();
            $stmt->bind_result($u, $p);
            if ($stmt->fetch()) {
                if ($password === $p || (function_exists('password_verify') && password_verify($password, $p))) {
                    $_SESSION['admin_username'] = $u;
                    header('Location: users.php');
                    exit;
                } else {
                    $error = 'Invalid credentials';
                }
            } else {
                $error = 'Invalid credentials';
            }
            $stmt->close();
        } else {
            $error = 'Server error';
        }
    } else {
        $error = 'Missing fields';
    }
}
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Admin Login</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/png" href="logo.png">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            min-height: 100vh;
            align-items: center;
            justify-content: center;
            background: #f5f7fb;
        }

        .page {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 14px;
            transform: translateY(-40px);
        }

        .box {
            width: 100%;
            max-width: 540px;
            background: #fff;
            padding: 24px;
            border-radius: 12px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, .12);
            text-align: center;
            overflow: hidden;
            margin: 0 16px;
        }

        form {
            display: flex;
            flex-direction: column;
            gap: 12px;
            margin: 0;
        }

        input {
            width: 100%;
            max-width: 100%;
            padding: 12px 14px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
        }

        button {
            width: 100%;
            max-width: 100%;
            padding: 12px 14px;
            min-height: 44px;
            background: #e0ccb9;
            color: #fff;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background: #e0ccb9;
        }

        .small {
            margin-top: 12px;
            font-size: 12px;
            color: #777;
        }

        .logo {
            width: 420px;
            height: auto;
            margin: 0 auto;
            display: block;
        }

        @media (max-width: 480px) {
            .logo {
                width: 280px;
            }

            .box {
                max-width: 100%;
                padding: 16px;
            }

            .page {
                transform: none;
            }
        }
    </style>
</head>

<body>
    <div class="page">
        <img src="logo.png" class="logo" alt="Logo">
        <div class="box">
            <form method="post" action="">
                <input type="text" name="username" placeholder="Username" required>
                <input type="password" name="password" placeholder="Password" required>
                <button type="submit">Login</button>
            </form>
            <div class="small"><a href="index.php">Back to site</a></div>
        </div>
    </div>
    <script>
        <?php if ($error !== ''): ?>
            Swal.fire({
                title: 'Login failed',
                text: '<?php echo htmlspecialchars($error, ENT_QUOTES); ?>',
                icon: 'error'
            });
        <?php endif; ?>
    </script>
</body>

</html>