import 'package:flutter/material.dart';
import '../models/hospital.dart';
import '../services/medical_service.dart';
import 'payment_screen.dart';

class BookingScreen extends StatefulWidget {
  final Hospital hospital;
  final Doctor doctor;
  
  const BookingScreen({
    required this.hospital,
    required this.doctor,
  });
  
  @override
  State<BookingScreen> createState() => _BookingScreenState();
}

class _BookingScreenState extends State<BookingScreen> {
  DateTime _selectedDate = DateTime.now().add(Duration(days: 1));
  String _selectedTime = '上午';
  final _nameController = TextEditingController();
  final _phoneController = TextEditingController();
  
  void _submit() {
    if (_nameController.text.isEmpty || _phoneController.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('请填写就诊人信息')),
      );
      return;
    }
    
    final appointment = Appointment(
      id: DateTime.now().toString(),
      hospitalName: widget.hospital.name,
      doctorName: widget.doctor.name,
      department: widget.doctor.department,
      time: _selectedDate,
      status: '待支付',
      fee: widget.doctor.fee,
    );
    
    MedicalService.addAppointment(appointment);
    
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(builder: (_) => PaymentScreen(appointment: appointment)),
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('预约挂号')),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('医生信息', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
            SizedBox(height: 12),
            Card(
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  children: [
                    _buildRow('医院', widget.hospital.name),
                    _buildRow('科室', widget.doctor.department),
                    _buildRow('医生', widget.doctor.name),
                    _buildRow('职称', widget.doctor.title),
                    _buildRow('挂号费', '¥${widget.doctor.fee}'),
                  ],
                ),
              ),
            ),
            SizedBox(height: 24),
            Text('选择时间', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
            SizedBox(height: 12),
            Row(
              children: [
                Expanded(
                  child: Text('${_selectedDate.month}月${_selectedDate.day}日'),
                ),
                TextButton(
                  onPressed: () async {
                    final date = await showDatePicker(
                      context: context,
                      initialDate: _selectedDate,
                      firstDate: DateTime.now(),
                      lastDate: DateTime.now().add(Duration(days: 30)),
                    );
                    if (date != null) setState(() => _selectedDate = date);
                  },
                  child: Text('选择日期'),
                ),
              ],
            ),
            Row(
              children: [
                Radio(
                  value: '上午',
                  groupValue: _selectedTime,
                  onChanged: (val) => setState(() => _selectedTime = val!),
                ),
                Text('上午'),
                SizedBox(width: 20),
                Radio(
                  value: '下午',
                  groupValue: _selectedTime,
                  onChanged: (val) => setState(() => _selectedTime = val!),
                ),
                Text('下午'),
              ],
            ),
            SizedBox(height: 24),
            Text('就诊人信息', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
            SizedBox(height: 12),
            TextField(
              controller: _nameController,
              decoration: InputDecoration(
                labelText: '姓名',
                border: OutlineInputBorder(),
              ),
            ),
            SizedBox(height: 12),
            TextField(
              controller: _phoneController,
              keyboardType: TextInputType.phone,
              decoration: InputDecoration(
                labelText: '手机号',
                border: OutlineInputBorder(),
              ),
            ),
            SizedBox(height: 40),
            SizedBox(
              width: double.infinity,
              height: 50,
              child: ElevatedButton(
                onPressed: _submit,
                style: ElevatedButton.styleFrom(
                  backgroundColor: Color(0xFF333333),
                  foregroundColor: Colors.white,
                ),
                child: Text('提交订单', style: TextStyle(fontSize: 16)),
              ),
            ),
          ],
        ),
      ),
    );
  }
  
  Widget _buildRow(String label, String value) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 4),
      child: Row(
        children: [
          Text('$label：', style: TextStyle(color: Colors.grey)),
          Text(value),
        ],
      ),
    );
  }
}
