class MedicationList{
  List<Medication>? medication = [];
  MedicationList.fromJson(Map<String, dynamic> json){
    var entryList =  json['entityList'] ?? {};
    List<Medication> tempList = [];
    for(var item in entryList){
      var medication = Medication.fromJson(item);
      tempList.add(medication);
    }
    medication?.addAll(tempList);
  }
}

class Medication {
  int? accessLevel;
  int? admissionNo;
  String? createdBy;
  String? doctorComments;
  String? doctorName;
  String? dosageDetail;
  int? itemID;
  int? lineItemNo;
  String? medication;
  String? nursingStation;
  int? orderNo;
  int? patientID;
  String? patientName;
  String? pharmacyRemarks;
  int? prescriptionNo;
  DateTime? startDatetime;
  int? status;
  String? statusName;
  String? accessLevelDescription;
  DateTime? stopDatetime;

  Medication({
    this.accessLevel,
    this.admissionNo,
    this.createdBy,
    this.doctorComments,
    this.doctorName,
    this.dosageDetail,
    this.itemID,
    this.lineItemNo,
    this.medication,
    this.nursingStation,
    this.orderNo,
    this.patientID,
    this.patientName,
    this.pharmacyRemarks,
    this.prescriptionNo,
    this.startDatetime,
    this.status,
    this.statusName,
    this.stopDatetime,
    this.accessLevelDescription,
  });

  factory Medication.fromJson(Map<String, dynamic> json) {
    return Medication(
      accessLevel: _parseInt(json['accessLevel']),
      admissionNo: _parseInt(json['admissionNo']),
      createdBy: json['createdBy']?.toString() ?? '',
      doctorComments: json['doctorComments']?.toString() ?? '',
      doctorName: json['doctorName']?.toString() ?? '',
      dosageDetail: json['dosageDetail']?.toString() ?? '',
      itemID: _parseInt(json['itemID']),
      lineItemNo: _parseInt(json['lineItemNo']),
      medication: json['medication']?.toString() ?? '',
      nursingStation: json['nursingStation']?.toString() ?? '',
      orderNo: _parseInt(json['orderNo']),
      patientID: _parseInt(json['patientID']),
      patientName: json['patientName']?.toString() ?? '',
      pharmacyRemarks: json['pharmacyRemarks']?.toString() ?? '-',
      prescriptionNo: _parseInt(json['prescriptionNo']),
      startDatetime: json['startDatetime'] != null ? DateTime.parse(json['startDatetime']) : null,
      status: _parseInt(json['status']),
      statusName: json['statusName']?.toString() ?? '',
      accessLevelDescription: json['accessLevelDescription']?.toString() ?? '',
      stopDatetime: json['stopDatetime'] != null ? DateTime.parse(json['stopDatetime']) : null,
    );
  }

  static int? _parseInt(dynamic value) {
    if (value == null) return null;
    if (value is int) return value;
    if (value is String) return int.tryParse(value);
    return null;
  }

  Map<String, dynamic> toJson() {
    return {
      'accessLevel': accessLevel,
      'admissionNo': admissionNo,
      'createdBy': createdBy,
      'doctorComments': doctorComments,
      'doctorName': doctorName,
      'dosageDetail': dosageDetail,
      'itemID': itemID,
      'lineItemNo': lineItemNo,
      'medication': medication,
      'nursingStation': nursingStation,
      'orderNo': orderNo,
      'patientID': patientID,
      'patientName': patientName,
      'pharmacyRemarks': pharmacyRemarks,
      'prescriptionNo': prescriptionNo,
      'startDatetime': startDatetime?.toIso8601String(),
      'status': status,
      'statusName': statusName,
      'stopDatetime': stopDatetime?.toIso8601String(),
    };
  }
}