// lib/pages/entry/entry_detail_page.dart import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:gifunavi/pages/entry/entry_controller.dart'; import 'package:gifunavi/model/event.dart'; import 'package:gifunavi/model/category.dart'; import 'package:gifunavi/model/team.dart'; import 'package:intl/intl.dart'; import 'package:gifunavi/widgets/error_widget.dart'; import 'package:timezone/timezone.dart' as tz; class EntryDetailPage extends GetView { const EntryDetailPage({super.key}); @override Widget build(BuildContext context) { final Map arguments = Get.arguments ?? {}; final mode = Get.arguments['mode'] as String? ?? 'new'; final entry = Get.arguments['entry']; if (mode == 'edit' && entry != null) { controller.initializeEditMode(entry); } return Scaffold( appBar: AppBar( title: Text(mode == 'new' ? 'エントリー登録' : 'エントリー詳細'), ), body: Obx(() { if (controller.isLoading.value) { return const Center(child: CircularProgressIndicator()); } if (controller.hasError.value) { return CustomErrorWidget( errorMessage: controller.errorMessage.value, onRetry: () => controller.loadInitialData(), ); } try { return Padding( padding: const EdgeInsets.all(16.0), child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildDropdown( label: 'イベント', items: controller.activeEvents, selectedId: controller.selectedEvent.value?.id, onChanged: (eventId) { final event = controller.activeEvents.firstWhereOrNull(( e) => e.id == eventId); if (event != null) { controller.updateEvent(event); } else { print('Event with id $eventId not found'); // 必要に応じてエラー処理を追加 } }, /* onChanged: (eventId) => controller.updateEvent( controller.activeEvents.firstWhere((e) => e.id == eventId) ), */ getDisplayName: (event) => event.eventName, getId: (event) => event.id, ), const SizedBox(height: 16), _buildDropdown( label: 'チーム', items: controller.teams, selectedId: controller.selectedTeam.value?.id, onChanged: (teamId) { final team = controller.teams.firstWhereOrNull((t) => t.id == teamId); if (team != null) { controller.updateTeam(team); } else { print('Team with id $teamId not found'); // 必要に応じてエラー処理を追加 } }, /* onChanged: (teamId) => controller.updateTeam( controller.teams.firstWhere((t) => t.id == teamId) ), */ getDisplayName: (team) => team.teamName, getId: (team) => team.id, ), const SizedBox(height: 16), _buildCategoryDropdown(), /* _buildDropdown() label: 'カテゴリ', items: controller.categories, selectedId: controller.selectedCategory.value?.id, onChanged: (categoryId) => controller.updateCategory( controller.categories.firstWhere((c) => c.id == categoryId) ), getDisplayName: (category) => category.categoryName, getId: (category) => category.id, ), */ const SizedBox(height: 16), ListTile( title: const Text('日付'), subtitle: Text( controller.selectedDate.value != null ? DateFormat('yyyy-MM-dd').format(tz.TZDateTime.from( controller.selectedDate.value!, tz.getLocation('Asia/Tokyo'))) : '日付を選択してください', ), onTap: () async { if (controller.selectedEvent.value == null) { Get.snackbar('Error', 'Please select an event first'); return; } final tz.TZDateTime now = tz.TZDateTime.now(tz .getLocation('Asia/Tokyo')); final tz.TZDateTime eventStart = tz.TZDateTime.from( controller.selectedEvent.value!.startDatetime, tz .getLocation('Asia/Tokyo')); final tz.TZDateTime eventEnd = tz.TZDateTime.from( controller.selectedEvent.value!.endDatetime, tz .getLocation('Asia/Tokyo')); final tz.TZDateTime initialDate = controller.selectedDate .value != null ? tz.TZDateTime.from(controller.selectedDate.value!, tz.getLocation('Asia/Tokyo')) : (now.isAfter(eventStart) ? now : eventStart); // 選択可能な最初の日付を設定(今日かイベント開始日のうち、より後の日付) final tz.TZDateTime firstDate = now.isAfter(eventStart) ? now : eventStart; final DateTime? picked = await showDatePicker( context: context, initialDate: initialDate.isAfter(firstDate) ? initialDate : firstDate, firstDate: firstDate, lastDate: eventEnd, ); if (picked != null) { controller.updateDate(tz.TZDateTime.from( picked, tz.getLocation('Asia/Tokyo'))); } }, ), const SizedBox(height: 32), if (mode == 'new') ElevatedButton( onPressed: () => controller.createEntry(), style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, foregroundColor: Colors.white, minimumSize: const Size(double.infinity, 50), ), child: const Text('エントリーを作成'), ) else Row( children: [ Expanded( child: ElevatedButton( onPressed: () => controller.deleteEntry(), style: ElevatedButton.styleFrom( backgroundColor: Colors.red, foregroundColor: Colors.white, minimumSize: const Size(0, 50), ), child: const Text('エントリーを削除'), ), ), const SizedBox(width: 16), Expanded( child: ElevatedButton( onPressed: () => controller.updateEntry(), style: ElevatedButton.styleFrom( backgroundColor: Colors.lightBlue, foregroundColor: Colors.white, minimumSize: const Size(0, 50), ), child: const Text('エントリーを更新'), ), ), ], ), ], ), ), ); } catch (e) { print('Error in EntryDetailPage: $e'); return const Center( child: Text('エラーが発生しました。もう一度お試しください。'), ); } }), ); } Widget _buildDropdown({ required String label, required List items, required int? selectedId, required void Function(int?) onChanged, required String Function(T) getDisplayName, required int Function(T) getId, }) { return DropdownButtonFormField( decoration: InputDecoration(labelText: label), value: selectedId, items: items.isNotEmpty ? items.map((item) => DropdownMenuItem( //items: items.map((item) => DropdownMenuItem( value: getId(item), child: Text(getDisplayName(item)), )).toList() : null, onChanged: (value) { if (value != null) { onChanged(value); } }, //onChanged: onChanged, ); } Widget _buildCategoryDropdown() { final eligibleCategories = controller.getFilteredCategories(); return DropdownButtonFormField( decoration: InputDecoration(labelText: 'カテゴリ'), value: controller.selectedCategory.value, items: eligibleCategories.isNotEmpty ? eligibleCategories.map((category) => DropdownMenuItem( //items: eligibleCategories.map((category) => DropdownMenuItem( value: category, child: Text(category.categoryName), )).toList() : null, onChanged: (value) { if (value != null) { controller.updateCategory(value); } }, //onChanged: (value) => controller.updateCategory(value), ); } Widget _buildCategoryDropdown_old() { final eligibleCategories = controller.categories.where((c) => c.baseCategory == controller.selectedCategory.value?.baseCategory ).toList(); return DropdownButtonFormField( decoration: InputDecoration(labelText: 'カテゴリ'), value: controller.selectedCategory.value, items: eligibleCategories.map((category) => DropdownMenuItem( value: category, child: Text(category.categoryName), )).toList(), onChanged: (value) => controller.updateCategory(value), ); } }