2026-01-07 08:06:07 +00:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
import {
|
|
|
|
|
|
Form,
|
|
|
|
|
|
FormControl,
|
|
|
|
|
|
FormField,
|
|
|
|
|
|
FormItem,
|
|
|
|
|
|
FormLabel,
|
|
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
|
import { getAgentById, updateAgent } from "@/service/agent";
|
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
|
import Swal from "sweetalert2";
|
|
|
|
|
|
import withReactContent from "sweetalert2-react-content";
|
|
|
|
|
|
import { X } from "lucide-react";
|
|
|
|
|
|
|
|
|
|
|
|
type AgentFormValues = {
|
|
|
|
|
|
fullName: string;
|
2026-01-22 06:30:45 +00:00
|
|
|
|
job_title: string;
|
2026-01-07 08:06:07 +00:00
|
|
|
|
position: string;
|
|
|
|
|
|
phone: string;
|
|
|
|
|
|
roles: string[];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-22 06:30:45 +00:00
|
|
|
|
const AGENT_TYPES = ["After Sales", "Sales", "Spv", "Branch Manager"];
|
2026-01-07 08:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
export default function UpdateAgentForm({ id }: { id: number }) {
|
2026-01-19 11:25:49 +00:00
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
const MySwal = withReactContent(Swal);
|
|
|
|
|
|
|
2026-01-07 08:06:07 +00:00
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
const [file, setFile] = useState<File | null>(null);
|
|
|
|
|
|
const [preview, setPreview] = useState<string | null>(null);
|
2026-01-19 11:25:49 +00:00
|
|
|
|
const [agentData, setAgentData] = useState<any>(null);
|
2026-01-07 08:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
const form = useForm<AgentFormValues>({
|
|
|
|
|
|
defaultValues: {
|
|
|
|
|
|
fullName: "",
|
|
|
|
|
|
position: "",
|
|
|
|
|
|
phone: "",
|
|
|
|
|
|
roles: [],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
/* ================= FETCH DATA ================= */
|
2026-01-07 08:06:07 +00:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
async function fetchData() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await getAgentById(id);
|
2026-01-19 11:25:49 +00:00
|
|
|
|
const agent = res?.data?.data;
|
|
|
|
|
|
if (!agent) return;
|
2026-01-07 08:06:07 +00:00
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
setAgentData(agent);
|
2026-01-07 08:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
form.reset({
|
2026-01-19 11:25:49 +00:00
|
|
|
|
fullName: agent.name,
|
2026-01-22 06:30:45 +00:00
|
|
|
|
job_title: agent.job_title,
|
2026-01-19 11:25:49 +00:00
|
|
|
|
phone: agent.phone,
|
2026-01-22 06:30:45 +00:00
|
|
|
|
roles: Array.isArray(agent.agent_type) ? agent.agent_type : [],
|
2026-01-07 08:06:07 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
// ✅ FOTO DARI API
|
|
|
|
|
|
setPreview(agent.profile_picture_url ?? null);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("FETCH AGENT ERROR:", error);
|
2026-01-07 08:06:07 +00:00
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fetchData();
|
|
|
|
|
|
}, [id, form]);
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
/* ================= FILE HANDLER ================= */
|
|
|
|
|
|
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
|
const selected = e.target.files?.[0];
|
|
|
|
|
|
if (!selected) return;
|
|
|
|
|
|
|
|
|
|
|
|
setFile(selected);
|
|
|
|
|
|
setPreview(URL.createObjectURL(selected));
|
2026-01-07 08:06:07 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleRemoveFile = () => {
|
|
|
|
|
|
setFile(null);
|
2026-01-19 11:25:49 +00:00
|
|
|
|
setPreview(agentData?.profile_picture_url ?? null);
|
2026-01-07 08:06:07 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
/* ================= SUBMIT ================= */
|
2026-01-07 08:06:07 +00:00
|
|
|
|
const onSubmit = async (data: AgentFormValues) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const formData = new FormData();
|
2026-01-22 06:30:45 +00:00
|
|
|
|
|
2026-01-07 08:06:07 +00:00
|
|
|
|
formData.append("name", data.fullName);
|
2026-01-22 06:30:45 +00:00
|
|
|
|
formData.append("job_title", data.job_title);
|
2026-01-07 08:06:07 +00:00
|
|
|
|
formData.append("phone", data.phone);
|
|
|
|
|
|
|
2026-01-22 06:30:45 +00:00
|
|
|
|
// ✅ MULTI agent_type
|
|
|
|
|
|
formData.append("agent_type", JSON.stringify(data.roles));
|
|
|
|
|
|
|
2026-01-07 08:06:07 +00:00
|
|
|
|
if (file) {
|
|
|
|
|
|
formData.append("file", file);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 06:30:45 +00:00
|
|
|
|
// DEBUG
|
|
|
|
|
|
for (const pair of formData.entries()) {
|
|
|
|
|
|
console.log(pair[0], pair[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 08:06:07 +00:00
|
|
|
|
await updateAgent(id, formData);
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
MySwal.fire({
|
|
|
|
|
|
title: "Berhasil",
|
|
|
|
|
|
text: "Data agen berhasil diperbarui",
|
|
|
|
|
|
icon: "success",
|
|
|
|
|
|
}).then(() => router.push("/admin/agent"));
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("UPDATE AGENT ERROR:", error);
|
2026-01-07 08:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
if (loading) return <p className="p-6">Loading...</p>;
|
2026-01-07 08:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<div className="bg-white p-6 rounded-lg shadow-sm">
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<h2 className="text-2xl font-bold text-teal-800 mb-6">Edit Agen</h2>
|
|
|
|
|
|
|
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
2026-01-19 11:25:49 +00:00
|
|
|
|
{/* BASIC INFO */}
|
|
|
|
|
|
<div className="grid md:grid-cols-3 gap-4">
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<FormField
|
|
|
|
|
|
control={form.control}
|
|
|
|
|
|
name="fullName"
|
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
|
<FormItem>
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<FormLabel>Nama Lengkap</FormLabel>
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<FormControl>
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<Input {...field} />
|
2026-01-07 08:06:07 +00:00
|
|
|
|
</FormControl>
|
|
|
|
|
|
</FormItem>
|
|
|
|
|
|
)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
|
|
control={form.control}
|
2026-01-22 06:30:45 +00:00
|
|
|
|
name="job_title"
|
2026-01-07 08:06:07 +00:00
|
|
|
|
render={({ field }) => (
|
|
|
|
|
|
<FormItem>
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<FormLabel>Jabatan</FormLabel>
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<FormControl>
|
2026-01-22 06:30:45 +00:00
|
|
|
|
<Input {...field} />
|
2026-01-07 08:06:07 +00:00
|
|
|
|
</FormControl>
|
|
|
|
|
|
</FormItem>
|
|
|
|
|
|
)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
|
|
control={form.control}
|
|
|
|
|
|
name="phone"
|
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
|
<FormItem>
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<FormLabel>No Telp</FormLabel>
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<FormControl>
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<Input {...field} />
|
2026-01-07 08:06:07 +00:00
|
|
|
|
</FormControl>
|
|
|
|
|
|
</FormItem>
|
|
|
|
|
|
)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
{/* JENIS AGEN */}
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<div>
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<FormLabel>Jenis Agen</FormLabel>
|
|
|
|
|
|
<div className="flex gap-6 mt-3">
|
|
|
|
|
|
{AGENT_TYPES.map((role) => (
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<FormField
|
|
|
|
|
|
key={role}
|
|
|
|
|
|
control={form.control}
|
|
|
|
|
|
name="roles"
|
2026-01-22 06:30:45 +00:00
|
|
|
|
render={({ field }) => {
|
|
|
|
|
|
const selected = field.value || [];
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<FormItem className="flex items-center gap-2">
|
|
|
|
|
|
<FormControl>
|
|
|
|
|
|
<Checkbox
|
|
|
|
|
|
checked={selected.includes(role)}
|
|
|
|
|
|
onCheckedChange={(checked) => {
|
|
|
|
|
|
const updated = checked
|
|
|
|
|
|
? [...selected, role] // ➕ tambah
|
|
|
|
|
|
: selected.filter((r) => r !== role); // ➖ hapus
|
|
|
|
|
|
|
|
|
|
|
|
field.onChange(updated);
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
<span>{role}</span>
|
|
|
|
|
|
</FormItem>
|
|
|
|
|
|
);
|
|
|
|
|
|
}}
|
2026-01-07 08:06:07 +00:00
|
|
|
|
/>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* FOTO */}
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<div>
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<FormLabel>Foto Agen</FormLabel>
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<div className="flex items-center gap-5 mt-3">
|
2026-01-07 08:06:07 +00:00
|
|
|
|
{preview && (
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<div className="relative w-28 h-28 rounded-md overflow-hidden border">
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<img
|
|
|
|
|
|
src={preview}
|
2026-01-19 11:25:49 +00:00
|
|
|
|
alt="Foto Agen"
|
2026-01-07 08:06:07 +00:00
|
|
|
|
className="w-full h-full object-cover"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={handleRemoveFile}
|
|
|
|
|
|
className="absolute top-1 right-1 bg-red-600 text-white p-1 rounded-full"
|
|
|
|
|
|
>
|
|
|
|
|
|
<X className="w-4 h-4" />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={() => document.getElementById("upload-photo")?.click()}
|
2026-01-19 11:25:49 +00:00
|
|
|
|
className="bg-teal-700 text-white"
|
2026-01-07 08:06:07 +00:00
|
|
|
|
>
|
|
|
|
|
|
Upload Baru
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
<input
|
|
|
|
|
|
id="upload-photo"
|
|
|
|
|
|
type="file"
|
|
|
|
|
|
accept="image/*"
|
2026-01-19 11:25:49 +00:00
|
|
|
|
className="hidden"
|
2026-01-07 08:06:07 +00:00
|
|
|
|
onChange={handleFileChange}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* BUTTON */}
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<div className="grid md:grid-cols-3 gap-4 pt-6">
|
2026-01-07 08:06:07 +00:00
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
2026-01-19 11:25:49 +00:00
|
|
|
|
variant="secondary"
|
2026-01-07 08:06:07 +00:00
|
|
|
|
onClick={() => router.back()}
|
|
|
|
|
|
>
|
|
|
|
|
|
Batal
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
2026-01-19 11:25:49 +00:00
|
|
|
|
<Button
|
|
|
|
|
|
type="submit"
|
|
|
|
|
|
className="md:col-span-2 bg-teal-700 text-white"
|
|
|
|
|
|
>
|
|
|
|
|
|
Simpan Perubahan
|
|
|
|
|
|
</Button>
|
2026-01-07 08:06:07 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</Form>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|