import React, { useState, useEffect } from "react"; import Typography from "../../components/Typography/Typography"; import { Grid } from "../../components/Grid/Grid"; import Button from "../../components/Button/Button"; import { useApiMutation } from "../../utils/useApiRequest"; import { useDrawerStore } from "../../context/zustand-store/appStore"; import MainSubmitInspection from "./MainSubmitInspection"; import { motion, AnimatePresence, Variants } from "framer-motion"; import { UserIcon, IdentificationIcon, PhoneIcon, CalendarIcon, CreditCardIcon, InformationCircleIcon, MapPinIcon, BuildingStorefrontIcon, } from "@heroicons/react/24/outline"; interface MainPlaceInfoProps { item: any; userlastseen?: string; isPoultry?: boolean; isLivestock?: boolean; } interface PersonData { _id: string; nationalcode: string; name: string; family: string; father: string; birthdate: string; mobile: string[]; info: string[]; card: string[]; } interface ApiResponse { data: PersonData[]; count: number; } interface PersonRegistryData { registeryOfficePersonId: number; nationalCode: string; firstName: string; lastName: string; fatherName: string; supervisorNationalCode: string | null; gender: boolean; identityNo: string; identitySeries: string; identitySerial: string; birthDate: string; birthDateUnix: number; isLive: boolean; deathDate: string | null; city: string; vilage: string | null; town: string | null; errorCode: number; errorDescription: string | null; createdOn: string; errorCodeSpecified: boolean | null; birthDateDay: number; birthDateMonth: number; birthDateYear: number; } interface PersonRegistryResponse { status: boolean; statusCode: number; data: PersonRegistryData; apiLogId: string; } interface UnitRegistryData { counter: number; serviceType: number; title: string; isicname: string; fullname: string; state: string; city: string; address: string; licenseNumber: string; licenseExpireDate: string; licenseType: string; licenseStatus: string; layerTwo: { phonenumber: string; unionName: string; postalcode: string; nationalcode: string; mobilenumber: string; nationalId: string | null; corporationName: string | null; licenseIssueDate: string; jobType: string; hasPartner: string; hasSteward: string; isForeigner: string; }; } interface UnitRegistryResponse { status?: boolean; statusCode?: number; data?: UnitRegistryData[]; } const MainPlaceInfo: React.FC = ({ item, userlastseen, }) => { const [personData, setPersonData] = useState([]); const [isLoadingPersonInfo, setIsLoadingPersonInfo] = useState(false); const [personRegistryData, setPersonRegistryData] = useState(null); const [unitRegistryData, setUnitRegistryData] = useState( [], ); const [isLoadingRegistry, setIsLoadingRegistry] = useState(false); const { openDrawer } = useDrawerStore(); const personInfoMutation = useApiMutation({ api: "people_info", method: "get", }); const personRegistryMutation = useApiMutation({ api: "national-documents", method: "get", }); const unitRegistryMutation = useApiMutation({ api: "national-documents", method: "get", }); // Auto-fetch person info and registry data if national_id has 10 digits useEffect(() => { const nationalId = item?.national_id; if ( nationalId && typeof nationalId === "string" && nationalId.length === 10 ) { fetchPersonInfo(nationalId); fetchRegistryData(nationalId); } }, [item?.national_id]); const fetchPersonInfo = async (nationalId: string) => { setIsLoadingPersonInfo(true); try { const result = await personInfoMutation.mutateAsync({ searchfield: "nationalcode", value: nationalId, }); if (result?.data && result.data.length > 0) { setPersonData(result.data); } else { setPersonData([]); } } catch (error: any) { console.error("Person info error:", error); setPersonData([]); } finally { setIsLoadingPersonInfo(false); } }; const fetchRegistryData = async (nationalCode: string) => { setIsLoadingRegistry(true); try { // Fetch person registry try { const personResult = await personRegistryMutation.mutateAsync({ type: "person", info: nationalCode, }); if (personResult?.status && personResult?.data) { setPersonRegistryData(personResult.data); } else { setPersonRegistryData(null); } } catch (error: any) { console.error("Person registry error:", error); setPersonRegistryData(null); } // Fetch unit registry try { const unitResult = await unitRegistryMutation.mutateAsync({ type: "guild", info: nationalCode, }); const unitsCandidate = unitResult?.data ?? (Array.isArray(unitResult) ? unitResult : []); setUnitRegistryData( Array.isArray(unitsCandidate) ? unitsCandidate : [], ); } catch (error: any) { console.error("Unit registry error:", error); setUnitRegistryData([]); } } catch (error) { console.error("Registry fetch error:", error); } finally { setIsLoadingRegistry(false); } }; const handleOpenInspection = () => { openDrawer({ title: "ثبت بازرسی", content: , direction: "left", }); }; const cardVariants: Variants = { hidden: { opacity: 0, y: 20, scale: 0.95 }, visible: (i: number) => ({ opacity: 1, y: 0, scale: 1, transition: { delay: i * 0.1, duration: 0.3, ease: "easeOut", }, }), }; const getDisplayName = (person: PersonData) => { if (person.family && person.name.includes(person.family)) { return person.name; } return person.family ? `${person.name} ${person.family}` : person.name; }; return ( {/* Place Basic Information */}
{item?.guilds_name || "نامشخص"} {userlastseen && ( آخرین بازدید: {userlastseen} )}
{item?.national_id && (
کد ملی {item.national_id}
)} {item?.lat && item?.lng && (
موقعیت {item.lat.toFixed(6)}, {item.lng.toFixed(6)}
)}
{/* Person Info Data */} {isLoadingPersonInfo && (
در حال بارگذاری اطلاعات شخص...
)} {personData.length > 0 && !isLoadingPersonInfo && ( اطلاعات شخص ({personData.length}) {personData.map((person, index) => (
{getDisplayName(person)} {person.father && ( فرزند {person.father} )}
کد ملی {person.nationalcode}
{person.birthdate && (
تاریخ تولد {person.birthdate}
)} {person.mobile && person.mobile.length > 0 && (
شماره تلفن
{person.mobile.map((mobile, idx) => ( {mobile} ))}
)}
{person.card && person.card.length > 0 && (
کارت های بانکی
{person.card.map((card, idx) => ( {card} ))}
)} {person.info && person.info.length > 0 && (
اطلاعات جمع آوری شده توسط هوش مصنوعی
{person.info.map((info, idx) => ( {info} ))}
)}
))}
)}
{/* Person Registry Data */} {isLoadingRegistry && (
در حال بارگذاری اطلاعات ثبت احوال...
)} {personRegistryData && !isLoadingRegistry && ( استعلام از ثبت احوال
{personRegistryData.firstName}{" "} {personRegistryData.lastName} {personRegistryData.fatherName && ( فرزند {personRegistryData.fatherName} )}
کد ملی {personRegistryData.nationalCode}
{personRegistryData.birthDate && (
تاریخ تولد {personRegistryData.birthDate}
)} {personRegistryData.city && (
شهر {personRegistryData.city} {personRegistryData.vilage && ` - ${personRegistryData.vilage}`}
)}
جنسیت {personRegistryData.gender ? "مرد" : "زن"}
وضعیت حیات {personRegistryData.isLive ? "زنده" : "فوت شده"}
)}
{/* Unit Registry Data */} {unitRegistryData.length > 0 && !isLoadingRegistry && ( استعلام واحد صنفی ({unitRegistryData.length}) {unitRegistryData.map((unit, index) => (
{unit.title} {unit.fullname}
کد ملی {unit.layerTwo.nationalcode}
شماره موبایل {unit.layerTwo.mobilenumber}
شماره تلفن {unit.layerTwo.phonenumber}
شماره پروانه {unit.licenseNumber}
نوع پروانه {unit.licenseType}
وضعیت پروانه {unit.licenseStatus}
تاریخ انقضا {unit.licenseExpireDate}
شهر / استان {unit.city} - {unit.state}
{unit.address && (
آدرس
{unit.address}
)} {unit.layerTwo.unionName && (
نام اتحادیه
{unit.layerTwo.unionName}
)}
))}
)}
); }; export default MainPlaceInfo;