import { sitecontrollerAll } from '@/servers/api/site'; import { EditOutlined } from '@ant-design/icons'; import { PageContainer } from '@ant-design/pro-components'; import { Outlet, history, request, useLocation, useParams } from '@umijs/max'; import { Button, Col, Menu, Row, Select, Spin, message } from 'antd'; import Sider from 'antd/es/layout/Sider'; import React, { useEffect, useState } from 'react'; import type { SiteItem } from '../List/index'; import EditSiteForm from './EditSiteForm'; const ShopLayout: React.FC = () => { const [sites, setSites] = useState([]); const [loading, setLoading] = useState(true); const { siteId } = useParams<{ siteId: string }>(); const location = useLocation(); const [editModalOpen, setEditModalOpen] = useState(false); const [editingSite, setEditingSite] = useState(null); const fetchSites = async () => { try { setEditingSiteLoading(true); const { data = [] } = await sitecontrollerAll(); setSites(data); if (!siteId && data.length > 0) { history.replace(`/site/shop/${data[0].id}/products`); } } catch (error) { console.error('Failed to fetch sites', error); } finally { setLoading(false); } }; useEffect(() => { fetchSites(); }, []); const handleSiteChange = (value: number) => { const currentPath = location.pathname; const parts = currentPath.split('/'); if (parts.length >= 5) { parts[3] = String(value); history.push(parts.join('/')); } else { history.push(`/site/shop/${value}/products`); } }; const handleMenuClick = (e: { key: string }) => { if (!siteId) return; history.push(`/site/shop/${siteId}/${e.key}`); }; const getSelectedKey = () => { const parts = location.pathname.split('/'); if (parts.length >= 5) { return parts[4]; } return 'products'; }; if (loading) { return ( ); } const handleFinish = async (values: any) => { if (!editingSite) { message.error('未找到要编辑的站点'); return false; } try { await request(`/site/${editingSite.id}`, { method: 'PUT', data: values, }); message.success('更新成功'); setEditModalOpen(false); fetchSites(); // 重新获取站点列表以更新数据 return true; } catch (error: any) { message.error(error.message || '操作失败'); return false; } }; return (