<?php
include 'koneksi.php';

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

    $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, alamat) 
            VALUES ('$email','$telp','$username','$password','$alamat')
        ");

        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:420px;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" autocomplete="off">
<input type="email" name="email" class="form-control mb-2" placeholder="Email" required autocomplete="off">

<input type="text" name="telp" class="form-control mb-2" placeholder="Nomor Telepon" required autocomplete="off">

<input type="text" name="username" class="form-control mb-2" placeholder="Username" required autocomplete="off">

<input type="password" name="password" class="form-control mb-2" placeholder="Password" required autocomplete="new-password">

<textarea name="alamat" class="form-control mb-3" placeholder="Alamat Pengiriman" rows="3" required autocomplete="off"></textarea>

        <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>