import React, { useState, useRef, useEffect } from 'react'; import { Head, Link, router } from '@inertiajs/react'; import axios from 'axios'; import { toast } from 'sonner'; import { Loader2, Users, DollarSign, MessageSquare, ChevronDown, Search } from 'lucide-react'; // Common country dial codes with flag emojis const COUNTRY_CODES = [ { code: 'AE', dial: '+971', flag: '🇦🇪', name: 'United Arab Emirates' }, { code: 'SA', dial: '+966', flag: '🇸🇦', name: 'Saudi Arabia' }, { code: 'IN', dial: '+91', flag: '🇮🇳', name: 'India' }, { code: 'PK', dial: '+92', flag: '🇵🇰', name: 'Pakistan' }, { code: 'PH', dial: '+63', flag: '🇵🇭', name: 'Philippines' }, { code: 'BD', dial: '+880', flag: '🇧🇩', name: 'Bangladesh' }, { code: 'LK', dial: '+94', flag: '🇱🇰', name: 'Sri Lanka' }, { code: 'NP', dial: '+977', flag: '🇳🇵', name: 'Nepal' }, { code: 'EG', dial: '+20', flag: '🇪🇬', name: 'Egypt' }, { code: 'JO', dial: '+962', flag: '🇯🇴', name: 'Jordan' }, { code: 'LB', dial: '+961', flag: '🇱🇧', name: 'Lebanon' }, { code: 'IQ', dial: '+964', flag: '🇮🇶', name: 'Iraq' }, { code: 'KW', dial: '+965', flag: '🇰🇼', name: 'Kuwait' }, { code: 'QA', dial: '+974', flag: '🇶🇦', name: 'Qatar' }, { code: 'BH', dial: '+973', flag: '🇧🇭', name: 'Bahrain' }, { code: 'OM', dial: '+968', flag: '🇴🇲', name: 'Oman' }, { code: 'ET', dial: '+251', flag: '🇪🇹', name: 'Ethiopia' }, { code: 'KE', dial: '+254', flag: '🇰🇪', name: 'Kenya' }, { code: 'UG', dial: '+256', flag: '🇺🇬', name: 'Uganda' }, { code: 'GH', dial: '+233', flag: '🇬🇭', name: 'Ghana' }, { code: 'NG', dial: '+234', flag: '🇳🇬', name: 'Nigeria' }, { code: 'ID', dial: '+62', flag: '🇮🇩', name: 'Indonesia' }, { code: 'MY', dial: '+60', flag: '🇲🇾', name: 'Malaysia' }, { code: 'GB', dial: '+44', flag: '🇬🇧', name: 'United Kingdom' }, { code: 'US', dial: '+1', flag: '🇺🇸', name: 'United States' }, ]; function CountryCodeSelector({ value, onChange, hasError }) { const [open, setOpen] = useState(false); const [search, setSearch] = useState(''); const ref = useRef(null); const searchRef = useRef(null); const selected = COUNTRY_CODES.find(c => c.dial === value) || COUNTRY_CODES[0]; const filtered = COUNTRY_CODES.filter(c => c.name.toLowerCase().includes(search.toLowerCase()) || c.dial.includes(search) ); useEffect(() => { const handler = (e) => { if (ref.current && !ref.current.contains(e.target)) setOpen(false); }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, []); useEffect(() => { if (open && searchRef.current) searchRef.current.focus(); }, [open]); return (
{open && (
{/* Search */}
setSearch(e.target.value)} placeholder="Search country..." className="flex-1 bg-transparent text-xs outline-none text-slate-700 placeholder-slate-400" />
{/* List */}
)}
); } export default function Register({ prefillData }) { const [countryCode, setCountryCode] = useState(prefillData?.country_code || '+971'); // Extract phone without country code let initialPhone = ''; if (prefillData?.phone && prefillData?.country_code) { if (prefillData.phone.startsWith(prefillData.country_code)) { initialPhone = prefillData.phone.substring(prefillData.country_code.length); } else { initialPhone = prefillData.phone; } } else if (prefillData?.phone) { initialPhone = prefillData.phone; } const [data, setData] = useState({ name: prefillData?.name || '', email: prefillData?.email || '', phone: initialPhone, address: prefillData?.address || '', }); const [loading, setLoading] = useState(false); const [errors, setErrors] = useState({}); const handleInputChange = (field, value) => { setData(prev => ({ ...prev, [field]: value })); if (errors[field]) { setErrors(prev => ({ ...prev, [field]: null })); } }; const validateForm = () => { const newErrors = {}; if (!data.name.trim()) { newErrors.name = 'Sponsor name is required.'; } if (!data.email.trim()) { newErrors.email = 'Email address is required.'; } else if (!/\S+@\S+\.\S+/.test(data.email)) { newErrors.email = 'Please enter a valid email address.'; } if (!data.phone.trim()) { newErrors.phone = 'Mobile number is required.'; } else if (!/^[0-9]{4,14}$/.test(data.phone.replace(/^0+/, ''))) { newErrors.phone = 'Please enter a valid mobile number (digits only, without country code).'; } if (!data.address.trim()) { newErrors.address = 'Address is required.'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e) => { e.preventDefault(); setErrors({}); if (!validateForm()) { toast.error('Validation failed. Please correct the fields in red.'); return; } setLoading(true); const formData = new FormData(); formData.append('name', data.name); formData.append('email', data.email); formData.append('phone', countryCode + data.phone.replace(/^0+/, '')); formData.append('country_code', countryCode); formData.append('address', data.address); try { await axios.post('/employer/register', formData, { headers: { 'Content-Type': 'multipart/form-data' } }); toast.success('Registration details saved! Verification code sent to your email.'); router.visit('/employer/verify-email'); } catch (err) { if (err.response) { if (err.response.status === 409) { const serverErrors = err.response.data?.errors || {}; setErrors(serverErrors); const errMsg = serverErrors.email || serverErrors.phone || 'Registration details already registered.'; toast.error(errMsg); } else if (err.response.status === 422) { setErrors(err.response.data.errors || {}); toast.error('Validation failed. Please correct the fields in red.'); } else { toast.error('Something went wrong. Please try again.'); } } else { toast.error('Network error. Please check your internet connection.'); } } finally { setLoading(false); } }; const renderStepIndicator = () => (
{[ { step: 1, label: 'Register' }, { step: 2, label: 'Verify' }, { step: 3, label: 'Emirates ID' }, { step: 4, label: 'Payment' }, { step: 5, label: 'Password' } ].map((s) => (
{s.step}
{s.step === 1 && ( {s.label} )}
{s.step < 5 && (
)} ))}
); return (
{/* Left Brand Panel (Desktop Only) */}
M
Migrant Portal
Employer Registration

Find verified domestic workers directly.

Unlock direct access to top domestic candidates with a Migrant premium subscription. Find, interview, and hire directly without middlemen.

{/* Stat Row */}
500+
Verified Workers
Premium
Employer Access
Direct
Messaging
© 2026 Migrant. All rights reserved.
{/* Right Registration Form */}
{renderStepIndicator()}

Create Account

Register your employer portal to start searching

{/* Company Name */} {/* Sponsor Name */}
handleInputChange('name', e.target.value)} placeholder="e.g. Abdullah Bin Ahmed" className={`w-full px-3.5 py-3 rounded-xl border text-sm focus:outline-none focus:ring-2 transition-all ${ errors.name ? 'border-red-500 focus:ring-red-500/20 focus:border-red-500' : 'border-slate-300 focus:ring-[#185FA5]/20 focus:border-[#185FA5]' }`} /> {errors.name && (

{Array.isArray(errors.name) ? errors.name[0] : errors.name}

)}
{/* Email */}
handleInputChange('email', e.target.value)} placeholder="employer@domain.com" className={`w-full px-3.5 py-3 rounded-xl border text-sm focus:outline-none focus:ring-2 transition-all ${ errors.email ? 'border-red-500 focus:ring-red-500/20 focus:border-red-500' : 'border-slate-300 focus:ring-[#185FA5]/20 focus:border-[#185FA5]' }`} /> {errors.email && (

{Array.isArray(errors.email) ? errors.email[0] : errors.email}

)}
{/* Mobile Number */}
handleInputChange('phone', e.target.value.replace(/[^0-9]/g, ''))} placeholder="501234567" className={`flex-1 px-3.5 py-3 rounded-r-xl border text-sm focus:outline-none focus:ring-2 transition-all ${ errors.phone ? 'border-red-500 focus:ring-red-500/20 focus:border-red-500' : 'border-slate-300 focus:ring-[#185FA5]/20 focus:border-[#185FA5]' }`} />
{errors.phone && (

{Array.isArray(errors.phone) ? errors.phone[0] : errors.phone}

)}
{/* Address */}