// lib/entry/entry_controller.dart import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:rogapp/model/entry.dart'; import 'package:rogapp/model/event.dart'; import 'package:rogapp/model/team.dart'; import 'package:rogapp/model/category.dart'; import 'package:rogapp/services/api_service.dart'; class EntryController extends GetxController { late ApiService _apiService; final entries = [].obs; final events = [].obs; final teams = [].obs; final categories = [].obs; final selectedEvent = Rx(null); final selectedTeam = Rx(null); final selectedCategory = Rx(null); final selectedDate = Rx(null); final currentEntry = Rx(null); final isLoading = true.obs; @override void onInit() async { super.onInit(); await initializeApiService(); await loadInitialData(); } Future initializeApiService() async { try { _apiService = await Get.putAsync(() => ApiService().init()); } catch (e) { print('Error initializing ApiService: $e'); Get.snackbar('Error', 'Failed to initialize API service'); } } Future loadInitialData() async { try { isLoading.value = true; await Future.wait([ fetchEntries(), fetchEvents(), fetchTeams(), fetchCategories(), ]); if (Get.arguments != null && Get.arguments['entry'] != null) { currentEntry.value = Get.arguments['entry']; initializeEditMode(currentEntry.value!); } else { // 新規作成モードの場合、最初のイベントを選択 if (events.isNotEmpty) { selectedEvent.value = events.first; selectedDate.value = events.first.startDatetime; } } } catch(e) { print('Error initializing data: $e'); Get.snackbar('Error', 'Failed to load initial data'); } finally { isLoading.value = false; } } void initializeEditMode(Entry entry) { currentEntry.value = entry; selectedEvent.value = entry.event; selectedTeam.value = entry.team; selectedCategory.value = entry.category; selectedDate.value = entry.date; } void updateEvent(Event? value) { selectedEvent.value = value; if (value != null) { // イベント変更時に日付を調整 if (selectedDate.value == null || selectedDate.value!.isBefore(value.startDatetime) || selectedDate.value!.isAfter(value.endDatetime)) { selectedDate.value = value.startDatetime; } } } void updateTeam(Team? value) => selectedTeam.value = value; void updateCategory(NewCategory? value) => selectedCategory.value = value; void updateDate(DateTime value) => selectedDate.value = value; /* void updateDate(DateTime value){ selectedDate.value = DateFormat('yyyy-MM-dd').format(value!) as DateTime?; } */ void _initializeEntryData() { if (currentEntry.value != null) { selectedEvent.value = currentEntry.value!.event; selectedTeam.value = currentEntry.value!.team; selectedCategory.value = currentEntry.value!.category; selectedDate.value = currentEntry.value!.date; } } Future fetchEntries() async { try { final fetchedEntries = await _apiService.getEntries(); entries.assignAll(fetchedEntries); } catch (e) { print('Error fetching entries: $e'); Get.snackbar('Error', 'Failed to fetch entries'); } } Future fetchEvents() async { try { final fetchedEvents = await _apiService.getEvents(); events.assignAll(fetchedEvents); } catch (e) { print('Error fetching events: $e'); Get.snackbar('Error', 'Failed to fetch events'); } } Future fetchTeams() async { try { final fetchedTeams = await _apiService.getTeams(); teams.assignAll(fetchedTeams); } catch (e) { print('Error fetching teams: $e'); Get.snackbar('Error', 'Failed to fetch team'); } } Future fetchCategories() async { try { final fetchedCategories = await _apiService.getCategories(); categories.assignAll(fetchedCategories); } catch (e) { print('Error fetching categories: $e'); Get.snackbar('Error', 'Failed to fetch categories'); } } Future createEntry() async { if (selectedEvent.value == null || selectedTeam.value == null || selectedCategory.value == null || selectedDate.value == null) { Get.snackbar('Error', 'Please fill all fields'); return; } try { final newEntry = await _apiService.createEntry( selectedTeam.value!.id, selectedEvent.value!.id, selectedCategory.value!.id, selectedDate.value!, ); entries.add(newEntry); Get.back(); } catch (e) { print('Error creating entry: $e'); Get.snackbar('Error', 'Failed to create entry'); } } Future updateEntry() async { if (currentEntry.value == null) { Get.snackbar('Error', 'No entry selected for update'); return; } try { final updatedEntry = await _apiService.updateEntry( currentEntry.value!.id, currentEntry.value!.team.id, selectedEvent.value!.id, selectedCategory.value!.id, selectedDate.value!, ); final index = entries.indexWhere((entry) => entry.id == updatedEntry.id); if (index != -1) { entries[index] = updatedEntry; } Get.back(); } catch (e) { print('Error updating entry: $e'); Get.snackbar('Error', 'Failed to update entry'); } } Future deleteEntry() async { if (currentEntry.value == null) { Get.snackbar('Error', 'No entry selected for deletion'); return; } try { await _apiService.deleteEntry(currentEntry.value!.id); entries.removeWhere((entry) => entry.id == currentEntry.value!.id); Get.back(); } catch (e) { print('Error deleting entry: $e'); Get.snackbar('Error', 'Failed to delete entry'); } } bool isOwner() { // Implement logic to check if the current user is the owner of the entry return true; // Placeholder } }