Sootzani/components/ComplainsDetails.tsx

443 lines
18 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"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 useData from "@/store/useData";
import { convertDate, convertFullTime } from "@/lib/utils";
import { Badge } from "./ui/badge";
import InspectorOperations from "./InspectorOperations";
import { postComplaintTracking } from "@/utils/citizenApi";
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";
import { ArrowDownToLine, ArrowRight, File } from "lucide-react";
export default function ComplainsDetails({
id,
role,
Data,
state,
}: {
id?: number;
role: string;
Data?: any;
state?: string;
}) {
const [open, setOpen] = useState<boolean>(
state == "FollowResult" ? false : true
);
const [password, setPassword] = useState<any>(null);
const router = useRouter();
const storeData = useData((state) => state.data);
let 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 getComplaintMutation = useMutation({
mutationKey: ["Complaint", id],
mutationFn: postComplaintTracking,
onSuccess: (res) => {
setOpen(false);
},
onError: (err: any | string) => {
toast(<>{err.response.data.detail}</>, {
className: "!bg-red-500 !text-white",
});
},
});
const handelPassword = () => {
const payload = { ...storeData, password: password };
getComplaintMutation.mutate({ Data: payload });
};
useEffect(() => {
useData.persist.rehydrate();
if (state == "FollowResult") {
setOpen(false);
}
}, []);
useEffect(() => {
const tk = localStorage.getItem("trackingNumber2");
const pw = localStorage.getItem("password2");
if (tk && pw) {
getComplaintMutation.mutate({
Data: { trackingNumber: tk, password: pw },
});
}
}, []);
data = getComplaintMutation?.data;
const possibleOperations = data?.possibleOperationsWithDescription;
if (possibleOperations?.length == 0) {
localStorage.removeItem("trackingNumber2");
localStorage.removeItem("password2");
}
return (
<>
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogContent dir="rtl" className="">
<AlertDialogHeader dir="rtl">
<AlertDialogTitle dir="rtl">رمز عبور</AlertDialogTitle>
<AlertDialogDescription className=" w-full mx-auto">
برای مشاهده درخواست رمز عبور خود را وارد نمایید.
</AlertDialogDescription>
</AlertDialogHeader>
<div className="grid items-center gap-1.5 w-full px-10 mx-auto">
{/* <Label htmlFor="picture">Picture</Label> */}
<Input
dir="ltr"
id="picture"
autoFocus
onChange={(e) => setPassword(e.target.value)}
className="bg-white hover:bg-white focus:bg-white text-center"
/>
</div>
<AlertDialogFooter className=" gap-y-2 max-md:w-full max-md:flex max-md:flex-col">
<Button
onClick={() => router.push("/Account/Complaints")}
type="button"
variant="outline"
className=" md:ml-2"
>
بازگشت
</Button>
{/* <DialogClose asChild> */}
<Button onClick={handelPassword} type="submit">
تایید
</Button>
{/* </DialogClose> */}
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{getComplaintMutation.data && !getComplaintMutation.isPending ? (
<>
{/* {state !== "FollowResult" && (
<button
onClick={() => router.push("/Account/Complaints")}
className="flex gap-1 my-2 cursor-pointer text-blue"
>
<span>
<ArrowRight />
</span>
<p>بازگشت</p>
</button>
)} */}
{/* Editt */}
<div className="flex items-start w-full gap-2 mt-2 max-lg:flex-col lg:h-[80vh] mb-10">
{/* 1 */}
<div className="flex flex-col w-full lg:w-1/3 gap-3 lg:sticky top-28 lg:h-[80vh] max-lg:order-3">
{/* ***** */}
<div className=" bg-[#E9E9E9] rounded-md p-4 px-10 flex flex-col lg:h-full">
<p className="text-[#464646] text-base font-bold mb-1">
انجام عملیات
</p>
<InspectorOperations
updateData={(payload) =>
getComplaintMutation.mutate({ Data: payload })
}
role={role}
complaintData={data}
password={password}
/>
</div>
</div>
{/* 2 */}
<div className="flex flex-col self-stretch w-full gap-2 rounded-lg lg:w-2/3 ">
<div className=" bg-[#E9E9E9] rounded-md py-4 px-10 flex flex-col md:flex-row gap-2 items-center justify-center">
{data?.user == null ? (
<>
<div className="relative flex flex-col w-full p-5 mt-2 border border-gray-400 rounded-md gap-y-1">
<p className=" absolute -top-3 right-2 bg-[#E9E9E9] px-2 text-sm text-gray-500">
اطلاعات کلی
</p>
<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">
{data.trackingNumber ? data.trackingNumber : "-"}
</span>
</p>
<p className="text-[#464646] text-sm font-medium">
زمان ثبت:{" "}
<span className="text-[#7C838A] text-sm font-medium">
{convertFullTime(data.registeredAt)}
</span>
</p>
</div>
</>
) : (
<>
<div className="relative flex flex-col p-5 mt-2 border border-gray-400 rounded-md gap-y-1 md:w-1/2">
<p className=" absolute -top-3 right-2 bg-[#E9E9E9] px-2 text-sm text-gray-500">
اطلاعات کلی
</p>
<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">
{convertFullTime(data.registeredAt)}
</span>
</p>
</div>
<div className="relative flex flex-col p-5 mt-2 border border-gray-400 rounded-md gap-y-1 md:w-1/2">
<p className=" absolute -top-3 right-2 bg-[#E9E9E9] px-2 text-sm text-gray-500">
اطلاعات شاکی
</p>
<p className="text-[#2e2a2a] text-sm font-medium">
نام:{" "}
<span className="text-[#7C838A] text-sm font-medium">
{data.user?.firstName ? data.user?.firstName : "-"}
</span>
</p>
<p className="text-[#464646] text-sm font-medium">
نام خانوادگی:{" "}
<span className="text-[#7C838A] text-sm font-medium">
{data.user?.lastName ? data.user?.lastName : "-"}
</span>
</p>
<p className="text-[#464646] text-sm font-medium">
کدملی:{" "}
<span className="text-[#7C838A] text-sm font-medium">
{data.user?.nationalId ? data.user?.nationalId : "-"}
</span>
</p>
<p className="text-[#464646] text-sm font-medium">
شماره موبایل:{" "}
<span className="text-[#7C838A] text-sm font-medium">
{data.user?.phoneNumber
? data.user?.phoneNumber
: "-"}
</span>
</p>
</div>
</>
)}
</div>
<div className="h-[calc(80vh-11.5em)] max-h-[calc(80vh-11.5em)] overflow-hidden">
<div className="bg-[#E9E9E9] rounded-md py-4 px-10 flex flex-col h-full ">
<p className="text-[#464646] text-base font-bold">
روند شکایت
</p>
<div
className="flex flex-col mt-4 gap-y-4 overflow-auto
scrollbar-thin scrollbar-thumb-gray-500 scrollbar-track-gray-100 "
>
{data.contents?.map((item: any) => {
return (
<div
key={item.id}
className="p-4 bg-white rounded-lg shadow-md "
>
<div className="flex items-center gap-4 justify-between">
<p className="">
<Badge>
{item.operationWithDescription.description} توسط{" "}
{item.sender == 0 ? "شهروند" : "بازرس"}
</Badge>
</p>
<p className="">
<span className="text-[#7C838A] text-sm">
{convertFullTime(item.dateTime)}
</span>
</p>
</div>
<div className=" text-[#464646] mt-2">
<span className="text-[#7C838A] text-sm">
{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 items-center w-full gap-2 mt-2 text-xs max-md:flex-col">
{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="flex items-center gap-2 truncate cursor-pointer "
>
<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>
</div>
{/* 3 */}
{/* ****** */}
</div>
</>
) : (
<>
{getComplaintMutation.isPending && (
<div className="flex justify-center w-full h-full mt-20">
<Triangle
visible={true}
height="100"
width="100"
color="#003778"
ariaLabel="triangle-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
)}
</>
)}
</>
);
}