<?php
require_once __DIR__ . '/connect.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = isset($_POST['name']) ? trim($_POST['name']) : '';
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    if ($name !== '' && $email !== '') {
        $hasEmailCol = false;
        if ($res = $conn->query("SHOW COLUMNS FROM users LIKE 'email'")) {
            $hasEmailCol = $res->num_rows > 0;
            $res->free();
        }
        if ($hasEmailCol) {
            $stmt = $conn->prepare('INSERT INTO users (name, email, created_at) VALUES (?, ?, NOW())');
            if ($stmt) {
                $stmt->bind_param('ss', $name, $email);
                $stmt->execute();
                $stmt->close();
            }
        } else {
            $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;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            min-height: 100vh;
            align-items: center;
            justify-content: center;
            background: #f7f7f7;
        }

        .page {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 14px;
            transform: translateY(-40px);
        }

        .container {
            width: 100%;
            max-width: 640px;
            background: rgba(255, 255, 255, 0.85);
            padding: 24px;
            border-radius: 12px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, .12);
            text-align: center;
            overflow: hidden;
            margin: 0 16px;
        }

        .logo {
            width: 420px;
            height: auto;
            margin: 0 auto;
            display: block;
        }

        form {
            display: flex;
            flex-direction: column;
            gap: 12px;
            margin: 0;
            width: 100%;
        }

        input[type="text"],
        input[type="email"] {
            display: block;
            width: 100%;
            max-width: 100%;
            padding: 12px 14px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            background: #fff;
            box-shadow: none;
            outline: none;
            -webkit-appearance: none;
            appearance: none;
        }

        button {
            display: block;
            width: 100%;
            max-width: 100%;
            padding: 12px 14px;
            min-height: 44px;
            background: #e0ccb9;
            color: #fff;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            -webkit-appearance: none;
            appearance: none;
        }

        button:hover {
            background: #e0ccb9;
        }

        @media (max-width: 480px) {
            .container {
                padding: 16px;
                max-width: 100%;
            }

            .page {
                transform: none;
            }
        }
    </style>
</head>

<body>
    <div class="page">
        <img src="logo.png" alt="Logo" class="logo">
        <div class="container">
            <form method="post" action="">
                <input type="text" name="name" placeholder="Your name" required>
                <input type="email" name="email" placeholder="Your email" required>
                <button type="submit">Register</button>
            </form>
        </div>
    </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>