import BreadCrumbs from "@/components/BreadCrumbs";
import ModalFile from "@/components/ModalFile";
import notification from "@/components/notificationGlobal";
import apiFetch from "@/lib/apiFetch";
import {
Anchor,
Badge,
Button,
Card,
Container,
Divider,
Flex,
Grid,
Group,
Modal,
Spoiler,
Stack,
Table,
Text,
Textarea,
Title,
} from "@mantine/core";
import { useDisclosure, useShallowEffect } from "@mantine/hooks";
import {
IconAlignJustified,
IconCategory,
IconFileCertificate,
IconInfoTriangle,
IconMapPin,
IconMessageReport,
IconPhone,
IconPhotoScan,
IconUser,
} from "@tabler/icons-react";
import type { User } from "generated/prisma";
import type { JsonValue } from "generated/prisma/runtime/library";
import _ from "lodash";
import { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import useSwr from "swr";
export default function DetailPengaduanPage() {
const dataMenu = [
{ title: "Dashboard", link: "/scr/dashboard/dashboard-home", active: false },
{ title: "Pengaduan", link: "/scr/dashboard/pengaduan/list", active: false },
{ title: "Detail Pengaduan", link: "#", active: true },
];
const { search } = useLocation();
const query = new URLSearchParams(search);
const id = query.get("id");
const { data, mutate, isLoading } = useSwr("/", () =>
apiFetch.api.pengaduan.detail.get({
query: {
id: id!,
},
}),
);
useShallowEffect(() => {
mutate();
}, []);
return (
{
mutate();
}}
/>
);
}
function DetailDataPengaduan({
data,
phone,
onAction,
}: {
data: any | null;
phone?: string | null;
onAction: () => void;
}) {
const [opened, { open, close }] = useDisclosure(false);
const [catModal, setCatModal] = useState<"tolak" | "terima">("tolak");
const [openedPreview, setOpenedPreview] = useState(false);
const [keterangan, setKeterangan] = useState("");
const [host, setHost] = useState(null);
const [permissions, setPermissions] = useState([]);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
async function fetchHost() {
const { data } = await apiFetch.api.user.find.get();
setHost(data?.user ?? null);
if (data?.permissions && Array.isArray(data.permissions)) {
const onlySetting = data.permissions.filter((p: any) =>
p.startsWith("pengaduan"),
);
setPermissions(onlySetting);
}
}
fetchHost();
}, []);
const handleKonfirmasi = async (cat: "terima" | "tolak") => {
try {
setIsLoading(true);
const res = await apiFetch.api.pengaduan["update-status"].post({
id: data?.id,
status:
cat == "tolak"
? "ditolak"
: data.status == "antrian"
? "diterima"
: data.status == "diterima"
? "dikerjakan"
: "selesai",
keterangan: keterangan,
idUser: host?.id ?? "",
});
if (res?.status === 200) {
const resWA = await apiFetch.api["send-wa"].pengaduan.post({
noPengaduan: data?.noPengaduan,
judulPengaduan: data?.title,
status:
cat == "tolak"
? "ditolak"
: data.status == "antrian"
? "diterima"
: data.status == "diterima"
? "dikerjakan"
: "selesai",
alasan: keterangan,
tlp: String(phone),
})
onAction();
close();
notification({
title: "Success",
message: "Success update pengaduan",
type: "success",
});
if (resWA?.status === 200) {
if (resWA.data?.success) {
notification({
title: "Success",
message: "Success send message to warga",
type: "success",
});
} else {
notification({
title: "Failed",
message: "Failed send message to warga",
type: "error",
});
}
} else {
notification({
title: "Failed",
message: "Failed send message to warga",
type: "error",
});
}
} else {
notification({
title: "Error",
message: "Failed to update pengaduan",
type: "error",
});
}
} catch (error) {
console.error(error);
notification({
title: "Error",
message: "Failed to update pengaduan",
type: "error",
});
} finally {
setIsLoading(false);
}
};
return (
<>
{/* MODAL KONFIRMASI */}
{catModal === "tolak" ? (
<>
Anda yakin ingin menolak pengaduan ini? Berikan alasan penolakan
{/* MODAL GAMBAR */}
setOpenedPreview(false)}
folder="pengaduan"
fileName={data?.image}
/>
Pengaduan
#{data?.noPengaduan}
{data?.status}
Judul
{_.upperFirst(data?.title)}
Lokasi
{_.upperFirst(data?.location)}
Kategori
{_.upperFirst(data?.category)}
Gambar
{data?.image != null && data?.image != "" ? (
{
setOpenedPreview(true);
}}
>
Lihat Gambar
) : (
-
)}
Detail
{_.upperFirst(data?.detail)}
{data?.keterangan && (
Keterangan
{_.upperFirst(data?.keterangan)}
)}
{data?.status === "antrian" ? (
{
setCatModal("tolak");
open();
}}
>
Tolak
{
setCatModal("terima");
open();
}}
>
Terima
) : data?.status === "diterima" ? (
{
setCatModal("terima");
open();
}}
>
Kerjakan
) : data?.status === "dikerjakan" ? (
{
setCatModal("terima");
open();
}}
>
Selesai
) : (
<>>
)}
>
);
}
function DetailDataHistori({ data }: { data: any }) {
return (
Riwayat Pengaduan
Tanggal
Deskripsi
Status
User
{data?.map((item: any) => (
{item.createdAt.toLocaleString("id-ID", {
day: "2-digit",
month: "short",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
hour12: false,
})}
{item.deskripsi}
{item.status}
{item.nameUser ? item.nameUser : "-"}
))}
);
}
function DetailUserPengaduan({ data }: { data: any }) {
return (
Warga
Nama
{data?.name}
Telepon
{data?.phone}
Jumlah Pengaduan
{data?.pengaduan}
Jumlah Pelayanan Surat
{data?.pelayanan}
);
}