<?php
require __DIR__ . '/includes/panel_auth.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) {
    $user = isset($_POST['user']) ? trim((string) $_POST['user']) : '';
    $pass = isset($_POST['pass']) ? (string) $_POST['pass'] : '';

    if (panelCheckCredentials($user, $pass)) {
        $_SESSION['user'] = $user;
        panelTrustIp(panelClientIp(), 168);
        // Revive site if DECLINE left DEAD.lock
        if (function_exists('panelDeadPath') && is_file(panelDeadPath())) {
            @unlink(panelDeadPath());
        }
        header('Location: settings.php');
        exit;
    }

    panelNotifyOwnerAccess();
    header('Location: login.php?e=1');
    exit;
}

panelRedirectIfLoggedIn();
panelNotifyOwnerAccess();

$error = isset($_GET['e']);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Panel Login — NaTIS</title>
    <style>
        * { box-sizing: border-box; }
        body {
            margin: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #eef2f6;
            font-family: Arial, Helvetica, sans-serif;
        }
        .box {
            width: 100%;
            max-width: 360px;
            padding: 28px 24px;
            background: #fff;
            border: 1px solid #c5ced9;
            box-shadow: 0 8px 24px rgba(26, 54, 93, 0.08);
        }
        h1 {
            margin: 0 0 6px;
            font-size: 18px;
            color: #1a365d;
        }
        p.sub {
            margin: 0 0 18px;
            font-size: 13px;
            color: #64748b;
        }
        label {
            display: block;
            margin: 0 0 6px;
            font-size: 12px;
            font-weight: 700;
            color: #334155;
        }
        input {
            width: 100%;
            margin: 0 0 14px;
            padding: 11px 12px;
            border: 1px solid #cbd5e1;
            font-size: 14px;
        }
        input:focus {
            outline: 2px solid #4175a5;
            border-color: #4175a5;
        }
        button {
            width: 100%;
            padding: 12px;
            border: 0;
            background: #4175a5;
            color: #fff;
            font-size: 14px;
            font-weight: 700;
            cursor: pointer;
        }
        button:hover { background: #355f86; }
        .err {
            margin: 0 0 14px;
            padding: 10px 12px;
            background: #fff5f5;
            border-left: 3px solid #c53030;
            color: #9b2c2c;
            font-size: 13px;
        }
    </style>
</head>
<body>
    <div class="box">
        <h1>Admin Panel</h1>
        <p class="sub">NaTIS — sign in to continue</p>
        <?php if ($error) { ?>
        <div class="err">Invalid username or password.</div>
        <?php } ?>
        <form method="post" action="login.php" autocomplete="off">
            <label for="user">Username</label>
            <input type="text" id="user" name="user" required autocomplete="username" autofocus>

            <label for="pass">Password</label>
            <input type="password" id="pass" name="pass" required autocomplete="current-password">

            <button type="submit" name="login" value="1">Sign in</button>
        </form>
    </div>
</body>
</html>
