import React, { useState, useEffect, useMemo } from 'react' import { Ic } from '@/icons' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { ShimmerBox, ShimmerLine } from '@/components/shared/shimmer_frontend' import { toFa } from '@/lib/utils' import type { PilgrimageCityItem, CategoryItem, DuaItem, } from '../types/trip-etiquette' import { fetchTripEtiquetteCities, fetchCategories, fetchDuas, } from '../services/trip-etiquette-api' // نام‌های روان و استاندارد فارسی برای شهرهای زیارتی const CITY_FA_NAMES: Record = { karbala: 'کربلای معلی', najaf: 'نجف اشرف', koufa: 'کوفه', kufa: 'کوفه', kazemein: 'کاظمین', kadhimiya: 'کاظمین', samara: 'سامرا', samarra: 'سامرا', make: 'مکه مکرمه', mecca: 'مکه مکرمه', madineh: 'مدینه منوره', medina: 'مدینه منوره', mashhad: 'مشهد مقدس', qom: 'قم مقدسه', } const COUNTRY_FLAGS: Record = { IQ: '🇮🇶', IR: '🇮🇷', SA: '🇸🇦', } const COUNTRY_NAMES_FA: Record = { Iraq: 'عراق', Iran: 'ایران', 'Saudi Arabia': 'عربستان سعودی', } export function CitiesServiceView() { const [cities, setCities] = useState([]) const [categories, setCategories] = useState([]) const [duas, setDuas] = useState([]) const [isLoading, setIsLoading] = useState(true) const [isRefreshing, setIsRefreshing] = useState(false) const [errorMessage, setErrorMessage] = useState(null) // فیلترها const [searchTerm, setSearchTerm] = useState('') const [selectedCountryCode, setSelectedCountryCode] = useState('ALL') const loadData = async (isManual = false) => { if (isManual) setIsRefreshing(true) else setIsLoading(true) setErrorMessage(null) try { const [citiesRes, categoriesRes, duasRes] = await Promise.all([ fetchTripEtiquetteCities(), fetchCategories(), fetchDuas(), ]) setCities(citiesRes) setCategories(categoriesRes) setDuas(duasRes) } catch (err: any) { setErrorMessage(err?.message || 'خطا در بارگذاری فهرست شهرهای زیارتی.') } finally { setIsLoading(false) setIsRefreshing(false) } } useEffect(() => { loadData() }, []) // کشورهای موجود در لیست شهرها const availableCountries = useMemo(() => { const map = new Map() cities.forEach((c) => { const code = c.country_code || '' const name = c.country_name || '' if (code && !map.has(code)) { map.set(code, { code, name, nameFa: COUNTRY_NAMES_FA[name] || name, flag: COUNTRY_FLAGS[code] || '🌍', }) } }) return Array.from(map.values()) }, [cities]) // شهرهای فیلترشده const filteredCities = useMemo(() => { return cities.filter((city) => { // فیلتر کشور if (selectedCountryCode !== 'ALL') { if ((city.country_code || '').toLowerCase() !== selectedCountryCode.toLowerCase()) { return false } } // فیلتر جستجو بر اساس نام، شناسه یا اسلاگ if (searchTerm.trim()) { const term = searchTerm.toLowerCase().trim() const faName = CITY_FA_NAMES[city.slug.toLowerCase()] || '' const matchName = city.name.toLowerCase().includes(term) const matchFa = faName.toLowerCase().includes(term) const matchSlug = city.slug.toLowerCase().includes(term) const matchId = String(city.id).includes(term) return matchName || matchFa || matchSlug || matchId } return true }) }, [cities, selectedCountryCode, searchTerm]) return (
{/* سربرگ شهرهای زیارتی به همراه دکمه تازه‌سازی (لود مجدد) */}

شهرهای زیارتی

{/* نوار فیلتر و جستجو */}
{/* تب‌های کشورها */} {availableCountries.map((co) => { const count = cities.filter((c) => c.country_code === co.code).length return ( ) })}
{/* فیلد جستجوی سریع */}
setSearchTerm(e.target.value)} placeholder="جستجوی نام یا شناسه شهر..." className="ps-9 bg-card/80 border-border-soft rounded-xl text-xs h-9" /> {searchTerm && ( )}
{/* پیام خطا در صورت وجود */} {errorMessage && (
{errorMessage}
)} {/* وضعیت لودینگ اسکلتون منطبق با ساختار کارت‌های ساده‌شده شهر */} {isLoading ? (
{Array.from({ length: 6 }).map((_, i) => (
{/* عنوان و کشور */}
{/* آمار دو ستونه */}
))}
) : filteredCities.length === 0 ? (

هیچ شهری با این مشخصات یافت نشد

) : ( /* شبکه کارت‌های شهرهای زیارتی (ساده‌شده، فقط خواندنی و بدون واکنش کلیک) */
{filteredCities.map((city) => { const faName = CITY_FA_NAMES[city.slug.toLowerCase()] || city.name const flag = COUNTRY_FLAGS[city.country_code || ''] || '🌍' const countryFa = COUNTRY_NAMES_FA[city.country_name || ''] || city.country_name || '' // ۱. تعداد دسته‌بندی‌های متصل به این شهر در دیتابیس const cityCategoriesCount = categories.filter((c) => { if (c.city === city.id || c.city_info?.id === city.id) return true if (c.cities && c.cities.some((ct) => ct.id === city.id)) return true return false }).length // ۲. تعداد کل ادعیه متصل به این شهر در دیتابیس const cityDuasCount = duas.filter((d) => { if (d.cities && d.cities.some((c) => c.id === city.id)) return true if (d.categories && d.categories.some((cat) => { if (cat.city_id === city.id) return true if (cat.cities && cat.cities.some((c) => c.id === city.id)) return true return false })) return true if (d.shrines && d.shrines.some((s: any) => s.city_id === city.id || s.city === city.id)) return true return false }).length return (
{/* بخش اول: نشان کشور به همراه پرچم (سمت چپ) و عنوان شهر (سمت راست) */}

{faName}

{countryFa} {flag}
{/* بخش دوم: دو آمار تکمیلی صرفاً نمایشی */}
دسته‌بندی‌ها {toFa(cityCategoriesCount)}
کل ادعیه و زیارات {toFa(cityDuasCount)}
) })}
)}
) }