import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.dart'; import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/product_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_data_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/loaded_space_widget.dart'; import 'package:syncrow_web/services/product_api.dart'; import 'package:syncrow_web/services/space_mana_api.dart'; import 'package:syncrow_web/web_layout/web_scaffold.dart'; class SpaceManagementPage extends StatefulWidget { const SpaceManagementPage({super.key}); @override SpaceManagementPageState createState() => SpaceManagementPageState(); } class SpaceManagementPageState extends State { CommunityModel? selectedCommunity; SpaceModel? selectedSpace; final CommunitySpaceManagementApi _api = CommunitySpaceManagementApi(); final ProductApi _productApi = ProductApi(); Map> communitySpaces = {}; double canvasWidth = 1000; double canvasHeight = 1000; List spaces = []; List connections = []; List products = []; bool isProductDataLoaded = false; @override void initState() { super.initState(); } @override Widget build(BuildContext context) { return BlocProvider( create: (context) => SpaceManagementBloc(_api, _productApi)..add(LoadCommunityAndSpacesEvent()), child: WebScaffold( appBarTitle: Text('Space Management', style: Theme.of(context).textTheme.headlineLarge), enableMenuSidebar: false, scaffoldBody: BlocBuilder(builder: (context, state) { if (state is SpaceManagementLoading) { return const Center(child: CircularProgressIndicator()); } else if (state is SpaceManagementLoaded) { int selectedIndex = state.communities.indexWhere( (community) => community.uuid == selectedCommunity?.uuid, ); if (selectedIndex != -1) { selectedCommunity = state.communities[selectedIndex]; } else { selectedCommunity = null; selectedSpace = null; } return LoadedSpaceView( communities: state.communities, selectedCommunity: selectedCommunity, selectedSpace: selectedSpace, products: state.products, onCommunitySelected: (community) { setState(() { selectedCommunity = community; }); }, onSpaceSelected: (space) { setState(() { selectedSpace = space; }); }, ); } else if (state is SpaceManagementError) { return Center(child: Text('Error: ${state.errorMessage}')); } return Container(); }), ), ); } }