<?php
require_once __DIR__ . '/connect.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = isset($_POST['name']) ? trim($_POST['name']) : '';
    if ($name !== '') {
        $stmt = $conn->prepare('INSERT INTO users (name, created_at) VALUES (?, NOW())');
        if ($stmt) { $stmt->bind_param('s', $name); $stmt->execute(); $stmt->close(); }
        header('Location: index.php?ok=1'); exit;
    } else { header('Location: index.php?ok=0'); exit; }
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Register</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="logo.png">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
* { box-sizing: border-box; }
html,body { height:100%; margin:0 }
body { font-family: Arial, sans-serif; display:flex; align-items:center; justify-content:center; background:url('logo.png') center center/cover no-repeat fixed; }
.wrap { width:100%; max-width:420px; padding:24px; background:rgba(255,255,255,0.85); border-radius:12px; box-shadow:0 10px 25px rgba(0,0,0,.12); overflow:hidden; margin:0 16px; }
h1 { margin:0 0 12px; font-size:20px; text-align:center; }
input[type="text"] { display:block; width:100%; padding:12px 14px; border:1px solid #ddd; border-radius:8px; font-size:16px; margin:0; }
button { display:block; margin-top:12px; width:100%; padding:12px 14px; background:#e0ccb9; color:#fff; border:none; border-radius:8px; font-size:16px; cursor:pointer; }
button:hover { background:#e0ccb9; }

@media (max-width: 480px) {
  .wrap { padding:16px; }
  h1 { font-size:18px; }
}
</style>
</head>
<body>
<div class="wrap">
<h1>Register</h1>
<form method="post" action="">
<input type="text" name="name" placeholder="Your name" required>
<button type="submit">Register</button>
</form>
</div>
<script>
<?php
if (isset($_GET['ok'])) {
    if ($_GET['ok'] === '1') {
        echo "Swal.fire({title:'Thank you for registering', icon:'success'});";
    } else {
        echo "Swal.fire({title:'Please enter your name', icon:'error'});";
    }
}
?>
</script>
</body>
</html>
