"use client"; import React, { useState } from "react"; import Link from "next/link"; import Cookies from "js-cookie"; // import { close, error, loading } from "@/config/swal"; import { useRouter } from "next/navigation"; import withReactContent from "sweetalert2-react-content"; import { Input } from "../ui/input"; import { Button } from "../ui/button"; import { Label } from "../ui/label"; // import { saveActivity } from "@/service/activity-log"; import Swal from "sweetalert2"; import { error } from "console"; import { EyeFilledIcon, EyeSlashFilledIcon } from "../icons"; import { RadioGroup, RadioGroupItem } from "../ui/radio-group"; import { registerTenant } from "@/service/auth"; export default function SignUp() { const router = useRouter(); const [isVisible, setIsVisible] = useState(false); const [isVisibleSetup, setIsVisibleSetup] = useState([false, false]); const [oldEmail, setOldEmail] = useState(""); const [newEmail, setNewEmail] = useState(""); const [passwordSetup, setPasswordSetup] = useState(""); const [confPasswordSetup, setConfPasswordSetup] = useState(""); const toggleVisibility = () => setIsVisible(!isVisible); const [needOtp, setNeedOtp] = useState(false); const [isFirstLogin, setFirstLogin] = useState(false); const [otpValue, setOtpValue] = useState(""); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [accessData, setAccessData] = useState(); const [profile, setProfile] = useState(); const [isValidEmail, setIsValidEmail] = useState(false); const [isResetPassword, setIsResetPassword] = useState(false); const [checkUsernameValue, setCheckUsernameValue] = useState(""); const MySwal = withReactContent(Swal); const setValUsername = (e: any) => { const uname = e.replaceAll(/[^\w.-]/g, ""); setUsername(uname.toLowerCase()); }; const onSubmit = () => { // Simpan userId dummy ke cookie Cookies.set("userId", "3"); // (Opsional) Simpan juga data lainnya jika dibutuhkan // Cookies.set("username", username); // Redirect ke halaman dashboard atau homepage router.push("/"); // Ganti dengan path sesuai kebutuhan }; const [step, setStep] = useState<"login" | "otp">("login"); const [email, setEmail] = useState(""); const [role, setRole] = useState("umum"); const [otp, setOtp] = useState(["", "", "", "", "", ""]); const [membership, setMembership] = useState(""); const [certNumber, setCertNumber] = useState(""); const [membershipType, setMembershipType] = useState(""); const [nrp, setNrp] = useState(""); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [whatsapp, setWhatsapp] = useState(""); const [namaTenant, setNamaTenant] = useState(""); const [tenantPassword, setTenantPassword] = useState(""); const [confirmTenantPassword, setConfirmTenantPassword] = useState(""); const [firstNameKontributor, setFirstNameKontributor] = useState(""); const [lastNameKontributor, setLastNameKontributor] = useState(""); const [whatsappKontributor, setWhatsappKontributor] = useState(""); const [namaPerusahaan, setNamaPerusahaan] = useState(""); const [kategoriPerusahaan, setKategoriPerusahaan] = useState(""); const [kontributorPassword, setKontributorPassword] = useState(""); const [confirmKontributorPassword, setConfirmKontributorPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [formErrors, setFormErrors] = useState({}); const handleSendOtp = (e: React.FormEvent) => { e.preventDefault(); // If role is tenant, handle tenant registration directly if (role === "tenant") { handleTenantRegistration(e); return; } // For other roles, proceed with OTP flow setStep("otp"); }; const handleVerifyOtp = (e: React.FormEvent) => { e.preventDefault(); const code = otp.join(""); // Verifikasi kode OTP console.log("Kode OTP:", code); }; const handleOtpChange = (index: number, value: string) => { if (!/^[0-9]?$/.test(value)) return; const newOtp = [...otp]; newOtp[index] = value; setOtp(newOtp); // Fokus otomatis ke input selanjutnya const nextInput = document.getElementById(`otp-${index + 1}`); if (value && nextInput) nextInput.focus(); }; // Form validation functions const validateEmail = (email: string) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; const validatePhone = (phone: string) => { const phoneRegex = /^[0-9+\-\s()]+$/; return phoneRegex.test(phone) && phone.length >= 10; }; const validatePassword = (password: string) => { return password.length >= 8; }; const validateTenantForm = () => { const errors: any = {}; if (!firstName.trim()) { errors.firstName = "First name is required"; } if (!lastName.trim()) { errors.lastName = "Last name is required"; } if (!email.trim()) { errors.email = "Email is required"; } else if (!validateEmail(email)) { errors.email = "Please enter a valid email address"; } if (!whatsapp.trim()) { errors.whatsapp = "WhatsApp number is required"; } else if (!validatePhone(whatsapp)) { errors.whatsapp = "Please enter a valid phone number"; } if (!namaTenant.trim()) { errors.namaTenant = "Tenant name is required"; } if (!tenantPassword) { errors.tenantPassword = "Password is required"; } else if (!validatePassword(tenantPassword)) { errors.tenantPassword = "Password must be at least 8 characters"; } if (!confirmTenantPassword) { errors.confirmTenantPassword = "Please confirm your password"; } else if (tenantPassword !== confirmTenantPassword) { errors.confirmTenantPassword = "Passwords do not match"; } setFormErrors(errors); return Object.keys(errors).length === 0; }; const handleTenantRegistration = async (e: React.FormEvent) => { e.preventDefault(); if (!validateTenantForm()) { return; } setIsLoading(true); setFormErrors({}); try { const registrationData = { adminUser: { address: "Jakarta", // Default address as per API requirement email: email, fullname: `${firstName} ${lastName}`, password: tenantPassword, phoneNumber: whatsapp, username: `${firstName}-${lastName}` // Using firstName + lastName as username }, client: { clientType: "sub_client", // Hardcoded as per API requirement name: namaTenant, parentClientId: "78356d32-52fa-4dfc-b836-6cebf4e3eead" // Hardcoded as per API requirement } }; const response = await registerTenant(registrationData); if (response.error) { MySwal.fire({ title: "Registration Failed", text: response.message || "An error occurred during registration", icon: "error", confirmButtonText: "OK" }); } else { MySwal.fire({ title: "Registration Successful", text: "Your tenant account has been created successfully!", icon: "success", confirmButtonText: "OK" }).then(() => { // Redirect to login page or dashboard router.push("/auth"); }); } } catch (error) { console.error("Registration error:", error); MySwal.fire({ title: "Registration Failed", text: "An unexpected error occurred. Please try again.", icon: "error", confirmButtonText: "OK" }); } finally { setIsLoading(false); } }; return (
{/* Left Side - Logo Section */}
Mikul News Logo

Portal NetIdhub

Platform beyond classic

{/* Decorative elements */}
{/* Right Side - Login Form */}
netidhub Logo

MENYATUKAN INDONESIA

{step === "login" ? (
{/* Radio Buttons */} setRole(val)} >
{/* Jurnalis: Select Keanggotaan */} {role === "jurnalis" && (
{/* Nomor Sertifikasi */} setCertNumber(e.target.value)} />
)} {/* Kontributor: Form Fields */} {role === "kontributor" && (
setFirstNameKontributor(e.target.value)} /> setLastNameKontributor(e.target.value)} />
setEmail(e.target.value)} /> setWhatsappKontributor(e.target.value)} /> setNamaPerusahaan(e.target.value)} />
setKontributorPassword(e.target.value)} className="pr-10" />
setConfirmKontributorPassword(e.target.value)} className="pr-10" />
)} {/* Tenant: Form Fields */} {role === "tenant" && (
setFirstName(e.target.value)} className={formErrors.firstName ? "border-red-500" : ""} /> {formErrors.firstName && (

{formErrors.firstName}

)}
setLastName(e.target.value)} className={formErrors.lastName ? "border-red-500" : ""} /> {formErrors.lastName && (

{formErrors.lastName}

)}
setEmail(e.target.value)} className={formErrors.email ? "border-red-500" : ""} /> {formErrors.email && (

{formErrors.email}

)}
setWhatsapp(e.target.value)} className={formErrors.whatsapp ? "border-red-500" : ""} /> {formErrors.whatsapp && (

{formErrors.whatsapp}

)}
setNamaTenant(e.target.value)} className={formErrors.namaTenant ? "border-red-500" : ""} /> {formErrors.namaTenant && (

{formErrors.namaTenant}

)}
setTenantPassword(e.target.value)} className={`pr-10 ${formErrors.tenantPassword ? "border-red-500" : ""}`} /> {formErrors.tenantPassword && (

{formErrors.tenantPassword}

)}
setConfirmTenantPassword(e.target.value)} className={`pr-10 ${formErrors.confirmTenantPassword ? "border-red-500" : ""}`} /> {formErrors.confirmTenantPassword && (

{formErrors.confirmTenantPassword}

)}
)} {/* Email Field (Selalu Ada, tapi posisi bergantung role) */} {role !== "tenant" && role !== "kontributor" && ( setEmail(e.target.value)} /> )} {/* Note */}

Dengan mendaftar, saya telah menyetujui{" "} Syarat dan Ketentuan {" "} serta{" "} Kebijakan Privasi

{/* Submit */} {/* Link Login */}

Sudah punya akun?{" "} Login

) : (

Masukkan Kode OTP

Silahkan cek inbox atau kotak spam pada email Anda.

{otp.map((value, index) => ( handleOtpChange(index, e.target.value)} className="w-10 h-12 text-center border border-gray-300 rounded-md text-lg focus:outline-none focus:ring-2 focus:ring-[#B89445]" /> ))}

Kirim Ulang OTP

)}
); }