import 'package:sk_base_mobile/util/common.util.dart';

class DeptModel extends TreeNode<DeptModel> {
  DeptModel({
    required this.id,
    required this.createdAt,
    required this.updatedAt,
    required this.name,
    required this.orderNo,
    this.parent,
    this.children,
  });

  final int id;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String name;
  final int orderNo;
  final DeptModel? parent;
  List<DeptModel>? children = [];

  factory DeptModel.fromJson(Map<String, dynamic> json) {
    return DeptModel(
      id: json["id"] ?? 0,
      createdAt: DateTime.tryParse(json["createdAt"] ?? ""),
      updatedAt: DateTime.tryParse(json["updatedAt"] ?? ""),
      name: json["name"] ?? "",
      orderNo: json["orderNo"] ?? 0,
      parent:
          json["parent"] == null ? null : DeptModel.fromJson(json["parent"]),
      children: json["children"] == null
          ? []
          : List<DeptModel>.from(
              json["children"]!.map((x) => DeptModel.fromJson(x))),
    );
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "createdAt": createdAt?.toIso8601String(),
        "updatedAt": updatedAt?.toIso8601String(),
        "name": name,
        "orderNo": orderNo,
        "parent": parent?.toJson(),
        "children": children?.map((x) => x.toJson()).toList(),
      };
}
