/// 交易订单模型
class OrderTrade {
  final int id;
  final String orderNo;
  final int userId;
  final String coinCode;
  final int direction; // 1=买入, 2=卖出
  final String price;
  final String quantity;
  final String amount;
  final int status; // 1=待处理, 2=已完成, 3=已取消
  final DateTime? createTime;
  final DateTime? updateTime;

  OrderTrade({
    required this.id,
    required this.orderNo,
    required this.userId,
    required this.coinCode,
    required this.direction,
    required this.price,
    required this.quantity,
    required this.amount,
    required this.status,
    this.createTime,
    this.updateTime,
  });

  factory OrderTrade.fromJson(Map<String, dynamic> json) {
    return OrderTrade(
      id: json['id'] as int? ?? 0,
      orderNo: json['orderNo'] as String? ?? '',
      userId: json['userId'] as int? ?? 0,
      coinCode: json['coinCode'] as String? ?? '',
      direction: json['direction'] as int? ?? 1,
      price: json['price']?.toString() ?? '0.00',
      quantity: json['quantity']?.toString() ?? '0',
      amount: json['amount']?.toString() ?? '0.00',
      status: json['status'] as int? ?? 1,
      createTime: json['createTime'] != null
          ? DateTime.tryParse(json['createTime'])
          : null,
      updateTime: json['updateTime'] != null
          ? DateTime.tryParse(json['updateTime'])
          : null,
    );
  }

  /// 方向文字
  String get directionText => direction == 1 ? '买入' : '卖出';

  /// 状态文字
  String get statusText {
    switch (status) {
      case 1:
        return '待处理';
      case 2:
        return '已完成';
      case 3:
        return '已取消';
      default:
        return '未知';
    }
  }

  /// 是否为买入
  bool get isBuy => direction == 1;
}

/// 充提订单模型
class OrderFund {
  final int id;
  final String orderNo;
  final int userId;
  final String username;
  final int type; // 1=充值, 2=提现
  final String amount;
  final int status;
  // 充值状态: 1=待付款, 2=待确认, 3=已完成, 4=已驳回, 5=已取消
  // 提现状态: 1=待审批, 2=已完成, 3=已驳回, 4=已取消
  final int? walletId; // 冷钱包ID(充值)
  final String? walletAddress; // 钱包地址(充值/提现)
  final String? withdrawContact; // 提现联系方式
  final String remark;
  final String? rejectReason;
  final String? adminRemark;
  final DateTime? createTime;
  final DateTime? payTime; // 用户确认打款时间
  final DateTime? confirmTime; // 管理员确认时间

  OrderFund({
    required this.id,
    required this.orderNo,
    required this.userId,
    required this.username,
    required this.type,
    required this.amount,
    required this.status,
    this.walletId,
    this.walletAddress,
    this.withdrawContact,
    required this.remark,
    this.rejectReason,
    this.adminRemark,
    this.createTime,
    this.payTime,
    this.confirmTime,
  });

  factory OrderFund.fromJson(Map<String, dynamic> json) {
    return OrderFund(
      id: json['id'] as int? ?? 0,
      orderNo: json['orderNo'] as String? ?? '',
      userId: json['userId'] as int? ?? 0,
      username: json['username'] as String? ?? '',
      type: json['type'] as int? ?? 1,
      amount: json['amount']?.toString() ?? '0.00',
      status: json['status'] as int? ?? 1,
      walletId: json['walletId'] as int?,
      walletAddress: json['walletAddress'] as String?,
      withdrawContact: json['withdrawContact'] as String?,
      remark: json['remark']?.toString() ?? '',
      rejectReason: json['rejectReason'] as String?,
      adminRemark: json['adminRemark'] as String?,
      createTime: json['createTime'] != null
          ? DateTime.tryParse(json['createTime'])
          : null,
      payTime: json['payTime'] != null
          ? DateTime.tryParse(json['payTime'])
          : null,
      confirmTime: json['confirmTime'] != null
          ? DateTime.tryParse(json['confirmTime'])
          : null,
    );
  }

  /// 订单类型文字
  String get typeText => type == 1 ? '充值' : '提现';

  /// 状态文字 (根据类型区分)
  String get statusText {
    if (type == 1) {
      // 充值状态
      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 '未知';
      }
    }
  }

  /// 是否为充值
  bool get isDeposit => type == 1;

  /// 是否可取消
  /// 充值: 仅待付款可取消
  /// 提现: 仅待审批可取消
  bool get canCancel {
    if (type == 1) {
      return status == 1; // 充值待付款
    } else {
      return status == 1; // 提现待审批
    }
  }

  /// 是否可确认打款 (仅充值待付款)
  bool get canConfirmPay => type == 1 && status == 1;
}

/// 冷钱包模型
class ColdWallet {
  final int id;
  final String name;
  final String address;
  final String network;
  final bool isDefault;
  final int status; // 0=禁用, 1=启用
  final DateTime? createTime;

  ColdWallet({
    required this.id,
    required this.name,
    required this.address,
    required this.network,
    required this.isDefault,
    required this.status,
    this.createTime,
  });

  factory ColdWallet.fromJson(Map<String, dynamic> json) {
    return ColdWallet(
      id: json['id'] as int? ?? 0,
      name: json['name'] as String? ?? '',
      address: json['address'] as String? ?? '',
      network: json['network'] as String? ?? 'TRC20',
      isDefault: json['isDefault'] as bool? ?? false,
      status: json['status'] as int? ?? 1,
      createTime: json['createTime'] != null
          ? DateTime.tryParse(json['createTime'])
          : null,
    );
  }

  bool get isEnabled => status == 1;
}
