<?php
require 'check_login.php';
$user = $_SESSION['username'];
$filePath = __DIR__ . "/users.txt";
$kontoDir = __DIR__ . "/konton/";
if (!is_dir($kontoDir))
mkdir($kontoDir);
$kontoFil = $kontoDir . $user . ".txt";
if (!file_exists($kontoFil))
file_put_contents($kontoFil, "1000\n");
// Läs saldo
$transaktioner = file($kontoFil, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$saldo = array_sum(array_map('intval', $transaktioner));
// Hantera insättning/uttag
if (isset($_POST['belopp'])) {
$belopp = intval($_POST['belopp']);
if ($_POST['typ'] === "uttag")
$belopp = -$belopp;
if ($saldo + $belopp < 0) {
$fel = "Otillräckligt saldo.";
} else {
file_put_contents($kontoFil, "$belopp\n", FILE_APPEND);
header("Location: user.php");
exit();
}
}
// Hantera kontoradering (efter bekräftelse)
if (isset($_GET['delete']) && $_GET['delete'] === "1") {
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$newLines = [];
foreach ($lines as $line) {
list($u, $p, $r) = explode(";", $line);
if ($u !== $user)
$newLines[] = $line;
}
// Uppdatera users.txt
file_put_contents($filePath, implode("\n", $newLines) . "\n");
// Ta bort konto-fil
if (file_exists($kontoFil))
unlink($kontoFil);
// Logga ut direkt efter borttagning
session_destroy();
header("Location: login.php?msg=Konto raderat permanent");
exit();
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Min bank</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f0f2f5;
margin: 0;
}
header,
footer {
background: #333;
color: white;
text-align: center;
padding: 20px 15px;
}
/* Knappar under välkommen */
.header-buttons {
margin-top: 15px;
display: flex;
gap: 10px;
justify-content: center;
flex-wrap: wrap;
}
.action-btn,
.logout {
color: white;
text-decoration: none;
padding: 8px 12px;
border-radius: 5px;
transition: 0.3s;
}
.action-btn {
background: #1b91f8;
}
.action-btn:hover {
background: #0268dc;
}
.delete-btn {
background: #d64b3b;
}
.delete-btn:hover {
background: #a9372a;
}
.logout {
background: #555;
}
.logout:hover {
background: #333;
}
main {
max-width: 900px;
margin: 20px auto;
display: flex;
flex-wrap: wrap;
gap: 20px;
}
section {
flex: 1;
min-width: 280px;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.1);
}
h2 {
margin-top: 0;
}
form {
display: flex;
flex-direction: column;
gap: 10px;
}
input[type="number"] {
padding: 8px;
border-radius: 5px;
border: 1px solid #ccc;
max-width: 200px;
}
form label {
display: flex;
align-items: center;
gap: 5px;
}
button {
max-width: 150px;
padding: 8px;
border-radius: 5px;
border: none;
background: #1b91f8;
color: white;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: #0268dc;
}
.error {
color: red;
}
.transaction-list {
max-height: 300px;
overflow-y: auto;
border-top: 1px solid #ddd;
margin-top: 10px;
padding-top: 10px;
}
.transaction-item.plus {
color: green;
}
.transaction-item.minus {
color: red;
}
</style>
<script>
function confirmDelete() {
if (confirm("Är du säker på att du vill radera ditt konto? Detta går inte att ångra!")) {
window.location = "user.php?delete=1";
}
}
</script>
</head>
<body>
<header>
<h1>Välkommen <?= htmlspecialchars($user) ?></h1>
<div class="header-buttons">
<a href="change_password.php" class="action-btn">Byt lösenord</a>
<a href="#" onclick="confirmDelete()" class="action-btn delete-btn">Ta bort konto</a>
<a href="logout.php" class="logout">Logga ut</a>
</div>
</header>
<main>
<section>
<h2>Saldo: <?= $saldo ?> kr</h2>
<?php if (!empty($fel))
echo "<p class='error'>$fel</p>"; ?>
<form method="post">
<input type="number" name="belopp" min="1" placeholder="Belopp" required>
<label><input type="radio" name="typ" value="insattning" checked> Insättning</label>
<label><input type="radio" name="typ" value="uttag"> Uttag</label>
<button>Utför</button>
</form>
</section>
<section>
<h2>Transaktioner</h2>
<div class="transaction-list">
<?php foreach (array_reverse($transaktioner) as $rad):
$belopp = intval($rad);
$klass = $belopp >= 0 ? "plus" : "minus"; ?>
<div class="transaction-item <?= $klass ?>">
<?= $belopp ?> kr
</div>
<?php endforeach; ?>
</div>
</section>
</main>
<footer>
<p>© 2025 Din Bank</p>
</footer>
</body>
</html>