<?php session_start(); ?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Register - Pengering Ikan IoT</title>

<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">

<style>
body { font-family: 'Poppins', sans-serif; }
</style>
</head>
<body style="
    background-image: url('assets/img/bg.jpg');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
"></body>

<body class="bg-[#f5f7fa] flex items-center justify-center min-h-screen">

<div class="bg-white p-8 rounded-3xl shadow-sm w-full max-w-md">

    <h2 class="text-2xl font-bold text-center mb-6">Daftar Akun</h2>

    <form id="registerForm" class="space-y-4">

        <div>
            <label class="text-sm">Username</label>
            <input name="reg_username" type="text"
                class="w-full mt-1 p-3 border rounded-xl"
                placeholder="Masukkan username" required>
        </div>

        <div>
            <label class="text-sm">Email</label>
            <input name="reg_email" type="email"
                class="w-full mt-1 p-3 border rounded-xl"
                placeholder="email@gmail.com" required>
        </div>

        <div>
            <label class="text-sm">Password</label>
            <input name="reg_password" type="password"
                class="w-full mt-1 p-3 border rounded-xl"
                placeholder="••••••" required>
        </div>

        <div>
            <label class="text-sm">Konfirmasi Password</label>
            <input name="reg_confirm" type="password"
                class="w-full mt-1 p-3 border rounded-xl"
                placeholder="••••••" required>
        </div>

        <button type="submit"
            class="w-full bg-green-500 text-white p-3 rounded-xl font-semibold">
            DAFTAR
        </button>

    </form>

    <p class="text-sm text-center mt-5">
        Sudah punya akun?
        <a href="index.php" class="text-blue-500 font-semibold">Login</a>
    </p>

</div>

<!-- 🔥 FIREBASE REGISTER -->
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js";

import { 
  getAuth, 
  createUserWithEmailAndPassword 
} from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";

import { 
  getDatabase, 
  ref, 
  set 
} from "https://www.gstatic.com/firebasejs/10.12.2/firebase-database.js";


// 🔥 CONFIG (ISI PUNYAMU)
const firebaseConfig = {
  apiKey: "AIzaSyC-Wdu6vNC0dRyF33S3_C2wpVt9I6wnp0w",
  authDomain: "smartoven-f9fdd.firebaseapp.com",
  databaseURL: "https://smartoven-f9fdd-default-rtdb.firebaseio.com/",
  projectId: "smartoven-f9fdd",
  appId: "1:1005261996032:web:1bcb483b922275f08f98e5"
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getDatabase(app);


// 🔥 REGISTER PROCESS
const form = document.getElementById("registerForm");

form.addEventListener("submit", async (e) => {
    e.preventDefault();

    const username = document.querySelector('[name="reg_username"]').value;
    const email = document.querySelector('[name="reg_email"]').value;
    const password = document.querySelector('[name="reg_password"]').value;
    const confirm = document.querySelector('[name="reg_confirm"]').value;

    if (password !== confirm) {
        alert("Password tidak sama!");
        return;
    }

    try {
        const userCred = await createUserWithEmailAndPassword(auth, email, password);
        const user = userCred.user;

        // 🔥 SIMPAN KE DATABASE
        await set(ref(db, "users/" + user.uid), {
            username: username,
            email: email
        });

        alert("Registrasi berhasil!");
        window.location.href = "index.php";

    } catch (err) {
        alert(err.message);
    }
});
</script>

</body>
</html>
