import React, { useEffect, useRef, useState } from 'react'; import { ActionBar, GlassCard, SearchBarPlus } from '@/components/ui'; import { PageContainer, ProFormSelect } from '@ant-design/pro-components'; import { Button, Image, Popconfirm, Space, Tag, message, Pagination } from 'antd'; import { PlusOutlined } from '@ant-design/icons'; import { ordersApi } from '@/services/orders'; import TourismFileForm, { TourismFileItem } from './components/TourismFileForm'; // 资源类型枚举(用于卡片展示的颜色) const TYPE_OPTIONS = [ { label: '视频', value: 5, color: '#f5222d' }, { label: '首页 Banner', value: 6, color: '#2f54eb' }, { label: '展示图片', value: 4, color: '#52c41a' }, { label: '景区详情', value: 3, color: '#722ed1' }, { label: '景区小图展示', value: 2, color: '#13c2c2' }, { label: '活动', value: 1, color: '#1890ff' }, ] as const; const STATUS_OPTIONS = [ { label: '启用', value: 0 }, { label: '禁用', value: 1 }, ] as const; const TourismFilePage: React.FC = () => { const [query, setQuery] = useState>({}); const [tourismOptions, setTourismOptions] = useState>([]); const [createOpen, setCreateOpen] = useState(false); const [editOpen, setEditOpen] = useState(false); const [current, setCurrent] = useState(); // 分组数据(按项目分组)与分页 const [groups, setGroups] = useState>([]); const [loading, setLoading] = useState(false); const fetchingRef = useRef(false); const [pagination, setPagination] = useState<{ current: number; pageSize: number; total: number }>({ current: 1, pageSize: 10, total: 0 }); // 加载可选项目列表(用于筛选与表单下拉) useEffect(() => { (async () => { try { const res = await ordersApi.tourism({ page: 1, limit: 1000, tenantId: query?.tenantId, includeChildren: query?.includeChildren }); const list: Array<{ id: number; tourismName: string }> = (res as any)?.data || []; setTourismOptions(list.map((it: any) => ({ label: it.tourismName, value: it.id }))); } catch {} })(); // 租户切换时刷新 }, [query?.tenantId, query?.includeChildren]); // 加载查询结果(含分页) const loadGroups = async (extra?: Record) => { const params = { ...(query || {}), ...(extra || {}), current: pagination.current, pageSize: pagination.pageSize }; if (fetchingRef.current) return; // 防抖去重 fetchingRef.current = true; setLoading(true); try { const res = await ordersApi.tourismFilesGroupedByTenant(params as any); setGroups(Array.isArray((res as any)?.data) ? (res as any).data : []); setPagination((p) => ({ ...p, total: Number((res as any)?.total || 0) })); } catch (e) { setGroups([]); setPagination((p) => ({ ...p, total: 0 })); } finally { fetchingRef.current = false; setLoading(false); } }; // 首次与查询/分页变化时刷新 useEffect(() => { loadGroups(); }, [query, pagination.current, pagination.pageSize]); return ( { // 查询提交后重置到第1页 setPagination((p) => ({ ...p, current: 1 })); setQuery(values); }} > } onClick={() => setCreateOpen(true)}>新增} /> {loading ? (
加载中...
) : (
{groups && groups.length ? ( groups.map((g) => (

{g.tourismName || `项目#${g.tourismId}`}

{(g.files || []).map((record: any) => (
{Number(record.type) === 5 ? (
{Number(record.status) === 0 ? '启用' : '禁用'} i.value === Number(record.type)) || ({} as any)).color || 'default'}> {(TYPE_OPTIONS.find((i) => i.value === Number(record.type)) || ({} as any)).label || '未知类型'}
{record.description || '-'}
{record.linkUrl ? ( ) : null}
{ setCurrent(record); setEditOpen(true); }} > 编辑 { const hide = message.loading('正在删除...'); try { await ordersApi.deleteTourismFile(record.id); message.success('删除成功'); loadGroups(); } catch (e) { message.error('删除失败'); } finally { hide(); } }} > 删除
))}
)) ) : (
暂无数据
)}
)} {/* 分页 */}
共 {pagination.total} 条 { setPagination({ current: cur, pageSize: (size as number) || pagination.pageSize, total: pagination.total }); }} />
{ const hide = message.loading('保存中...'); try { await ordersApi.saveOrUpdateTourismFile({ ...values, status: values.status ?? 0 }); message.success('新增成功'); loadGroups(); return true; } catch (e) { message.error('新增失败'); return false; } finally { hide(); } }} /> { const hide = message.loading('保存中...'); try { await ordersApi.saveOrUpdateTourismFile({ ...(current || {}), ...values }); message.success('修改成功'); loadGroups(); return true; } catch (e) { message.error('修改失败'); return false; } finally { hide(); } }} />
); }; export default TourismFilePage;