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

class PaymentScreen extends StatefulWidget {
  final Appointment appointment;
  
  const PaymentScreen({required this.appointment});
  
  @override
  State<PaymentScreen> createState() => _PaymentScreenState();
}

class _PaymentScreenState extends State<PaymentScreen> {
  String _selectedMethod = '微信支付';
  bool _isProcessing = false;
  
  void _pay() async {
    setState(() => _isProcessing = true);
    
    // 模拟支付处理
    await Future.delayed(Duration(seconds: 2));
    
    setState(() {
      _isProcessing = false;
      widget.appointment.status = '待就诊';
    });
    
    if (mounted) {
      showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) => AlertDialog(
          title: Row(
            children: [
              Icon(Icons.check_circle, color: Colors.green, size: 32),
              SizedBox(width: 12),
              Text('支付成功'),
            ],
          ),
          content: Text('您的预约已确认，请按时就诊'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                Navigator.pop(context);
              },
              child: Text('确定'),
            ),
          ],
        ),
      );
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('支付')),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  // 订单信息
                  Container(
                    padding: EdgeInsets.all(16),
                    color: Colors.grey[50],
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('订单信息', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
                        SizedBox(height: 12),
                        _buildRow('医院', widget.appointment.hospitalName),
                        _buildRow('科室', widget.appointment.department),
                        _buildRow('医生', widget.appointment.doctorName),
                        _buildRow(
                          '就诊时间',
                          '${widget.appointment.time.month}月${widget.appointment.time.day}日',
                        ),
                        Divider(height: 24),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('合计金额', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
                            Text(
                              '¥${widget.appointment.fee}',
                              style: TextStyle(fontSize: 24, color: Colors.red, fontWeight: FontWeight.bold),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 20),
                  // 支付方式
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('支付方式', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
                        SizedBox(height: 12),
                        _buildPaymentMethod('微信支付', Icons.wechat),
                        _buildPaymentMethod('支付宝', Icons.account_balance_wallet),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          // 支付按钮
          Container(
            padding: EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  color: Colors.black12,
                  blurRadius: 4,
                  offset: Offset(0, -2),
                ),
              ],
            ),
            child: SizedBox(
              width: double.infinity,
              height: 50,
              child: ElevatedButton(
                onPressed: _isProcessing ? null : _pay,
                style: ElevatedButton.styleFrom(
                  backgroundColor: Color(0xFF333333),
                  foregroundColor: Colors.white,
                ),
                child: _isProcessing
                    ? Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          SizedBox(
                            width: 20,
                            height: 20,
                            child: CircularProgressIndicator(
                              color: Colors.white,
                              strokeWidth: 2,
                            ),
                          ),
                          SizedBox(width: 12),
                          Text('支付中...'),
                        ],
                      )
                    : Text('确认支付 ¥${widget.appointment.fee}', 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),
        ],
      ),
    );
  }
  
  Widget _buildPaymentMethod(String name, IconData icon) {
    final selected = _selectedMethod == name;
    return Card(
      child: InkWell(
        onTap: () => setState(() => _selectedMethod = name),
        child: Padding(
          padding: EdgeInsets.all(16),
          child: Row(
            children: [
              Icon(icon, size: 32, color: selected ? Color(0xFF333333) : Colors.grey),
              SizedBox(width: 16),
              Expanded(child: Text(name, style: TextStyle(fontSize: 16))),
              if (selected)
                Icon(Icons.check_circle, color: Color(0xFF333333)),
            ],
          ),
        ),
      ),
    );
  }
}
