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

class OrderListScreen extends StatefulWidget {
  @override
  State<OrderListScreen> createState() => _OrderListScreenState();
}

class _OrderListScreenState extends State<OrderListScreen> {
  @override
  Widget build(BuildContext context) {
    final orders = MedicalService.appointments;
    
    return Scaffold(
      appBar: AppBar(title: Text('我的订单')),
      body: orders.isEmpty
          ? Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.inbox, size: 80, color: Colors.grey[300]),
                  SizedBox(height: 16),
                  Text('暂无订单', style: TextStyle(color: Colors.grey)),
                ],
              ),
            )
          : ListView.builder(
              padding: EdgeInsets.all(16),
              itemCount: orders.length,
              itemBuilder: (context, index) {
                final order = orders[index];
                return Card(
                  margin: EdgeInsets.only(bottom: 12),
                  child: Padding(
                    padding: EdgeInsets.all(16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              order.hospitalName,
                              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                            ),
                            _buildStatusChip(order.status),
                          ],
                        ),
                        SizedBox(height: 12),
                        _buildInfoRow(Icons.medical_services, '${order.department} - ${order.doctorName}'),
                        _buildInfoRow(
                          Icons.access_time,
                          '${order.time.month}月${order.time.day}日',
                        ),
                        _buildInfoRow(Icons.attach_money, '¥${order.fee}'),
                        SizedBox(height: 12),
                        if (order.status == '待支付')
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              TextButton(
                                onPressed: () {
                                  setState(() {
                                    order.status = '已取消';
                                  });
                                },
                                child: Text('取消订单'),
                              ),
                              SizedBox(width: 8),
                              ElevatedButton(
                                onPressed: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (_) => PaymentScreen(appointment: order),
                                    ),
                                  ).then((_) => setState(() {}));
                                },
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: Color(0xFF333333),
                                  foregroundColor: Colors.white,
                                ),
                                child: Text('去支付'),
                              ),
                            ],
                          ),
                      ],
                    ),
                  ),
                );
              },
            ),
    );
  }
  
  Widget _buildStatusChip(String status) {
    Color color;
    switch (status) {
      case '待支付':
        color = Colors.orange;
        break;
      case '已支付':
      case '待就诊':
        color = Colors.green;
        break;
      case '已完成':
        color = Colors.blue;
        break;
      case '已取消':
        color = Colors.grey;
        break;
      default:
        color = Colors.grey;
    }
    
    return Chip(
      label: Text(status, style: TextStyle(color: Colors.white, fontSize: 12)),
      backgroundColor: color,
      padding: EdgeInsets.zero,
    );
  }
  
  Widget _buildInfoRow(IconData icon, String text) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 4),
      child: Row(
        children: [
          Icon(icon, size: 16, color: Colors.grey),
          SizedBox(width: 8),
          Text(text, style: TextStyle(color: Colors.grey[700])),
        ],
      ),
    );
  }
}
