class HistoryTransactionModel {
  HistoryTransactionModel({
    this.orderId,
    this.totalPrice,
    this.date,
    this.dateExpired,
    this.paymentDetail,
    this.statusPayment,
    this.courses,
    this.discountPrice,
    this.token,
  });

  final String? orderId;
  final DateTime? date;
  final DateTime? dateExpired;
  final List<DataCourses>? courses;
  final int? totalPrice;
  final int? discountPrice;
  final PaymentDetail? paymentDetail;
  final String? statusPayment;
  final String? token; 

  factory HistoryTransactionModel.fromJson(Map<String, dynamic> json) {
    int timestamp = int.parse(json["date"]);
    int timestampExpired = int.parse(json["date_expired"]);
    return HistoryTransactionModel(
        orderId: json['order_id'] ?? '',
        totalPrice: json["sub_total"] ?? 0,
        discountPrice: json["discount_price"] ?? 0,
        date: DateTime.fromMillisecondsSinceEpoch(timestamp * 1000),
        dateExpired: DateTime.fromMillisecondsSinceEpoch(timestamp * 1000),
        paymentDetail: PaymentDetail.fromJson(json["payment_detail"] ?? {}),
        statusPayment: json["status"] ?? '',
        token: json['token'] ?? '',
        courses: json["course"] != null
            ? List<DataCourses>.from(
                json["course"].map((x) => DataCourses.fromJson(x))).toList()
            : []);
  }

  Map<dynamic, dynamic> toJson() => {
        'order_id': orderId,
        "sub_total": totalPrice,
        "discountPrice": discountPrice,
        "payment_type": paymentDetail,
        "status": statusPayment,
        "token": token, 
        'course': courses != null
            ? List<dynamic>.from(courses!.map((x) => x.toJson())).toList()
            : []
      };
}

class DataCourses {
  final String? courseId, thumbnail, title, instructor, price;

  DataCourses(
      {this.courseId, this.title, this.price, this.instructor, this.thumbnail});

  factory DataCourses.fromJson(Map<String, dynamic> json) => DataCourses(
        courseId: json['id'] ?? '',
        title: json['title'] ?? '',
        price: json['price'] ?? '',
        thumbnail: json['thumbnail'] ?? '',
        instructor: json['instructor'] ?? '',
      );

  Map<String, dynamic> toJson() => {
        'id': courseId,
        'title': title,
        'price': price,
        'thumbnail': thumbnail,
        'instructor': instructor
      };
}

class PaymentDetail {
  String? paymentType;
  String? bank;
  String? vaNumber;
  String? store;

  PaymentDetail({this.paymentType, this.bank, this.vaNumber, this.store});

  PaymentDetail.fromJson(Map<String, dynamic> json) {
    paymentType = json['payment_type'] ?? '';
    bank = json['bank'] ?? '';
    vaNumber = json['va_number'] ?? '';
    store = json['store'] ?? '';
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['payment_type'] = this.paymentType;
    data['bank'] = this.bank;
    data['va_number'] = this.vaNumber;
    return data;
  }
}
