"use client"; import React, { useEffect, useState } from "react"; import greenIcon from "../public/img/greenPoint.svg"; import blueIcon from "../public/img/bluepoint.svg"; import Image from "next/image"; import { ComplainDetailsItems } from "@/constants"; import { useMutation } from "@tanstack/react-query"; import { OperateByInspector, getComplaint } from "@/utils/inspectorApi"; import useData from "@/store/useData"; import { Decrypt, convertDate, convertFullTime } from "@/lib/utils"; import { Badge } from "./ui/badge"; import InspectorOperations from "./InspectorOperations"; import { ArrowDownToLine, ArrowRight, File } from "lucide-react"; import { Separator } from "./ui/separator"; import secureLocalStorage from "react-secure-storage"; import { Checkbox } from "./ui/checkbox"; import { useQuery } from "@tanstack/react-query"; import { Button } from "@/components/ui/button"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { Input } from "./ui/input"; import { toast } from "sonner"; import { useRouter } from "next/navigation"; import { Triangle } from "react-loader-spinner"; export default function ComplainsDetails({ id, role, Data, }: { id?: number; role: string; Data?: any; }) { const storeData = useData((state) => state.data); const [open, setOpen] = useState(true); const [check, setCheck] = useState(false); const [complaintData, setComplaintData] = useState(""); const [selectedFile, setSelectedFile] = useState(null); const [key, setKey] = useState(null); const router = useRouter(); let data; const getComplaintMutation = useMutation({ mutationKey: ["Complaint", id], mutationFn: getComplaint, onSuccess: (res) => { localStorage.setItem("trackingNumber", res.trackingNumber); }, onError: (err: any | string) => { toast(<>{err.response.data.detail}, { className: "!bg-red-500 !text-white", }); }, }); useEffect(() => { const storedData = localStorage.getItem("CompliantData"); const data = JSON.parse(storedData as string); const prvKey = localStorage.getItem("privateKey") as string; const cipherKeyInspector = data.cipherKeyInspector; const newDcKey = Decrypt(cipherKeyInspector, prvKey); const pw = newDcKey as string; localStorage.setItem("trackingNumber", data.trackingNumber); if (pw) { getComplaintMutation.mutate({ trackingNumber: data.trackingNumber, password: pw, }); setOpen(false); } }, []); useEffect(() => { const storedData = localStorage.getItem("CompliantData"); const data = JSON.parse(storedData as string); setComplaintData(data); }, []); if (getComplaintMutation.isSuccess) { data = getComplaintMutation.data; } const openFileInNewTab = (base64Data: any, fileType: any) => { const blob = b64toBlob(base64Data, fileType); const blobUrl = URL.createObjectURL(blob); const newTab = window.open(); if (newTab) { newTab.document.write( 'File Preview' ); // Check the file type and handle accordingly if (fileType.includes("image")) { newTab.document.write( `File Preview` ); } else if (fileType === "application/pdf") { newTab.document.write( `` ); } else { newTab.document.write( `Open in New Tab` ); } newTab.document.write(""); newTab.document.close(); URL.revokeObjectURL(blobUrl); } else { console.error("Unable to open a new tab."); } }; const downloadFile = (base64Data: any, fileName: any, fileType: any) => { const blob = b64toBlob(base64Data, fileType); const blobUrl = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = blobUrl; link.download = fileName; link.click(); URL.revokeObjectURL(blobUrl); }; const b64toBlob = (b64Data: any, contentType = "", sliceSize = 512) => { const byteCharacters = atob(b64Data); const byteArrays = []; for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { const slice = byteCharacters.slice(offset, offset + sliceSize); const byteNumbers = new Array(slice.length); for (let i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } const byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } return new Blob(byteArrays, { type: contentType }); }; const handleFileChange = (event: any) => { const file = event.target.files[0]; const reader = new FileReader(); reader.onload = (e) => { const contents = e?.target?.result; setSelectedFile(contents); }; reader.readAsText(file); }; const checkKey2 = (item: any) => { const dcKey = Decrypt(item.cipherKeyInspector, selectedFile); console.log(dcKey); if (dcKey != false && dcKey != null) { if (check) { localStorage.setItem("privateKey", selectedFile); } localStorage.setItem("trackingNumber", item.trackingNumber); setKey(dcKey); getComplaintMutation.mutate({ trackingNumber: item.trackingNumber, password: dcKey as string, }); setOpen(false); } else { toast("کلید شما معتبر نمی باشد.", { className: "!bg-red-500 !text-white", }); } }; data = getComplaintMutation?.data; return ( <> بارگذاری کلید خصوصی برای مشاهده شکایت لطفا کلید خود را آپلود نمایید.
{/* */}
{getComplaintMutation.data && !getComplaintMutation.isPending ? ( <>
{/* 1 */}

انجام عملیات

getComplaintMutation.mutate(data)} role={role} complaintData={data} id={id} key2={key} />
{/* 2 */}
{data.user == null ? ( <>

اطلاعات کلی

موضوع شکایت:{" "} {data?.title}

دستگاه مربوطه:{" "} {data?.complaintOrganization ? data?.complaintOrganization.title : "-"}

فرد مربوطه:{" "} {data.complaining ? data.complaining : "-"}

کد پیگیری:{" "} {data.trackingNumber ? data.trackingNumber : "-"}

زمان ثبت:{" "} {convertFullTime(data.registeredAt)}

) : ( <>

اطلاعات کلی

موضوع شکایت:{" "} {data?.title}

دستگاه مربوطه:{" "} {data?.complaintOrganization ? data?.complaintOrganization.title : "-"}

فرد مربوطه:{" "} {data.complaining ? data.complaining : "-"}

کد پیگیری:{" "} {data.trackingNumber ? data.trackingNumber : "-"}

زمان ثبت:{" "} {convertFullTime(data.registeredAt)}

اطلاعات شاکی

نام:{" "} {data.user?.firstName ? data.user?.firstName : "-"}

نام خانوادگی:{" "} {data.user?.lastName ? data.user?.lastName : "-"}

کدملی:{" "} {data.user?.nationalId ? data.user?.nationalId : "-"}

شماره موبایل:{" "} {data.user?.phoneNumber ? data.user?.phoneNumber : "-"}

{/* کد پیگیری:{" "} {data.trackingNumber ? data.trackingNumber : "-"} */}

)}

روند شکایت

{data.contents?.map((item: any) => { return (

{item.sender == 0 ? "شهروند" : "بازرس"}

تاریخ : {convertDate(item.dateTime)}

{item.text ? item.text : "-"}
{item.media.length !== 0 && ( )}
{item?.media.map((item: any, index: number) => { return ( ); })}
); })}
{/* 3 */}

انجام عملیات

getComplaintMutation.mutate(data)} role={role} complaintData={data} id={id} key2={key} />
) : ( <> {getComplaintMutation.isPending && (
)} )} ); }