<?php
$lifetime = 60 * 60 * 12; // 12 часов
session_set_cookie_params([
    'lifetime' => $lifetime,
    'path' => '/',
    'secure' => isset($_SERVER['HTTPS']),
    'httponly' => true,
    'samesite' => 'Lax'
]);
ini_set('session.gc_maxlifetime', $lifetime);
session_start();

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input_login = $_POST['login'] ?? '';
    $input_password = $_POST['password'] ?? '';
    $data = json_decode(file_get_contents('admin_parol.json'), true);

    if (!$data) {
        $error = 'Файл пароля не найден или повреждён.';
    } elseif (
        $input_login === $data['login'] &&
        $input_password === $data['password']
    ) {
        $_SESSION['admin_logged_in'] = true;
    } else {
        $error = 'Неверный логин или пароль!';
    }
}

// Если авторизован — показываем панель, иначе форму входа
$isAuth = isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <script src="https://cdn.tailwindcss.com"></script>

    <meta charset="UTF-8">
    <title>Админ-панель</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.tailwindcss.com">
    <style>
        body { margin: 0; background: #f3f4f6; }
        .sidebar-space { margin-left: 16rem; } /* 64 * 0.25rem = 16rem */
        @media (max-width: 900px) {
            .sidebar-space { margin-left: 0; }
        }
    </style>
</head>
<body>
<?php if ($isAuth): ?>
    <?php include 'admin_sidebar.php'; ?>
    <div class="sidebar-space flex items-center justify-center min-h-screen">
        <div class="bg-white rounded-2xl shadow-xl p-8 max-w-lg w-full text-center">
            <h1 class="text-3xl font-bold mb-4 text-blue-700">Добро пожаловать в админ-панель!</h1>
            <p class="text-gray-600 mb-8">Вы успешно вошли. Используйте меню слева для навигации.</p>
            <img src="https://cdn-icons-png.flaticon.com/512/3135/3135715.png" alt="Welcome" class="mx-auto w-32 h-32 mb-2 opacity-90" loading="lazy">
        </div>
    </div>
<?php else: ?>
    <div class="flex min-h-screen items-center justify-center">
        <form class="bg-white rounded-2xl shadow-xl p-8 w-full max-w-sm" method="post" autocomplete="off">
            <div class="text-2xl font-bold mb-6 text-blue-700 text-center">Вход в админ-панель</div>
            <?php if ($error): ?>
                <div class="bg-red-100 text-red-700 rounded p-2 mb-4 text-center"><?= htmlspecialchars($error) ?></div>
            <?php endif; ?>
            <div class="mb-4">
                <label for="login" class="block mb-1 font-semibold">Логин</label>
                <input type="text" id="login" name="login" class="w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500" required autofocus>
            </div>
            <div class="mb-4">
                <label for="password" class="block mb-1 font-semibold">Пароль</label>
                <input type="password" id="password" name="password" class="w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500" required>
            </div>
            <button class="w-full py-2 rounded bg-blue-700 text-white font-bold hover:bg-blue-800 transition" type="submit">Войти</button>
        </form>
    </div>
<?php endif; ?>
</body>
</html>
