436 lines
16 KiB
TypeScript
436 lines
16 KiB
TypeScript
"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 } 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 { 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<boolean>(true);
|
||
const [check, setCheck] = useState<any>(false);
|
||
const [complaintData, setComplaintData] = useState<any>("");
|
||
const [selectedFile, setSelectedFile] = useState<any>(null);
|
||
const [key, setKey] = useState<any>(null);
|
||
const router = useRouter();
|
||
let data;
|
||
|
||
const getComplaintMutation = useMutation({
|
||
mutationKey: ["Complaint", id],
|
||
mutationFn: getComplaint,
|
||
onSuccess: (res) => {},
|
||
onError: (err) => {},
|
||
});
|
||
|
||
useEffect(() => {
|
||
const tn = localStorage.getItem("trackingNumber") as string;
|
||
const pw = localStorage.getItem("privateKey") as string;
|
||
console.log(tn, pw);
|
||
if (tn != null && pw != null) {
|
||
getComplaintMutation.mutate({
|
||
trackingNumber: tn as string,
|
||
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(
|
||
'<html style="height: 100%;"><head><title>File Preview</title></head><body style="margin: 0; padding: 0; height: 100%; overflow: hidden;">'
|
||
);
|
||
|
||
// Check the file type and handle accordingly
|
||
if (fileType.includes("image")) {
|
||
newTab.document.write(
|
||
`<img src="data:${fileType};base64,${base64Data}" alt="File Preview"/>`
|
||
);
|
||
} else if (fileType === "application/pdf") {
|
||
newTab.document.write(
|
||
`<iframe width="100%" height="100%" src="${blobUrl}"></iframe>`
|
||
);
|
||
} else {
|
||
newTab.document.write(
|
||
`<a href="${blobUrl}" target="_blank">Open in New Tab</a>`
|
||
);
|
||
}
|
||
|
||
newTab.document.write("</body></html>");
|
||
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("trackingNumber", item.trackingNumber);
|
||
localStorage.setItem("privateKey", dcKey);
|
||
}
|
||
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 (
|
||
<>
|
||
<AlertDialog open={open} onOpenChange={setOpen}>
|
||
<AlertDialogContent dir="rtl" className="">
|
||
<AlertDialogHeader dir="rtl">
|
||
<AlertDialogTitle dir="rtl">بارگذاری کلید خصوصی</AlertDialogTitle>
|
||
<AlertDialogDescription>
|
||
برای مشاهده شکایت لطفا کلید خود را آپلود نمایید.
|
||
</AlertDialogDescription>
|
||
</AlertDialogHeader>
|
||
<div className="grid w-full max-w-sm items-center gap-1.5 px-8">
|
||
<Input
|
||
dir="rtl"
|
||
id="picture"
|
||
onChange={handleFileChange}
|
||
type="file"
|
||
accept=".txt"
|
||
className=" bg-white hover:bg-white focus:bg-white"
|
||
/>
|
||
</div>
|
||
<div className="flex items-center space-x-2 px-8">
|
||
<Checkbox
|
||
checked={check}
|
||
onCheckedChange={setCheck}
|
||
id="terms"
|
||
className=" ml-2"
|
||
/>
|
||
<label
|
||
htmlFor="terms"
|
||
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||
>
|
||
ذخیره کلید برای دفعات بعد
|
||
</label>
|
||
</div>
|
||
<AlertDialogFooter className=" gap-y-2 max-md:w-full max-md:flex max-md:flex-col">
|
||
{/* <DialogClose asChild></DialogClose> */}
|
||
<Button
|
||
variant="outline"
|
||
onClick={() => {
|
||
router.push("/UserPanel/ControllerDashboard/Complaints");
|
||
}}
|
||
type="submit"
|
||
className=" md:ml-2"
|
||
>
|
||
بازگشت
|
||
</Button>
|
||
<Button
|
||
className=" bg-blue hover:bg-blue/90"
|
||
onClick={() => {
|
||
checkKey2(complaintData);
|
||
}}
|
||
type="submit"
|
||
>
|
||
ذخیره
|
||
</Button>
|
||
</AlertDialogFooter>
|
||
</AlertDialogContent>
|
||
</AlertDialog>
|
||
{getComplaintMutation.data && !getComplaintMutation.isPending ? (
|
||
<>
|
||
<button
|
||
onClick={() =>
|
||
router.push("/UserPanel/ControllerDashboard/Complaints")
|
||
}
|
||
className=" my-2 flex gap-1 cursor-pointer text-blue "
|
||
>
|
||
<span>
|
||
<ArrowRight />
|
||
</span>
|
||
<p>بازگشت</p>
|
||
</button>
|
||
<div className="w-full flex max-lg:flex-col gap-3 items-start">
|
||
<div className="flex flex-col w-full lg:w-1/3 lg:order-2 gap-3 lg:sticky top-20">
|
||
<div className=" bg-[#E9E9E9] rounded-md py-4 px-10 flex flex-col">
|
||
<p className="text-[#464646] text-base font-bold">
|
||
گزارش شکایت
|
||
</p>
|
||
<div className="mt-2 flex flex-col gap-y-1">
|
||
<p className="text-[#2e2a2a] text-sm font-medium">
|
||
موضوع شکایت:{" "}
|
||
<span className="text-[#7C838A] text-sm font-medium">
|
||
{data?.title}
|
||
</span>
|
||
</p>
|
||
<p className="text-[#464646] text-sm font-medium">
|
||
دستگاه مربوطه:{" "}
|
||
<span className="text-[#7C838A] text-sm font-medium">
|
||
{data?.complaintOrganization
|
||
? data?.complaintOrganization.title
|
||
: "-"}
|
||
</span>
|
||
</p>
|
||
<p className="text-[#464646] text-sm font-medium">
|
||
پرسنل مربوطه:{" "}
|
||
<span className="text-[#7C838A] text-sm font-medium">
|
||
{data.complaining ? data.complaining : "-"}
|
||
</span>
|
||
</p>
|
||
<p className="text-[#464646] text-sm font-medium">
|
||
زمان ثبت:{" "}
|
||
<span className="text-[#7C838A] text-sm font-medium">
|
||
{convertDate(data.registeredAt)}
|
||
</span>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div className=" max-lg:hidden order-2 bg-[#E9E9E9] rounded-md p-4 px-10 flex flex-col">
|
||
<p className="text-[#464646] text-base font-bold mb-1">
|
||
انجام عملیات
|
||
</p>
|
||
<InspectorOperations
|
||
updateData={(data) => getComplaintMutation.mutate(data)}
|
||
role={role}
|
||
complaintData={data}
|
||
id={id}
|
||
key2={key}
|
||
/>
|
||
</div>
|
||
</div>
|
||
<div className="w-full lg:w-2/3 self-stretch bg-[#E9E9E9] p-4 px-10 rounded-lg">
|
||
<p className="text-[#464646] text-base font-bold">روند شکایت</p>
|
||
<div className=" flex flex-col gap-y-4 mt-4">
|
||
{data.contents?.map((item: any) => {
|
||
return (
|
||
<div
|
||
key={item.id}
|
||
className=" bg-white p-4 rounded-lg shadow-md"
|
||
>
|
||
<div className=" flex gap-4 items-center">
|
||
<p className="">
|
||
<Badge>{item.sender == 0 ? "شهروند" : "بازرس"}</Badge>
|
||
</p>
|
||
<p className="">
|
||
تاریخ :
|
||
<span className="text-[#7C838A]">
|
||
{convertDate(item.dateTime)}
|
||
</span>
|
||
</p>
|
||
</div>
|
||
<div className=" text-[#464646] mt-2">
|
||
<span className="text-[#7C838A]">
|
||
{item.text ? item.text : "-"}
|
||
</span>
|
||
</div>
|
||
{item.media.length !== 0 && (
|
||
<Separator className=" mt-2 mb-1" />
|
||
)}
|
||
<div className=" text-[#464646] mt-2">
|
||
<div className=" flex flex-wrap max-md:flex-col w-full items-center gap-2 text-xs mt-2">
|
||
{item?.media.map((item: any, index: number) => {
|
||
return (
|
||
<div
|
||
key={index}
|
||
className=" bg-gray-100 text-[#7C838A] w-full md:w-[30%] rounded-md h-12 p-2 flex justify-between gap-3 items-center"
|
||
>
|
||
<a
|
||
onClick={() =>
|
||
openFileInNewTab(item.data, item.mimeType)
|
||
}
|
||
className=" cursor-pointer flex gap-2 truncate items-center"
|
||
>
|
||
<File className=" w-6 h-6" />
|
||
<p className=" max-w-[5rem] truncate">
|
||
{item.title}
|
||
</p>
|
||
</a>
|
||
<ArrowDownToLine
|
||
className=" cursor-pointer"
|
||
onClick={() =>
|
||
downloadFile(
|
||
item.data,
|
||
item.title,
|
||
item.mimeType
|
||
)
|
||
}
|
||
/>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
<div className=" lg:hidden order-2 w-full bg-[#E9E9E9] rounded-md py-4 px-10 flex flex-col">
|
||
<p className="text-[#464646] text-base font-bold mb-1">
|
||
انجام عملیات
|
||
</p>
|
||
<InspectorOperations
|
||
updateData={(data) => getComplaintMutation.mutate(data)}
|
||
role={role}
|
||
complaintData={data}
|
||
id={id}
|
||
key2={key}
|
||
/>
|
||
</div>
|
||
{/* Left */}
|
||
{/* <div className="w-full md:w-1/3 bg-white shadow-lg rounded-md pt-3 px-4 flex flex-row md:flex-col max-md:flex-wrap">
|
||
<p className="text-[#464646] text-base font-bold mb-3">
|
||
وضعیت درخواست
|
||
</p>
|
||
|
||
{ComplainDetailsItems.map((item, index, array) => {
|
||
return (
|
||
<div
|
||
className="w-full justify-start items-start gap-4 inline-flex "
|
||
key={item.id}
|
||
>
|
||
<div className="grow shrink basis-0 pb-2 flex-col justify-start items-start gap-1 inline-flex">
|
||
<div className="self-stretch text-right text-lime-800 text-lg font-semibold leading-[30px]">
|
||
{item.title}
|
||
</div>
|
||
<div className="self-stretch text-right text-zinc-700 text-base font-normal uppercase">
|
||
{item.desc}
|
||
</div>
|
||
<div className="self-stretch text-right text-zinc-700 text-base font-normal">
|
||
{item.date} <br /> {item.time}
|
||
</div>
|
||
</div>
|
||
|
||
<div className="w-6 self-stretch flex-col justify-start items-center gap-1 inline-flex">
|
||
<div className="py-0.5 justify-start items-start gap-2.5 inline-flex">
|
||
<Image
|
||
src={greenIcon}
|
||
alt="greenIcon"
|
||
width={100}
|
||
height={100}
|
||
/>
|
||
</div>
|
||
<div className="w-1 grow shrink basis-0 bg-lime-800 rounded-[1px]"></div>
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div> */}
|
||
</div>
|
||
</>
|
||
) : (
|
||
<>
|
||
{getComplaintMutation.isPending && (
|
||
<div className="w-full h-full flex justify-center mt-20">
|
||
<Triangle
|
||
visible={true}
|
||
height="100"
|
||
width="100"
|
||
color="#003778"
|
||
ariaLabel="triangle-loading"
|
||
wrapperStyle={{}}
|
||
wrapperClass=""
|
||
/>
|
||
</div>
|
||
)}
|
||
</>
|
||
)}
|
||
</>
|
||
);
|
||
}
|