"use client"; "use client"; import React, { useEffect, useState } from "react"; import { useForm, Controller } from "react-hook-form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Card } from "@/components/ui/card"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { useParams } from "next/navigation"; import { SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Avatar, AvatarImage } from "@/components/ui/avatar"; import { getCuratorUser, getEscalationDiscussion, getQuestionTicket, getTicketingDetail, getTicketingInternalDetail, getTicketingInternalDiscussion, saveTicketInternalReply, saveTicketsQuestion, } from "@/service/communication/communication"; import { Textarea } from "@/components/ui/textarea"; import { Icon } from "@iconify/react/dist/iconify.js"; import { Link } from "@/i18n/routing"; import { loading } from "@/lib/swal"; import { id } from "date-fns/locale"; import { DetailTicket } from "../ticketing/info-lainnya-types"; import { Description } from "@radix-ui/react-toast"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { ChevronDownIcon } from "lucide-react"; import { getOperatorUser } from "@/service/management-user/management-user"; import makeAnimated from "react-select/animated"; import Select, { ActionMeta, MultiValue } from "react-select"; import { Checkbox } from "@/components/ui/checkbox"; interface Option { id: string; label: string; value: string; fullname: string; userLevel: string; userLevelId: string; } // const taskSchema = z.object({ // title: z.string().optional(), // description: z.string().min(2, { // message: "Narasi Penugasan harus lebih dari 2 karakter.", // }), // }); const taskSchema = z.object({ title: z.string().optional(), description: z.string().min(2, { message: "Narasi Penugasan harus lebih dari 2 karakter.", }), }); export type replyDetail = { id: number; message: string; createdAt: string; messageFrom: { id: number; fullname: string; }; messageTo: { id: number; fullname: string; }; }; const optionsData = [ { value: "1", label: "Low" }, { value: "2", label: "Medium" }, { value: "3", label: "High" }, ]; type OptionType = { value: string; label: string; }; export default function FormQuestionsForward() { const MySwal = withReactContent(Swal); const { id } = useParams() as { id: string }; const [detail, setDetail] = useState(); const [ticketReply, setTicketReply] = useState([]); const [replyVisible, setReplyVisible] = useState(false); const [listDiscussion, setListDiscussion] = useState(); const [message, setMessage] = useState(""); const [detailTickets, setDetailTickets] = useState(null); // const [selectedPriority, setSelectedPriority] = useState(""); const [selectedPriority, setSelectedPriority] = useState( null, ); const [replyMessage, setReplyMessage] = useState(""); const [selectedStatus, setSelectedStatus] = useState(""); const [operatorOpt, setOperatorOpt] = useState< { id: string; label: string; value: string }[] >([]); const [selectedOperator, setSelectedOperator] = useState([]); const [options, setOptions] = useState([]); const animatedComponent = makeAnimated(); const [isCollaboration, setIsCollaboration] = useState(false); const [selectedOption, setSelectedOption] = useState([]); const [replies, setReplies] = useState([ { id: 1, name: "Mabes Polri - Approver", message: "test", timestamp: "2024-12-20 00:56:10", }, { id: 2, name: "Mabes Polri - Approver", message: "balas", timestamp: "2025-01-18 17:42:48", }, ]); const { control, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: zodResolver(taskSchema), defaultValues: { title: "", description: "", }, }); useEffect(() => { async function initState() { const response = await getQuestionTicket(id); const data = response?.data?.data; setDetail(data); if (data) { reset({ title: data.message ?? "", description: data.emergencyIssue?.description ?? "", }); } setDetailTickets(data); if (detailTickets?.emergencyIssue) { reset({ title: detailTickets.emergencyIssue.title || "", description: detailTickets.emergencyIssue.description || "", }); // setSelectedPriority(String(detailTickets.emergencyIssue.urgencyId)); // setSelectedStatus(String(detailTickets.statusId)); // jika ada } } initState(); getTicketReply(); getUser(); }, [id, reset]); useEffect(() => { async function getOperator() { const res = await getOperatorUser(detailTickets?.typeId); if (res?.data !== null) { const rawUser = res?.data?.data; const optionArr = rawUser?.map((option: any) => ({ id: option.id, label: option.fullName, value: option.id, })); setOperatorOpt(optionArr); } } getOperator(); }, [detailTickets]); async function getTicketReply() { const res = await getTicketingInternalDiscussion(id); if (res?.data !== null) { setTicketReply(res?.data?.data); } } const onSubmit = async (data: any) => { try { const payload = { id, title: data.title, description: data.description, priorityId: selectedPriority?.value, statusId: selectedStatus, typeId: detailTickets?.typeId, parentCommentId: detailTickets?.feedId, operatorTeam: selectedOperator.join(","), isEscalation: true, communicationTeam: selectedOption.map((item) => item.id).join(","), isCollaboration: isCollaboration, }; const response = await saveTicketsQuestion(payload); MySwal.fire({ title: "Sukses", text: "Data berhasil diperbarui.", icon: "success", }).then(() => { window.location.href = "/in/supervisor/communications/questions/all"; }); getTicketReply(); } catch (error) { console.error("Gagal update:", error); MySwal.fire({ title: "Error", text: "Terjadi kesalahan saat memperbarui.", icon: "error", }); } }; const handleChange = ( selected: MultiValue