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 */}
{/* List */}
{filtered.length === 0 ? (
- No results found
) : filtered.map(c => (
-
))}
)}
);
}
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) */}
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 */}
© 2026 Migrant. All rights reserved.
{/* Right Registration Form */}
{renderStepIndicator()}
Create Account
Register your employer portal to start searching
Already have an account?{' '}
Sign in
);
}