enum UserRole { employee, approver, finance, admin }

class UserModel {
  final String id;
  final String userName;
  final String realName;
  final UserRole role;
  final String deptId;
  final String deptName;
  final String position;
  final String phone;
  final String email;
  final String avatarUrl;

  const UserModel({
    required this.id,
    this.userName = '',
    this.realName = '',
    this.role = UserRole.employee,
    this.deptId = '',
    this.deptName = '',
    this.position = '',
    this.phone = '',
    this.email = '',
    this.avatarUrl = '',
  });

  bool get isApprover => role == UserRole.approver || role == UserRole.admin;
  bool get isFinance => role == UserRole.finance || role == UserRole.admin;
  bool get isAdmin => role == UserRole.admin;

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'] as String? ?? '',
      userName: json['userName'] as String? ?? '',
      realName: json['realName'] as String? ?? '',
      role: _parseRole(json['role'] as String?),
      deptId: json['deptId'] as String? ?? '',
      deptName: json['deptName'] as String? ?? '',
      position: json['position'] as String? ?? '',
      phone: json['phone'] as String? ?? '',
      email: json['email'] as String? ?? '',
      avatarUrl: json['avatarUrl'] as String? ?? '',
    );
  }

  static UserRole _parseRole(String? role) {
    switch (role) {
      case 'approver': return UserRole.approver;
      case 'finance': return UserRole.finance;
      case 'admin': return UserRole.admin;
      default: return UserRole.employee;
    }
  }

  static const mock = UserModel(
    id: 'U001',
    userName: 'zhangsan',
    realName: '张三',
    role: UserRole.employee,
    deptId: 'D001',
    deptName: '销售部',
    position: '销售经理',
  );
}
