"use client";
import { useState } from "react";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import {
DropdownMenu,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
} from "@/components/ui/dropdown-menu";
import { PhosphorIcon } from "@/components/ui/collection-grid-disclosure";
import { ChevronDownIcon } from "@/components/ui/phosphor-icons";
import { cn } from "@/lib/utils";
import { RadiusSlider } from "@/components/poverty/radius-slider";
import type { UserMarker } from "@/components/leaflet-map";
const BACKEND_URL = process.env.NEXT_PUBLIC_BACKEND_URL ?? "http://localhost:4000";
const RELIGION_OPTIONS = [
{ value: "mosque", label: "Masjid (Islam)", icon: "mosque" },
{ value: "church", label: "Gereja (Kristen)", icon: "church" },
{ value: "cathedral", label: "Gereja Katolik", icon: "crown-cross" },
{ value: "temple", label: "Pura (Hindu)", icon: "hands-praying" },
{ value: "vihara", label: "Vihara (Buddha)", icon: "hands-praying" },
{ value: "klenteng", label: "Klenteng (Konghucu)", icon: "hands-praying" },
{ value: "synagogue", label: "Sinagog (Yahudi)", icon: "synagogue" },
];
interface PendingClick { lat: number; lng: number; }
interface ReligionDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
pendingClick: PendingClick | null;
onConfirm: (marker: UserMarker) => void;
}
function Field({ label, children }: { label: string; children: React.ReactNode }) {
return (
{label}
{children}
);
}
export function ReligionDialog({
open,
onOpenChange,
pendingClick,
onConfirm,
}: ReligionDialogProps) {
const [religionType, setReligionType] = useState(null);
const [placeName, setPlaceName] = useState("");
const [radiusMeters, setRadiusMeters] = useState(0);
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState(null);
const selectedReligion = RELIGION_OPTIONS.find((o) => o.value === religionType);
function reset() {
setReligionType(null);
setPlaceName("");
setRadiusMeters(0);
setError(null);
}
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
if (!pendingClick || !religionType) return;
setIsSubmitting(true);
setError(null);
try {
const token = localStorage.getItem("auth_token");
const label = RELIGION_OPTIONS.find((o) => o.value === religionType)?.label ?? religionType;
const name = placeName.trim() || label;
const res = await fetch(`${BACKEND_URL}/poi`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
body: JSON.stringify({
name,
poi_type: "religion",
religion_subtype: religionType,
latitude: pendingClick.lat,
longitude: pendingClick.lng,
radius_meters: radiusMeters,
}),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data.error ?? "Gagal menyimpan data");
}
const saved = await res.json();
onConfirm({
id: `poi-${saved.id}`,
lat: pendingClick.lat,
lng: pendingClick.lng,
type: religionType,
name,
meta: { poi_type: "religion", notes: saved.notes ?? undefined, radius: saved.radius_meters ?? 0 },
});
reset();
onOpenChange(false);
} catch (err: unknown) {
setError(err instanceof Error ? err.message : "Terjadi kesalahan");
} finally {
setIsSubmitting(false);
}
}
function handleCancel() {
reset();
onOpenChange(false);
}
return (
);
}