import 'package:flutter/material.dart'; import 'package:laserscouter/core/api.dart'; import 'notespage.dart'; import 'teamadder.dart'; import 'package:to_csv/to_csv.dart' as csv_export; import 'package:shared_preferences/shared_preferences.dart'; class TeamPicker extends StatefulWidget { final String eventCode; final String eventName; final bool isCustomOnly; const TeamPicker({super.key, required this.eventCode, required this.eventName,this.isCustomOnly = false}); @override State createState() => _TeamPickerState(); } class _TeamPickerState extends State { List teamNames = []; List teamCodes = []; bool isLoading = true; String? errorMessage; @override void initState() { super.initState(); _fetchTeams(); } Future _exportData() async { List header = []; header.add('Team Number'); header.add('Drivetrain Type'); header.add('Has Vision'); header.add('Climb Level'); header.add('Trenchable'); header.add('Fuel Capacity'); header.add('Bot Position'); header.add('Auton Rundown'); header.add('General Observations'); List> data = []; for (int i = 0; i < teamCodes.length; i++) { String generateKey(String field) { return '${teamCodes[i]}_${widget.eventCode}_$field'; } SharedPreferences prefs = await SharedPreferences.getInstance(); String? autonRundown = prefs.getString(generateKey('autonRundown')); String? botPosition = prefs.getString(generateKey('botPosition')); String? generalObservations = prefs.getString(generateKey('generalObservations')); String? driveTrainType = prefs.getString(generateKey('driveTrainType')); String? hasVision = prefs.getBool(generateKey('hasVision')).toString(); String? climbLevel = prefs.getDouble(generateKey('climbLevel')).toString(); String? trenchable = prefs.getBool(generateKey('trenchable')).toString(); String? fuelCapacity = prefs.getDouble(generateKey('fuelCapacity')).toString(); if (hasVision == 'null') { hasVision = ''; } if (climbLevel == 'null') { climbLevel = ''; } if (trenchable == 'null') { trenchable = ''; } if (fuelCapacity == 'null') { fuelCapacity = ''; } List teamData = []; teamData.add(teamCodes[i]); teamData.add(driveTrainType ?? ''); teamData.add(hasVision); teamData.add(climbLevel); teamData.add(trenchable); teamData.add(fuelCapacity); teamData.add(botPosition ?? ''); teamData.add(autonRundown ?? ''); teamData.add(generalObservations ?? ''); data.add(teamData); } csv_export.myCSV(header, data, setHeadersInFirstRow: true, emptyRowsConfig: {1: 1}, fileName: 'laserscouter_${widget.eventCode}.csv'); } Future _fetchTeams() async { try { List apiTeamNames = []; List apiTeamCodes = []; if (!widget.isCustomOnly) { final EventSearchResult apiResult = await eventSearch(widget.eventCode); apiTeamNames = apiResult.teamNames; apiTeamCodes = apiResult.teamCodes; } final SharedPreferences prefs = await SharedPreferences.getInstance(); final List customTeamNames = prefs.getStringList('custom_team_names_${widget.eventName}') ?? []; final List customTeamCodes = prefs.getStringList('custom_team_numbers_${widget.eventCode}') ?? []; List combinedNames = []; List combinedCodes = []; for (int i = 0; i < apiTeamCodes.length; i++) { if (!combinedCodes.contains(apiTeamCodes[i])) { combinedCodes.add(apiTeamCodes[i]); if (i < apiTeamNames.length) { combinedNames.add(apiTeamNames[i]); } else { combinedNames.add(''); } } } for (int i = 0; i < customTeamCodes.length; i++) { if (!combinedCodes.contains(customTeamCodes[i])) { combinedCodes.add(customTeamCodes[i]); if (i < customTeamNames.length) { combinedNames.add(customTeamNames[i]); } else { combinedNames.add(''); } } } if (mounted) { setState(() { teamNames = combinedNames.toList(); teamCodes = combinedCodes.toList(); isLoading = false; }); } } catch (e) { if (mounted) { setState(() { errorMessage = widget.isCustomOnly ? "Could not load custom teams." : "Failed to load teams. Please try again."; isLoading = false; }); } } } Future _refreshTeams() async { setState(() { isLoading = true; }); await _fetchTeams(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Teams'), actions: [ IconButton( onPressed: () async { await Navigator.push( context, MaterialPageRoute( builder: (context) => TeamAdder(eventCode: widget.eventCode,), ), ); _refreshTeams(); }, icon: Icon(Icons.add), ) ] ), body: _buildBody(), floatingActionButton: FloatingActionButton( onPressed: _exportData, child: const Icon(Icons.share), ), ); } Widget _buildBody() { if (isLoading) { return const Center(child: CircularProgressIndicator()); } if (errorMessage != null) { return Center( child: Padding( padding: const EdgeInsets.all(16.0), child: Text( errorMessage!, textAlign: TextAlign.center, style: Theme.of(context).textTheme.bodyLarge?.copyWith( color: Colors.red, ), ), ), ); } if (teamCodes.isEmpty) { return const Center( child: Text('No teams found for this event.'), ); } return ListView.builder( itemCount: teamCodes.length, itemBuilder: (context, index) { return ListTile( title: Text('${teamCodes[index]} - ${teamNames[index]}'), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => NotesPage( teamName: teamNames[index], eventCode: widget.eventCode, teamCode: teamCodes[index], ), ), ); }, ); }, ); } }