<?php
session_start();
include('includes/config.php');

if(isset($_POST['register'])){
    $nama = mysqli_real_escape_string($conn, $_POST['nama']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

    
    $cek = mysqli_query($conn, "SELECT * FROM users WHERE username='$username' OR email='$email'");
    if(mysqli_num_rows($cek) > 0){
        $error = "Username atau email sudah digunakan!";
    } else {
        $sql = "INSERT INTO users (nama, email, username, password, role) 
                VALUES ('$nama','$email','$username','$password','user')";
        if(mysqli_query($conn, $sql)){
            $success = "Registrasi berhasil! Silakan <a href='login_user.php' style='color:#ffeb3b;'>login</a>.";
        } else {
            $error = "Terjadi kesalahan saat registrasi!";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register User - Alfateeh Sepatuku</title>
<style>
body { font-family: 'Poppins', sans-serif; background:#f5f7fa; display:flex; justify-content:center; align-items:center; height:100vh; }
.register-container { background:#1e88e5; padding:40px 30px; border-radius:15px; width:380px; text-align:center; color:#fff; }
.register-container h1 { font-size:2rem; margin-bottom:30px; }
.register-container input { width:100%; padding:12px; margin:10px 0; border-radius:8px; border:none; font-size:1rem; }
.register-container button { width:100%; padding:12px; margin-top:15px; border:none; border-radius:8px; background:#00b4d8; font-weight:700; cursor:pointer; }
.register-container button:hover { background:#1565c0; }
.error-message { color:#ffeb3b; margin-bottom:15px; font-weight:600; }
.success-message { color:#b2ff59; margin-bottom:15px; font-weight:600; }
.login-link { display:block; margin-top:15px; color:#b2ebf2; text-decoration:none; }
.login-link:hover { text-decoration:underline; }
</style>
</head>
<body>
<div class="register-container">
    <h1>Daftar</h1>
    <?php 
    if(isset($error)) echo "<div class='error-message'>$error</div>"; 
    if(isset($success)) echo "<div class='success-message'>$success</div>";
    ?>
    <form method="POST">
        <input type="text" name="nama" placeholder="Nama Lengkap" required>
        <input type="email" name="email" placeholder="Email" required>
        <input type="text" name="username" placeholder="Username" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit" name="register">Daftar</button>
    </form>
    <a href="login_user.php" class="login-link">Sudah punya akun? Login di sini</a>
</div>
</body>
</html>
