"use client"; import { useState } from "react"; import { apiFetch, ApiError } from "@/lib/api"; import { useList } from "@/lib/useResource"; import { useToast } from "@/components/toast"; import { DataTable, type Column } from "@/components/DataTable"; import { Button, ConfirmDialog, Field, Input, Textarea, Select, Switch, Modal, PageHeader, Badge, } from "@/components/ui"; import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons"; import { toFa } from "@/lib/utils"; interface SurveyOption { id?: number; label?: string; } interface SurveyQuestion { id: number; question?: string; description?: string; type?: string; order?: number; is_active?: boolean; options?: SurveyOption[]; } const TYPE_LABELS: Record = { single: "تک‌گزینه", multiple: "چندگزینه", }; export default function SurveysPage() { const { data, loading, error, reload } = useList("/survey-questions"); const toast = useToast(); const [editing, setEditing] = useState(null); const [open, setOpen] = useState(false); const [saving, setSaving] = useState(false); const [question, setQuestion] = useState(""); const [description, setDescription] = useState(""); const [type, setType] = useState("single"); const [order, setOrder] = useState("0"); const [isActive, setIsActive] = useState(true); const [options, setOptions] = useState([""]); const [deleting, setDeleting] = useState(null); const [removing, setRemoving] = useState(false); function openCreate() { setEditing(null); setQuestion(""); setDescription(""); setType("single"); setOrder("0"); setIsActive(true); setOptions([""]); setOpen(true); } function openEdit(row: SurveyQuestion) { setEditing(row); setQuestion(row.question ?? ""); setDescription(row.description ?? ""); setType(row.type ?? "single"); setOrder(String(row.order ?? 0)); setIsActive(row.is_active ?? true); setOptions( row.options && row.options.length ? row.options.map((o) => o.label ?? "") : [""], ); setOpen(true); } function setOptionAt(index: number, value: string) { setOptions((prev) => prev.map((o, i) => (i === index ? value : o))); } function addOption() { setOptions((prev) => [...prev, ""]); } function removeOption(index: number) { setOptions((prev) => prev.length > 1 ? prev.filter((_, i) => i !== index) : prev, ); } async function save(e: React.FormEvent) { e.preventDefault(); setSaving(true); try { const body = { question, description, type, order: Number(order), is_active: isActive, options: options .map((label) => label.trim()) .filter((label) => label !== "") .map((label) => ({ label })), }; if (editing) { await apiFetch(`/survey-questions/${editing.id}`, { method: "PUT", body, }); toast.success("پرسش ویرایش شد."); } else { await apiFetch("/survey-questions", { method: "POST", body }); toast.success("پرسش افزوده شد."); } setOpen(false); reload(); } catch (err) { toast.error(err instanceof ApiError ? err.message : "خطا در ذخیره‌سازی"); } finally { setSaving(false); } } async function confirmDelete() { if (!deleting) return; setRemoving(true); try { await apiFetch(`/survey-questions/${deleting.id}`, { method: "DELETE" }); toast.success("پرسش حذف شد."); setDeleting(null); reload(); } catch (err) { toast.error(err instanceof ApiError ? err.message : "خطا در حذف"); } finally { setRemoving(false); } } const columns: Column[] = [ { key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-20" }, { key: "question", header: "پرسش", render: (r) => r.question ?? "—" }, { key: "type", header: "نوع", render: (r) => (r.type ? TYPE_LABELS[r.type] ?? r.type : "—"), }, { key: "order", header: "ترتیب", render: (r) => (r.order != null ? toFa(r.order) : "—"), className: "w-20", }, { key: "is_active", header: "وضعیت", render: (r) => ( {r.is_active ? "فعال" : "غیرفعال"} ), }, ]; return (
} onClick={openCreate}> پرسش جدید } /> } onClick={openCreate}> پرسش جدید } actions={(row) => ( <> )} /> setOpen(false)} title={editing ? "ویرایش پرسش" : "پرسش جدید"} footer={ <> } >
setQuestion(e.target.value)} placeholder="متن پرسش" required autoFocus />