import { Button } from '@/components/ui/button';
import { Calendar } from '@/components/ui/calendar';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { useForm } from '@inertiajs/react';
import AppLayout from '@/layouts/app-layout';
import { Head } from '@inertiajs/react';
import { type BreadcrumbItem } from '@/types';
import InputError from '@/components/input-error';
import { format } from 'date-fns';
import { CalendarIcon } from 'lucide-react';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { cn } from '@/lib/utils';
interface InsuranceFormProps {
mode: 'create' | 'edit';
insurance?: {
id?: string;
code: string;
name: string;
address?: string;
phone_number?: string;
email?: string;
contact_person?: string;
contact_person_phone?: string;
coverage_percentage: number;
coverage_description?: string;
agreement_details?: string;
agreement_start_date?: string;
agreement_end_date?: string;
agreement_file_path?: string;
is_active: boolean;
};
defaults?: {
coverage_percentage?: number;
is_active?: boolean;
};
}
const breadcrumbs: BreadcrumbItem[] = [
{ title: 'Dashboard', href: '/dashboard' },
{ title: 'Asuransi', href: '/insurances' },
{ title: 'Form', href: '#' },
];
const toLocalISOString = (date: Date) => {
const offset = date.getTimezoneOffset();
const localDate = new Date(date.getTime() - (offset * 60 * 1000));
return localDate.toISOString().split('T')[0];
};
export default function InsuranceForm({ mode, insurance, defaults }: InsuranceFormProps) {
const { data, setData, post, put, processing, errors, reset } = useForm({
code: insurance?.code || '',
name: insurance?.name || '',
address: insurance?.address || '',
phone_number: insurance?.phone_number || '',
email: insurance?.email || '',
contact_person: insurance?.contact_person || '',
contact_person_phone: insurance?.contact_person_phone || '',
coverage_percentage: insurance?.coverage_percentage || defaults?.coverage_percentage || 100,
coverage_description: insurance?.coverage_description || '',
agreement_details: insurance?.agreement_details || '',
agreement_start_date: insurance?.agreement_start_date || '',
agreement_end_date: insurance?.agreement_end_date || '',
agreement_file_path: insurance?.agreement_file_path || '',
is_active: insurance?.is_active ?? defaults?.is_active ?? true,
});
const onSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (mode === 'create') {
post(route('insurances.store'), {
onSuccess: () => reset(),
});
} else {
put(route('insurances.update', insurance?.id), {
preserveScroll: true,
});
}
};
return (