import 'package:flutter/material.dart';
import '../models/activity.dart';
import '../services/activity_service.dart';

class CreateActivityScreen extends StatefulWidget {
  @override
  State<CreateActivityScreen> createState() => _CreateActivityScreenState();
}

class _CreateActivityScreenState extends State<CreateActivityScreen> {
  final _titleController = TextEditingController();
  final _locationController = TextEditingController();
  final _descController = TextEditingController();
  String _selectedCategory = '太极';
  int _maxParticipants = 10;
  DateTime _selectedDate = DateTime.now().add(Duration(days: 1));
  TimeOfDay _selectedTime = TimeOfDay(hour: 9, minute: 0);
  
  void _submit() {
    if (_titleController.text.isEmpty || _locationController.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('请填写完整信息')),
      );
      return;
    }
    
    final activity = Activity(
      id: DateTime.now().toString(),
      title: _titleController.text,
      category: _selectedCategory,
      time: DateTime(
        _selectedDate.year,
        _selectedDate.month,
        _selectedDate.day,
        _selectedTime.hour,
        _selectedTime.minute,
      ),
      location: _locationController.text,
      maxParticipants: _maxParticipants,
      creatorName: '我',
      description: _descController.text,
    );
    
    ActivityService.addActivity(activity);
    
    Navigator.pop(context);
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('活动创建成功')),
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('发起活动')),
      body: ListView(
        padding: EdgeInsets.all(16),
        children: [
          TextField(
            controller: _titleController,
            decoration: InputDecoration(
              labelText: '活动名称',
              border: OutlineInputBorder(),
            ),
          ),
          SizedBox(height: 16),
          DropdownButtonFormField<String>(
            value: _selectedCategory,
            decoration: InputDecoration(
              labelText: '活动类型',
              border: OutlineInputBorder(),
            ),
            items: ActivityService.categories
                .skip(1)
                .map((c) => DropdownMenuItem(value: c, child: Text(c)))
                .toList(),
            onChanged: (val) => setState(() => _selectedCategory = val!),
          ),
          SizedBox(height: 16),
          TextField(
            controller: _locationController,
            decoration: InputDecoration(
              labelText: '活动地点',
              border: OutlineInputBorder(),
            ),
          ),
          SizedBox(height: 16),
          ListTile(
            title: Text('人数限制'),
            trailing: DropdownButton<int>(
              value: _maxParticipants,
              items: [5, 8, 10, 12, 15]
                  .map((n) => DropdownMenuItem(value: n, child: Text('$n 人')))
                  .toList(),
              onChanged: (val) => setState(() => _maxParticipants = val!),
            ),
            contentPadding: EdgeInsets.zero,
          ),
          SizedBox(height: 16),
          TextField(
            controller: _descController,
            maxLines: 3,
            decoration: InputDecoration(
              labelText: '活动描述（选填）',
              border: OutlineInputBorder(),
            ),
          ),
          SizedBox(height: 40),
          SizedBox(
            height: 50,
            child: ElevatedButton(
              onPressed: _submit,
              style: ElevatedButton.styleFrom(
                backgroundColor: Color(0xFF333333),
                foregroundColor: Colors.white,
              ),
              child: Text('发布活动', style: TextStyle(fontSize: 16)),
            ),
          ),
        ],
      ),
    );
  }
}
