<?php
include 'koneksi.php';

if (isset($_POST['register'])) {
    $email = trim($_POST['email']);
    $telp = trim($_POST['telp']);
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    $cekUser = mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");
    if (mysqli_num_rows($cekUser) > 0) {
        echo "<script>alert('Username sudah digunakan');</script>";
    } else {
        mysqli_query($conn, "INSERT INTO users (email, telp, username, password) 
        VALUES ('$email','$telp','$username','$password')");

        echo "<script>alert('Registrasi berhasil'); window.location='index.php';</script>";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Register - Warung Pojok</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="display:flex;justify-content:center;align-items:center;height:100vh;background:#f4f6f9;">

<div style="width:400px;background:white;padding:30px;border-radius:15px;box-shadow:0 5px 15px rgba(0,0,0,0.2);">
    
    <h3 style="text-align:center;">Registrasi Akun</h3>

    <form method="POST">
        <input type="email" name="email" class="form-control mb-2" placeholder="Email" required>
        <input type="text" name="telp" class="form-control mb-2" placeholder="Nomor Telepon" required>
        <input type="text" name="username" class="form-control mb-2" placeholder="Username" required>
        <input type="password" name="password" class="form-control mb-2" placeholder="Password" required>

        <button type="submit" name="register" class="btn btn-success w-100">Daftar</button>
    </form>

    <br>
    <a href="index.php">← Kembali ke Login</a>

</div>

</body>
</html>