"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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { htmlToString } from "@/utils/globals"; import { getTicketingInternalDetail, getTicketingInternalDiscussion, saveTicketInternalReply } from "@/service/service/communication/communication"; import { Icon } from "@iconify/react/dist/iconify.js"; const taskSchema = z.object({ title: z.string().min(1, { message: "Judul diperlukan" }), naration: z.string().min(2, { message: "Narasi Penugasan harus lebih dari 2 karakter.", }), }); export type taskDetail = { id: number; title: string; createdAt: string; referenceNumber: string | number; createdBy: { id: number; fullname: string; }; sendTo: { id: number; fullname: string; }; status: { id: number; name: string; }; priority: { id: number; name: string; }; broadcastType: string; narration: string; is_active: string; }; export type replyDetail = { id: number; message: string; createdAt: string; messageFrom: { id: number; fullname: string; }; messageTo: { id: number; fullname: string; }; }; export type internalDetail = { id: number; message: string; createdAt: string; createdBy: { id: number; fullname: string; }; sendTo: { id: number; fullname: string; }; }; export default function FormDetailInternal() { const MySwal = withReactContent(Swal); const { id } = useParams() as { id: string }; const [detail, setDetail] = useState(); const [ticketReply, setTicketReply] = useState([]); const [ticketInternal, setTicketInternal] = useState( null ); const [replyVisible, setReplyVisible] = useState(false); const [replyMessage, setReplyMessage] = useState(""); const [selectedPriority, setSelectedPriority] = useState(""); const [selectedStatus, setSelectedStatus] = useState(""); const { control, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(taskSchema), }); useEffect(() => { async function initState() { if (id) { const response = await getTicketingInternalDetail(id); setTicketInternal(response?.data?.data || null); setDetail(response?.data?.data); } } initState(); getTicketReply(); }, [id]); async function getTicketReply() { const res = await getTicketingInternalDiscussion(id); if (res?.data !== null) { setTicketReply(res?.data?.data); } } const handleReply = () => { setReplyVisible((prev) => !prev); }; const handleSendReply = async () => { if (replyMessage.trim() === "") { MySwal.fire({ title: "Error", text: "Pesan tidak boleh kosong!", icon: "error", }); return; } const data = { ticketId: id, message: replyMessage, }; try { await saveTicketInternalReply(data); await getTicketReply(); MySwal.fire({ title: "Sukses", text: "Pesan berhasil dikirim.", icon: "success", }); setReplyMessage(""); setReplyVisible(false); } catch (error) { MySwal.fire({ title: "Error", text: "Gagal mengirim balasan.", icon: "error", }); console.error("Error sending reply:", error); } }; return (
{replyVisible && (