// lib/pages/entry/entry_detail_page.dart import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:rogapp/pages/entry/entry_controller.dart'; import 'package:rogapp/model/event.dart'; import 'package:rogapp/model/category.dart'; import 'package:rogapp/model/team.dart'; import 'package:intl/intl.dart'; class EntryDetailPage extends GetView { @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 Center(child: CircularProgressIndicator()); } return Padding( padding: EdgeInsets.all(16.0), child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildDropdown( label: 'イベント', items: controller.events, selectedId: controller.selectedEvent.value?.id, onChanged: (eventId) => controller.updateEvent( controller.events.firstWhere((e) => e.id == eventId) ), getDisplayName: (event) => event.eventName, getId: (event) => event.id, ), SizedBox(height: 16), _buildDropdown( label: 'チーム', items: controller.teams, selectedId: controller.selectedTeam.value?.id, onChanged: (teamId) => controller.updateTeam( controller.teams.firstWhere((t) => t.id == teamId) ), getDisplayName: (team) => team.teamName, getId: (team) => team.id, ), SizedBox(height: 16), _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, ), SizedBox(height: 16), ListTile( title: Text('日付'), subtitle: Text( controller.selectedDate.value != null ? DateFormat('yyyy-MM-dd').format(controller.selectedDate.value!) : '日付を選択してください', ), onTap: () async { if (controller.selectedEvent.value == null) { Get.snackbar('Error', 'Please select an event first'); return; } final DateTime? picked = await showDatePicker( context: context, initialDate: controller.selectedDate.value ?? controller.selectedEvent.value!.startDatetime, firstDate: controller.selectedEvent.value!.startDatetime, lastDate: controller.selectedEvent.value!.endDatetime, ); if (picked != null) { controller.updateDate(picked); } }, ), SizedBox(height: 32), if (mode == 'new') ElevatedButton( child: Text('エントリーを作成'), onPressed: () => controller.createEntry(), style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, minimumSize: Size(double.infinity, 50), ), ) else Row( children: [ Expanded( child: ElevatedButton( child: Text('エントリーを削除'), onPressed: () => controller.deleteEntry(), style: ElevatedButton.styleFrom( backgroundColor: Colors.red, minimumSize: Size(0, 50), ), ), ), SizedBox(width: 16), Expanded( child: ElevatedButton( child: Text('エントリーを更新'), onPressed: () => controller.updateEntry(), style: ElevatedButton.styleFrom( backgroundColor: Colors.lightBlue, minimumSize: Size(0, 50), ), ), ), ], ), ], ), ), ); }), ); } 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.map((item) => DropdownMenuItem( value: getId(item), child: Text(getDisplayName(item)), )).toList(), onChanged: onChanged, ); } }