import React, { useState, useEffect } from 'react'; import { Link, useLocation } from 'react-router-dom'; import { Home, FileInput, FileOutput, Users, Package, Settings, Menu, X, TrendingUp } from 'lucide-react'; interface LayoutProps { children: React.ReactNode; } const Layout: React.FC = ({ children }) => { const [isSidebarOpen, setSidebarOpen] = useState(false); const location = useLocation(); const [pigmiPopup, setPigmiPopup] = useState<{ open: boolean; message: string }>(() => ({ open: false, message: '', })); const getPigmiState = () => { try { const stored = localStorage.getItem('pp_pigmi_config'); if (!stored) return null; const parsed = JSON.parse(stored); const enabled = typeof parsed.enabled === 'boolean' ? parsed.enabled : false; const message = typeof parsed.message === 'string' ? parsed.message : ''; const time = typeof parsed.time === 'string' && parsed.time ? parsed.time : '09:00'; const dayOfMonth = typeof parsed.dayOfMonth === 'number' && !Number.isNaN(parsed.dayOfMonth) && parsed.dayOfMonth >= 1 && parsed.dayOfMonth <= 31 ? parsed.dayOfMonth : 1; if (!enabled || !message.trim()) return null; const now = new Date(); const key = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`; const lastKey = localStorage.getItem('pp_pigmi_last_shown_month') || ''; const [hh, mm] = String(time).split(':').map((v: string) => Number(v)); const lastDay = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate(); const day = Math.max(1, Math.min(lastDay, dayOfMonth)); const scheduled = new Date(now.getFullYear(), now.getMonth(), day, hh || 0, mm || 0, 0, 0); return { key, lastKey, message: message.trim(), dueNow: now.getTime() >= scheduled.getTime(), alreadyShownThisMonth: lastKey === key, }; } catch (error) { console.error('Error reading pigmi config', error); return null; } }; const triggerPigmiIfDue = () => { const pigmi = getPigmiState(); if (!pigmi) return; if (pigmi.dueNow && !pigmi.alreadyShownThisMonth) { try { localStorage.setItem('pp_pigmi_last_shown_month', pigmi.key); } catch (error) { console.error('Error saving pigmi month key', error); } setPigmiPopup({ open: true, message: pigmi.message }); } }; useEffect(() => { triggerPigmiIfDue(); const id = window.setInterval(() => triggerPigmiIfDue(), 30_000); return () => window.clearInterval(id); }, []); const navItems = [ { path: '/', label: 'डॅशबोर्ड (Home)', icon: Home }, { path: '/jawaak', label: 'जावक बिल (Sales)', icon: FileOutput }, { path: '/awaak', label: 'आवक बिल (Purchase)', icon: FileInput }, { path: '/ledger', label: 'पार्टी लेजर (Ledger)', icon: Users }, { path: '/stock', label: 'स्टॉक रिपोर्ट (Stock)', icon: Package }, { path: '/analysis', label: 'अ‍ॅनालिसीस (Analysis)', icon: TrendingUp }, { path: '/settings', label: 'सेटिंग्स (Settings)', icon: Settings }, ]; const isActive = (path: string) => location.pathname === path; return (
{pigmiPopup.open && (
setPigmiPopup((p) => ({ ...p, open: false }))} />
Pigmi Reminder
{pigmiPopup.message}
)} {/* Mobile Sidebar Overlay */} {isSidebarOpen && (
setSidebarOpen(false)} /> )} {/* Sidebar */} {/* Main Content */}
{/* Top Header */}

{navItems.find(i => isActive(i.path))?.label.split(' (')[0] || 'Mirchi Vyapar'}

{new Date().toLocaleDateString('mr-IN', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })}
{/* Page Content */}
{children}
); }; export default Layout;