import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:gifunavi/pages/entry/event_entries_controller.dart';
import 'package:timezone/timezone.dart' as tz;

class EventEntriesPage_old extends GetView<EventEntriesController> {
  const EventEntriesPage_old({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('イベント参加')),
      body: Obx(() => ListView.builder(
        itemCount: controller.entries.length,
        itemBuilder: (context, index) {
          final entry = controller.entries[index];
          return ListTile(
            title: Text(entry.event.eventName),
            subtitle: Text('${entry.category.categoryName} - ${entry.date}'),
            onTap: () => controller.joinEvent(entry),
          );
        },
      )),
    );
  }
}

class EventEntriesPage extends GetView<EventEntriesController> {
  const EventEntriesPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Obx(() => Text(controller.showTodayEntries.value ? 'イベント参加' : 'イベント参照')),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Obx(() => Text(
                  controller.showTodayEntries.value ? '本日のエントリー' : 'すべてのエントリー',
                  style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                )),
                Obx(() => Switch(
                  value: !controller.showTodayEntries.value,
                  onChanged: (value) {
                    controller.toggleShowTodayEntries();
                  },
                  activeColor: Colors.blue,
                )),
              ],
            ),
          ),
          Expanded(
            child: Obx(() {
              if (controller.filteredEntries.isEmpty) {
                return const Center(
                  child: Text('表示するエントリーがありません。'),
                );
              }
              return ListView.builder(
                itemCount: controller.filteredEntries.length,
                itemBuilder: (context, index) {
                  final entry = controller.filteredEntries[index];
                  return ListTile(
                    title: Row(
                      children: [
                        Expanded(
                          child: Text('${_formatDate(entry.date)}: ${entry.event.eventName}'),
                        ),
                        Text(entry.team.teamName, style: const TextStyle(fontWeight: FontWeight.bold)),
                      ],
                    ),
                    subtitle: Row(
                      children: [
                        Expanded(
                          child: Text('カテゴリー: ${entry.category.categoryName}'),
                        ),
                        Text('ゼッケン: ${entry.zekkenNumber ?? "未設定"}'),
                      ],
                    ),
                    onTap: () async {
                      await controller.joinEvent(entry);
                    },
                  );
                },
              );
            }),
          ),
        ],
      ),
    );
  }

  String _formatDate(DateTime? date) {
    if (date == null) {
      return '日時未設定';
    }
    final jstDate = tz.TZDateTime.from(date, tz.getLocation('Asia/Tokyo'));
    return DateFormat('yyyy-MM-dd').format(jstDate);
  }
}

