class KRMessageList {
  final int total;
  final List<KRMessage> announcements;

  KRMessageList({
    this.total = 0,
    List<KRMessage>? announcements,
  }) : announcements = announcements ?? [];

  factory KRMessageList.fromJson(Map<String, dynamic> json) {
    return KRMessageList(
      total: json['total'] as int? ?? 0,
      announcements: (json['announcements'] as List<dynamic>?)
              ?.map((e) => KRMessage.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'total': total,
      'announcements': announcements.map((e) => e.toJson()).toList(),
    };
  }
}

class KRMessage {
  final int id;
  final String title;
  final String content;
  final bool show;
  final bool pinned;
  final bool popup;
  final int createdAt;
  final int updatedAt;
  final String dataStr = "";

  // 通用时间格式化方法
  String kr_formatDateTime(int timestamp, {String format = 'yyyy-MM-dd HH:mm'}) {
    if (timestamp == 0) return '';
    final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp );
    
    return format
        .replaceAll('yyyy', dateTime.year.toString())
        .replaceAll('MM', dateTime.month.toString().padLeft(2, '0'))
        .replaceAll('dd', dateTime.day.toString().padLeft(2, '0'))
        .replaceAll('HH', dateTime.hour.toString().padLeft(2, '0'))
        .replaceAll('mm', dateTime.minute.toString().padLeft(2, '0'))
        .replaceAll('ss', dateTime.second.toString().padLeft(2, '0'));
  }

  // 获取格式化的创建时间字符串
  String get kr_formattedCreatedAt => kr_formatDateTime(createdAt);

  // 获取格式化的更新时间字符串
  String get kr_formattedUpdatedAt => kr_formatDateTime(updatedAt);

  KRMessage({
    this.id = 0,
    this.title = '',
    this.content = '',
    this.show = false,
    this.pinned = false,
    this.popup = false,
    this.createdAt = 0,
    this.updatedAt = 0,
  });

  factory KRMessage.fromJson(Map<String, dynamic> json) {
    return KRMessage(
      id: json['id'] as int? ?? 0,
      title: json['title'] as String? ?? '',
      content: json['content'] as String? ?? '',
      show: json['show'] as bool? ?? false,
      pinned: json['pinned'] as bool? ?? false,
      popup: json['popup'] as bool? ?? false,
      createdAt: json['created_at'] as int? ?? 0,
      updatedAt: json['updated_at'] as int? ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'content': content,
      'show': show,
      'pinned': pinned,
      'popup': popup,
      'created_at': createdAt,
      'updated_at': updatedAt,
    };
  }
}
  