import 'package:flutter/cupertino.dart';

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

class InterventionHistory {
  String? authizationFormText;
  int? authorizedAction;
  int? commentedBy;
  String? commentedByName;
  String? interventionDesc;
  int? interventionId;
  String? remark;
  TextEditingController remarksController = TextEditingController();

  InterventionHistory({
    this.authizationFormText,
    this.authorizedAction,
    this.commentedBy,
    this.commentedByName,
    this.interventionDesc,
    this.interventionId,
    this.remark,
  });

  factory InterventionHistory.fromJson(Map<String, dynamic> json) {
    print('the remark is ${json['remark']}');
    return InterventionHistory(
      authizationFormText: json['authizationFormText']?.toString() ?? '',
      authorizedAction: _parseInt(json['authorizedAction']),
      commentedBy: _parseInt(json['commentedBy']),
      commentedByName: json['commentedByName']?.toString() ?? '',
      interventionDesc: json['interventionDesc']?.toString() ?? '',
      interventionId: _parseInt(json['interventionId']),
      remark: json['remark']?.toString() ?? '',
    );
  }

  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 {
      'authizationFormText': authizationFormText,
      'authorizedAction': authorizedAction,
      'commentedBy': commentedBy,
      'commentedByName': commentedByName,
      'interventionDesc': interventionDesc,
      'interventionId': interventionId,
      'remark': remark,
    };
  }
}