import React, { useState } from 'react';
import { Head, Link, useForm } from '@inertiajs/react';
import EmployerLayout from '@/Layouts/EmployerLayout';
import {
Plus,
ArrowLeft,
Briefcase,
MapPin,
DollarSign,
Calendar,
LayoutGrid,
Users,
FileText,
Sparkles,
ShieldCheck
} from 'lucide-react';
import { toast } from 'sonner';
export default function Create({ categories: initialCategories }) {
const categories = initialCategories || [
'Electrician', 'Mason', 'Plumber', 'Cleaner', 'Site Supervisor', 'Driver', 'General Helper'
];
const { data, setData, post, processing, errors } = useForm({
title: '',
workers_needed: 1,
location: '',
salary: '',
job_type: 'Full Time',
start_date: '',
description: '',
requirements: ''
});
const [clientErrors, setClientErrors] = useState({});
const handleSubmit = (e) => {
e.preventDefault();
// Client-side validation
const newErrors = {};
if (!data.title.trim()) {
newErrors.title = 'Job title is required.';
}
if (!data.workers_needed || data.workers_needed < 1) {
newErrors.workers_needed = 'Workers needed must be at least 1.';
}
if (!data.location.trim()) {
newErrors.location = 'Job location is required.';
}
if (!data.salary || data.salary < 0) {
newErrors.salary = 'Monthly salary must be a positive number.';
}
if (!data.start_date) {
newErrors.start_date = 'Start date is required.';
}
if (!data.description.trim()) {
newErrors.description = 'Job description is required.';
}
if (Object.keys(newErrors).length > 0) {
setClientErrors(newErrors);
toast.error('Validation failed. Please correct the fields in red.');
return;
}
setClientErrors({});
post('/employer/jobs/create', {
onSuccess: () => {
toast.success('Job posted successfully.');
},
onError: () => {
toast.error('Failed to post job. Please check the inputs.');
}
});
};
return (
Fill in the details to find the perfect workers