import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:provider/provider.dart';
import '../../../core/theme/app_spacing.dart';
import '../../../core/theme/app_color_scheme.dart';
import '../../../data/models/order_models.dart';
import '../../../providers/asset_provider.dart';

class _FundOrderCard extends StatelessWidget {
  final OrderFund order;

  const _FundOrderCard({required this.order});

  Color _getStatusColor(int status, bool isDeposit) {
    if (isDeposit) {
      // 充值状态: 1=待付款, 2=待确认, 3=已完成, 4=已驳回, 5=已取消
      switch (status) {
        case 1:
          return AppColorScheme.warning;
        case 2:
          return AppColorScheme.info;
        case 3:
          return AppColorScheme.success;
        case 4:
          return AppColorScheme.error;
        case 5:
          return AppColorScheme.muted;
        default:
          return AppColorScheme.muted;
      }
    } else {
      // 提现状态: 1=待审批, 2=已完成, 3=已驳回, 4=已取消
      switch (status) {
        case 1:
          return AppColorScheme.warning;
        case 2:
          return AppColorScheme.success;
        case 3:
          return AppColorScheme.error;
        case 4:
          return AppColorScheme.muted;
        default:
          return AppColorScheme.muted;
      }
    }
  }

  String _getStatusText(int status, bool isDeposit) {
    if (isDeposit) {
      switch (status) {
        case 1:
          return '待付款';
        case 2:
          return '待确认';
        case 3:
          return '已完成';
        case 4:
          return '已驳回';
        case 5:
          return '已取消';
        default:
          return '未知';
      }
    } else {
      switch (status) {
        case 1:
          return '待审批';
        case 2:
          return '已完成';
        case 3:
          return '已驳回';
        case 4:
          return '已取消';
        default:
          return '未知';
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final isDeposit = order.type == 1;
    final statusColor = _getStatusColor(order.status, isDeposit);

    return ShadCard(
      padding: AppSpacing.cardPadding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '${isDeposit ? '+' : '-'}${order.amount} USDT',
                style: theme.textTheme.large.copyWith(
                  color: statusColor,
                  fontWeight: FontWeight.bold,
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: statusColor.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  _getStatusText(order.status, isDeposit),
                  style: theme.textTheme.small.copyWith(color: statusColor),
                ),
              ),
            ],
          ),
          SizedBox(height: AppSpacing.sm),
          Row(
            children: [
              Text('订单号: ', style: theme.textTheme.muted),
              Text(order.orderNo, style: theme.textTheme.small),
            ],
          ),
          SizedBox(height: AppSpacing.xs),
          Row(
            children: [
              Text('创建时间: ', style: theme.textTheme.muted),
              Text(
                order.createTime?.toString() ?? '无',
                style: theme.textTheme.small,
              ),
            ],
          ),
          if (order.rejectReason != null && order.rejectReason!.isNotEmpty) ...[
            SizedBox(height: AppSpacing.xs),
            Row(
              children: [
                Text('驳回原因: ', style: theme.textTheme.muted),
                Expanded(
                  child: Text(
                    order.rejectReason!,
                    style: theme.textTheme.small.copyWith(color: AppColorScheme.error),
                  ),
                ),
              ],
            ),
          ],
          if (order.status == 1 && isDeposit) ...[
            SizedBox(height: AppSpacing.md),
            Row(
              children: [
                Expanded(
                  child: ShadButton.outline(
                    onPressed: () => _handleConfirmPay(context),
                    child: const Text('已打款'),
                  ),
                ),
                SizedBox(width: AppSpacing.sm),
                Expanded(
                  child: ShadButton.outline(
                    onPressed: () => _handleCancel(context),
                    child: const Text('取消订单'),
                  ),
                ),
              ],
            ),
          ],
        ],
      ),
    );
  }

  Future<void> _handleConfirmPay(BuildContext context) async {
    final response = await context.read<AssetProvider>().confirmPay(order.orderNo);
    if (context.mounted) {
      if (response.success) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: const Text('已确认打款，请等待审核')),
        );
        context.read<AssetProvider>().loadFundOrders();
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(response.message ?? '确认失败')),
        );
      }
    }
  }

  Future<void> _handleCancel(BuildContext context) async {
    final response = await context.read<AssetProvider>().cancelOrder(order.orderNo);
    if (context.mounted) {
      if (response.success) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: const Text('订单已取消')),
        );
        context.read<AssetProvider>().loadFundOrders();
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(response.message ?? '取消失败')),
        );
      }
    }
  }
}
