<?php
session_start();

// Credenciais padrão do administrador
$admin_username = 'admin';
$admin_password = 'admin';

if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($username === $admin_username && $password === $admin_password) {
        $_SESSION['admin_logged_in'] = true;
        header('Location: manage_tokens.php');
        exit();
    } else {
        $error = 'Nome de usuário ou senha inválidos.';
    }
}

if (isset($_POST['update_credentials'])) {
    $new_username = $_POST['new_username'];
    $new_password = $_POST['new_password'];

    if (!empty($new_username) && !empty($new_password)) {
        $script_path = __FILE__;
        $script_content = file_get_contents($script_path);
        $script_content = preg_replace(
            "/\\\$admin_username = 'admin';/",
            "\$admin_username = 'admin';",
            $script_content
        );
        $script_content = preg_replace(
            "/\\\$admin_password = 'admin';/",
            "\$admin_password = 'admin';",
            $script_content
        );
        file_put_contents($script_path, $script_content);
        $success = 'Credenciais atualizadas com sucesso! Atualize a página para refletir as mudanças.';
    } else {
        $update_error = 'Por favor, preencha ambos os campos para atualizar as credenciais.';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Login</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body { font-size: 14px; }
        .container { max-width: 500px; padding: 20px; }
        h1, h2 { font-size: 20px; }
        .form-group { margin-bottom: 10px; }
        .btn { padding: 5px 10px; font-size: 14px; }
    </style>
</head>
<body>
<div class="container">
    <h1>Admin Login</h1>
    <form method="post">
        <div class="form-group">
            <label for="username">Username:</label>
            <input type="text" class="form-control" name="username" id="username" required>
        </div>
        <div class="form-group">
            <label for="password">Password:</label>
            <input type="password" class="form-control" name="password" id="password" required>
        </div>
        <?php if (isset($error)) { ?>
            <div class="alert alert-danger">
                <?= htmlspecialchars($error) ?>
            </div>
        <?php } ?>
        <button type="submit" class="btn btn-primary" name="login">Login</button>
    </form>

    <hr>

    <h2>Cadastrar Novo Usuário e Senha</h2>
    <form method="post">
        <div class="form-group">
            <label for="new_username">Novo Username:</label>
            <input type="text" class="form-control" name="new_username" id="new_username" required>
        </div>
        <div class="form-group">
            <label for="new_password">Nova Senha:</label>
            <input type="password" class="form-control" name="new_password" id="new_password" required>
        </div>
        <?php if (isset($update_error)) { ?>
            <div class="alert alert-danger">
                <?= htmlspecialchars($update_error) ?>
            </div>
        <?php } ?>
        <?php if (isset($success)) { ?>
            <div class="alert alert-success" id="success-message">
                <?= htmlspecialchars($success) ?>
            </div>
        <?php } ?>
        <button type="submit" class="btn btn-success" name="update_credentials">Atualizar</button>
    </form>
</div>
<script>
    setTimeout(() => {
        const successMessage = document.getElementById('success-message');
        if (successMessage) successMessage.style.display = 'none';
    }, 5000);
</script>
</body>
</html>
