import React, { useState } from "react"; import { base44 } from "@/api/base44Client"; import { useMutation, useQueryClient, useQuery } from "@tanstack/react-query"; import { useNavigate } from "react-router-dom"; import { createPageUrl } from "@/utils"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { MapPin, ArrowLeft, Check } from "lucide-react"; import PageHeader from "@/components/common/PageHeader"; import { useToast } from "@/components/ui/use-toast"; export default function AddSector() { const navigate = useNavigate(); const queryClient = useQueryClient(); const { toast } = useToast(); const { data: enterprises = [] } = useQuery({ queryKey: ['enterprises'], queryFn: () => base44.entities.Enterprise.list(), initialData: [], }); const generateSectorNumber = () => { const randomNum = Math.floor(1000 + Math.random() * 9000); return `SN-${randomNum}`; }; const [sectorData, setSectorData] = useState({ sector_number: generateSectorNumber(), sector_name: "", sector_code: "", parent_enterprise_id: "", parent_enterprise_name: "", sector_type: "Food Service", sector_policies: { uniform_requirements: "", certification_requirements: [], special_training: [] }, is_active: true }); const createSectorMutation = useMutation({ mutationFn: (data) => base44.entities.Sector.create(data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['sectors'] }); toast({ title: "Sector Created", description: "Sector has been successfully added to the platform", }); navigate(createPageUrl("SectorManagement")); }, }); const handleSubmit = (e) => { e.preventDefault(); createSectorMutation.mutate(sectorData); }; const handleEnterpriseChange = (enterpriseId) => { const enterprise = enterprises.find(e => e.id === enterpriseId); setSectorData({ ...sectorData, parent_enterprise_id: enterpriseId, parent_enterprise_name: enterprise?.enterprise_name || "" }); }; return (