37705-vm/index.php
Flatlogic Bot f96cd48907 v1
2026-01-22 13:33:07 +00:00

136 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/db/config.php';
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
try {
$sessions = db()->query("SELECT *, TIMESTAMPDIFF(MINUTE, start_time, end_time) as duration_mins FROM sleep_sessions ORDER BY start_time DESC LIMIT 10")->fetchAll();
$stats = db()->query("SELECT AVG(TIMESTAMPDIFF(MINUTE, start_time, end_time)) as avg_duration FROM sleep_sessions")->fetch();
$avg_duration = $stats['avg_duration'] ? round($stats['avg_duration'] / 60, 1) : 0;
} catch (PDOException $e) {
$sessions = [];
$avg_duration = 0;
}
$now = date('Y-m-d H:i:s');
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sleep Tracker - Better Nights, Better Days</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
</head>
<body class="bg-night text-light">
<nav class="navbar navbar-expand-lg navbar-dark bg-transparent">
<div class="container">
<a class="navbar-brand fw-bold" href="index.php">🌙 SleepTracker</a>
</div>
</nav>
<main class="container py-5">
<div class="row mb-5">
<div class="col-lg-8 mx-auto text-center">
<h1 class="display-4 fw-bold mb-3">Track Your Sleep</h1>
<p class="lead opacity-75">Consistency is the key to quality rest. Log your sessions and improve your health.</p>
</div>
</div>
<div class="row g-4">
<div class="col-md-4">
<div class="card glass-card p-4">
<h3 class="h5 mb-4">Log New Session</h3>
<form action="add_session.php" method="POST">
<div class="mb-3">
<label for="start_time" class="form-label small opacity-75">Went to bed</label>
<input type="datetime-local" class="form-control" id="start_time" name="start_time" required>
</div>
<div class="mb-3">
<label for="end_time" class="form-label small opacity-75">Woke up</label>
<input type="datetime-local" class="form-control" id="end_time" name="end_time" required>
</div>
<div class="mb-4">
<label class="form-label small opacity-75">Sleep Rating</label>
<div class="d-flex justify-content-between">
<?php for ($i = 1; $i <= 5; $i++): ?>
<div class="form-check form-check-inline m-0">
<input class="form-check-input d-none" type="radio" name="rating" id="r<?= $i ?>" value="<?= $i ?>">
<label class="rating-label" for="r<?= $i ?>"><?= $i ?></label>
</div>
<?php endfor; ?>
</div>
</div>
<button type="submit" class="btn btn-primary w-100 py-3 fw-bold">Save Session</button>
</form>
</div>
<div class="card glass-card mt-4 p-4 text-center">
<p class="mb-0 opacity-75 small">Average Sleep Duration</p>
<h2 class="display-5 fw-bold mb-0"><?= $avg_duration ?> <small class="h6">hours</small></h2>
</div>
</div>
<div class="col-md-8">
<div class="card glass-card p-4">
<h3 class="h5 mb-4">Recent Sessions</h3>
<?php if (empty($sessions)): ?>
<div class="text-center py-5">
<p class="opacity-50">No sessions logged yet. Start tonight!</p>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-dark table-hover align-middle">
<thead>
<tr>
<th>Date</th>
<th>Start</th>
<th>End</th>
<th>Duration</th>
<th>Rating</th>
</tr>
</thead>
<tbody>
<?php foreach ($sessions as $s): ?>
<tr>
<td><?= date('M j', strtotime($s['start_time'])) ?></td>
<td><?= date('H:i', strtotime($s['start_time'])) ?></td>
<td><?= date('H:i', strtotime($s['end_time'])) ?></td>
<td>
<?php
$h = floor($s['duration_mins'] / 60);
$m = $s['duration_mins'] % 60;
echo "{$h}h {$m}m";
?>
</td>
<td>
<span class="badge bg-<?= $s['rating'] >= 4 ? 'success' : ($s['rating'] >= 3 ? 'warning' : 'danger') ?>">
<?= str_repeat('★', $s['rating']) ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</main>
<footer class="container text-center py-5 opacity-50">
<p>&copy; <?= date('Y') ?> SleepTracker App. Keep dreaming.</p>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>