import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useStoreSession } from '../contexts/StoreSessionContext'; import { request } from '../lib/api'; function formatShopHours(store: Record | null) { const parts: string[] = []; const openTime = store?.openTime ? String(store.openTime) : ''; const closeTime = store?.closeTime ? String(store.closeTime) : ''; const openTime2 = store?.openTime2 ? String(store.openTime2) : ''; const closeTime2 = store?.closeTime2 ? String(store.closeTime2) : ''; if (openTime && closeTime) parts.push(`${openTime} - ${closeTime}`); if (openTime2 && closeTime2) parts.push(`${openTime2} - ${closeTime2}`); return parts.length ? parts.join(',') : '09:30 - 22:00'; } export default function StatusPage() { const navigate = useNavigate(); const { resetSession } = useStoreSession(); const [open, setOpen] = useState(true); const [permanentlyClosed, setPermanentlyClosed] = useState(false); const [store, setStore] = useState | null>(null); const [lastUpdate, setLastUpdate] = useState(''); const [showModal, setShowModal] = useState(false); const [pendingOpen, setPendingOpen] = useState(null); const [error, setError] = useState(''); useEffect(() => { request('SHOP_H5', '/shop/dashboard').then((d) => { const s = d.store as Record; setStore(s); const status = String(s?.status || ''); setPermanentlyClosed(status === 'CLOSED'); setOpen(status === 'OPEN'); if (s?.updatedAt) { setLastUpdate(new Date(String(s.updatedAt)).toLocaleString('zh-CN')); } }); }, []); function requestToggle(next: boolean) { if (permanentlyClosed) return; if (next === open) return; setPendingOpen(next); setShowModal(true); } async function confirmToggle() { if (pendingOpen === null || permanentlyClosed) return; const next = pendingOpen ? 'OPEN' : 'PAUSED'; try { setError(''); await request('SHOP_H5', '/shop/store/status', { method: 'PUT', body: JSON.stringify({ status: next }), }); setOpen(pendingOpen); setLastUpdate(new Date().toLocaleString('zh-CN')); } catch (e) { setError(e instanceof Error ? e.message : '状态切换失败'); } finally { setShowModal(false); setPendingOpen(null); } } const hoursText = formatShopHours(store); const statusLabel = permanentlyClosed ? '永久关闭' : open ? '营业中' : '临时闭店'; return (

门店管理

storefront
{open && !permanentlyClosed ? 'check_circle' : 'cancel'}

{statusLabel}

{permanentlyClosed ? (

总部已永久关闭本店,门店端无法自行恢复营业

) : ( )}

营业时间

{hoursText}

{lastUpdate &&

最后修改于 {lastUpdate}

} {error ?

{error}

: null}
info

{permanentlyClosed ? '门店已永久关闭。如需重新营业,请联系总部或合伙人处理。' : open ? '当前处于营业状态,用户可在您的门店核销餐券。' : '当前处于临时闭店状态,用户将无法看到您的门店或进行核销。'}

{showModal && (

确认切换状态?

{pendingOpen ? '切换至“营业中”后,用户可正常选择本店核销餐券。' : '切换至“临时闭店”后,用户将无法选择本店核销餐券。'}

)}
); }