"use client"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Checkbox } from "@/components/ui/checkbox"; import { Button } from "@/components/ui/button"; import { createAgent } from "@/service/agent"; import { useRouter } from "next/navigation"; import withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; type AgentFormValues = { fullName: string; position: string; phone: string; roles: string[]; profileImage: File | null; }; const agentTypes = ["After Sales", "Sales", "Spv", "Branch Manager"]; export default function AddAgentForm() { const [previewImg, setPreviewImg] = useState(null); const router = useRouter(); const MySwal = withReactContent(Swal); const form = useForm({ defaultValues: { fullName: "", position: "", phone: "", roles: [], profileImage: null, }, }); const handleImageChange = (file?: File) => { if (!file) return; form.setValue("profileImage", file); const preview = URL.createObjectURL(file); setPreviewImg(preview); }; const onSubmit = async (data: AgentFormValues) => { try { const payload = { name: data.fullName, job_title: data.position, phone: data.phone, agent_type: data.roles, // langsung array profile_picture_path: data.profileImage ? data.profileImage.name : "", }; const res = await createAgent(payload); console.log("SUCCESS:", res); } catch (err) { console.error("ERROR CREATE AGENT:", err); } successSubmit("/admin/agent"); }; function successSubmit(redirect: any) { MySwal.fire({ title: "Sukses", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { router.push(redirect); } }); } return (

Form Tambah Agen

{/* GRID INPUT */}
{/* Nama */} ( Nama Lengkap * )} /> {/* Jabatan */} ( Jabatan * )} /> {/* No Telp */} ( No Telp * )} />
{/* CHECKBOX ROLE */}
Pilih Jenis Agen *
{agentTypes.map((role) => ( { const selected = field.value || []; return ( { const updated = checked ? [...selected, role] : selected.filter((i) => i !== role); field.onChange(updated); }} /> {role} ); }} /> ))}
{/* UPLOAD FOTO */}
Upload Foto Profile *
{previewImg ? ( ) : (

Klik untuk upload atau drag & drop

PNG, JPG (max 2 MB)

)} handleImageChange(e.target.files?.[0])} />
{/* BUTTON */}
); }