import React, { useState } from 'react';
import { Head, Link, router } from '@inertiajs/react';
import EmployerLayout from '../../../Layouts/EmployerLayout';
import { useTranslation } from '../../../lib/LanguageContext';
import {
CheckCircle2,
Globe2,
Briefcase,
DollarSign,
Calendar,
HeartHandshake,
Languages,
MessageSquare,
Bookmark,
ArrowLeft,
Sparkles,
FileText,
ShieldCheck,
Search,
Star,
Share2,
Flag,
X,
UserCheck,
AlertTriangle,
User,
Phone,
MapPin
} from 'lucide-react';
import { toast } from 'sonner';
const getLanguageFlag = (lang) => {
const flags = {
'English': '🇬🇧',
'Arabic': '🇦🇪',
'Tagalog': '🇵🇭',
'Hindi': '🇮🇳',
'Swahili': '🇰🇪',
'French': '🇫🇷',
'Indonesian': '🇮🇩'
};
return flags[lang] || '🌐';
};
const getExpiryStatus = (expiryDate) => {
if (!expiryDate) return { text: 'Expiry Pending', color: 'text-slate-500 bg-slate-50 border-slate-200' };
const expiry = new Date(expiryDate);
const today = new Date();
expiry.setHours(0,0,0,0);
today.setHours(0,0,0,0);
const diffTime = expiry.getTime() - today.getTime();
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays < 0) {
return {
text: 'Expired',
color: 'text-rose-700 bg-rose-50 border-rose-200 font-black'
};
} else if (diffDays === 0) {
return {
text: 'Expires today',
color: 'text-amber-700 bg-amber-50 border-amber-200 font-black'
};
} else {
return {
text: `Valid until ${expiryDate}`,
color: 'text-emerald-700 bg-emerald-50 border-emerald-150 font-black'
};
}
};
const safeRender = (value) => {
if (value === null || value === undefined) return 'N/A';
if (typeof value === 'object') {
if (value.name) return String(value.name);
if (value.full_name) return String(value.full_name);
return JSON.stringify(value);
}
return String(value);
};
export default function Show({ worker }) {
const { t } = useTranslation();
const passportDoc = worker.documents?.find(d => d.type === 'passport');
const emiratesIdDoc = worker.documents?.find(d => d.type === 'emirates_id' || d.type === 'eid');
const visaDoc = worker.documents?.find(d => d.type === 'visa');
const [showReportModal, setShowReportModal] = useState(false);
const [reportReason, setReportReason] = useState('');
const [reportDetails, setReportDetails] = useState('');
const [isShortlisted, setIsShortlisted] = useState(false);
// Hiring flow states
const [availabilityStatus, setAvailabilityStatus] = useState(worker.availability_status);
const [showHiredModal, setShowHiredModal] = useState(false);
const [showHireConfirmModal, setShowHireConfirmModal] = useState(false);
// Reviews state
const [showReviewModal, setShowReviewModal] = useState(false);
const [newRating, setNewRating] = useState(5);
const [newComment, setNewComment] = useState('');
const [workerReviews, setWorkerReviews] = useState(worker.reviews || []);
const handleShare = () => {
navigator.clipboard.writeText(window.location.href);
toast.success(t('profile_link_copied', 'Profile link copied to clipboard!'), {
description: t('profile_link_copied_desc', 'You can now share this URL with your family or sponsor contacts.')
});
};
const handleReportSubmit = (e) => {
e.preventDefault();
if (!reportReason) {
toast.error(t('select_report_reason', 'Please select a reason for reporting.'));
return;
}
toast.success(t('report_submitted', 'Worker report submitted successfully'), {
description: t('report_submitted_desc', "Our admin compliance team will investigate this worker's credentials within 24 hours.")
});
setShowReportModal(false);
setReportReason('');
setReportDetails('');
};
const handleReviewSubmit = (e) => {
e.preventDefault();
if (!newComment.trim()) {
toast.error(t('add_comment', 'Please add a comment.'));
return;
}
const newReview = {
id: Date.now(),
employer_name: t('you_verified_sponsor', 'You (Verified Sponsor)'),
rating: newRating,
date: t('today', 'Today'),
comment: newComment,
};
setWorkerReviews([newReview, ...workerReviews]);
toast.success(t('review_posted', 'Review posted successfully!'), {
description: t('review_posted_desc', 'Your trust feedback is live and helps other sponsors hire securely.')
});
setShowReviewModal(false);
setNewComment('');
};
const handleToggleShortlist = () => {
setIsShortlisted(!isShortlisted);
router.post('/employer/shortlist/toggle', { worker_id: worker.id }, {
preserveScroll: true,
onSuccess: () => {
toast.success(isShortlisted ? t('removed_from_shortlist', 'Removed from Shortlist') : t('added_to_shortlist', 'Added to Shortlist'));
}
});
};
const handleMarkHired = () => {
router.post(`/employer/workers/${worker.id}/mark-hired`, {}, {
preserveScroll: true,
onSuccess: () => {
setAvailabilityStatus('Hired');
setShowHiredModal(true);
toast.success(t('marked_hired', 'Worker successfully marked as Hired!'), {
description: t('marked_hired_desc', 'Sponsorship direct match finalized. Please leave an anonymous trust review!')
});
}
});
};
return (
{/* Back Link */}
{t('back_to_find_workers', 'BACK TO FIND WORKERS')}
{/* Header Card with Clean Premium White BG */}
{worker.photo ? (
) : (
)}
{worker.name}
{worker.main_profession && (
{worker.main_profession}
)}
{/* Passport verification status bar & Visa Status */}
{worker.visa_expiry_date ? (
Visa Exp: {worker.visa_expiry_date}
) : (
Visa Expiry Pending
)}
{worker.visa_status && (worker.visa_status.toLowerCase().includes('tourist') || worker.visa_status.toLowerCase().includes('visit')) ? (
{worker.visa_status} ({t('warning', 'Warning')}: {t('visit_visa', 'Visit Visa')})
) : worker.visa_status && worker.visa_status.toLowerCase().includes('cancelled') ? (
{worker.visa_status} ({t('warning', 'Warning')}: {t('cancelled', 'Cancelled')})
) : (
{worker.visa_status}
)}
{worker.nationality}
•
{worker.age} {t('years_old', 'Years Old')}
{workerReviews.length > 0 && (
{worker.rating} / 5.0 ({workerReviews.length} {t('reviews', 'reviews')})
)}
{/* Sponsor Actions Block */}
{/* Stage 3 Connect: Start In-App Chat */}
{t('start_direct_chat', 'Start Direct Chat')}
{/* Stage 4 Hire: Mark Hired (Finalize Hiring Direct Flow) */}
{availabilityStatus !== 'Hired' && (
setShowHireConfirmModal(true)}
className="bg-emerald-600 hover:bg-emerald-700 text-white border border-emerald-700 px-8 py-3 rounded-xl font-black text-sm shadow-md transition-all flex items-center justify-center space-x-2 flex-1 sm:flex-initial scale-105 active:scale-100"
>
{t('mark_hired_action', 'Mark Hired')}
)}
{/* Main Details Grid */}
{/* Left Column: Quick Stats */}
{t('worker_details_title', 'Worker Details')}
{t('name', 'Name')}
{worker.name}
{worker.main_profession && (
{t('main_profession', 'Main Profession')}
{worker.main_profession}
)}
{t('gender', 'Gender')}
{worker.gender}
{t('salary_expectations', 'Salary Expectations')}
{worker.salary} {t('aed', 'AED')} / {t('mo', 'mo')}
{t('work_experience', 'Work Experience')}
{worker.experience}
{t('job_type', 'Job Type')}
{worker.preferred_job_type}
{t('accommodation_preference', 'Accommodation Preference')}
{worker.live_in_out}
{t('current_location_status', 'Current Location')}
{worker.in_country ? t('in_country', 'In-Country') : t('out_country', 'Out-of-Country')}
{worker.in_country && (
{t('visa_type', 'Visa Type')}
{worker.visa_status || t('pending', 'Pending')}
)}
{/* Languages Spoken with visual flags */}
{t('languages_spoken', 'Languages Spoken')}
{worker.languages?.map(lang => (
{getLanguageFlag(lang)}
{lang}
))}
{/* Right Column: Bio & Skills */}
{/* Verified Skills & Competencies */}
{t('verified_skills_competencies', 'Verified Skills & Competencies')}
{worker.skills?.map(skill => (
{skill}
))}
{/* Verified Documents Section */}
{t('verified_legal_docs', 'Verified Legal Documents (UAE MOHRE Vetted)')}
{/* Document Card: Passport */}
{passportDoc ? (
{t('passport_details', 'Passport Details')}
{(() => {
const expiryStatus = getExpiryStatus(passportDoc.expiry_date);
return (
{expiryStatus.text}
);
})()}
{passportDoc.ocr_accuracy && (
{passportDoc.ocr_accuracy}% Match
)}
{passportDoc.ocr_data ? (
{t('given_names', 'Given Names')}
{safeRender(passportDoc.ocr_data.given_names)}
{t('surname', 'Surname')}
{safeRender(passportDoc.ocr_data.surname)}
{t('passport_number', 'Passport Number')}
{safeRender(passportDoc.ocr_data.passport_number || passportDoc.number)}
{t('sex', 'Sex')}
{safeRender(passportDoc.ocr_data.sex)}
{t('date_of_birth', 'Date of Birth')}
{safeRender(passportDoc.ocr_data.date_of_birth)}
{t('place_of_birth', 'Place of Birth')}
{safeRender(passportDoc.ocr_data.place_of_birth)}
{t('nationality', 'Nationality')}
{safeRender(passportDoc.ocr_data.nationality || worker.nationality)}
{t('issuing_country', 'Issuing Country')}
{safeRender(passportDoc.ocr_data.issuing_country)}
{t('issue_date', 'Date of Issue')}
{safeRender(passportDoc.ocr_data.date_of_issue || passportDoc.issue_date)}
{t('expiry_date', 'Date of Expiry')}
{safeRender(passportDoc.ocr_data.date_of_expiry || passportDoc.expiry_date)}
{t('authority', 'Authority')}
{safeRender(passportDoc.ocr_data.authority)}
{t('document_type', 'Document Type')}
{safeRender(passportDoc.ocr_data.document_type || passportDoc.type || 'passport')}
) : (
{t('document_id', 'Document ID')}
{passportDoc.number}
{t('issue_date', 'Issue')}
{passportDoc.issue_date || 'N/A'}
{t('expiry_date', 'Expiry')}
{passportDoc.expiry_date || 'N/A'}
)}
UAE PDPL Compliant Scan Storage (Permanently Purged)
{passportDoc.file_path && (
View Passport Scan
)}
) : (
{t('passport_pending', 'Passport Pending')}
{t('passport_pending_desc', 'Candidate has not uploaded a passport document yet.')}
)}
{/* Document Card: Emirates ID */}
{emiratesIdDoc && (
{t('emirates_id_details', 'Emirates ID Details')}
{(() => {
const expiryStatus = getExpiryStatus(emiratesIdDoc.expiry_date);
return (
{expiryStatus.text}
);
})()}
{emiratesIdDoc.ocr_accuracy && (
{emiratesIdDoc.ocr_accuracy}% Match
)}
{emiratesIdDoc.ocr_data ? (
{t('emirates_id_number', 'Emirates ID Number')}
{safeRender(emiratesIdDoc.ocr_data.emirates_id_number || emiratesIdDoc.number)}
{t('full_name', 'Full Name')}
{safeRender(emiratesIdDoc.ocr_data.name || emiratesIdDoc.ocr_data.full_name)}
{t('nationality', 'Nationality')}
{safeRender(emiratesIdDoc.ocr_data.nationality)}
{t('date_of_birth', 'Date of Birth')}
{safeRender(emiratesIdDoc.ocr_data.date_of_birth || emiratesIdDoc.ocr_data.dob)}
{t('gender', 'Gender')}
{safeRender(emiratesIdDoc.ocr_data.gender || emiratesIdDoc.ocr_data.sex)}
{t('occupation', 'Occupation')}
{safeRender(emiratesIdDoc.ocr_data.occupation || emiratesIdDoc.ocr_data.profession)}
{t('employer', 'Employer')}
{safeRender(emiratesIdDoc.ocr_data.employer)}
{t('issue_date', 'Issue Date')}
{safeRender(emiratesIdDoc.ocr_data.issue_date || emiratesIdDoc.issue_date)}
{t('expiry_date', 'Expiry Date')}
{safeRender(emiratesIdDoc.ocr_data.expiry_date || emiratesIdDoc.expiry_date)}
) : (
{t('document_id', 'Document ID')}
{emiratesIdDoc.number}
{t('issue_date', 'Issue')}
{emiratesIdDoc.issue_date || 'N/A'}
{t('expiry_date', 'Expiry')}
{emiratesIdDoc.expiry_date || 'N/A'}
)}
UAE PDPL Compliant Scan Storage (Permanently Purged)
{emiratesIdDoc.file_path && (
View Emirates ID Scan
)}
)}
{/* Document Card: Visa */}
{visaDoc ? (
{t('visa_details', 'Entry Visa Details')}
{(() => {
const expiryStatus = getExpiryStatus(visaDoc.expiry_date || worker.visa_expiry_date);
return (
{expiryStatus.text}
);
})()}
{worker.visa_status && (worker.visa_status.toLowerCase().includes('tourist') || worker.visa_status.toLowerCase().includes('visit') || worker.visa_status.toLowerCase().includes('cancelled')) ? (
{t('transfer_required', 'Transfer Required')}
) : (
{t('clear_records', 'Clear Records')}
)}
{visaDoc.ocr_data ? (() => {
const sponsorObj = visaDoc.ocr_data?.sponsor;
const isSponsorObj = sponsorObj && typeof sponsorObj === 'object' && !Array.isArray(sponsorObj);
const sponsorName = isSponsorObj
? (sponsorObj.name || sponsorObj.sponsor_name)
: (visaDoc.ocr_data?.sponsor_name || (typeof sponsorObj === 'string' ? sponsorObj : ''));
const sponsorPhone = isSponsorObj
? (sponsorObj.phone || sponsorObj.mobile || sponsorObj.mobile_number)
: '';
const sponsorAddress = isSponsorObj
? (sponsorObj.address || '')
: '';
return (
{t('full_name', 'Full Name')}
{safeRender(visaDoc.ocr_data.full_name || worker.name)}
{t('entry_permit_no', 'Entry Permit No')}
{safeRender(visaDoc.ocr_data.entry_permit_no || visaDoc.number)}
{t('uid_no', 'UID No')}
{safeRender(visaDoc.ocr_data.uid_no)}
{t('visa_type', 'Visa Type')}
{safeRender(visaDoc.ocr_data.visa_type || 'ENTRY PERMIT')}
{t('passport_no', 'Passport No')}
{safeRender(visaDoc.ocr_data.passport_no || visaDoc.ocr_data.passport_number)}
{t('profession', 'Profession')}
{safeRender(visaDoc.ocr_data.profession || worker.visa_status)}
{t('sponsor_name', 'Sponsor Name')}
{safeRender(sponsorName || visaDoc.ocr_data.sponsor_name)}
{t('sponsor_phone', 'Sponsor Phone')}
{safeRender(sponsorPhone)}
{t('sponsor_address', 'Sponsor Address')}
{safeRender(sponsorAddress)}
{t('nationality', 'Nationality')}
{safeRender(visaDoc.ocr_data.nationality || worker.nationality)}
{t('country', 'Country')}
{safeRender(visaDoc.ocr_data.country || 'United Arab Emirates')}
{t('date_of_birth', 'Date of Birth')}
{safeRender(visaDoc.ocr_data.date_of_birth || visaDoc.ocr_data.dob)}
{t('place_of_birth', 'Place of Birth')}
{safeRender(visaDoc.ocr_data.place_of_birth)}
{t('issue_date', 'Issue Date')}
{safeRender(visaDoc.ocr_data.issue_date || visaDoc.issue_date)}
{t('valid_until', 'Valid Until')}
{safeRender(visaDoc.ocr_data.valid_until || visaDoc.ocr_data.expiry_date || visaDoc.expiry_date)}
{t('document_type', 'Document Type')}
{safeRender(visaDoc.ocr_data.document_type || visaDoc.type || 'uae_visa')}
{t('ocr_accuracy', 'OCR Accuracy')}
{(visaDoc.ocr_accuracy || visaDoc.ocr_data?.ocr_accuracy || 99)}%
);
})() : (
{t('document_id', 'Document ID')}
{visaDoc.number}
{t('issue_date', 'Issue')}
{visaDoc.issue_date || 'N/A'}
{t('expiry_date', 'Expiry')}
{visaDoc.expiry_date || 'N/A'}
)}
UAE PDPL Compliant Scan Storage (Permanently Purged)
{visaDoc.file_path && (
View Visa Scan
)}
) : (
{t('visa_pending', 'Visa Pending')}
{t('visa_pending_desc', 'Candidate has not uploaded a visa document yet.')}
)}
{/* Star Ratings & Trust Review logs */}
{t('sponsor_reviews_star_ratings', 'Sponsor Reviews & Star Ratings')}
{t('reviews_posted_by_sponsors_desc', 'Ratings can only be posted by verified sponsors who completed verified direct hiring.')}
setShowReviewModal(true)}
className="bg-[#185FA5] hover:bg-[#144f8a] text-white px-5 py-2.5 rounded-xl text-xs font-bold shadow-xs transition-colors"
>
{t('post_trust_review', 'Post a Trust Review')}
{/* Reviews List */}
{workerReviews.map((review) => (
{review.employer_name}
{review.date}
{[...Array(5)].map((_, i) => (
))}
"{review.comment}"
))}
{workerReviews.length === 0 && (
{t('no_reviews_yet', 'No reviews posted yet.')}
)}
{/* Stage 4/5 Hired Flow Success Modal Overlay */}
{showHiredModal && (
{t('direct_sponsoring_finalized', 'Direct Sponsoring Finalized!')}
{t('hired_under_sponsorship_desc', '{name} is successfully marked as Hired under your sponsorship! This direct matching conforms exactly with Stage 4 of our UAE MOHRE zero-middlemen flow.').replace('{name}', worker.name)}
{t('next_step_post_hire_review', 'Next Step: Stage 5 Post-Hire Review')}
{t('help_community_post_review', "Help the UAE community hire safely by posting an anonymous review describing {name}'s childcare, cooking, driving, or domestic skills.").replace('{name}', worker.name)}
{
setShowHiredModal(false);
setShowReviewModal(true);
}}
className="w-full bg-[#185FA5] hover:bg-[#144f8a] text-white py-3 rounded-xl flex items-center justify-center space-x-2"
>
{t('leave_trust_review', 'Leave a Trust Review')}
{t('back_to_worker_directory', 'Back to Worker Directory')}
)}
{/* Report Abuse Modal Overlay */}
{showReportModal && (
)}
{/* Trust Review Modal Overlay */}
{showReviewModal && (
setShowReviewModal(false)}
className="absolute top-4 right-4 text-slate-400 hover:text-slate-600"
>
{t('submit_performance_review', 'Submit a Verified Performance Review')}
{t('feedback_helps_sponsors_desc', 'Your feedback helps other sponsors make secure and direct hiring choices.')}
{t('star_rating', 'Star Rating')}
{[1, 2, 3, 4, 5].map((star) => (
setNewRating(star)}
className="p-1 hover:scale-110 transition-transform"
>
))}
{t('share_your_experience', 'Share Your Experience')}
setNewComment(e.target.value)}
placeholder={t('share_experience_placeholder', 'Tell other sponsors about childcare skills, cleaning, cooking style, driving safety, etc...')}
className="w-full p-2.5 border border-slate-300 rounded-xl text-xs focus:outline-none"
/>
setShowReviewModal(false)}
className="px-4 py-2 border border-slate-200 hover:bg-slate-50 rounded-lg"
>
{t('cancel', 'Cancel')}
{t('post_trust_review_action', 'Post Trust Review')}
)}
{/* Hired Confirmation Modal */}
{showHireConfirmModal && (
{t('confirm_hire_title', 'Confirm Hiring')}
{t('confirm_hire_desc', 'Verify this worker with your needs and make sure to hire this worker. If hired, you can view this worker in the Hired Workers page.')}
setShowHireConfirmModal(false)}
className="flex-1 py-3 border border-slate-200 hover:bg-slate-50 rounded-xl transition-colors"
>
{t('cancel', 'Cancel')}
{
setShowHireConfirmModal(false);
handleMarkHired();
}}
className="flex-1 bg-emerald-600 hover:bg-emerald-700 text-white py-3 rounded-xl shadow-sm transition-colors"
>
{t('confirm_hire_action', 'Confirm Hire')}
)}
);
}