import 'package:sk_base_mobile/models/dept.model.dart';
import 'package:sk_base_mobile/models/role.model.dart';

class UserInfoModel {
  UserInfoModel({
    this.id,
    this.createdAt,
    this.updatedAt,
    this.username,
    this.psalt,
    this.nickname,
    this.avatar,
    this.qq,
    this.email,
    this.phone,
    this.remark,
    this.status,
    this.dept,
    this.roles = const [],
  });

  final int? id;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  String? username;
  String? psalt;
  String? nickname;
  String? avatar;
  String? qq;
  String? email;
  String? phone;
  String? remark;
  int? status;
  DeptModel? dept;
  List<RoleModel> roles;

  factory UserInfoModel.fromJson(Map<String, dynamic> json) {
    return UserInfoModel(
      id: json["id"],
      createdAt: DateTime.tryParse(json["createdAt"] ?? ""),
      updatedAt: DateTime.tryParse(json["updatedAt"] ?? ""),
      username: json["username"],
      psalt: json["psalt"],
      nickname: json["nickname"],
      avatar: json["avatar"],
      qq: json["qq"],
      email: json["email"],
      phone: json["phone"],
      remark: json["remark"],
      status: json["status"],
      dept: json["dept"] == null ? null : DeptModel.fromJson(json["dept"]),
      roles: json["roles"] == null
          ? []
          : List<RoleModel>.from(
              json["roles"]!.map((x) => RoleModel.fromJson(x))),
    );
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "createdAt": createdAt?.toIso8601String(),
        "updatedAt": updatedAt?.toIso8601String(),
        "username": username,
        "psalt": psalt,
        "nickname": nickname,
        "avatar": avatar,
        "qq": qq,
        "email": email,
        "phone": phone,
        "remark": remark,
        "status": status,
        "dept": dept?.toJson(),
        "roles": roles.map((x) => x.toJson()).toList(),
      };
}
